2018 revision to classic DataBus AVC code.

Dependencies:   LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell

Committer:
shimniok
Date:
Fri Dec 28 17:10:43 2018 +0000
Revision:
33:74c514aea0a1
Parent:
32:eb673f6f5734
Child:
35:c42e7e29c3bd
debounce improvements

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