2018 revision to classic DataBus AVC code.

Dependencies:   LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell

Committer:
shimniok
Date:
Mon Dec 31 23:34:28 2018 +0000
Revision:
35:c42e7e29c3bd
Parent:
33:74c514aea0a1
Child:
37:b8259500dbd3
Added servo control for esc and steering

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