From bbc73b4e31eb3511a74c8108e3be2d42963a2fcb Mon Sep 17 00:00:00 2001 From: VladislavOstapov Date: Sat, 7 Jan 2023 12:13:39 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BB=D0=B8=D0=BD=D0=BA=D0=BE=D0=B2=D0=BA=D0=B8=20=D0=B8=D0=B7?= =?UTF-8?q?-=D0=B7=D0=B0=20hla-pause,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BE=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BA=D0=BB?= =?UTF-8?q?=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=B0=D1=83=D0=B7?= =?UTF-8?q?=D1=8B=20=D0=B8=20=D0=BD=D0=BE=D1=87=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B6=D0=B8=D0=BC=D0=B0.=20=D0=B4=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=82=D1=80=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20=D0=BA=D0=BE=D0=B4?= =?UTF-8?q?=D0=B0=20=D1=80=D0=BE=D0=B1=D0=BE=D1=82=D0=BE=D0=B2=20=D0=BD?= =?UTF-8?q?=D0=B0=20"=D0=BF=D0=B0=D0=BD=D0=B5=D0=BB=D1=8C"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- emulator.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- emulator.h | 1 - scheduler.c | 2 +- utils.c | 44 ++++++++++++++++++++++++-------------------- utils.h | 2 +- 5 files changed, 72 insertions(+), 28 deletions(-) diff --git a/emulator.cpp b/emulator.cpp index f390b58..83ef0d6 100644 --- a/emulator.cpp +++ b/emulator.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #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; } \ No newline at end of file diff --git a/emulator.h b/emulator.h index f655ca8..c069ba1 100644 --- a/emulator.h +++ b/emulator.h @@ -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; diff --git a/scheduler.c b/scheduler.c index 6a9b371..f4ac760 100644 --- a/scheduler.c +++ b/scheduler.c @@ -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 } } diff --git a/utils.c b/utils.c index 657f6fe..56ebb09 100644 --- a/utils.c +++ b/utils.c @@ -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); } } } diff --git a/utils.h b/utils.h index 13dc6ae..03fe251 100644 --- a/utils.h +++ b/utils.h @@ -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