2018 revision to classic DataBus AVC code.
Dependencies: LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell
main.cpp@30:ed791f1f7f7d, 2018-12-26 (annotated)
- Committer:
- shimniok
- Date:
- Wed Dec 26 19:34:17 2018 +0000
- Revision:
- 30:ed791f1f7f7d
- Parent:
- 29:cb2f55fbfe9c
- Child:
- 31:20a95043adb0
Implemented pushbutton, pushbutton toggles logging
Who changed what in which revision?
User | Revision | Line number | New 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 | 30:ed791f1f7f7d | 35 | InterruptIn lbutton(LBUTTON, PullUp); // left button |
shimniok | 30:ed791f1f7f7d | 36 | InterruptIn cbutton(CBUTTON, PullUp); // center button |
shimniok | 30:ed791f1f7f7d | 37 | InterruptIn rbutton(RBUTTON, PullUp); // right button |
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 | 29:cb2f55fbfe9c | 51 | Logger logger("/log/test.csv"); |
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 | 30:ed791f1f7f7d | 65 | u->gyro(d.gyro, 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 | 30:ed791f1f7f7d | 74 | int last = 0; |
shimniok | 30:ed791f1f7f7d | 75 | const int BUTTON_DEBOUNCE_MS = 100; |
shimniok | 30:ed791f1f7f7d | 76 | |
shimniok | 30:ed791f1f7f7d | 77 | void button_event() { |
shimniok | 30:ed791f1f7f7d | 78 | int now = Kernel::get_ms_count(); |
shimniok | 30:ed791f1f7f7d | 79 | if ((now - last) > BUTTON_DEBOUNCE_MS) { |
shimniok | 30:ed791f1f7f7d | 80 | if (logger.enabled()) { |
shimniok | 30:ed791f1f7f7d | 81 | logQueue.call(&logger, &Logger::stop); |
shimniok | 30:ed791f1f7f7d | 82 | led3 = 0; |
shimniok | 30:ed791f1f7f7d | 83 | } else { |
shimniok | 30:ed791f1f7f7d | 84 | logQueue.call(&logger, &Logger::start); |
shimniok | 30:ed791f1f7f7d | 85 | led3 = 1; |
shimniok | 30:ed791f1f7f7d | 86 | } |
shimniok | 30:ed791f1f7f7d | 87 | } |
shimniok | 30:ed791f1f7f7d | 88 | last = now; |
shimniok | 30:ed791f1f7f7d | 89 | } |
shimniok | 30:ed791f1f7f7d | 90 | |
shimniok | 30:ed791f1f7f7d | 91 | |
shimniok | 30:ed791f1f7f7d | 92 | /////////////////////////////////////////////////////////////////////////////// |
shimniok | 24:a7f92dfc5310 | 93 | // GPS |
shimniok | 24:a7f92dfc5310 | 94 | Ublox6 ublox; |
shimniok | 24:a7f92dfc5310 | 95 | EventQueue gpsQueue(8 * EVENTS_EVENT_SIZE); |
shimniok | 23:5e61cf4a8c34 | 96 | |
shimniok | 23:5e61cf4a8c34 | 97 | // Callback for gps parse data ready |
shimniok | 23:5e61cf4a8c34 | 98 | void gps_callback() { |
shimniok | 24:a7f92dfc5310 | 99 | GpsData d; |
shimniok | 24:a7f92dfc5310 | 100 | |
shimniok | 25:b8176ebb96c6 | 101 | led2 = !led2; |
shimniok | 24:a7f92dfc5310 | 102 | ublox.read(d.latitude, d.longitude, d.course, d.speed, d.hdop, d.svcount); |
shimniok | 30:ed791f1f7f7d | 103 | d.timestamp = Kernel::get_ms_count(); |
shimniok | 29:cb2f55fbfe9c | 104 | logQueue.call(&logger, &Logger::log_gps, d); |
shimniok | 23:5e61cf4a8c34 | 105 | } |
shimniok | 29:cb2f55fbfe9c | 106 | |
shimniok | 22:4d62bd16f037 | 107 | // ISR for GPS serial, passes off to thread |
shimniok | 20:043987d06f8d | 108 | void gps_handler() { |
shimniok | 22:4d62bd16f037 | 109 | while (s.readable()) { |
shimniok | 22:4d62bd16f037 | 110 | int c = s.getc(); |
shimniok | 22:4d62bd16f037 | 111 | gpsQueue.call(&ublox, &Ublox6::parse, c); |
shimniok | 16:eb28d0f64a9b | 112 | } |
shimniok | 19:0d1728091519 | 113 | } |
shimniok | 16:eb28d0f64a9b | 114 | |
shimniok | 24:a7f92dfc5310 | 115 | /////////////////////////////////////////////////////////////////////////////// |
shimniok | 24:a7f92dfc5310 | 116 | // Shell |
shimniok | 30:ed791f1f7f7d | 117 | SimpleShell sh("/log"); |
shimniok | 9:fc3575d2cbbf | 118 | |
shimniok | 24:a7f92dfc5310 | 119 | void test(int argc, char **argv) { |
shimniok | 1:7019a60fd585 | 120 | printf("Hello world!\n"); |
shimniok | 1:7019a60fd585 | 121 | } |
shimniok | 1:7019a60fd585 | 122 | |
shimniok | 24:a7f92dfc5310 | 123 | void stats(int argc, char **argv) { |
shimniok | 24:a7f92dfc5310 | 124 | SystemReport r(200); |
shimniok | 24:a7f92dfc5310 | 125 | r.report_state(); |
shimniok | 24:a7f92dfc5310 | 126 | return; |
shimniok | 24:a7f92dfc5310 | 127 | } |
shimniok | 24:a7f92dfc5310 | 128 | |
shimniok | 24:a7f92dfc5310 | 129 | void read_gps(int argc, char **argv) |
shimniok | 18:3f8a8f6e3cc1 | 130 | { |
shimniok | 18:3f8a8f6e3cc1 | 131 | double lat=0; |
shimniok | 18:3f8a8f6e3cc1 | 132 | double lon=0; |
shimniok | 18:3f8a8f6e3cc1 | 133 | float course=0; |
shimniok | 18:3f8a8f6e3cc1 | 134 | float speed=0; |
shimniok | 18:3f8a8f6e3cc1 | 135 | float hdop=0.0; |
shimniok | 18:3f8a8f6e3cc1 | 136 | int svcount=0; |
shimniok | 18:3f8a8f6e3cc1 | 137 | |
shimniok | 18:3f8a8f6e3cc1 | 138 | ublox.read(lat, lon, course, speed, hdop, svcount); |
shimniok | 18:3f8a8f6e3cc1 | 139 | printf("%3.7f %3.7f\n", lat, lon); |
shimniok | 18:3f8a8f6e3cc1 | 140 | printf("hdg=%03.1f deg spd=%3.1f m/s\n", course, speed); |
shimniok | 18:3f8a8f6e3cc1 | 141 | printf("%d %f\n", svcount, hdop); |
shimniok | 18:3f8a8f6e3cc1 | 142 | } |
shimniok | 18:3f8a8f6e3cc1 | 143 | |
shimniok | 24:a7f92dfc5310 | 144 | void read_gyro(int argc, char **argv) |
shimniok | 9:fc3575d2cbbf | 145 | { |
shimniok | 12:3cd91e150d9c | 146 | int g[3]; |
shimniok | 13:5566df1250f1 | 147 | float dt; |
shimniok | 12:3cd91e150d9c | 148 | |
shimniok | 11:8ec858b7c6d1 | 149 | Updater *u = Updater::instance(); |
shimniok | 11:8ec858b7c6d1 | 150 | |
shimniok | 13:5566df1250f1 | 151 | u->gyro(g, dt); |
shimniok | 11:8ec858b7c6d1 | 152 | |
shimniok | 13:5566df1250f1 | 153 | printf("Gyro: %d, %d, %d - dt: %f\n", g[0], g[1], g[2], dt); |
shimniok | 12:3cd91e150d9c | 154 | } |
shimniok | 12:3cd91e150d9c | 155 | |
shimniok | 24:a7f92dfc5310 | 156 | void read_enc(int argc, char **argv) |
shimniok | 14:1dd83e626153 | 157 | { |
shimniok | 14:1dd83e626153 | 158 | Updater *u = Updater::instance(); |
shimniok | 14:1dd83e626153 | 159 | |
shimniok | 14:1dd83e626153 | 160 | printf("Encoder: %d\n", u->encoder()); |
shimniok | 14:1dd83e626153 | 161 | } |
shimniok | 14:1dd83e626153 | 162 | |
shimniok | 29:cb2f55fbfe9c | 163 | void log(int argc, char **argv) |
shimniok | 29:cb2f55fbfe9c | 164 | { |
shimniok | 29:cb2f55fbfe9c | 165 | char *usage = "usage: log [start|stop]"; |
shimniok | 29:cb2f55fbfe9c | 166 | |
shimniok | 29:cb2f55fbfe9c | 167 | if (argc == 1) { |
shimniok | 29:cb2f55fbfe9c | 168 | printf("logging "); |
shimniok | 29:cb2f55fbfe9c | 169 | if (logger.enabled()) |
shimniok | 29:cb2f55fbfe9c | 170 | printf("enabled"); |
shimniok | 29:cb2f55fbfe9c | 171 | else |
shimniok | 29:cb2f55fbfe9c | 172 | printf("disabled"); |
shimniok | 29:cb2f55fbfe9c | 173 | printf("\n"); |
shimniok | 29:cb2f55fbfe9c | 174 | } else if (argc == 2) { |
shimniok | 29:cb2f55fbfe9c | 175 | if (!strcmp("start", argv[1])) { |
shimniok | 29:cb2f55fbfe9c | 176 | logger.start(); |
shimniok | 29:cb2f55fbfe9c | 177 | } else if (!strcmp("stop", argv[1])) { |
shimniok | 29:cb2f55fbfe9c | 178 | logger.stop(); |
shimniok | 29:cb2f55fbfe9c | 179 | } else { |
shimniok | 29:cb2f55fbfe9c | 180 | puts(usage); |
shimniok | 29:cb2f55fbfe9c | 181 | } |
shimniok | 29:cb2f55fbfe9c | 182 | } else { |
shimniok | 29:cb2f55fbfe9c | 183 | puts(usage); |
shimniok | 29:cb2f55fbfe9c | 184 | } |
shimniok | 29:cb2f55fbfe9c | 185 | } |
shimniok | 29:cb2f55fbfe9c | 186 | |
shimniok | 29:cb2f55fbfe9c | 187 | |
shimniok | 24:a7f92dfc5310 | 188 | void bridge_uart1(int argc, char **argv) { |
shimniok | 20:043987d06f8d | 189 | RawSerial s(UART1TX, UART1RX, 38400); |
shimniok | 20:043987d06f8d | 190 | while (1) { |
shimniok | 20:043987d06f8d | 191 | if (pc.readable()) s.putc(pc.getc()); |
shimniok | 20:043987d06f8d | 192 | if (s.readable()) pc.putc(s.getc()); |
shimniok | 20:043987d06f8d | 193 | } |
shimniok | 20:043987d06f8d | 194 | } |
shimniok | 20:043987d06f8d | 195 | |
shimniok | 24:a7f92dfc5310 | 196 | void reset(int argc, char **argv) |
shimniok | 12:3cd91e150d9c | 197 | { |
shimniok | 12:3cd91e150d9c | 198 | NVIC_SystemReset(); |
shimniok | 9:fc3575d2cbbf | 199 | } |
shimniok | 9:fc3575d2cbbf | 200 | |
shimniok | 24:a7f92dfc5310 | 201 | /////////////////////////////////////////////////////////////////////////////// |
shimniok | 24:a7f92dfc5310 | 202 | // MAIN |
shimniok | 1:7019a60fd585 | 203 | |
shimniok | 0:7e98bbfd102a | 204 | // main() runs in its own thread in the OS |
shimniok | 0:7e98bbfd102a | 205 | int main() |
shimniok | 11:8ec858b7c6d1 | 206 | { |
shimniok | 20:043987d06f8d | 207 | //bridge_uart1(); |
shimniok | 24:a7f92dfc5310 | 208 | |
shimniok | 25:b8176ebb96c6 | 209 | //Kernel::attach_idle_hook(idler); |
shimniok | 20:043987d06f8d | 210 | |
shimniok | 0:7e98bbfd102a | 211 | printf("Bootup...\n"); |
shimniok | 0:7e98bbfd102a | 212 | fflush(stdout); |
shimniok | 1:7019a60fd585 | 213 | |
shimniok | 4:de7feb458652 | 214 | printf("Loading config...\n"); |
shimniok | 7:1f2661b840ed | 215 | config.add("intercept_distance", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 216 | config.add("waypoint_threshold", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 217 | config.add("minimum_turning_radius", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 218 | config.add("wheelbase", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 219 | config.add("track_width", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 220 | config.add("tire_circumference", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 221 | config.add("encoder_stripes", Config::INT); |
shimniok | 7:1f2661b840ed | 222 | config.add("esc_brake", Config::INT); |
shimniok | 7:1f2661b840ed | 223 | config.add("esc_off", Config::INT); |
shimniok | 7:1f2661b840ed | 224 | config.add("esc_max", Config::INT); |
shimniok | 7:1f2661b840ed | 225 | config.add("turn_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 226 | config.add("turn_distance", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 227 | config.add("start_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 228 | config.add("cruise_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 229 | config.add("speed_kp", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 230 | config.add("speed_ki", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 231 | config.add("speed_kd", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 232 | config.add("steer_center", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 233 | config.add("steer_scale", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 234 | config.add("gyro_scale", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 235 | config.add("gps_valid_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 236 | |
shimniok | 4:de7feb458652 | 237 | if (config.load("/etc/2018cfg.txt")) { |
shimniok | 4:de7feb458652 | 238 | printf("error loading config\n"); |
shimniok | 4:de7feb458652 | 239 | } |
shimniok | 10:9fb3feb38746 | 240 | |
shimniok | 30:ed791f1f7f7d | 241 | printf("Starting buttons...\n"); |
shimniok | 30:ed791f1f7f7d | 242 | lbutton.rise(buttonQueue.event(button_event)); |
shimniok | 30:ed791f1f7f7d | 243 | //cbutton.rise(buttonQueue.event(button_event)); |
shimniok | 30:ed791f1f7f7d | 244 | //rbutton.rise(buttonQueue.event(button_event)); |
shimniok | 30:ed791f1f7f7d | 245 | Thread buttonThread(osPriorityNormal, 256, 0, "buttons"); |
shimniok | 30:ed791f1f7f7d | 246 | buttonThread.start(callback(&buttonQueue, &EventQueue::dispatch_forever)); |
shimniok | 30:ed791f1f7f7d | 247 | |
shimniok | 24:a7f92dfc5310 | 248 | printf("Starting updater...\n"); |
shimniok | 25:b8176ebb96c6 | 249 | u->attach(updater_callback); |
shimniok | 24:a7f92dfc5310 | 250 | Thread updaterThread(osPriorityRealtime, 512, 0, "updater"); |
shimniok | 26:2dc31a801cc8 | 251 | updaterQueue.call_every(20, u, &Updater::update); |
shimniok | 26:2dc31a801cc8 | 252 | updaterThread.start(callback(&updaterQueue, &EventQueue::dispatch_forever)); |
shimniok | 24:a7f92dfc5310 | 253 | |
shimniok | 24:a7f92dfc5310 | 254 | printf("Starting gps...\n"); |
shimniok | 29:cb2f55fbfe9c | 255 | Thread gpsThread(osPriorityHigh, 2048, 0, "gps"); |
shimniok | 24:a7f92dfc5310 | 256 | gpsThread.start(callback(&gpsQueue, &EventQueue::dispatch_forever)); |
shimniok | 24:a7f92dfc5310 | 257 | ublox.subscribe(gps_callback); |
shimniok | 24:a7f92dfc5310 | 258 | s.attach(gps_handler); |
shimniok | 24:a7f92dfc5310 | 259 | |
shimniok | 24:a7f92dfc5310 | 260 | printf("Starting logging...\n"); |
shimniok | 29:cb2f55fbfe9c | 261 | Thread logThread(osPriorityNormal, 2048, 0, "log"); |
shimniok | 24:a7f92dfc5310 | 262 | logThread.start(callback(&logQueue, &EventQueue::dispatch_forever)); |
shimniok | 24:a7f92dfc5310 | 263 | |
shimniok | 13:5566df1250f1 | 264 | printf("Starting shell...\n"); |
shimniok | 30:ed791f1f7f7d | 265 | sh.command(test, "test"); |
shimniok | 30:ed791f1f7f7d | 266 | sh.command(read_gyro, "gyro"); |
shimniok | 30:ed791f1f7f7d | 267 | sh.command(read_enc, "enc"); |
shimniok | 30:ed791f1f7f7d | 268 | sh.command(read_gps, "gps"); |
shimniok | 30:ed791f1f7f7d | 269 | sh.command(reset, "reset"); |
shimniok | 30:ed791f1f7f7d | 270 | sh.command(stats, "stats"); |
shimniok | 30:ed791f1f7f7d | 271 | sh.command(log, "log"); |
shimniok | 24:a7f92dfc5310 | 272 | sh.run(); |
shimniok | 22:4d62bd16f037 | 273 | |
shimniok | 0:7e98bbfd102a | 274 | while (true) { |
shimniok | 0:7e98bbfd102a | 275 | // Blink LED and wait 0.5 seconds |
shimniok | 0:7e98bbfd102a | 276 | led1 = !led1; |
shimniok | 0:7e98bbfd102a | 277 | wait(0.5f); |
shimniok | 0:7e98bbfd102a | 278 | } |
shimniok | 0:7e98bbfd102a | 279 | } |