summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-05-27 13:49:35 +0200
committerlonkaars <loek@pipeframe.xyz>2022-05-27 13:49:35 +0200
commitdef257fcd9769d3572dbf5bdd076e4ce470fc8ec (patch)
tree4fd442954b5c6ff6319c20da03a28406f1bbf5ad
parent4c4d045329c4a149bae0b53952c39c14243e1870 (diff)
remove windows sim compatibility and fix 0xff byte handling
-rw-r--r--robot/readme.md16
-rw-r--r--robot/sercomm.c15
-rw-r--r--robot/sim.c35
-rw-r--r--robot/sim.h19
-rw-r--r--robot/tests/dirc.binbin6 -> 7 bytes
-rw-r--r--robot/tests/ping.binbin3 -> 4 bytes
-rw-r--r--shared/serial_parse.c23
-rw-r--r--shared/serial_parse.h11
8 files changed, 38 insertions, 81 deletions
diff --git a/robot/readme.md b/robot/readme.md
index 168be02..e6ab294 100644
--- a/robot/readme.md
+++ b/robot/readme.md
@@ -112,19 +112,15 @@ this list will probably get updated from time to time:
global todo:
-- [ ] start robot in calibration mode
- [ ] assume robot starts in maze
-- [ ] 'crosswalk' transition detection in seperate file (used by grid and maze
- mode)
+- [ ] 'crosswalk' transition detection and line following in seperate file
+ (used by grid, maze, and charge mode)
- [ ] client software architecture
-- [x] mode 'return' buffer
-- [x] clear global timer at start of cycle instead of just for mode selection
- module (for last ping time measurement)
-- [ ] calibrate (line-detecting) light sensors in setup.c, or manually by
- placing the robot and pressing a button (maybe make this a seperate mode)
-- [ ] create labeled timer functions like nodejs `console.time()` and
+- [ ] enter mode_scal by placing the robot on a white surface and pressing a
+ button
+- [x] create labeled timer functions like nodejs `console.time()` and
`console.timeEnd()` (use for serial read timeout constraint)
-- [ ] `serial_parse` doesn't properly handle escaped `0xff` bytes in listen
+- [x] `serial_parse` doesn't properly handle escaped `0xff` bytes in listen
mode
### hypervisor
diff --git a/robot/sercomm.c b/robot/sercomm.c
index 83d6419..e0ea39d 100644
--- a/robot/sercomm.c
+++ b/robot/sercomm.c
@@ -26,16 +26,21 @@ void w2_sercomm_main() {
#endif
// read and parse data
while (serial_get_received_bytes() != g_w2_serial_buffer_index) {
- w2_serial_parse(g_w2_serial_buffer[g_w2_serial_buffer_index]);
+ if (!w2_serial_parse(g_w2_serial_buffer[g_w2_serial_buffer_index]))
+ w2_errcatch_throw(W2_E_WARN_SERIAL_NOISY);
g_w2_serial_buffer_index = (g_w2_serial_buffer_index + 1) % W2_SERIAL_READ_BUFFER_SIZE;
}
// send data
while (g_w2_sercomm_offset != g_w2_sercomm_index) {
- w2_s_bin *data = g_w2_sercomm_buffer[g_w2_sercomm_offset];
- char *data_cast = malloc(data->bytes);
- memcpy(data_cast, data->data, data->bytes);
- serial_send(data_cast, data->bytes);
+ w2_s_bin *data = g_w2_sercomm_buffer[g_w2_sercomm_offset];
+#ifdef W2_SIM
+ w2_sim_print_serial(data);
+#endif
+ for (uint8_t i = 0; i < data->bytes; i++) {
+ uint8_t byte = data->data[i];
+ byte == 0xff ? serial_send("\xff\xff", 2) : serial_send((char *)&byte, 1);
+ }
g_w2_sercomm_offset = (g_w2_sercomm_offset + 1) % W2_SERCOMM_BUFFER_SIZE;
}
}
diff --git a/robot/sim.c b/robot/sim.c
index 4efdc4d..6283694 100644
--- a/robot/sim.c
+++ b/robot/sim.c
@@ -3,17 +3,7 @@
#include <string.h>
#include <stdint.h>
#include <unistd.h>
-
-#ifdef W2_HOST_LINUX
#include <termios.h>
-#endif
-
-#ifdef W2_HOST_WIN32
-// garbage os compatibility
-#include <windows.h>
-#include <timeapi.h>
-#define STDIN_FILENO 0
-#endif
#include "sim.h"
#include "../shared/consts.h"
@@ -21,12 +11,7 @@
#include "sercomm.h"
#include "errcatch.h"
-#ifdef W2_HOST_LINUX
struct timespec reference_time; // NOLINT
-#endif
-#ifdef W2_HOST_WIN32
-DWORD reference_time; // NOLINT
-#endif
bool g_w2_sim_headless = false;
static const char* const W2_CMD_NAMES[] = {
@@ -96,16 +81,11 @@ void serial_send(char* message, unsigned int length) {
return;
}
- if (DBG_ENABLE_PRINTFUNC) simprintfunc("serial_send", "<see below>, %u", length);
-
- if (!DBG_ENABLE_SERIAL) return;
- w2_s_bin *bin = w2_bin_s_alloc(length, (uint8_t*) message);
- w2_sim_print_serial(bin);
- free(bin);
+ if (DBG_ENABLE_PRINTFUNC) simprintfunc("serial_send", "<%u byte%s>", length, length == 1 ? "" : "s");
}
void serial_receive_ring(char* buffer, unsigned char size) {
- simprintfunc("serial_receive_ring", PTR_FMT ", %u", (uint64_t) buffer, size);
+ simprintfunc("serial_receive_ring", "0x%016lx, %u", (uint64_t) buffer, size);
}
unsigned char serial_get_received_bytes() {
@@ -118,21 +98,12 @@ void w2_sim_setup(int argc, char **argv) {
g_w2_sim_headless = true;
// disable echo and enable raw mode
-#ifdef W2_HOST_LINUX
struct termios term;
tcgetattr(STDIN_FILENO, &term);
term.c_lflag &= ~(ECHO | ICANON);
term.c_cc[VTIME] = 0;
term.c_cc[VMIN] = 0;
tcsetattr(STDIN_FILENO, 0, &term);
-#endif
-#ifdef W2_HOST_WIN32
- DWORD mode;
- HANDLE console = GetStdHandle(STD_INPUT_HANDLE);
-
- GetConsoleMode(console, &mode);
- SetConsoleMode(console, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
-#endif
// debug error
// w2_errcatch_throw(W2_E_WARN_BATTERY_LOW);
@@ -170,7 +141,7 @@ unsigned char get_single_debounced_button_press(unsigned char buttons) {
}
void qtr_read(unsigned int* sensor_values, unsigned char read_mode) {
- simprintfunc("qtr_read", PTR_FMT ", %s", (uint64_t) sensor_values, read_mode == QTR_EMITTERS_ON ? "QTR_EMITTERS_ON" : "???");
+ simprintfunc("qtr_read", "0x%016lx, %s", (uint64_t) sensor_values, read_mode == QTR_EMITTERS_ON ? "QTR_EMITTERS_ON" : "???");
sensor_values[0] = 0;
sensor_values[1] = 0;
sensor_values[2] = 0;
diff --git a/robot/sim.h b/robot/sim.h
index aa587cd..249414d 100644
--- a/robot/sim.h
+++ b/robot/sim.h
@@ -24,7 +24,6 @@ extern bool g_w2_sim_headless;
#define DBG_BYTES_PER_LINE 16
// debug colors
-#ifdef W2_HOST_LINUX
#define COL_BLK "\e[0;30m"
#define COL_RED "\e[0;31m"
#define COL_GRN "\e[0;32m"
@@ -34,18 +33,6 @@ extern bool g_w2_sim_headless;
#define COL_CYN "\e[0;36m"
#define COL_WHT "\e[0;37m"
#define COL_RST "\e[0m"
-#endif
-#ifdef W2_HOST_WIN32
-#define COL_BLK ""
-#define COL_RED ""
-#define COL_GRN ""
-#define COL_YEL ""
-#define COL_BLU ""
-#define COL_MAG ""
-#define COL_CYN ""
-#define COL_WHT ""
-#define COL_RST ""
-#endif
// debug stdout print macros
#define simprintf(message, ...) if (!g_w2_sim_headless) printf(COL_RED "[SIM] " COL_RST message, ##__VA_ARGS__)
@@ -54,12 +41,6 @@ extern bool g_w2_sim_headless;
COL_CYN name COL_RST "(" COL_YEL fmt COL_RST ")\n", ##__VA_ARGS__); }
#define simwarn(message, ...) if (DBG_ENABLE_SIMWARN) { simprintf(COL_YEL "[WARN] " COL_RST message, ##__VA_ARGS__); }
#define siminfo(message, ...) if (DBG_ENABLE_SIMINFO) { simprintf(COL_MAG "[INFO] " COL_RST message, ##__VA_ARGS__); }
-#ifdef W2_HOST_LINUX
-#define PTR_FMT "0x%016lx"
-#endif
-#ifdef W2_HOST_WIN32
-#define PTR_FMT "0x%016llx"
-#endif
#define BUTTON_A 0
#define BUTTON_B 1
diff --git a/robot/tests/dirc.bin b/robot/tests/dirc.bin
index 1aea35c..8d141e8 100644
--- a/robot/tests/dirc.bin
+++ b/robot/tests/dirc.bin
Binary files differ
diff --git a/robot/tests/ping.bin b/robot/tests/ping.bin
index 456804e..2bbe45e 100644
--- a/robot/tests/ping.bin
+++ b/robot/tests/ping.bin
Binary files differ
diff --git a/shared/serial_parse.c b/shared/serial_parse.c
index 3bc8e3b..b1b4f50 100644
--- a/shared/serial_parse.c
+++ b/shared/serial_parse.c
@@ -3,8 +3,7 @@
#include "consts.h"
#include "serial_parse.h"
-// TODO: give this function last time of byte, and measure if >5ms, throw warning
-void w2_serial_parse(uint8_t byte) {
+bool w2_serial_parse(uint8_t byte) {
static uint8_t current_message[W2_SERIAL_READ_BUFFER_SIZE] = {0};
static uint8_t current_message_index = 0;
static uint8_t complete_message_length = 2;
@@ -14,18 +13,14 @@ void w2_serial_parse(uint8_t byte) {
if (byte == W2_SERIAL_START_BYTE) {
attentive = !attentive;
- // if (attentive && listening) {
- // current_message[current_message_index++] = byte;
- // }
- } else {
- // activate listen after non-0xff byte after 0xff
- if (attentive && !listening) {
- attentive = false;
- listening = true;
- }
+ if (attentive && listening) return W2_SERIAL_READ_SUCCESS;
+ } else if (attentive) {
+ attentive = false;
+ listening = !listening;
+ if (!listening) return W2_SERIAL_READ_FAILURE;
}
- if (!listening) return;
+ if (!listening) return W2_SERIAL_READ_SUCCESS;
current_message[current_message_index++] = byte;
complete_message_length = w2_cmd_sizeof(current_message, current_message_index);
@@ -38,6 +33,8 @@ void w2_serial_parse(uint8_t byte) {
complete_message_length = 1;
attentive = false;
listening = false;
- return;
+ return W2_SERIAL_READ_SUCCESS;
}
+
+ return W2_SERIAL_READ_SUCCESS;
}
diff --git a/shared/serial_parse.h b/shared/serial_parse.h
index 03c420f..0816ea1 100644
--- a/shared/serial_parse.h
+++ b/shared/serial_parse.h
@@ -4,7 +4,14 @@
#include <stdint.h>
+#include "bool.h"
#include "protocol.h"
-/** parse serial data byte by byte */
-void w2_serial_parse(uint8_t byte);
+#define W2_SERIAL_READ_SUCCESS true
+#define W2_SERIAL_READ_FAILURE false
+
+/**
+ * parse serial data byte by byte
+ * @return true if read success, false if read fails
+ */
+bool w2_serial_parse(uint8_t byte);