From edfa25fcd8edad43998f50a2144d30a6f966c1c8 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 26 May 2022 21:52:30 +0200 Subject: fix configuration on windows --- robot/makefile | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'robot') diff --git a/robot/makefile b/robot/makefile index 4039ae3..85ed2e3 100644 --- a/robot/makefile +++ b/robot/makefile @@ -30,10 +30,6 @@ AVRDUDE=avrdude CC=$(PREFIX)gcc OBJ2HEX=$(PREFIX)objcopy -# debug build info string -BUILD_STR=$(shell git update-index -q --refresh; git describe --tags --dirty='*' --broken='x' | cut -c1-20) -CFLAGS += -DW2_BUILD_STR=\"$(BUILD_STR)\" - clean:: rm -f *.o out.hex a.out @@ -57,4 +53,4 @@ format: clang-tidy --fix-errors $(SOURCES) $(HEADERS) compile_commands: clean - bear -- make + compiledb make -- cgit v1.2.3 From 3fd5d966f3b0cabd9f6931bc1ca416408f9e101c Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 26 May 2022 22:16:55 +0200 Subject: compile sim on windows without errors --- robot/makefile | 11 ++++++----- robot/sim.c | 35 ++++++++++++++++++++++++++++++++--- robot/sim.h | 13 +++++++++++++ shared/makefile | 9 --------- shared/os.mk | 12 ++++++++++++ 5 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 shared/os.mk (limited to 'robot') diff --git a/robot/makefile b/robot/makefile index 85ed2e3..a913bd7 100644 --- a/robot/makefile +++ b/robot/makefile @@ -6,18 +6,19 @@ MCU ?= atmega168 AVRDUDE_DEVICE ?= m168 PORT ?= /dev/ttyACM0 +SIM = true CFLAGS=-g -Wall $(DEVICE_SPECIFIC_CFLAGS) -Os LDFLAGS=-Wl,-gc-sections -Wl,-relax -all: $(if $(SIM), a.out, out.hex) +include ../shared/os.mk +all: $(if $(SIM), $(TARGET), out.hex) SOURCES := $(filter-out sim.c, $(wildcard *.c)) HEADERS := $(filter-out sim.h, $(wildcard *.h)) include ../shared/makefile # simulation -# SIM = true CFLAGS += $(if $(SIM), -DW2_SIM, -mcall-prologues -mmcu=$(MCU)) LDFLAGS += $(if $(SIM), , -lpololu_$(DEVICE)) PREFIX := $(if $(SIM), , avr-) @@ -31,15 +32,15 @@ CC=$(PREFIX)gcc OBJ2HEX=$(PREFIX)objcopy clean:: - rm -f *.o out.hex a.out + rm -f *.o out.hex $(TARGET) -a.out: $(OBJECTS) +$(TARGET): $(OBJECTS) $(CC) $(OBJECTS) $(CFLAGS) $(LDFLAGS) .o: $(CC) -c $(CFLAGS) $< -out.hex: a.out +out.hex: $(TARGET) $(OBJ2HEX) -R .eeprom -O ihex $< $@ $(info build $(BUILD_STR) complete) diff --git a/robot/sim.c b/robot/sim.c index 0f1c7e6..ba9aa4a 100644 --- a/robot/sim.c +++ b/robot/sim.c @@ -2,16 +2,31 @@ #include #include #include -#include #include +#ifdef W2_HOST_LINUX +#include +#endif + +#ifdef W2_HOST_WIN32 +// garbage os compatibility +#include +#include +#define STDIN_FILENO 0 +#endif + #include "sim.h" #include "../shared/consts.h" #include "../shared/protocol.h" #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[] = { @@ -38,15 +53,20 @@ static const char* const W2_CMD_DIRECTIONS[] = { void time_reset() { simprintfunc("time_reset", ""); +#ifdef W2_HOST_LINUX clock_gettime(CLOCK_MONOTONIC, &reference_time); +#endif } unsigned long get_ms() { simprintfunc("get_ms", ""); +#ifdef W2_HOST_LINUX struct timespec elapsed; clock_gettime(CLOCK_MONOTONIC, &elapsed); return ((elapsed.tv_sec * 1000) + (elapsed.tv_nsec / 1000000)) - ((reference_time.tv_sec * 1000) + (reference_time.tv_nsec / 1000000)); +#endif + return 0; } void red_led(unsigned char on) { @@ -85,7 +105,7 @@ void serial_send(char* message, unsigned int length) { } void serial_receive_ring(char* buffer, unsigned char size) { - simprintfunc("serial_receive_ring", "0x%016lx, %u", (unsigned long) buffer, size); + simprintfunc("serial_receive_ring", "0x%016llx, %u", (uint64_t) buffer, size); } unsigned char serial_get_received_bytes() { @@ -98,12 +118,21 @@ 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); @@ -141,7 +170,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", "0x%016lx, %s", (uint64_t) sensor_values, read_mode == QTR_EMITTERS_ON ? "QTR_EMITTERS_ON" : "???"); + simprintfunc("qtr_read", "0x%016llx, %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 ab1cd77..08c1d8a 100644 --- a/robot/sim.h +++ b/robot/sim.h @@ -22,6 +22,7 @@ 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" @@ -31,6 +32,18 @@ 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__) diff --git a/shared/makefile b/shared/makefile index 7f0ceb9..e5a6ff6 100644 --- a/shared/makefile +++ b/shared/makefile @@ -5,14 +5,5 @@ HEADERS += $(wildcard ../shared/*.h) BUILD_STR=$(shell git update-index -q --refresh; git describe --tags --dirty='*' --broken='x' | cut -c1-20) CFLAGS += -DW2_BUILD_STR=\"$(BUILD_STR)\" -# os info -OS=$(strip $(shell uname -o)) -ifeq ($(OS),GNU/Linux) -CFLAGS += -DW2_HOST_LINUX -endif -ifeq ($(OS),Msys) -CFLAGS += -DW2_HOST_WIN32 -endif - clean:: rm -f ../shared/*.o diff --git a/shared/os.mk b/shared/os.mk new file mode 100644 index 0000000..e4ede42 --- /dev/null +++ b/shared/os.mk @@ -0,0 +1,12 @@ +# os info +OS=$(strip $(shell uname -o)) +ifeq ($(OS),GNU/Linux) +CFLAGS += -DW2_HOST_LINUX +LINUX := true +TARGET := a.out +endif +ifeq ($(OS),Msys) +CFLAGS += -DW2_HOST_WIN32 +WIN32 := true +TARGET := a.exe +endif \ No newline at end of file -- cgit v1.2.3 From b6460d729aa683643e7ad83a9921f335f0fdba93 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 26 May 2022 22:17:19 +0200 Subject: disable sim on master branch --- robot/makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'robot') diff --git a/robot/makefile b/robot/makefile index a913bd7..f65552a 100644 --- a/robot/makefile +++ b/robot/makefile @@ -6,7 +6,7 @@ MCU ?= atmega168 AVRDUDE_DEVICE ?= m168 PORT ?= /dev/ttyACM0 -SIM = true +# SIM = true CFLAGS=-g -Wall $(DEVICE_SPECIFIC_CFLAGS) -Os LDFLAGS=-Wl,-gc-sections -Wl,-relax -- cgit v1.2.3 From bfa494588da0def96c42577e284c6d8990eb31f2 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 26 May 2022 22:46:17 +0200 Subject: add doxygen --- .gitignore | 1 + Doxyfile | 21 +++++++++++++++++++++ makefile | 5 ++++- robot/errcatch.h | 2 ++ robot/hypervisor.h | 2 ++ robot/io.h | 6 ++++-- robot/main.h | 2 ++ robot/mode_chrg.h | 2 ++ robot/mode_dirc.h | 2 ++ robot/mode_grid.h | 2 ++ robot/mode_halt.h | 2 ++ robot/mode_lcal.h | 2 ++ robot/mode_maze.h | 2 ++ robot/mode_scal.h | 2 ++ robot/mode_spin.h | 2 ++ robot/modes.h | 2 ++ robot/orangutan_shim.h | 2 ++ robot/sercomm.h | 2 ++ robot/setup.h | 2 ++ robot/sim.h | 2 ++ shared/bin.h | 3 +++ shared/consts.h | 4 +++- shared/errors.h | 2 ++ shared/protocol.h | 2 ++ shared/semver.h | 2 ++ shared/serial_parse.h | 2 ++ shared/util.h | 2 ++ 27 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 Doxyfile (limited to 'robot') diff --git a/.gitignore b/.gitignore index d493881..74f4594 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ client/main .cache *.log scripts/InstallationLog.txt +docs/ diff --git a/Doxyfile b/Doxyfile new file mode 100644 index 0000000..8c225e9 --- /dev/null +++ b/Doxyfile @@ -0,0 +1,21 @@ +DOXYFILE_ENCODING = UTF-8 +PROJECT_NAME = "wall-e2" +# PROJECT_NUMBER = +PROJECT_BRIEF = Project RobotRun software +OUTPUT_DIRECTORY = docs/ +CREATE_SUBDIRS = NO +ALLOW_UNICODE_NAMES = NO +OUTPUT_LANGUAGE = English +TAB_SIZE = 4 +FILE_PATTERNS = *.c \ + *.h \ + *.md +RECURSIVE = YES +USE_MDFILE_AS_MAINPAGE = ./readme.md +SOURCE_BROWSER = YES +INLINE_SOURCES = YES +STRIP_CODE_COMMENTS = NO +REFERENCED_BY_RELATION = YES +REFERENCES_RELATION = YES +CALL_GRAPH = YES +# INPUT = robot/ diff --git a/makefile b/makefile index 7795106..832bde9 100644 --- a/makefile +++ b/makefile @@ -5,4 +5,7 @@ all clean format compile_commands: $(SUBDIRS) FORCE $(SUBDIRS): FORCE $(MAKE) -C $@ $(MAKECMDGOALS) -FORCE: \ No newline at end of file +FORCE: + +docs: + doxygen diff --git a/robot/errcatch.h b/robot/errcatch.h index 836da1b..add4ece 100644 --- a/robot/errcatch.h +++ b/robot/errcatch.h @@ -1,5 +1,7 @@ #pragma once +/** @file errcatch.h */ + #include #include "../shared/consts.h" diff --git a/robot/hypervisor.h b/robot/hypervisor.h index 5008c8f..b5dc0ab 100644 --- a/robot/hypervisor.h +++ b/robot/hypervisor.h @@ -1,5 +1,7 @@ #pragma once +/** @file hypervisor.h */ + #include extern uint64_t g_w2_hypervisor_cycles; diff --git a/robot/io.h b/robot/io.h index 3d91799..be0e9b9 100644 --- a/robot/io.h +++ b/robot/io.h @@ -1,9 +1,11 @@ #pragma once +/** @file io.h */ + #include "../shared/protocol.h" -/** i/o module main */ +/** @brief i/o module main */ void w2_io_main(); -/** global struct containing all i/o */ +/** @brief global struct containing all i/o */ extern w2_s_io_all g_w2_io; diff --git a/robot/main.h b/robot/main.h index 5b0a1b2..58f849b 100644 --- a/robot/main.h +++ b/robot/main.h @@ -1,4 +1,6 @@ #pragma once +/** @file main.h */ + /** program entrypoint */ int main(); diff --git a/robot/mode_chrg.h b/robot/mode_chrg.h index d9b5cc0..a870e58 100644 --- a/robot/mode_chrg.h +++ b/robot/mode_chrg.h @@ -1,5 +1,7 @@ #pragma once +/** @file mode_chrg.h */ + /** * charge station mode * diff --git a/robot/mode_dirc.h b/robot/mode_dirc.h index 5b9bbf4..12c967a 100644 --- a/robot/mode_dirc.h +++ b/robot/mode_dirc.h @@ -1,5 +1,7 @@ #pragma once +/** @file mode_dirc.h */ + #include extern int16_t g_w2_mode_dirc_motor_l; diff --git a/robot/mode_grid.h b/robot/mode_grid.h index fcf9100..55093ad 100644 --- a/robot/mode_grid.h +++ b/robot/mode_grid.h @@ -1,5 +1,7 @@ #pragma once +/** @file mode_grid.h */ + /** * warehouse mode * diff --git a/robot/mode_halt.h b/robot/mode_halt.h index d92905e..b5ac11f 100644 --- a/robot/mode_halt.h +++ b/robot/mode_halt.h @@ -1,5 +1,7 @@ #pragma once +/** @file mode_halt.h */ + /** * halt (emergency) mode * diff --git a/robot/mode_lcal.h b/robot/mode_lcal.h index dd373f0..5a43701 100644 --- a/robot/mode_lcal.h +++ b/robot/mode_lcal.h @@ -1,5 +1,7 @@ #pragma once +/** @file mode_lcal.h */ + /** * calibration mode * diff --git a/robot/mode_maze.h b/robot/mode_maze.h index 9fbeb8c..e7490b9 100644 --- a/robot/mode_maze.h +++ b/robot/mode_maze.h @@ -1,5 +1,7 @@ #pragma once +/** @file mode_maze.h */ + /** * maze mode * diff --git a/robot/mode_scal.h b/robot/mode_scal.h index 4f1f04d..c427d4b 100644 --- a/robot/mode_scal.h +++ b/robot/mode_scal.h @@ -1,5 +1,7 @@ #pragma once +/** @file mode_scal.h */ + /** * sensor calibration mode * diff --git a/robot/mode_spin.h b/robot/mode_spin.h index 926137e..1f721dc 100644 --- a/robot/mode_spin.h +++ b/robot/mode_spin.h @@ -1,5 +1,7 @@ #pragma once +/** @file mode_spin.h */ + /** * wet floor simulation * diff --git a/robot/modes.h b/robot/modes.h index 3423a0f..122be4a 100644 --- a/robot/modes.h +++ b/robot/modes.h @@ -1,5 +1,7 @@ #pragma once +/** @file modes.h */ + #include "../shared/consts.h" #include "mode_chrg.h" diff --git a/robot/orangutan_shim.h b/robot/orangutan_shim.h index 10420bd..cbb0995 100644 --- a/robot/orangutan_shim.h +++ b/robot/orangutan_shim.h @@ -1,5 +1,7 @@ #pragma once +/** @file orangutan_shim.h */ + #ifdef W2_SIM #include "sim.h" #else diff --git a/robot/sercomm.h b/robot/sercomm.h index b1f69c7..13625c0 100644 --- a/robot/sercomm.h +++ b/robot/sercomm.h @@ -1,5 +1,7 @@ #pragma once +/** @file sercomm.h */ + #include "../shared/bin.h" #include "../shared/consts.h" #include "../shared/protocol.h" diff --git a/robot/setup.h b/robot/setup.h index 17ac78d..450e102 100644 --- a/robot/setup.h +++ b/robot/setup.h @@ -1,5 +1,7 @@ #pragma once +/** @file setup.h */ + /** * runs once at startup, plays beep when setup finishes * diff --git a/robot/sim.h b/robot/sim.h index 08c1d8a..76b57f8 100644 --- a/robot/sim.h +++ b/robot/sim.h @@ -1,5 +1,7 @@ #pragma once +/** @file sim.h */ + #include #include #include diff --git a/shared/bin.h b/shared/bin.h index af3a249..48485c8 100644 --- a/shared/bin.h +++ b/shared/bin.h @@ -1,4 +1,7 @@ #pragma once + +/** @file bin.h */ + /** * helper file for binary data * diff --git a/shared/consts.h b/shared/consts.h index 70ab5b2..50b16b9 100644 --- a/shared/consts.h +++ b/shared/consts.h @@ -1,5 +1,7 @@ #pragma once +/** @file consts.h */ + #ifndef W2_BUILD_STR // is defined by CFLAGS += -DW2_BUILD_STR in makefile #define W2_BUILD_STR ("????????") @@ -30,4 +32,4 @@ /** battery voltage sensor pinout */ #define W2_BATTERY_PIN 6 /** side-facing distance sensor pinout */ -#define W2_SIDE_SENSOR_PIN 7 \ No newline at end of file +#define W2_SIDE_SENSOR_PIN 7 diff --git a/shared/errors.h b/shared/errors.h index ac8e95f..4c9f7c8 100644 --- a/shared/errors.h +++ b/shared/errors.h @@ -1,5 +1,7 @@ #pragma once +/** @file errors.h */ + #include #define W2_E_TYPE_MASK (0b11 << 6) diff --git a/shared/protocol.h b/shared/protocol.h index 7aa5e9f..05cde03 100644 --- a/shared/protocol.h +++ b/shared/protocol.h @@ -1,5 +1,7 @@ #pragma once +/** @file protocol.h */ + #include #include diff --git a/shared/semver.h b/shared/semver.h index a99ae61..6a1da79 100644 --- a/shared/semver.h +++ b/shared/semver.h @@ -1,5 +1,7 @@ #pragma once +/** @file semver.h */ + #include typedef struct { diff --git a/shared/serial_parse.h b/shared/serial_parse.h index a1c8fb9..03c420f 100644 --- a/shared/serial_parse.h +++ b/shared/serial_parse.h @@ -1,5 +1,7 @@ #pragma once +/** @file serial_parse.h */ + #include #include "protocol.h" diff --git a/shared/util.h b/shared/util.h index 9e4d8ac..465e043 100644 --- a/shared/util.h +++ b/shared/util.h @@ -1,5 +1,7 @@ #pragma once +/** @file util.h */ + #define W2_MIN(a, b) (((a) < (b)) ? (a) : (b)) #define W2_MAX(a, b) (((a) > (b)) ? (a) : (b)) #define W2_RANGE(min, val, max) W2_MIN(max, W2_MAX(val, min)) -- cgit v1.2.3 From 3bbe41a04b6053a3a3a902384850cfcbd38b0686 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 27 May 2022 11:40:05 +0200 Subject: move things around and add todo's to robot readme --- robot/io.h | 2 +- robot/readme.md | 20 ++++++++++--------- shared/bool.h | 9 +++++++++ shared/io.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ shared/protocol.h | 56 ++-------------------------------------------------- 5 files changed, 82 insertions(+), 64 deletions(-) create mode 100644 shared/bool.h create mode 100644 shared/io.h (limited to 'robot') diff --git a/robot/io.h b/robot/io.h index be0e9b9..64ce293 100644 --- a/robot/io.h +++ b/robot/io.h @@ -2,7 +2,7 @@ /** @file io.h */ -#include "../shared/protocol.h" +#include "../shared/io.h" /** @brief i/o module main */ void w2_io_main(); diff --git a/robot/readme.md b/robot/readme.md index b27c06f..25e730f 100644 --- a/robot/readme.md +++ b/robot/readme.md @@ -72,15 +72,15 @@ what they're supposed to do: this list will probably get updated from time to time: -- modules shouldn't create any global state variables, they should use `static` - variables instead. -- modules are run cyclically, so they shouldn't take more than +- logic modules shouldn't create any global state variables if possible, they + should use `static` variables instead +- logic modules are run cyclically, so they shouldn't take more than `W2_MAX_MODULE_CYCLE_MS` to execute (this is an arbitrary number, and may be - changed). + changed) - documentation comments should follow the [javadoc-style doxygen format](https://wiki.scilab.org/Doxygen%20documentation%20Examples) and be - placed in header (.h) files if possible. this only applies to public members - (e.g. no local variables or module-internal code). + placed in header (.h) files. this only applies to public members (e.g. no + local variables or module-internal code) - code style is mostly handled by `clang-format` and `clang-tidy`, but you should still follow these naming conventions (`` indicate placeholders): @@ -93,15 +93,15 @@ this list will probably get updated from time to time: |enum|`w2_e_`|`w2_e_errorcodes`; `w2_e_serial_commands`| this again only applies to public members. local variables should still have - short descriptive names, but shouldn't be prefixed with `w2_*`. + short descriptive names, but shouldn't be prefixed with `w2_*` - arbitrary numbers should be aliased to `#define` statements or `enum`s if - part of a series. + part of a series - general constants should be placed in `consts.h` - don't import `` directly, instead use `"orangutan_shim.h"` to keep code compatible with the simulator - don't use ``, instead use `"../shared/protocol.h"`. this makes sure that `bool` values are equal to `uint8_t`. they're functionally - identical. + identical ## todo @@ -119,6 +119,8 @@ global todo: placing the robot and pressing a button (maybe make this a seperate mode) - [ ] 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 + mode ### hypervisor diff --git a/shared/bool.h b/shared/bool.h new file mode 100644 index 0000000..9ea97f7 --- /dev/null +++ b/shared/bool.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +/** @file bool.h */ + +typedef uint8_t bool; +#define false 0 /* NOLINT */ +#define true 1 /* NOLINT */ diff --git a/shared/io.h b/shared/io.h new file mode 100644 index 0000000..584ad1e --- /dev/null +++ b/shared/io.h @@ -0,0 +1,59 @@ +#pragma once + +#include + +#include "bool.h" + +#pragma pack(push, 1) + +/** momentary button input struct */ +typedef struct { + bool pressed; +} w2_s_i_push; + +/** qtr contrast sensor input struct */ +typedef struct { + uint16_t range; +} w2_s_i_contrast; + +/** distance sensor input struct */ +typedef struct { + uint16_t detection; +} w2_s_i_distance; + +/** battery input struct */ +typedef struct { + uint16_t charge_level; +} w2_s_i_battery; + +/** motor output struct */ +typedef struct { + int16_t speed; +} w2_s_o_motor; + +/** underside led output struct */ +typedef struct { + bool on; +} w2_s_o_led; + +/** lcd output struct */ +typedef struct { + char text[16]; +} w2_s_o_display; + +/** struct containing all i/o */ +typedef struct { + w2_s_i_push button[5]; + w2_s_i_contrast qtr[5]; + w2_s_i_distance front_distance; + w2_s_i_distance side_distance; + w2_s_i_battery battery; + + w2_s_o_motor motor_left; + w2_s_o_motor motor_right; + w2_s_o_led led_red; + w2_s_o_led led_green; + w2_s_o_display lcd; +} w2_s_io_all; + +#pragma pack(pop) diff --git a/shared/protocol.h b/shared/protocol.h index 05cde03..2e1e4ea 100644 --- a/shared/protocol.h +++ b/shared/protocol.h @@ -6,11 +6,9 @@ #include #include "bin.h" +#include "bool.h" #include "consts.h" - -typedef uint8_t bool; -#define false 0 /* NOLINT */ -#define true 1 /* NOLINT */ +#include "io.h" #define W2_SERIAL_START_BYTE 0xff @@ -54,56 +52,6 @@ typedef enum { #pragma pack(push, 1) -/** momentary button input struct */ -typedef struct { - bool pressed; -} w2_s_i_push; - -/** qtr contrast sensor input struct */ -typedef struct { - uint16_t range; -} w2_s_i_contrast; - -/** distance sensor input struct */ -typedef struct { - uint16_t detection; -} w2_s_i_distance; - -/** battery input struct */ -typedef struct { - uint16_t charge_level; -} w2_s_i_battery; - -/** motor output struct */ -typedef struct { - int16_t speed; -} w2_s_o_motor; - -/** underside led output struct */ -typedef struct { - bool on; -} w2_s_o_led; - -/** lcd output struct */ -typedef struct { - char text[16]; -} w2_s_o_display; - -/** struct containing all i/o */ -typedef struct { - w2_s_i_push button[5]; - w2_s_i_contrast qtr[5]; - w2_s_i_distance front_distance; - w2_s_i_distance side_distance; - w2_s_i_battery battery; - - w2_s_o_motor motor_left; - w2_s_o_motor motor_right; - w2_s_o_led led_red; - w2_s_o_led led_green; - w2_s_o_display lcd; -} w2_s_io_all; - typedef struct { uint8_t opcode; uint8_t id; -- cgit v1.2.3 From 05318790dcbd6714a2adb3532e902a56a6638ca0 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 27 May 2022 12:16:53 +0200 Subject: edit robot readme --- robot/readme.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'robot') diff --git a/robot/readme.md b/robot/readme.md index 25e730f..0c8f626 100644 --- a/robot/readme.md +++ b/robot/readme.md @@ -39,10 +39,15 @@ organizational and form more of a software 'skeleton', while the 'maze' and ┌────────┴───────┐┌─────────┴────────┐┌────────┴─────────┐┌─────┴──────┐ │ Error handling ││ I/O Read & Write ││ PC communication ││ Mode logic │ └────────────────┘└──────────────────┘└──────────────────┘└─────┬──────┘ - ┌──────────┬──────────────┬───────────────┤ - ┌───┴──┐┌──────┴────┐┌────────┴───────┐┌──────┴──────┐ - *modes* -> │ Maze ││ Warehouse ││ Emergency stop ││ Calibration │ - └──────┘└───────────┘└────────────────┘└─────────────┘ + │ + Maze ─┤ + Warehouse ─┤ + Emergency stop ─┤ + *modes* -> Line finding ─┤ + Charge station ─┤ + Direct control ─┤ + Wet floor ─┤ + Sensor calibration ─┘ ``` this diagram roughly describes how different parts of the robot software are @@ -64,7 +69,7 @@ what they're supposed to do: |emergency stop |`mode_halt `|may 31|Fiona| stops all execution until emergency mode is reset by software or user| |line finding |`mode_lcal `|may 31|Fiona| find line by turning on own axis if lost| |charge station |`mode_chrg `|may 31|Fiona| go to the charging station transition in the grid, and continue until a black circle is found| -|direct control |`mode_dirc `|may 31|Loek| respond to [DIRC](../protocol.md#DIRC) commands| +|direct control |`mode_dirc `|done|Loek| respond to [DIRC](../protocol.md#DIRC) commands| |wet floor |`mode_spin `|may 31|Fiona| spin uncontrollably (simulating wet floor??)| |sensor calibration|`mode_scal `|may 31|Jorn & Abdullaahi| calibrate underside uv sensors| -- cgit v1.2.3 From 4c4d045329c4a149bae0b53952c39c14243e1870 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 27 May 2022 12:53:42 +0200 Subject: throw error on noisy serial channel --- robot/- | 0 robot/hypervisor.c | 21 +++++++++++++++------ robot/hypervisor.h | 8 ++++++++ robot/readme.md | 2 +- robot/sercomm.c | 21 +++++++++++++++++++++ robot/sim.c | 4 ++-- robot/sim.h | 6 ++++++ shared/consts.h | 15 +++++++++------ shared/errors.h | 2 -- shared/protocol.c | 19 ------------------- 10 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 robot/- (limited to 'robot') diff --git a/robot/- b/robot/- new file mode 100644 index 0000000..e69de29 diff --git a/robot/hypervisor.c b/robot/hypervisor.c index 0baa406..1fd3ac2 100644 --- a/robot/hypervisor.c +++ b/robot/hypervisor.c @@ -6,12 +6,13 @@ #include "orangutan_shim.h" #include "sercomm.h" -uint64_t g_w2_hypervisor_cycles = 0; -uint64_t g_w2_hypervisor_uptime_ms = 0; -unsigned long g_w2_hypervisor_ema_sercomm_ms = 0; -unsigned long g_w2_hypervisor_ema_errcatch_ms = 0; -unsigned long g_w2_hypervisor_ema_io_ms = 0; -unsigned long g_w2_hypervisor_ema_mode_ms = 0; +uint64_t g_w2_hypervisor_cycles = 0; +uint64_t g_w2_hypervisor_uptime_ms = 0; +unsigned long g_w2_hypervisor_ema_sercomm_ms = 0; +unsigned long g_w2_hypervisor_ema_errcatch_ms = 0; +unsigned long g_w2_hypervisor_ema_io_ms = 0; +unsigned long g_w2_hypervisor_ema_mode_ms = 0; +uint64_t g_w2_hypervisor_timers[W2_HYPERVISOR_TIMER_COUNT] = {0}; void w2_hypervisor_main() { #ifdef W2_SIM @@ -51,3 +52,11 @@ void w2_hypervisor_main() { g_w2_hypervisor_cycles++; } + +void w2_hypervisor_time_start(uint8_t label) { + g_w2_hypervisor_timers[label] = g_w2_hypervisor_uptime_ms; +} + +uint64_t w2_hypervisor_time_end(uint8_t label) { + return g_w2_hypervisor_uptime_ms - g_w2_hypervisor_timers[label]; +} diff --git a/robot/hypervisor.h b/robot/hypervisor.h index b5dc0ab..589d324 100644 --- a/robot/hypervisor.h +++ b/robot/hypervisor.h @@ -4,6 +4,9 @@ #include +/** amount of parallel timers */ +#define W2_HYPERVISOR_TIMER_COUNT (1) + extern uint64_t g_w2_hypervisor_cycles; extern uint64_t g_w2_hypervisor_uptime_ms; @@ -18,3 +21,8 @@ extern unsigned long g_w2_hypervisor_ema_mode_ms; * stores global variables and controls when other modules run */ void w2_hypervisor_main(); + +/** start timer with label `label` */ +void w2_hypervisor_time_start(uint8_t label); +/** stop timer with label `label` */ +uint64_t w2_hypervisor_time_end(uint8_t label); diff --git a/robot/readme.md b/robot/readme.md index 0c8f626..168be02 100644 --- a/robot/readme.md +++ b/robot/readme.md @@ -43,7 +43,7 @@ organizational and form more of a software 'skeleton', while the 'maze' and Maze ─┤ Warehouse ─┤ Emergency stop ─┤ - *modes* -> Line finding ─┤ + *logic modes* -> Line finding ─┤ Charge station ─┤ Direct control ─┤ Wet floor ─┤ diff --git a/robot/sercomm.c b/robot/sercomm.c index 2736030..83d6419 100644 --- a/robot/sercomm.c +++ b/robot/sercomm.c @@ -3,6 +3,7 @@ #include "../shared/bin.h" #include "../shared/serial_parse.h" +#include "errcatch.h" #include "hypervisor.h" #include "io.h" #include "mode_dirc.h" @@ -51,6 +52,26 @@ void w2_sercomm_append_msg(w2_s_bin *data) { g_w2_sercomm_index = next_index; } +void w2_cmd_handler(uint8_t data[W2_SERIAL_READ_BUFFER_SIZE], uint8_t data_length) { + w2_s_bin *copy = w2_bin_s_alloc(data_length, data); + void (*handler)(w2_s_bin *) = g_w2_cmd_handlers[data[0]]; + + if (handler == NULL) { +#ifdef W2_SIM + // TODO throw warning + simwarn("unknown serial message with code 0x%02x\n", data[0]); +#endif + w2_errcatch_throw(W2_E_WARN_SERIAL_NOISY); + } else { +#ifdef W2_SIM + w2_sim_print_serial(copy); +#endif + handler(copy); + } + + free(copy); +} + void w2_cmd_ping_rx(w2_s_bin *data) { w2_s_cmd_ping_rx *message = malloc(w2_cmd_sizeof(data->data, data->bytes)); memcpy(message, data->data, data->bytes); diff --git a/robot/sim.c b/robot/sim.c index ba9aa4a..4efdc4d 100644 --- a/robot/sim.c +++ b/robot/sim.c @@ -105,7 +105,7 @@ void serial_send(char* message, unsigned int length) { } void serial_receive_ring(char* buffer, unsigned char size) { - simprintfunc("serial_receive_ring", "0x%016llx, %u", (uint64_t) buffer, size); + simprintfunc("serial_receive_ring", PTR_FMT ", %u", (uint64_t) buffer, size); } unsigned char serial_get_received_bytes() { @@ -170,7 +170,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", "0x%016llx, %s", (uint64_t) sensor_values, read_mode == QTR_EMITTERS_ON ? "QTR_EMITTERS_ON" : "???"); + simprintfunc("qtr_read", PTR_FMT ", %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 76b57f8..aa587cd 100644 --- a/robot/sim.h +++ b/robot/sim.h @@ -54,6 +54,12 @@ 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/shared/consts.h b/shared/consts.h index 50b16b9..cdd96b3 100644 --- a/shared/consts.h +++ b/shared/consts.h @@ -12,21 +12,24 @@ #warning "host operating system unknown" #endif -/** max logic module execution time in milliseconds */ -#define W2_MAX_MODULE_CYCLE_MS (20) /** serial baud rate (bit/s) */ #define W2_SERIAL_BAUD (9600) +/** size of input (receive) buffer (in bytes) */ +#define W2_SERIAL_READ_BUFFER_SIZE (255) + /** size of the error handling buffer (in errors, not bytes) */ #define W2_ERROR_BUFFER_SIZE (16) /** size of the serial communication buffer (in messages, not bytes) */ #define W2_SERCOMM_BUFFER_SIZE (16) -/** size of input (receive) buffer (in bytes) */ -#define W2_SERIAL_READ_BUFFER_SIZE (255) -/** exponential moving average new measurement weight (double 0-1) */ -#define W2_EMA_WEIGHT (0.10) /** size of mode history buffer */ #define W2_MODE_HISTORY_BUFFER_SIZE (4) +/** max logic module execution time in milliseconds */ +#define W2_MAX_MODULE_CYCLE_MS (20) + +/** exponential moving average new measurement weight (double 0-1) */ +#define W2_EMA_WEIGHT (0.10) + /** front-facing distance sensor pinout */ #define W2_FRONT_SENSOR_PIN 5 /** battery voltage sensor pinout */ diff --git a/shared/errors.h b/shared/errors.h index 4c9f7c8..344a506 100644 --- a/shared/errors.h +++ b/shared/errors.h @@ -45,8 +45,6 @@ typedef enum { W2_E_WARN_SERCOMM_BUFFER_FULL = 0x06 | W2_E_TYPE_WARN, /** semver minor version doesn't match */ W2_E_WARN_VERSION_INCOMPATIBLE = 0x07 | W2_E_TYPE_WARN, - /** serial byte took to long to receive */ - W2_E_WARN_SERIAL_TIMEOUT = 0x08 | W2_E_TYPE_WARN, /** unknown message encountered (noisy channel?) */ W2_E_WARN_SERIAL_NOISY = 0x09 | W2_E_TYPE_WARN, /** mode history index out of bounds */ diff --git a/shared/protocol.c b/shared/protocol.c index fcc9fa4..9be1d31 100644 --- a/shared/protocol.c +++ b/shared/protocol.c @@ -74,22 +74,3 @@ size_t w2_cmd_expt_tx_sizeof(w2_s_bin *data) { size_t w2_cmd_mcfg_rx_sizeof(w2_s_bin *data) { return W2_DYN_MEMBER_SIZEOF(w2_s_cmd_mcfg_rx, 3, w2_s_cmd_mcfg_feature); } - -void w2_cmd_handler(uint8_t data[W2_SERIAL_READ_BUFFER_SIZE], uint8_t data_length) { - w2_s_bin *copy = w2_bin_s_alloc(data_length, data); - void (*handler)(w2_s_bin *) = g_w2_cmd_handlers[data[0]]; - - if (handler == NULL) { -#ifdef W2_SIM - // TODO throw warning - simwarn("unknown serial message with code 0x%02x\n", data[0]); -#endif - } else { -#ifdef W2_SIM - w2_sim_print_serial(copy); -#endif - handler(copy); - } - - free(copy); -} -- cgit v1.2.3 From def257fcd9769d3572dbf5bdd076e4ce470fc8ec Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 27 May 2022 13:49:35 +0200 Subject: remove windows sim compatibility and fix 0xff byte handling --- robot/readme.md | 16 ++++++---------- robot/sercomm.c | 15 ++++++++++----- robot/sim.c | 35 +++-------------------------------- robot/sim.h | 19 ------------------- robot/tests/dirc.bin | Bin 6 -> 7 bytes robot/tests/ping.bin | Bin 3 -> 4 bytes shared/serial_parse.c | 23 ++++++++++------------- shared/serial_parse.h | 11 +++++++++-- 8 files changed, 38 insertions(+), 81 deletions(-) (limited to 'robot') 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 #include #include - -#ifdef W2_HOST_LINUX #include -#endif - -#ifdef W2_HOST_WIN32 -// garbage os compatibility -#include -#include -#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", ", %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 Binary files a/robot/tests/dirc.bin and b/robot/tests/dirc.bin differ diff --git a/robot/tests/ping.bin b/robot/tests/ping.bin index 456804e..2bbe45e 100644 Binary files a/robot/tests/ping.bin and b/robot/tests/ping.bin 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 +#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); -- cgit v1.2.3 From 31daa9db97a0d0e094c20ac8f3891d3633610039 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 27 May 2022 13:51:18 +0200 Subject: send start byte on robot side too --- robot/sercomm.c | 1 + 1 file changed, 1 insertion(+) (limited to 'robot') diff --git a/robot/sercomm.c b/robot/sercomm.c index e0ea39d..608cb04 100644 --- a/robot/sercomm.c +++ b/robot/sercomm.c @@ -37,6 +37,7 @@ void w2_sercomm_main() { #ifdef W2_SIM w2_sim_print_serial(data); #endif + serial_send("\xff", 1); 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); -- cgit v1.2.3 From 5882adbf21363e63b1069f6321ca7c08d1fa9b41 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 27 May 2022 13:58:12 +0200 Subject: implement SRES --- protocol.md | 4 ++-- robot/sercomm.c | 19 ++++++++++++++++++- robot/setup.c | 3 ++- shared/protocol.h | 2 ++ 4 files changed, 24 insertions(+), 4 deletions(-) (limited to 'robot') diff --git a/protocol.md b/protocol.md index 7c7c843..bc6a8c0 100644 --- a/protocol.md +++ b/protocol.md @@ -39,7 +39,7 @@ readability. |`0x08`|[DIRC](#dirc)|yes|`r <-- c`|direct control |`0x0a`|[CORD](#cord)|no|`r <=> c`|coordinate |`0x0c`|[BOMD](#bomd)|no|`r <=> c`|backorder modify -|`0x0e`|[SRES](#sres)|no|`r <-- c`|soft reset +|`0x0e`|[SRES](#sres)|yes|`r <-- c`|soft reset |`0x10`|[MCFG](#mcfg)|no|`r <-- c`|map config |`0x12`|[SENS](#sens)|yes|`r <-> c`|sensor data |`0x14`|[INFO](#info)|yes|`r <-> c`|info @@ -223,7 +223,7 @@ _status_ is one of: _type_ is one of: - 0: reinitialize all global state -- 1: reset emergency mode +- 1: go to previous mode ### MCFG diff --git a/robot/sercomm.c b/robot/sercomm.c index 608cb04..07c4bd8 100644 --- a/robot/sercomm.c +++ b/robot/sercomm.c @@ -117,7 +117,24 @@ void w2_cmd_cord_rx(w2_s_bin *data) { return; } void w2_cmd_bomd_rx(w2_s_bin *data) { return; } -void w2_cmd_sres_rx(w2_s_bin *data) { return; } +void w2_cmd_sres_rx(w2_s_bin *data) { + w2_s_cmd_sres_rx *message = malloc(w2_cmd_sizeof(data->data, data->bytes)); + memcpy(message, data->data, data->bytes); + + switch (message->type) { + case W2_CMD_SRES_RX_TYPE_REINITGS: { + // TODO: soft-reset + break; + } + case W2_CMD_SRES_RX_TYPE_PREVMODE: { + w2_modes_call(W2_M_PREV); + break; + } + default: { + w2_errcatch_throw(W2_E_WARN_SERIAL_NOISY); + } + } +} void w2_cmd_mcfg_rx(w2_s_bin *data) { return; } diff --git a/robot/setup.c b/robot/setup.c index 2c426a3..95b201e 100644 --- a/robot/setup.c +++ b/robot/setup.c @@ -28,7 +28,8 @@ void w2_setup_main() { time_reset(); // set default mode - w2_modes_swap(W2_M_HALT); + w2_modes_swap(W2_M_MAZE); + w2_modes_call(W2_M_HALT); // indicate startup done play("L50 c>c"); diff --git a/shared/protocol.h b/shared/protocol.h index 2e1e4ea..93e53f4 100644 --- a/shared/protocol.h +++ b/shared/protocol.h @@ -114,6 +114,8 @@ typedef struct { uint8_t status; } w2_s_cmd_bomd_tx; +#define W2_CMD_SRES_RX_TYPE_REINITGS 0 +#define W2_CMD_SRES_RX_TYPE_PREVMODE 1 typedef struct { uint8_t opcode; uint8_t type; -- cgit v1.2.3