From cef95354c69745f782a95d37f4b0e7bbf02e106a Mon Sep 17 00:00:00 2001 From: lonkaars Date: Wed, 24 May 2023 18:03:05 +0200 Subject: uart working --- nicla/serial_test.py | 19 ++++++++++--------- 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 --- 10 files changed, 70 insertions(+), 53 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 diff --git a/nicla/serial_test.py b/nicla/serial_test.py index bef43a2..c8b84e5 100644 --- a/nicla/serial_test.py +++ b/nicla/serial_test.py @@ -1,21 +1,22 @@ -from pyb import Pin, delay +from pyb import Pin, delay, udelay zumo_tx = Pin("PA10", Pin.IN) zumo_rx = Pin("PA9", Pin.OUT_PP) -def uart_send(s): +def uart_send(byte): zumo_rx.value(0) - byte = ord(s) - print("START BIT") - delay(2) + udelay(1000) for x in range(8): bit = (byte & (1 << 7)) >> 7 byte <<= 1 zumo_rx.value(bit) - print(f"BIT[{x}] = {bit}") - delay(2) - print("STOP BIT") + udelay(1000) zumo_rx.value(1) while True: - uart_send("a") + # uart_send("a") + for x in range(8): + n = 1 << x + uart_send(n) + print(f"0x{n:02x}") + delay(1000) 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