Исправлена ошибка линковки из-за hla-pause, добавлено переключение паузы и ночного режима. добавлена трассировка кода роботов на "панель"

This commit is contained in:
VladislavOstapov 2023-01-07 12:13:39 +03:00
parent ec7f508931
commit bbc73b4e31
5 changed files with 72 additions and 28 deletions

View File

@ -5,6 +5,7 @@
#include <cstring>
#include <iostream>
#include <libc.h>
#include <netinet/tcp.h>
#include "emulator.h"
#include "robot.h"
@ -48,6 +49,7 @@ char one_robot_mode = (char)(hla_robot1_en ^ hla_robot2_en);
char scheduler_en = 1;
char scheduler_start_signal = 1;
char auto_mode_pause = 0;
char hla_night_mode = 0;
short etching_zone = 0, galvanizing_zone = 0;
@ -154,7 +156,7 @@ static void showAll() {
image_init();
image_draw_borders();
char tmp[64];
sprintf(tmp, "Lock1=%02d Lock2=%02d", robot1_lock_zone, robot2_lock_zone);
sprintf(tmp, "Lock1=%2d Lock2=%2d", robot1_lock_zone, robot2_lock_zone);
image_insert_sprite(0, 2, tmp);
int barrels_count = 0, barrels_time = 0;
@ -176,9 +178,12 @@ static void showAll() {
max_time = barrels_time;
}
sprintf(tmp, "barrels=%02d time=%04d max_time=%04d", barrels_count, barrels_time, max_time);
sprintf(tmp, "barrels=%2d time=%d max_time=%d", barrels_count, barrels_time, max_time);
image_insert_sprite(0, 25, tmp);
sprintf(tmp, "MODE: night=%d pause=%d", hla_night_mode, auto_mode_pause);
image_insert_sprite(0, 70, tmp);
// рисование бочек
for (int i = 0; i < BARRELS_COUNT; i++) {
const auto& b = barrels[i];
@ -220,26 +225,36 @@ static void open_socket() {
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd < 0) {
printf("\n Socket creation error \n");
printf("Socket creation error\n");
exit(-1);
}
// setting socket options
int flag = 1;
if(setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag)) == -1){
printf("setsockopt TCP_NODELAY failed for server socket on address 127.0.0.1\n");
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(40000);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
printf("Invalid address/ Address not supported\n");
exit(-1);
}
if (connect(sock_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
printf("Connection Failed \n");
printf("Connection Failed\n");
exit(-1);
}
}
int main() {
robot1.mx.correct_sensor = true;
robot2.mx.correct_sensor = true;
open_socket();
current_tic = 0;
@ -253,12 +268,31 @@ int main() {
scheduler_main();
send_str("\033c");
showAll();
if (message) {
std::cout << message << std::endl;
send_str(message);
send_str("\n");
}
if (!one_robot_mode) {
if (!robot2.mx.correct_sensor) {
if (robot2.dx.current_zone + 1 >= robot1.dx.current_zone) {
printf("ASSERTION FAILED: FOUND ROBOTS CORRUPTION\n");
if (robot1_code.PC >= 0)
debug_print_robot_code(&robot1_code, 1, 0);
if (robot2_code.PC >= 0)
debug_print_robot_code(&robot2_code, 2, 0);
break;
}
}
}
if (robot1_code.PC >= 0)
debug_print_robot_code(&robot1_code, 1, sock_fd);
if (robot2_code.PC >= 0)
debug_print_robot_code(&robot2_code, 2, sock_fd);
send_str("cmd >> ");
std::string in;
while (true) {
@ -292,6 +326,12 @@ int main() {
} else if (in == "2") {
button_load = 1;
message = "Нажата кнопка загрузки 2";
} else if (in == "n") {
hla_night_mode = !hla_night_mode;
message = "Переключен ночной режим";
} else if (in == "p") {
auto_mode_pause = !auto_mode_pause;
message = "Переключен режим паузы";
} else {
message = "Неизвестная команда. q - выход, u - выгрузка, 1 - загрузка 1, 2 - загрузка 2";
}
@ -299,6 +339,7 @@ int main() {
current_tic++;
}
fsync(sock_fd);
close(sock_fd);
return 0;
}

View File

@ -20,7 +20,6 @@ void scheduler_main();
// Кнопки с панели
extern char hla_auto_mode;
extern char hla_night_mode;
extern char hla_pause;
extern char hla_correct_command;

View File

@ -91,7 +91,7 @@ void schedule_one_robot(const struct scheduler_task* tasks, const struct robot_r
r->dx.current_zone, robot_id);
#ifdef EMULATOR
debug_print_robot_code(code, robot_id);
debug_print_robot_code(code, robot_id, 0);
#endif
}
}

44
utils.c
View File

