From e943ed3992044c601c3eb036a07ff9f68db52c8b Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 7 Jun 2023 13:54:17 +0200 Subject: zumo update --- zumo/control.cpp | 10 ++++------ zumo/pid.cpp | 3 +-- zumo/protocol.cpp | 16 +++++++++++++--- zumo/protocol.h | 3 +-- zumo/zumo.ino | 12 +++++++++--- 5 files changed, 28 insertions(+), 16 deletions(-) (limited to 'zumo') diff --git a/zumo/control.cpp b/zumo/control.cpp index 778969a..71e7537 100644 --- a/zumo/control.cpp +++ b/zumo/control.cpp @@ -6,11 +6,11 @@ #include "control.h" #define DUI_SPEED_MOD 96.0f -#define DUI_MOTOR_DIFF 0.6f +#define DUI_MOTOR_DIFF 0.62f void apply_state(dui_state_t *state) { - float motor_l = 0.5f * state->speed * (+1.f * state->steer * DUI_MOTOR_DIFF - DUI_MOTOR_DIFF + 2) * state->speed_mod * DUI_SPEED_MOD; - float motor_r = 0.5f * state->speed * (-1.f * state->steer * DUI_MOTOR_DIFF - DUI_MOTOR_DIFF + 2) * state->speed_mod * DUI_SPEED_MOD; + float motor_l = 0.5f * state->Speed * (+1.f * state->steer * DUI_MOTOR_DIFF - DUI_MOTOR_DIFF + 2) * state->speed_mod * DUI_SPEED_MOD; + float motor_r = 0.5f * state->Speed * (-1.f * state->steer * DUI_MOTOR_DIFF - DUI_MOTOR_DIFF + 2) * state->speed_mod * DUI_SPEED_MOD; Serial.print(motor_l); Serial.print(" "); @@ -18,8 +18,7 @@ void apply_state(dui_state_t *state) { Serial.println(""); - Zumo32U4Motors::setLeftSpeed((int16_t) motor_l); - Zumo32U4Motors::setRightSpeed((int16_t) motor_r); + Zumo32U4Motors::setSpeeds((int16_t) motor_l,(int16_t) motor_r); // TODO: print sign on OLED screen } @@ -38,6 +37,5 @@ unsigned char uart_read() { } delayMicroseconds(1000); // wait out stop bit - return byte; } diff --git a/zumo/pid.cpp b/zumo/pid.cpp index 7c1cbda..17588e3 100644 --- a/zumo/pid.cpp +++ b/zumo/pid.cpp @@ -45,8 +45,7 @@ PID speed_pid = PID(); PID steer_pid = PID(); PID speed_mod_pid = PID(); void apply_pid(dui_state_t* target, dui_state_t* current) { - current->speed = speed_pid.iter(target->speed); + current->Speed = speed_pid.iter(target->Speed); current->steer = steer_pid.iter(target->steer); current->speed_mod = speed_mod_pid.iter(target->speed_mod); } - diff --git a/zumo/protocol.cpp b/zumo/protocol.cpp index ab8397a..d176c24 100644 --- a/zumo/protocol.cpp +++ b/zumo/protocol.cpp @@ -1,5 +1,8 @@ -#include "protocol.h" +#include +#include "protocol.h" +#include +#include #define DUI_CMD_NULL 0x00 #define DUI_CMD_SIGN_START 0x01 #define DUI_CMD_SIGN_END 0x0f @@ -7,15 +10,22 @@ #define DUI_CMD_SPEED_END 0x1f #define DUI_CMD_STEER_START 0x20 #define DUI_CMD_STEER_END 0xff +Zumo32U4OLED display; void handle_cmd(unsigned char cmd, dui_state_t *state) { + Serial.println(cmd,HEX); + if (cmd == DUI_CMD_NULL) return; else if (DUI_CMD_SIGN_START <= cmd && cmd <= DUI_CMD_SIGN_END) { state->current_sign = (dui_e_sign) (cmd - DUI_CMD_SIGN_START); + display.clear(); + display.print(state->current_sign+1); + display.gotoXY(0, 1); } else if (DUI_CMD_SPEED_START <= cmd && cmd <= DUI_CMD_SPEED_END) { - state->speed = (float) (cmd - DUI_CMD_SPEED_START) / (float) (DUI_CMD_SPEED_END - DUI_CMD_SPEED_START); + Serial.print(" Hallo: " ); + state->Speed = (float) (cmd - DUI_CMD_SPEED_START) / (float) (DUI_CMD_SPEED_END - DUI_CMD_SPEED_START); + Serial.println(state->Speed); } else if (DUI_CMD_STEER_START <= cmd && cmd <= DUI_CMD_STEER_END) { state->steer = (float) (cmd - DUI_CMD_STEER_START) / (float) (DUI_CMD_STEER_END - DUI_CMD_STEER_START) * (float) 2 - (float) 1; } } - diff --git a/zumo/protocol.h b/zumo/protocol.h index a1f9951..5eb2343 100644 --- a/zumo/protocol.h +++ b/zumo/protocol.h @@ -21,11 +21,10 @@ typedef enum { typedef struct { float steer; /** @brief steer value (-1 is left, +1 is right) */ - float speed; /** @brief speed (0-15) */ + float Speed; /** @brief speed (0-15) */ dui_e_sign current_sign; /** @brief last seen sign */ float speed_mod; /** @brief global speed multiplier */ } dui_state_t; /** @brief read and apply cmd to state */ void handle_cmd(unsigned char cmd, dui_state_t *state); - diff --git a/zumo/zumo.ino b/zumo/zumo.ino index 1a64381..a4f4d6b 100644 --- a/zumo/zumo.ino +++ b/zumo/zumo.ino @@ -2,19 +2,20 @@ #include #include + #include "control.h" #include "protocol.h" #include "pid.h" dui_state_t g_dui_target_state = { .steer = 0.0f, - .speed = 0.0f, + .Speed = 0.0f, .current_sign = DUI_SIGN_NONE, .speed_mod = 1.f, }; dui_state_t g_dui_current_state = { .steer = 0.f, - .speed = 0.f, + .Speed = 0.f, .current_sign = DUI_SIGN_NONE, .speed_mod = 1.f, }; @@ -22,10 +23,13 @@ dui_state_t g_dui_current_state = { void setup() { pinMode(DUI_PINOUT_NICLA_TX, OUTPUT); pinMode(DUI_PINOUT_NICLA_RX, INPUT_PULLUP); - Serial.begin(115200); + Serial.begin(11500); + } void loop() { + + static unsigned char cmd_old = 0x00; for (unsigned int i = 0; i < 1000; i++) { digitalWrite(DUI_PINOUT_NICLA_TX, LOW); @@ -40,4 +44,6 @@ void loop() { g_dui_current_state.current_sign = g_dui_target_state.current_sign; apply_state(&g_dui_current_state); + + } -- cgit v1.2.3 From 2903e61cbe2eeff3121f67da516ea195999f0bba Mon Sep 17 00:00:00 2001 From: lonkaars Date: Wed, 7 Jun 2023 14:13:04 +0200 Subject: string lookup for display --- zumo/protocol.cpp | 12 ++++++------ zumo/protocol.h | 12 ++++++++++++ zumo/strlutest/.gitignore | 1 + zumo/strlutest/makefile | 21 +++++++++++++++++++++ zumo/strlutest/strlutest.cpp | 10 ++++++++++ zumo/zumo.ino | 6 ------ 6 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 zumo/strlutest/.gitignore create mode 100644 zumo/strlutest/makefile create mode 100644 zumo/strlutest/strlutest.cpp (limited to 'zumo') diff --git a/zumo/protocol.cpp b/zumo/protocol.cpp index d176c24..06e2993 100644 --- a/zumo/protocol.cpp +++ b/zumo/protocol.cpp @@ -13,18 +13,18 @@ Zumo32U4OLED display; void handle_cmd(unsigned char cmd, dui_state_t *state) { - Serial.println(cmd,HEX); + Serial.println(cmd, HEX); if (cmd == DUI_CMD_NULL) return; else if (DUI_CMD_SIGN_START <= cmd && cmd <= DUI_CMD_SIGN_END) { state->current_sign = (dui_e_sign) (cmd - DUI_CMD_SIGN_START); - display.clear(); - display.print(state->current_sign+1); - display.gotoXY(0, 1); + display.clear(); + if (DUI_SIGN_LOOKUP[state->current_sign] == nullptr) return; + display.print(DUI_SIGN_LOOKUP[state->current_sign]); } else if (DUI_CMD_SPEED_START <= cmd && cmd <= DUI_CMD_SPEED_END) { - Serial.print(" Hallo: " ); + Serial.print(" Hallo: " ); state->Speed = (float) (cmd - DUI_CMD_SPEED_START) / (float) (DUI_CMD_SPEED_END - DUI_CMD_SPEED_START); - Serial.println(state->Speed); + Serial.println(state->Speed); } else if (DUI_CMD_STEER_START <= cmd && cmd <= DUI_CMD_STEER_END) { state->steer = (float) (cmd - DUI_CMD_STEER_START) / (float) (DUI_CMD_STEER_END - DUI_CMD_STEER_START) * (float) 2 - (float) 1; } diff --git a/zumo/protocol.h b/zumo/protocol.h index 5eb2343..ea3e27d 100644 --- a/zumo/protocol.h +++ b/zumo/protocol.h @@ -19,6 +19,18 @@ typedef enum { DUI_SIGN_LIGHT_GO, /** @brief traffic light green (keep current speed) */ } dui_e_sign; +const char* const DUI_SIGN_LOOKUP[16] = { + [DUI_SIGN_NONE] = "", + [DUI_SIGN_STOP] = "bord\nstop", + [DUI_SIGN_LEFT] = "bord\nlinks", + [DUI_SIGN_RIGHT] = "bord\nrechts", + [DUI_SIGN_SPEED_LIMIT_LOW] = "snelheid\nlaag", + [DUI_SIGN_SPEED_LIMIT_HIGH] = "snelheid\nhoog", + [DUI_SIGN_LIGHT_STOP] = "stop.l.\nrood", + [DUI_SIGN_LIGHT_FLOOR_IT] = "stop.l.\ngeel", + [DUI_SIGN_LIGHT_GO] = "stop.l.\ngroen", +}; + typedef struct { float steer; /** @brief steer value (-1 is left, +1 is right) */ float Speed; /** @brief speed (0-15) */ diff --git a/zumo/strlutest/.gitignore b/zumo/strlutest/.gitignore new file mode 100644 index 0000000..15010c5 --- /dev/null +++ b/zumo/strlutest/.gitignore @@ -0,0 +1 @@ +strlutest diff --git a/zumo/strlutest/makefile b/zumo/strlutest/makefile new file mode 100644 index 0000000..f05e0ba --- /dev/null +++ b/zumo/strlutest/makefile @@ -0,0 +1,21 @@ +CPP = g++ +LD = g++ +RM = rm -f +CFLAGS = +LFLAGS = +TARGET = strlutest + +SRCS := strlutest.cpp +OBJS := strlutest.o + +all: strlutest + +%.o: %.cpp + $(CPP) -c $(CFLAGS) $< -o $@ + +$(TARGET): $(OBJS) + $(LD) $^ $(LFLAGS) -o $@ + +clean: + $(RM) $(TARGET) $(OBJS) + diff --git a/zumo/strlutest/strlutest.cpp b/zumo/strlutest/strlutest.cpp new file mode 100644 index 0000000..72ec591 --- /dev/null +++ b/zumo/strlutest/strlutest.cpp @@ -0,0 +1,10 @@ +#include + +#include "../protocol.h" + +int main() { + for (unsigned int i = 0; i < 15; i++) { + if (DUI_SIGN_LOOKUP[i] == NULL) continue; + printf("%d: %s\n", i, DUI_SIGN_LOOKUP[i]); + } +} diff --git a/zumo/zumo.ino b/zumo/zumo.ino index a4f4d6b..9e79581 100644 --- a/zumo/zumo.ino +++ b/zumo/zumo.ino @@ -2,7 +2,6 @@ #include #include - #include "control.h" #include "protocol.h" #include "pid.h" @@ -24,12 +23,9 @@ void setup() { pinMode(DUI_PINOUT_NICLA_TX, OUTPUT); pinMode(DUI_PINOUT_NICLA_RX, INPUT_PULLUP); Serial.begin(11500); - } void loop() { - - static unsigned char cmd_old = 0x00; for (unsigned int i = 0; i < 1000; i++) { digitalWrite(DUI_PINOUT_NICLA_TX, LOW); @@ -44,6 +40,4 @@ void loop() { g_dui_current_state.current_sign = g_dui_target_state.current_sign; apply_state(&g_dui_current_state); - - } -- cgit v1.2.3