2018 revision to classic DataBus AVC code.

Dependencies:   LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell

Committer:
shimniok
Date:
Wed Jan 02 18:20:47 2019 +0000
Revision:
37:b8259500dbd3
Parent:
35:c42e7e29c3bd
Child:
38:6fec81f85221
Implemented LCD display abstraction

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 0:7e98bbfd102a 1 /* mbed Microcontroller Library
shimniok 0:7e98bbfd102a 2 * Copyright (c) 2018 ARM Limited
shimniok 0:7e98bbfd102a 3 * SPDX-License-Identifier: Apache-2.0
shimniok 0:7e98bbfd102a 4 */
shimniok 0:7e98bbfd102a 5
shimniok 0:7e98bbfd102a 6 #include "mbed.h"
shimniok 0:7e98bbfd102a 7 #include <stdio.h>
shimniok 0:7e98bbfd102a 8 #include <errno.h>
shimniok 16:eb28d0f64a9b 9 #include "pinouts.h"
shimniok 0:7e98bbfd102a 10 #include "stats_report.h"
shimniok 0:7e98bbfd102a 11 #include "SDBlockDevice.h"
shimniok 0:7e98bbfd102a 12 #include "FATFileSystem.h"
shimniok 35:c42e7e29c3bd 13 #include "Servo.h"
shimniok 0:7e98bbfd102a 14 #include "SimpleShell.h"
shimniok 4:de7feb458652 15 #include "Config.h"
shimniok 11:8ec858b7c6d1 16 #include "Updater.h"
shimniok 16:eb28d0f64a9b 17 #include "Ublox6.h"
shimniok 24:a7f92dfc5310 18 #include "Logger.h"
shimniok 24:a7f92dfc5310 19 #include "SystemState.h"
shimniok 37:b8259500dbd3 20 #include "Display.h"
shimniok 0:7e98bbfd102a 21
shimniok 24:a7f92dfc5310 22 ///////////////////////////////////////////////////////////////////////////////
shimniok 24:a7f92dfc5310 23 // Config
shimniok 24:a7f92dfc5310 24 Config config;
shimniok 24:a7f92dfc5310 25
shimniok 24:a7f92dfc5310 26 ///////////////////////////////////////////////////////////////////////////////
shimniok 24:a7f92dfc5310 27 // Devices
shimniok 30:ed791f1f7f7d 28 DigitalOut led1(LED1);
shimniok 30:ed791f1f7f7d 29 DigitalOut led2(LED2);
shimniok 30:ed791f1f7f7d 30 DigitalOut led3(LED3);
shimniok 30:ed791f1f7f7d 31 DigitalOut led4(LED4);
shimniok 37:b8259500dbd3 32 Display display(UART3TX, UART3RX);
shimniok 0:7e98bbfd102a 33 LocalFileSystem lfs("etc");
shimniok 24:a7f92dfc5310 34 SDBlockDevice bd(p5, p6, p7, p8); // MOSI, MISO, CLK, CS
shimniok 24:a7f92dfc5310 35 FATFileSystem ffs("log", &bd);
shimniok 24:a7f92dfc5310 36 Serial pc(USBTX, USBRX);
shimniok 20:043987d06f8d 37 RawSerial s(UART1TX, UART1RX, 38400);
shimniok 33:74c514aea0a1 38 InterruptIn lbutton(LBUTTON, PullUp); // button interrupts
shimniok 33:74c514aea0a1 39 InterruptIn cbutton(CBUTTON, PullUp);
shimniok 33:74c514aea0a1 40 InterruptIn rbutton(RBUTTON, PullUp);
shimniok 35:c42e7e29c3bd 41 Servo steer(STEERING);
shimniok 35:c42e7e29c3bd 42 Servo esc(THROTTLE);
shimniok 20:043987d06f8d 43
shimniok 24:a7f92dfc5310 44 ///////////////////////////////////////////////////////////////////////////////
shimniok 24:a7f92dfc5310 45 // Idle hook
shimniok 24:a7f92dfc5310 46 void idler() {
shimniok 24:a7f92dfc5310 47 while(1) {
shimniok 24:a7f92dfc5310 48 led1 = !led1;
shimniok 24:a7f92dfc5310 49 wait(0.1);
shimniok 24:a7f92dfc5310 50 }
shimniok 24:a7f92dfc5310 51 }
shimniok 19:0d1728091519 52
shimniok 24:a7f92dfc5310 53 ///////////////////////////////////////////////////////////////////////////////
shimniok 24:a7f92dfc5310 54 // Logging
shimniok 29:cb2f55fbfe9c 55 EventQueue logQueue(16 * EVENTS_EVENT_SIZE);
shimniok 31:20a95043adb0 56 Logger logger;
shimniok 25:b8176ebb96c6 57
shimniok 25:b8176ebb96c6 58 ///////////////////////////////////////////////////////////////////////////////
shimniok 25:b8176ebb96c6 59 // Updater
shimniok 25:b8176ebb96c6 60 Updater *u = Updater::instance();
shimniok 26:2dc31a801cc8 61 EventQueue updaterQueue(8 * EVENTS_EVENT_SIZE);
shimniok 25:b8176ebb96c6 62
shimniok 25:b8176ebb96c6 63 void updater_callback() {
shimniok 30:ed791f1f7f7d 64 SensorData d;
shimniok 30:ed791f1f7f7d 65 float dt;
shimniok 30:ed791f1f7f7d 66
shimniok 25:b8176ebb96c6 67 led1 = !led1;
shimniok 30:ed791f1f7f7d 68 d.timestamp = Kernel::get_ms_count();
shimniok 30:ed791f1f7f7d 69 d.encoder = u->encoder();
shimniok 32:eb673f6f5734 70 u->imu(d.gyro, d.accel, d.mag, dt);
shimniok 30:ed791f1f7f7d 71 logQueue.call(&logger, &Logger::log_sensors, d);
shimniok 37:b8259500dbd3 72 //lcdQueue.call ...
shimniok 25:b8176ebb96c6 73 }
shimniok 25:b8176ebb96c6 74
shimniok 24:a7f92dfc5310 75 ///////////////////////////////////////////////////////////////////////////////
shimniok 30:ed791f1f7f7d 76 // Buttons
shimniok 30:ed791f1f7f7d 77
shimniok 30:ed791f1f7f7d 78 //enum button_mask { LEFT = 0x01, CENTER = 0x02, RIGHT = 0x04 };
shimniok 30:ed791f1f7f7d 79 EventQueue buttonQueue(16 * EVENTS_EVENT_SIZE);
shimniok 33:74c514aea0a1 80 const int BUTTON_DEBOUNCE_SAMPLES = 20;
shimniok 33:74c514aea0a1 81 const int BUTTON_DEBOUNCE_THRESH = 8;
shimniok 33:74c514aea0a1 82 const int BUTTON_SAMPLE_MS = 0.005;
shimniok 30:ed791f1f7f7d 83
shimniok 30:ed791f1f7f7d 84 void button_event() {
shimniok 33:74c514aea0a1 85 int samples = 0;
shimniok 33:74c514aea0a1 86
shimniok 33:74c514aea0a1 87 // disable subsequent interrupts
shimniok 33:74c514aea0a1 88 lbutton.fall(NULL);
shimniok 33:74c514aea0a1 89
shimniok 33:74c514aea0a1 90 // sample repeatedly to debounce
shimniok 33:74c514aea0a1 91 for (int i=0; i < BUTTON_DEBOUNCE_SAMPLES; i++) {
shimniok 33:74c514aea0a1 92 if (lbutton == 0) samples++;
shimniok 33:74c514aea0a1 93 wait(BUTTON_SAMPLE_MS);
shimniok 33:74c514aea0a1 94 }
shimniok 33:74c514aea0a1 95
shimniok 33:74c514aea0a1 96 if (samples > BUTTON_DEBOUNCE_THRESH) {
shimniok 30:ed791f1f7f7d 97 if (logger.enabled()) {
shimniok 30:ed791f1f7f7d 98 logQueue.call(&logger, &Logger::stop);
shimniok 30:ed791f1f7f7d 99 led3 = 0;
shimniok 30:ed791f1f7f7d 100 } else {
shimniok 30:ed791f1f7f7d 101 logQueue.call(&logger, &Logger::start);
shimniok 30:ed791f1f7f7d 102 led3 = 1;
shimniok 30:ed791f1f7f7d 103 }
shimniok 30:ed791f1f7f7d 104 }
shimniok 33:74c514aea0a1 105
shimniok 33:74c514aea0a1 106 lbutton.fall(buttonQueue.event(button_event));
shimniok 30:ed791f1f7f7d 107 }
shimniok 30:ed791f1f7f7d 108
shimniok 30:ed791f1f7f7d 109
shimniok 30:ed791f1f7f7d 110 ///////////////////////////////////////////////////////////////////////////////
shimniok 24:a7f92dfc5310 111 // GPS
shimniok 24:a7f92dfc5310 112 Ublox6 ublox;
shimniok 24:a7f92dfc5310 113 EventQueue gpsQueue(8 * EVENTS_EVENT_SIZE);
shimniok 23:5e61cf4a8c34 114
shimniok 23:5e61cf4a8c34 115 // Callback for gps parse data ready
shimniok 23:5e61cf4a8c34 116 void gps_callback() {
shimniok 24:a7f92dfc5310 117 GpsData d;
shimniok 24:a7f92dfc5310 118
shimniok 25:b8176ebb96c6 119 led2 = !led2;
shimniok 24:a7f92dfc5310 120 ublox.read(d.latitude, d.longitude, d.course, d.speed, d.hdop, d.svcount);
shimniok 30:ed791f1f7f7d 121 d.timestamp = Kernel::get_ms_count();
shimniok 29:cb2f55fbfe9c 122 logQueue.call(&logger, &Logger::log_gps, d);
shimniok 37:b8259500dbd3 123 //lcdQueue.call ...
shimniok 23:5e61cf4a8c34 124 }
shimniok 29:cb2f55fbfe9c 125
shimniok 22:4d62bd16f037 126 // ISR for GPS serial, passes off to thread
shimniok 20:043987d06f8d 127 void gps_handler() {
shimniok 22:4d62bd16f037 128 while (s.readable()) {
shimniok 22:4d62bd16f037 129 int c = s.getc();
shimniok 22:4d62bd16f037 130 gpsQueue.call(&ublox, &Ublox6::parse, c);
shimniok 16:eb28d0f64a9b 131 }
shimniok 19:0d1728091519 132 }
shimniok 16:eb28d0f64a9b 133
shimniok 24:a7f92dfc5310 134 ///////////////////////////////////////////////////////////////////////////////
shimniok 24:a7f92dfc5310 135 // Shell
shimniok 30:ed791f1f7f7d 136 SimpleShell sh("/log");
shimniok 9:fc3575d2cbbf 137
shimniok 24:a7f92dfc5310 138 void test(int argc, char **argv) {
shimniok 35:c42e7e29c3bd 139 //printf("Hello world!\n");
shimniok 35:c42e7e29c3bd 140 char file[32];
shimniok 35:c42e7e29c3bd 141
shimniok 35:c42e7e29c3bd 142 for (int i=30; i < 40; i++) {
shimniok 35:c42e7e29c3bd 143 sprintf(file, "/log/%04d.csv", i);
shimniok 35:c42e7e29c3bd 144 printf("removing <%s>\n", file);
shimniok 35:c42e7e29c3bd 145 int stat = remove(file);
shimniok 35:c42e7e29c3bd 146 printf("return: %d\n", stat);
shimniok 35:c42e7e29c3bd 147 }
shimniok 1:7019a60fd585 148 }
shimniok 1:7019a60fd585 149
shimniok 24:a7f92dfc5310 150 void stats(int argc, char **argv) {
shimniok 24:a7f92dfc5310 151 SystemReport r(200);
shimniok 24:a7f92dfc5310 152 r.report_state();
shimniok 24:a7f92dfc5310 153 return;
shimniok 24:a7f92dfc5310 154 }
shimniok 24:a7f92dfc5310 155
shimniok 24:a7f92dfc5310 156 void read_gps(int argc, char **argv)
shimniok 18:3f8a8f6e3cc1 157 {
shimniok 18:3f8a8f6e3cc1 158 double lat=0;
shimniok 18:3f8a8f6e3cc1 159 double lon=0;
shimniok 18:3f8a8f6e3cc1 160 float course=0;
shimniok 18:3f8a8f6e3cc1 161 float speed=0;
shimniok 18:3f8a8f6e3cc1 162 float hdop=0.0;
shimniok 18:3f8a8f6e3cc1 163 int svcount=0;
shimniok 18:3f8a8f6e3cc1 164
shimniok 18:3f8a8f6e3cc1 165 ublox.read(lat, lon, course, speed, hdop, svcount);
shimniok 18:3f8a8f6e3cc1 166 printf("%3.7f %3.7f\n", lat, lon);
shimniok 18:3f8a8f6e3cc1 167 printf("hdg=%03.1f deg spd=%3.1f m/s\n", course, speed);
shimniok 18:3f8a8f6e3cc1 168 printf("%d %f\n", svcount, hdop);
shimniok 18:3f8a8f6e3cc1 169 }
shimniok 18:3f8a8f6e3cc1 170
shimniok 32:eb673f6f5734 171 void read_imu(int argc, char **argv)
shimniok 9:fc3575d2cbbf 172 {
shimniok 12:3cd91e150d9c 173 int g[3];
shimniok 32:eb673f6f5734 174 int a[3];
shimniok 32:eb673f6f5734 175 int m[3];
shimniok 13:5566df1250f1 176 float dt;
shimniok 12:3cd91e150d9c 177
shimniok 11:8ec858b7c6d1 178 Updater *u = Updater::instance();
shimniok 11:8ec858b7c6d1 179
shimniok 32:eb673f6f5734 180 u->imu(g, a, m, dt);
shimniok 32:eb673f6f5734 181
shimniok 32:eb673f6f5734 182 printf(" Gyro: %d, %d, %d\n", g[0], g[1], g[2]);
shimniok 32:eb673f6f5734 183 printf(" Accel: %d, %d, %d\n", a[0], a[1], a[2]);
shimniok 32:eb673f6f5734 184 printf(" Mag: %d, %d, %d\n", m[0], m[1], m[2]);
shimniok 32:eb673f6f5734 185 printf(" dt: %f\n", dt);
shimniok 12:3cd91e150d9c 186 }
shimniok 12:3cd91e150d9c 187
shimniok 24:a7f92dfc5310 188 void read_enc(int argc, char **argv)
shimniok 14:1dd83e626153 189 {
shimniok 14:1dd83e626153 190 Updater *u = Updater::instance();
shimniok 14:1dd83e626153 191
shimniok 14:1dd83e626153 192 printf("Encoder: %d\n", u->encoder());
shimniok 14:1dd83e626153 193 }
shimniok 14:1dd83e626153 194
shimniok 29:cb2f55fbfe9c 195 void log(int argc, char **argv)
shimniok 29:cb2f55fbfe9c 196 {
shimniok 29:cb2f55fbfe9c 197 char *usage = "usage: log [start|stop]";
shimniok 29:cb2f55fbfe9c 198
shimniok 29:cb2f55fbfe9c 199 if (argc == 1) {
shimniok 29:cb2f55fbfe9c 200 printf("logging ");
shimniok 29:cb2f55fbfe9c 201 if (logger.enabled())
shimniok 29:cb2f55fbfe9c 202 printf("enabled");
shimniok 29:cb2f55fbfe9c 203 else
shimniok 29:cb2f55fbfe9c 204 printf("disabled");
shimniok 29:cb2f55fbfe9c 205 printf("\n");
shimniok 29:cb2f55fbfe9c 206 } else if (argc == 2) {
shimniok 29:cb2f55fbfe9c 207 if (!strcmp("start", argv[1])) {
shimniok 29:cb2f55fbfe9c 208 logger.start();
shimniok 29:cb2f55fbfe9c 209 } else if (!strcmp("stop", argv[1])) {
shimniok 29:cb2f55fbfe9c 210 logger.stop();
shimniok 29:cb2f55fbfe9c 211 } else {
shimniok 29:cb2f55fbfe9c 212 puts(usage);
shimniok 29:cb2f55fbfe9c 213 }
shimniok 29:cb2f55fbfe9c 214 } else {
shimniok 29:cb2f55fbfe9c 215 puts(usage);
shimniok 29:cb2f55fbfe9c 216 }
shimniok 29:cb2f55fbfe9c 217 }
shimniok 29:cb2f55fbfe9c 218
shimniok 29:cb2f55fbfe9c 219
shimniok 24:a7f92dfc5310 220 void bridge_uart1(int argc, char **argv) {
shimniok 20:043987d06f8d 221 RawSerial s(UART1TX, UART1RX, 38400);
shimniok 20:043987d06f8d 222 while (1) {
shimniok 20:043987d06f8d 223 if (pc.readable()) s.putc(pc.getc());
shimniok 20:043987d06f8d 224 if (s.readable()) pc.putc(s.getc());
shimniok 20:043987d06f8d 225 }
shimniok 20:043987d06f8d 226 }
shimniok 20:043987d06f8d 227
shimniok 24:a7f92dfc5310 228 void reset(int argc, char **argv)
shimniok 12:3cd91e150d9c 229 {
shimniok 12:3cd91e150d9c 230 NVIC_SystemReset();
shimniok 9:fc3575d2cbbf 231 }
shimniok 9:fc3575d2cbbf 232
shimniok 24:a7f92dfc5310 233 ///////////////////////////////////////////////////////////////////////////////
shimniok 24:a7f92dfc5310 234 // MAIN
shimniok 1:7019a60fd585 235
shimniok 0:7e98bbfd102a 236 // main() runs in its own thread in the OS
shimniok 0:7e98bbfd102a 237 int main()
shimniok 11:8ec858b7c6d1 238 {
shimniok 20:043987d06f8d 239 //bridge_uart1();
shimniok 24:a7f92dfc5310 240
shimniok 25:b8176ebb96c6 241 //Kernel::attach_idle_hook(idler);
shimniok 20:043987d06f8d 242
shimniok 37:b8259500dbd3 243 display.status("Bootup...");
shimniok 0:7e98bbfd102a 244 printf("Bootup...\n");
shimniok 0:7e98bbfd102a 245 fflush(stdout);
shimniok 1:7019a60fd585 246
shimniok 35:c42e7e29c3bd 247 steer = 0.50;
shimniok 35:c42e7e29c3bd 248 esc = 0.00;
shimniok 35:c42e7e29c3bd 249
shimniok 4:de7feb458652 250 printf("Loading config...\n");
shimniok 37:b8259500dbd3 251 display.status("Loading config");
shimniok 7:1f2661b840ed 252 config.add("intercept_distance", Config::DOUBLE);
shimniok 7:1f2661b840ed 253 config.add("waypoint_threshold", Config::DOUBLE);
shimniok 7:1f2661b840ed 254 config.add("minimum_turning_radius", Config::DOUBLE);
shimniok 7:1f2661b840ed 255 config.add("wheelbase", Config::DOUBLE);
shimniok 7:1f2661b840ed 256 config.add("track_width", Config::DOUBLE);
shimniok 7:1f2661b840ed 257 config.add("tire_circumference", Config::DOUBLE);
shimniok 7:1f2661b840ed 258 config.add("encoder_stripes", Config::INT);
shimniok 7:1f2661b840ed 259 config.add("esc_brake", Config::INT);
shimniok 7:1f2661b840ed 260 config.add("esc_off", Config::INT);
shimniok 7:1f2661b840ed 261 config.add("esc_max", Config::INT);
shimniok 7:1f2661b840ed 262 config.add("turn_speed", Config::DOUBLE);
shimniok 7:1f2661b840ed 263 config.add("turn_distance", Config::DOUBLE);
shimniok 7:1f2661b840ed 264 config.add("start_speed", Config::DOUBLE);
shimniok 7:1f2661b840ed 265 config.add("cruise_speed", Config::DOUBLE);
shimniok 7:1f2661b840ed 266 config.add("speed_kp", Config::DOUBLE);
shimniok 7:1f2661b840ed 267 config.add("speed_ki", Config::DOUBLE);
shimniok 7:1f2661b840ed 268 config.add("speed_kd", Config::DOUBLE);
shimniok 7:1f2661b840ed 269 config.add("steer_center", Config::DOUBLE);
shimniok 7:1f2661b840ed 270 config.add("steer_scale", Config::DOUBLE);
shimniok 7:1f2661b840ed 271 config.add("gyro_scale", Config::DOUBLE);
shimniok 7:1f2661b840ed 272 config.add("gps_valid_speed", Config::DOUBLE);
shimniok 35:c42e7e29c3bd 273
shimniok 4:de7feb458652 274 if (config.load("/etc/2018cfg.txt")) {
shimniok 4:de7feb458652 275 printf("error loading config\n");
shimniok 37:b8259500dbd3 276 display.status("config load error");
shimniok 4:de7feb458652 277 }
shimniok 10:9fb3feb38746 278
shimniok 30:ed791f1f7f7d 279 printf("Starting buttons...\n");
shimniok 37:b8259500dbd3 280 display.status("Starting buttons");
shimniok 33:74c514aea0a1 281 lbutton.fall(buttonQueue.event(button_event));
shimniok 30:ed791f1f7f7d 282 //cbutton.rise(buttonQueue.event(button_event));
shimniok 30:ed791f1f7f7d 283 //rbutton.rise(buttonQueue.event(button_event));
shimniok 30:ed791f1f7f7d 284 Thread buttonThread(osPriorityNormal, 256, 0, "buttons");
shimniok 30:ed791f1f7f7d 285 buttonThread.start(callback(&buttonQueue, &EventQueue::dispatch_forever));
shimniok 30:ed791f1f7f7d 286
shimniok 24:a7f92dfc5310 287 printf("Starting updater...\n");
shimniok 37:b8259500dbd3 288 display.status("Starting updater");
shimniok 25:b8176ebb96c6 289 u->attach(updater_callback);
shimniok 24:a7f92dfc5310 290 Thread updaterThread(osPriorityRealtime, 512, 0, "updater");
shimniok 26:2dc31a801cc8 291 updaterQueue.call_every(20, u, &Updater::update);
shimniok 26:2dc31a801cc8 292 updaterThread.start(callback(&updaterQueue, &EventQueue::dispatch_forever));
shimniok 24:a7f92dfc5310 293
shimniok 24:a7f92dfc5310 294 printf("Starting gps...\n");
shimniok 37:b8259500dbd3 295 display.status("Starting gps");
shimniok 29:cb2f55fbfe9c 296 Thread gpsThread(osPriorityHigh, 2048, 0, "gps");
shimniok 24:a7f92dfc5310 297 gpsThread.start(callback(&gpsQueue, &EventQueue::dispatch_forever));
shimniok 24:a7f92dfc5310 298 ublox.subscribe(gps_callback);
shimniok 24:a7f92dfc5310 299 s.attach(gps_handler);
shimniok 24:a7f92dfc5310 300
shimniok 37:b8259500dbd3 301 printf("Starting logging...\n");
shimniok 37:b8259500dbd3 302 display.status("Starting logging");
shimniok 29:cb2f55fbfe9c 303 Thread logThread(osPriorityNormal, 2048, 0, "log");
shimniok 24:a7f92dfc5310 304 logThread.start(callback(&logQueue, &EventQueue::dispatch_forever));
shimniok 24:a7f92dfc5310 305
shimniok 13:5566df1250f1 306 printf("Starting shell...\n");
shimniok 37:b8259500dbd3 307 display.status("Starting shell");
shimniok 30:ed791f1f7f7d 308 sh.command(test, "test");
shimniok 32:eb673f6f5734 309 sh.command(read_imu, "imu");
shimniok 30:ed791f1f7f7d 310 sh.command(read_enc, "enc");
shimniok 30:ed791f1f7f7d 311 sh.command(read_gps, "gps");
shimniok 30:ed791f1f7f7d 312 sh.command(reset, "reset");
shimniok 30:ed791f1f7f7d 313 sh.command(stats, "stats");
shimniok 30:ed791f1f7f7d 314 sh.command(log, "log");
shimniok 37:b8259500dbd3 315
shimniok 37:b8259500dbd3 316 display.status("DataBus 2018");
shimniok 24:a7f92dfc5310 317 sh.run();
shimniok 22:4d62bd16f037 318
shimniok 0:7e98bbfd102a 319 while (true) {
shimniok 0:7e98bbfd102a 320 // Blink LED and wait 0.5 seconds
shimniok 0:7e98bbfd102a 321 led1 = !led1;
shimniok 0:7e98bbfd102a 322 wait(0.5f);
shimniok 0:7e98bbfd102a 323 }
shimniok 0:7e98bbfd102a 324 }