@ -285,75 +285,79 @@ char remove_barrel_from_zone(short zone) {
#ifdef EMULATOR
void debug_print_robot_code(const struct robot_code* code, const short robot_id) {
// printf("INFO: code length is %d\n", cmd_index);
printf("Code for R%d, B%d:\n", robot_id, code->barrel_id);
void debug_print_robot_code(const struct robot_code *code, const short robot_id, int fd) {
// dprintf(fd, "INFO: code length is %d\n", cmd_index);
dprintf(fd, "Code for R%d, B%d:\n", robot_id, code->barrel_id);
for (int i = 0; i < 16; i++) {
const short cmd_arg = (short)(code->code[i] & (short)(~ROBOT_CMD_MASK));
printf("%5d 0x%04X", i, code->code[i] & 0xFFFF);
if (code->PC == i) {
dprintf(fd, "==>%2d 0x%04X", i, code->code[i] & 0xFFFF);
} else {
dprintf(fd, " %2d 0x%04X", i, code->code[i] & 0xFFFF);
}
if ((code->code[i] & ROBOT_CMD_MASK) == ROBOT_CMD_END_code) {
printf(" END\n");
dprintf(fd, " END\n");
break;
}
switch ((short)(code->code[i] & (short)ROBOT_CMD_MASK)) {
case ROBOT_CMD_MOVE_TO_ZONE_code:
printf(" move to zone %d (with barrel: %d)\n", cmd_arg & (~ROBOT_WITH_BARREL), (cmd_arg & ROBOT_WITH_BARREL) != 0);
dprintf(fd, " move to zone %d (with barrel: %d)\n", cmd_arg & (~ROBOT_WITH_BARREL), (cmd_arg & ROBOT_WITH_BARREL) != 0);
break;
case ROBOT_CMD_MOVE_OFF_code:
printf(" move to offset pos\n");
dprintf(fd, " move to offset pos\n");
break;
case ROBOT_CMD_MOVE_ACCURATE_code:
printf(" move to accurate pos\n");
dprintf(fd, " move to accurate pos\n");
break;
case ROBOT_CMD_UP_code:
printf(" up (with barrel: %d)\n", (cmd_arg & ROBOT_WITH_BARREL) != 0);
dprintf(fd, " up (with barrel: %d)\n", (cmd_arg & ROBOT_WITH_BARREL) != 0);
break;
// в эмуляторе не важно где я, поэтому тут обе команды вниз обрабатываются одинаково
case ROBOT_CMD_DOWN_code:
printf(" down (with barrel: %d)\n", (cmd_arg & ROBOT_WITH_BARREL) != 0);
dprintf(fd, " down (with barrel: %d)\n", (cmd_arg & ROBOT_WITH_BARREL) != 0);
break;
case ROBOT_CMD_WAIT_code:
printf(" wait %d secs\n", cmd_arg);
dprintf(fd, " wait %d secs\n", cmd_arg);
break;
case ROBOT_CMD_TMR_SET_code:
printf(" set barrel timer %d secs\n", cmd_arg);
dprintf(fd, " set barrel timer %d secs\n", cmd_arg);
break;
case ROBOT_CMD_SET_LOCK_ZONE_code:
printf(" set lock zone %d\n", cmd_arg);
dprintf(fd, " set lock zone %d\n", cmd_arg);
break;
case ROBOT_CMD_CORRECT_AXIS_code:
if (cmd_arg == ROBOT_AXIS_X) {
printf(" correct axis: X\n");
dprintf(fd, " correct axis: X\n");
} else if (cmd_arg == ROBOT_AXIS_Z) {
printf(" correct axis: Z\n");
dprintf(fd, " correct axis: Z\n");
} else {
printf(" correct axis: INVALID (%d)\n", cmd_arg);
dprintf(fd, " correct axis: INVALID (%d)\n", cmd_arg);
}
break;
case ROBOT_CMD_INC_ZONE_code:
if (cmd_arg == ROBOT_ZONE_GAL) {
printf(" increment zone: galvanic\n");
dprintf(fd, " increment zone: galvanic\n");
} else if (cmd_arg == ROBOT_ZONE_ETCH) {
printf(" increment zone: etching\n");
dprintf(fd, " increment zone: etching\n");
} else {
printf(" increment zone: INVALID (0x%4X)\n", cmd_arg);
dprintf(fd, " increment zone: INVALID (0x%4X)\n", cmd_arg);
}
break;
default:
printf(" UNKNOWN: 0x%04X\n", code->code[i] & 0xFFFF);
dprintf(fd, " UNKNOWN: 0x%04X\n", code->code[i] & 0xFFFF);
}
}
}

View File

@ -347,7 +347,7 @@ void create_operation(struct robot_code *code, const short barrel_id, const shor
const short current_zone, const short robot_id);
#ifdef EMULATOR
void debug_print_robot_code(const struct robot_code* code, const short robot_id);
void debug_print_robot_code(const struct robot_code *code, const short robot_id, int fd);
#endif
#ifdef __cplusplus