From cef95354c69745f782a95d37f4b0e7bbf02e106a Mon Sep 17 00:00:00 2001 From: lonkaars Date: Wed, 24 May 2023 18:03:05 +0200 Subject: uart working --- zumo/.gitignore | 1 - zumo/pidtest.cpp | 18 ------------------ zumo/pidtest.mk | 21 --------------------- zumo/pidtest/.gitignore | 1 + zumo/pidtest/makefile | 21 +++++++++++++++++++++ zumo/pidtest/pidtest.cpp | 18 ++++++++++++++++++ zumo/protocol.cpp | 18 +++++++++++++++++- zumo/protocol.h | 3 +++ zumo/zumo.ino | 3 --- 9 files changed, 60 insertions(+), 44 deletions(-) delete mode 100644 zumo/pidtest.cpp delete mode 100644 zumo/pidtest.mk create mode 100644 zumo/pidtest/.gitignore create mode 100644 zumo/pidtest/makefile create mode 100644 zumo/pidtest/pidtest.cpp (limited to 'zumo') diff --git a/zumo/.gitignore b/zumo/.gitignore index e45f7a2..5761abc 100644 --- a/zumo/.gitignore +++ b/zumo/.gitignore @@ -1,2 +1 @@ *.o -pidtest diff --git a/zumo/pidtest.cpp b/zumo/pidtest.cpp deleted file mode 100644 index b9ce50b..0000000 --- a/zumo/pidtest.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include - -#include "pid.h" - -int main() { - float P, I, D; - P = -0.02; - I = 0.13; - D = -300; - PID test(P, I, D); - test.reset(0.0); - - fprintf(stderr, "P: %.3f :: I: %.3f :: D: %.3f\n", P, I, D); - for (unsigned int i = 0; i < 100; i++) { - printf("%2.8f\n", test.iter(i < 50 ? 1.0 : 0.0)); - } -} diff --git a/zumo/pidtest.mk b/zumo/pidtest.mk deleted file mode 100644 index 5ffe1e1..0000000 --- a/zumo/pidtest.mk +++ /dev/null @@ -1,21 +0,0 @@ -CPP = g++ -LD = g++ -RM = rm -f -CFLAGS = -LFLAGS = -TARGET = pidtest - -SRCS := pidtest.cpp pid.cpp -OBJS := pidtest.o pid.o - -all: pidtest - -%.o: %.cpp - $(CPP) -c $(CFLAGS) $< -o $@ - -$(TARGET): $(OBJS) - $(LD) $^ $(LFLAGS) -o $@ - -clean: - $(RM) $(TARGET) $(OBJS) - diff --git a/zumo/pidtest/.gitignore b/zumo/pidtest/.gitignore new file mode 100644 index 0000000..41dc46e --- /dev/null +++ b/zumo/pidtest/.gitignore @@ -0,0 +1 @@ +pidtest diff --git a/zumo/pidtest/makefile b/zumo/pidtest/makefile new file mode 100644 index 0000000..832d60b --- /dev/null +++ b/zumo/pidtest/makefile @@ -0,0 +1,21 @@ +CPP = g++ +LD = g++ +RM = rm -f +CFLAGS = +LFLAGS = +TARGET = pidtest + +SRCS := pidtest.cpp ../pid.cpp +OBJS := pidtest.o ../pid.o + +all: pidtest + +%.o: %.cpp + $(CPP) -c $(CFLAGS) $< -o $@ + +$(TARGET): $(OBJS) + $(LD) $^ $(LFLAGS) -o $@ + +clean: + $(RM) $(TARGET) $(OBJS) + diff --git a/zumo/pidtest/pidtest.cpp b/zumo/pidtest/pidtest.cpp new file mode 100644 index 0000000..4d047fb --- /dev/null +++ b/zumo/pidtest/pidtest.cpp @@ -0,0 +1,18 @@ +#include +#include + +#include "../pid.h" + +int main() { + float P, I, D; + P = -0.02; + I = 0.13; + D = -300; + PID test(P, I, D); + test.reset(0.0); + + fprintf(stderr, "P: %.3f :: I: %.3f :: D: %.3f\n", P, I, D); + for (unsigned int i = 0; i < 100; i++) { + printf("%2.8f\n", test.iter(i < 50 ? 1.0 : 0.0)); + } +} diff --git a/zumo/protocol.cpp b/zumo/protocol.cpp index fea8a00..f11827c 100644 --- a/zumo/protocol.cpp +++ b/zumo/protocol.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include "protocol.h" @@ -34,6 +36,20 @@ void apply_state(dui_state_t *state) { // TODO: print sign on OLED screen } +inline bool rx() { return !digitalRead(DUI_PINOUT_NICLA_RX); } + unsigned char uart_read() { - return 0x00; + if (rx() == true) return 0x00; // return immediately if line is idle + + delayMicroseconds(1500); // wait out start bit + + unsigned char byte = 0x00; + for (unsigned int i = 0; i < 8; i++) { + byte = (byte << 1) | rx(); + delayMicroseconds(1000); + } + + delayMicroseconds(1000); // wait out stop bit + + return byte; } diff --git a/zumo/protocol.h b/zumo/protocol.h index 662a5ce..9db7902 100644 --- a/zumo/protocol.h +++ b/zumo/protocol.h @@ -1,5 +1,8 @@ #pragma once +#define DUI_PINOUT_NICLA_TX 13 +#define DUI_PINOUT_NICLA_RX 14 + typedef enum { DUI_CMD_NULL, DUI_CMD_SIGN, diff --git a/zumo/zumo.ino b/zumo/zumo.ino index c65c2f9..e53513b 100644 --- a/zumo/zumo.ino +++ b/zumo/zumo.ino @@ -5,9 +5,6 @@ #include "protocol.h" #include "pid.h" -#define DUI_PINOUT_NICLA_TX 13 -#define DUI_PINOUT_NICLA_RX 14 - dui_state_t g_dui_target_state = { .steer = 1.0f, .speed = 1.0f, -- cgit v1.2.3