diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | client/main.c | 30 | ||||
-rw-r--r-- | client/makefile | 3 | ||||
-rw-r--r-- | client/readme.md | 22 | ||||
-rw-r--r-- | client/serial.h | 18 | ||||
-rw-r--r-- | client/serial_linux.c | 97 | ||||
-rw-r--r-- | client/serial_win32.c | 10 | ||||
-rw-r--r-- | readme.md | 13 | ||||
-rw-r--r-- | robot/- | 0 | ||||
-rw-r--r-- | robot/makefile | 4 | ||||
-rw-r--r-- | robot/readme.md | 9 | ||||
-rw-r--r-- | robot/sim.c | 9 | ||||
-rw-r--r-- | robot/sim.h | 14 |
13 files changed, 212 insertions, 18 deletions
@@ -9,3 +9,4 @@ client/main *.log scripts/InstallationLog.txt docs/ +robot/tty diff --git a/client/main.c b/client/main.c index 76e8197..d51f30b 100644 --- a/client/main.c +++ b/client/main.c @@ -1 +1,29 @@ -int main() { return 0; } +#include "serial.h" + +#include <stdio.h> +#include <unistd.h> + +int main(int argc, char **argv) { + if (argc < 2) { + printf("usage: %s <serial port>\n", argv[0]); + return 1; + } + + if (w2_serial_open(argv[1]) == 0) { + printf("serial port open fout"); + return 1; + } + + printf("writing...\n"); + bool success = w2_serial_write("\xff\x14", 2); + printf("writing %s\n", success ? "succeeded" : "failed"); + + printf("reading...\n"); + while (1) { + int res = w2_serial_read(); + if (res == -1) continue; + + printf("%02x ", (uint8_t)res); + fflush(stdout); + } +} diff --git a/client/makefile b/client/makefile index f79dd11..87d06a9 100644 --- a/client/makefile +++ b/client/makefile @@ -4,11 +4,14 @@ RM = rm -f CFLAGS = EXECNAME = main +include ../shared/os.mk + all: $(EXECNAME) SOURCES := $(wildcard *.c) HEADERS := $(wildcard *.h) # include ../shared/makefile + OBJECTS := $(patsubst %.c,%.o, $(SOURCES)) .o: diff --git a/client/readme.md b/client/readme.md index 8b4baee..2a8c19f 100644 --- a/client/readme.md +++ b/client/readme.md @@ -8,17 +8,17 @@ this page is WIP |feature|due|status|author|description| |-|-|-|-|-| -|view warnings / errors| -|direct control| -|configure map| -|input orders| -|enable/disable emergency mode| -|enable/disable sensor calibration mode| -|enable/disable wet floor mode| -|read sensor values| -|set display contents|optional -|play music|optional -|control leds|optional +|view warnings / errors|may 31|unimplemented||see a log of parsed warnings/errors +|direct control|may 31|unimplemented||directly control the robot with tank-style controls +|configure map|june 04|unimplemented||set map width/height and define entry/exitpoints +|input orders|june 04|unimplemented||type orders with lists of coordinates to visit +|enable/disable emergency mode|may 31|unimplemented||self-explanatory +|enable/disable sensor calibration mode|may 31|unimplemented||self-explanatory +|enable/disable wet floor mode|may 31|unimplemented||self-explanatory +|read sensor values|june 04|unimplemented||dashboard that displays all i/o as bar graphs +|set display contents|optional|unimplemented||send text to display on lcd +|play music|optional|unimplemented||play tunes +|control leds|optional|unimplemented||turn on/off underside leds ## interface diff --git a/client/serial.h b/client/serial.h new file mode 100644 index 0000000..caa3cda --- /dev/null +++ b/client/serial.h @@ -0,0 +1,18 @@ +#pragma once + +#include "../shared/bool.h" + +/** @file serial.h */ + +/** + * read byte from serial port + * + * @return -1 if read fails, else char read + */ +int w2_serial_read(); +/** write `data` with length `length` to serial port */ +bool w2_serial_write(char *data, uint8_t length); +/** open serial port */ +bool w2_serial_open(const char *port_name); +/** close serial port */ +void w2_serial_close(); diff --git a/client/serial_linux.c b/client/serial_linux.c new file mode 100644 index 0000000..080dca1 --- /dev/null +++ b/client/serial_linux.c @@ -0,0 +1,97 @@ +#ifdef W2_HOST_LINUX + +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <termios.h> +#include <unistd.h> + +#include "../shared/consts.h" +#include "serial.h" + +struct termios g_w2_tty; +struct termios g_w2_tty_old; +int g_w2_serial_handle; + +char g_w2_serial_buffer[W2_SERIAL_READ_BUFFER_SIZE]; +uint8_t g_w2_serial_buffer_index; +uint8_t g_w2_serial_buffer_head; + +speed_t w2_baud_map(int baud) { + switch (baud) { + case 9600: + return B9600; + case 19200: + return B19200; + case 38400: + return B38400; + case 57600: + return B57600; + case 115200: + return B115200; + case 230400: + return B230400; + case 460800: + return B460800; + case 500000: + return B500000; + case 576000: + return B576000; + case 921600: + return B921600; + case 1000000: + return B1000000; + case 1152000: + return B1152000; + case 1500000: + return B1500000; + case 2000000: + return B2000000; + case 2500000: + return B2500000; + case 3000000: + return B3000000; + case 3500000: + return B3500000; + case 4000000: + return B4000000; + default: + return B0; + } +} + +int w2_serial_read() { + int return_val; + int bytes = read(g_w2_serial_handle, &return_val, 1); + return return_val == -1 || bytes != 1 ? -1 : (uint8_t)return_val; +} + +bool w2_serial_write(char *data, uint8_t length) { + return write(g_w2_serial_handle, data, length) == length; +} + +bool w2_serial_open(const char *port_name) { + g_w2_serial_handle = open(port_name, O_RDWR | O_NONBLOCK); + if (g_w2_serial_handle < 0 || tcgetattr(g_w2_serial_handle, &g_w2_tty) != 0) return false; + + g_w2_tty_old = g_w2_tty; + + speed_t baud = w2_baud_map(W2_SERIAL_BAUD); + cfsetospeed(&g_w2_tty, baud); + cfsetispeed(&g_w2_tty, baud); + + g_w2_tty.c_cc[VMIN] = 0; + g_w2_tty.c_cc[VTIME] = 0; + + cfmakeraw(&g_w2_tty); + + tcflush(g_w2_serial_handle, TCIFLUSH); + + if (tcsetattr(g_w2_serial_handle, TCSANOW, &g_w2_tty) != 0) return false; + + return true; +} + +void w2_serial_close() { close(g_w2_serial_handle); } + +#endif diff --git a/client/serial_win32.c b/client/serial_win32.c new file mode 100644 index 0000000..9406a34 --- /dev/null +++ b/client/serial_win32.c @@ -0,0 +1,10 @@ +#ifdef W2_HOST_WIN32 + +#include "serial.h" + +bool w2_serial_read(uint8_t *target, uint8_t bytes); +bool w2_serial_write(uint8_t *target, uint8_t bytes); +void w2_serial_open(); +void w2_serial_close(); + +#endif @@ -6,6 +6,19 @@ this project is divided in two subfolders, one for robot code, and one for client code that runs on your PC and is able to control the robot remotely. +## supported features + +the build toolchain is compatible with windows and linux, though some features +are only supported on linux: + +|feature|windows|linux| +|-|:-:|:-:| +|robot code compilation (avr)|yes|yes| +|robot exec upload|yes|yes| +|client code compilation|yes|yes| +|robot code simulation (x86)| |yes| +|use client with robot sim| |yes| + ## toolchain installation on windows > look in the scripts/ subdirectory if you're concerned about what these diff --git a/robot/- b/robot/- deleted file mode 100644 index e69de29..0000000 --- a/robot/- +++ /dev/null diff --git a/robot/makefile b/robot/makefile index f65552a..11e8509 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 @@ -19,7 +19,7 @@ HEADERS := $(filter-out sim.h, $(wildcard *.h)) include ../shared/makefile # simulation -CFLAGS += $(if $(SIM), -DW2_SIM, -mcall-prologues -mmcu=$(MCU)) +CFLAGS += $(if $(SIM), -DW2_SIM -DDBG_ENABLE_COLOR, -mcall-prologues -mmcu=$(MCU)) LDFLAGS += $(if $(SIM), , -lpololu_$(DEVICE)) PREFIX := $(if $(SIM), , avr-) SOURCES += $(if $(SIM), sim.c, ) diff --git a/robot/readme.md b/robot/readme.md index e6ab294..8995dfb 100644 --- a/robot/readme.md +++ b/robot/readme.md @@ -22,7 +22,14 @@ SIM = true`, the robot code can be compiled for desktop debugging instead. all used pololu functions must be manually implemented in sim.c for this to work, but it allows easier debugging. *it's important that the `orangutan_shim.h` header is used instead of including `<pololu/orangutan.h>` directly for this to -keep working!* +keep working!* if you want to use the simulation robot code with the client, +compile the sim like normal, and use `socat` to create a pseudo-tty and foward +stdio. this pseudo-tty can be used as the com port argument for the client. +here's an example command that creates a tty device in this folder: + +``` +./a.out headless | socat pty,raw,echo=0,link=tty - +``` ## module hierarchy diff --git a/robot/sim.c b/robot/sim.c index 6283694..34e4932 100644 --- a/robot/sim.c +++ b/robot/sim.c @@ -4,6 +4,7 @@ #include <stdint.h> #include <unistd.h> #include <termios.h> +#include <fcntl.h> #include "sim.h" #include "../shared/consts.h" @@ -78,10 +79,11 @@ void serial_send(char* message, unsigned int length) { if (g_w2_sim_headless) { for (unsigned int byte = 0; byte < length; byte++) putc(message[byte] & 0xff, stdout); + fflush(stdout); return; } - if (DBG_ENABLE_PRINTFUNC) simprintfunc("serial_send", "<%u byte%s>", length, length == 1 ? "" : "s"); + simprintfunc("serial_send", "0x%02x", (uint8_t) message[0]); } void serial_receive_ring(char* buffer, unsigned char size) { @@ -98,11 +100,12 @@ void w2_sim_setup(int argc, char **argv) { g_w2_sim_headless = true; // disable echo and enable raw mode + fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); struct termios term; tcgetattr(STDIN_FILENO, &term); term.c_lflag &= ~(ECHO | ICANON); term.c_cc[VTIME] = 0; - term.c_cc[VMIN] = 0; + term.c_cc[VMIN] = 1; tcsetattr(STDIN_FILENO, 0, &term); // debug error @@ -110,6 +113,8 @@ void w2_sim_setup(int argc, char **argv) { } void w2_sim_cycle_begin() { + fflush(stdout); + // read bytes from stdin while(read(STDIN_FILENO, (g_w2_serial_buffer + sizeof(char) * g_w2_serial_buffer_head), 1) > 0) g_w2_serial_buffer_head = (g_w2_serial_buffer_head + 1) % W2_SERIAL_READ_BUFFER_SIZE; diff --git a/robot/sim.h b/robot/sim.h index 249414d..5dbd936 100644 --- a/robot/sim.h +++ b/robot/sim.h @@ -18,12 +18,13 @@ extern bool g_w2_sim_headless; #define DBG_ENABLE_CYCLEINFO (0) #define DBG_ENABLE_SERIAL (1) -#define DBG_MAX_CYCLES (10) +#define DBG_MAX_CYCLES (-1) // debug print options #define DBG_BYTES_PER_LINE 16 // debug colors +#ifdef DBG_ENABLE_COLOR #define COL_BLK "\e[0;30m" #define COL_RED "\e[0;31m" #define COL_GRN "\e[0;32m" @@ -33,6 +34,17 @@ extern bool g_w2_sim_headless; #define COL_CYN "\e[0;36m" #define COL_WHT "\e[0;37m" #define COL_RST "\e[0m" +#else +#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__) |