![](/media/cache/profiles/2012_sf_avc_095.jpg.50x50_q85.jpg)
2018 revision to classic DataBus AVC code.
Dependencies: LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell
main.cpp@27:bfe141fad082, 2018-12-21 (annotated)
- Committer:
- shimniok
- Date:
- Fri Dec 21 21:26:59 2018 +0000
- Revision:
- 27:bfe141fad082
- Parent:
- 26:2dc31a801cc8
- Child:
- 29:cb2f55fbfe9c
remove commented test code
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 | 0:7e98bbfd102a | 26 | LocalFileSystem lfs("etc"); |
shimniok | 24:a7f92dfc5310 | 27 | SDBlockDevice bd(p5, p6, p7, p8); // MOSI, MISO, CLK, CS |
shimniok | 24:a7f92dfc5310 | 28 | FATFileSystem ffs("log", &bd); |
shimniok | 24:a7f92dfc5310 | 29 | Serial pc(USBTX, USBRX); |
shimniok | 0:7e98bbfd102a | 30 | DigitalOut led1(LED1); |
shimniok | 25:b8176ebb96c6 | 31 | DigitalOut led2(LED2); |
shimniok | 20:043987d06f8d | 32 | RawSerial s(UART1TX, UART1RX, 38400); |
shimniok | 20:043987d06f8d | 33 | |
shimniok | 24:a7f92dfc5310 | 34 | /////////////////////////////////////////////////////////////////////////////// |
shimniok | 24:a7f92dfc5310 | 35 | // Idle hook |
shimniok | 24:a7f92dfc5310 | 36 | void idler() { |
shimniok | 24:a7f92dfc5310 | 37 | while(1) { |
shimniok | 24:a7f92dfc5310 | 38 | led1 = !led1; |
shimniok | 24:a7f92dfc5310 | 39 | wait(0.1); |
shimniok | 24:a7f92dfc5310 | 40 | } |
shimniok | 24:a7f92dfc5310 | 41 | } |
shimniok | 19:0d1728091519 | 42 | |
shimniok | 24:a7f92dfc5310 | 43 | /////////////////////////////////////////////////////////////////////////////// |
shimniok | 24:a7f92dfc5310 | 44 | // Logging |
shimniok | 24:a7f92dfc5310 | 45 | EventQueue logQueue(8 * EVENTS_EVENT_SIZE); |
shimniok | 24:a7f92dfc5310 | 46 | Logger logger("/etc/test.log"); |
shimniok | 24:a7f92dfc5310 | 47 | |
shimniok | 25:b8176ebb96c6 | 48 | |
shimniok | 25:b8176ebb96c6 | 49 | /////////////////////////////////////////////////////////////////////////////// |
shimniok | 25:b8176ebb96c6 | 50 | // Updater |
shimniok | 25:b8176ebb96c6 | 51 | Updater *u = Updater::instance(); |
shimniok | 26:2dc31a801cc8 | 52 | EventQueue updaterQueue(8 * EVENTS_EVENT_SIZE); |
shimniok | 25:b8176ebb96c6 | 53 | |
shimniok | 25:b8176ebb96c6 | 54 | void updater_callback() { |
shimniok | 25:b8176ebb96c6 | 55 | led1 = !led1; |
shimniok | 25:b8176ebb96c6 | 56 | } |
shimniok | 25:b8176ebb96c6 | 57 | |
shimniok | 24:a7f92dfc5310 | 58 | /////////////////////////////////////////////////////////////////////////////// |
shimniok | 24:a7f92dfc5310 | 59 | // GPS |
shimniok | 24:a7f92dfc5310 | 60 | Ublox6 ublox; |
shimniok | 24:a7f92dfc5310 | 61 | EventQueue gpsQueue(8 * EVENTS_EVENT_SIZE); |
shimniok | 23:5e61cf4a8c34 | 62 | |
shimniok | 23:5e61cf4a8c34 | 63 | // Callback for gps parse data ready |
shimniok | 23:5e61cf4a8c34 | 64 | void gps_callback() { |
shimniok | 24:a7f92dfc5310 | 65 | GpsData d; |
shimniok | 24:a7f92dfc5310 | 66 | |
shimniok | 25:b8176ebb96c6 | 67 | led2 = !led2; |
shimniok | 24:a7f92dfc5310 | 68 | ublox.read(d.latitude, d.longitude, d.course, d.speed, d.hdop, d.svcount); |
shimniok | 24:a7f92dfc5310 | 69 | //logQueue.call(&logger, &Logger::log_gps, d); |
shimniok | 23:5e61cf4a8c34 | 70 | } |
shimniok | 23:5e61cf4a8c34 | 71 | |
shimniok | 22:4d62bd16f037 | 72 | // ISR for GPS serial, passes off to thread |
shimniok | 20:043987d06f8d | 73 | void gps_handler() { |
shimniok | 22:4d62bd16f037 | 74 | while (s.readable()) { |
shimniok | 22:4d62bd16f037 | 75 | int c = s.getc(); |
shimniok | 22:4d62bd16f037 | 76 | gpsQueue.call(&ublox, &Ublox6::parse, c); |
shimniok | 16:eb28d0f64a9b | 77 | } |
shimniok | 19:0d1728091519 | 78 | } |
shimniok | 16:eb28d0f64a9b | 79 | |
shimniok | 24:a7f92dfc5310 | 80 | /////////////////////////////////////////////////////////////////////////////// |
shimniok | 24:a7f92dfc5310 | 81 | // Shell |
shimniok | 24:a7f92dfc5310 | 82 | SimpleShell sh; |
shimniok | 9:fc3575d2cbbf | 83 | |
shimniok | 24:a7f92dfc5310 | 84 | void test(int argc, char **argv) { |
shimniok | 1:7019a60fd585 | 85 | printf("Hello world!\n"); |
shimniok | 1:7019a60fd585 | 86 | } |
shimniok | 1:7019a60fd585 | 87 | |
shimniok | 24:a7f92dfc5310 | 88 | void stats(int argc, char **argv) { |
shimniok | 24:a7f92dfc5310 | 89 | SystemReport r(200); |
shimniok | 24:a7f92dfc5310 | 90 | r.report_state(); |
shimniok | 24:a7f92dfc5310 | 91 | return; |
shimniok | 24:a7f92dfc5310 | 92 | } |
shimniok | 24:a7f92dfc5310 | 93 | |
shimniok | 24:a7f92dfc5310 | 94 | void read_gps(int argc, char **argv) |
shimniok | 18:3f8a8f6e3cc1 | 95 | { |
shimniok | 18:3f8a8f6e3cc1 | 96 | double lat=0; |
shimniok | 18:3f8a8f6e3cc1 | 97 | double lon=0; |
shimniok | 18:3f8a8f6e3cc1 | 98 | float course=0; |
shimniok | 18:3f8a8f6e3cc1 | 99 | float speed=0; |
shimniok | 18:3f8a8f6e3cc1 | 100 | float hdop=0.0; |
shimniok | 18:3f8a8f6e3cc1 | 101 | int svcount=0; |
shimniok | 18:3f8a8f6e3cc1 | 102 | |
shimniok | 18:3f8a8f6e3cc1 | 103 | ublox.read(lat, lon, course, speed, hdop, svcount); |
shimniok | 18:3f8a8f6e3cc1 | 104 | printf("%3.7f %3.7f\n", lat, lon); |
shimniok | 18:3f8a8f6e3cc1 | 105 | printf("hdg=%03.1f deg spd=%3.1f m/s\n", course, speed); |
shimniok | 18:3f8a8f6e3cc1 | 106 | printf("%d %f\n", svcount, hdop); |
shimniok | 18:3f8a8f6e3cc1 | 107 | } |
shimniok | 18:3f8a8f6e3cc1 | 108 | |
shimniok | 24:a7f92dfc5310 | 109 | void read_gyro(int argc, char **argv) |
shimniok | 9:fc3575d2cbbf | 110 | { |
shimniok | 12:3cd91e150d9c | 111 | int g[3]; |
shimniok | 13:5566df1250f1 | 112 | float dt; |
shimniok | 12:3cd91e150d9c | 113 | |
shimniok | 11:8ec858b7c6d1 | 114 | Updater *u = Updater::instance(); |
shimniok | 11:8ec858b7c6d1 | 115 | |
shimniok | 13:5566df1250f1 | 116 | u->gyro(g, dt); |
shimniok | 11:8ec858b7c6d1 | 117 | |
shimniok | 13:5566df1250f1 | 118 | printf("Gyro: %d, %d, %d - dt: %f\n", g[0], g[1], g[2], dt); |
shimniok | 12:3cd91e150d9c | 119 | } |
shimniok | 12:3cd91e150d9c | 120 | |
shimniok | 24:a7f92dfc5310 | 121 | void read_enc(int argc, char **argv) |
shimniok | 14:1dd83e626153 | 122 | { |
shimniok | 14:1dd83e626153 | 123 | Updater *u = Updater::instance(); |
shimniok | 14:1dd83e626153 | 124 | |
shimniok | 14:1dd83e626153 | 125 | printf("Encoder: %d\n", u->encoder()); |
shimniok | 14:1dd83e626153 | 126 | } |
shimniok | 14:1dd83e626153 | 127 | |
shimniok | 24:a7f92dfc5310 | 128 | void bridge_uart1(int argc, char **argv) { |
shimniok | 20:043987d06f8d | 129 | RawSerial s(UART1TX, UART1RX, 38400); |
shimniok | 20:043987d06f8d | 130 | while (1) { |
shimniok | 20:043987d06f8d | 131 | if (pc.readable()) s.putc(pc.getc()); |
shimniok | 20:043987d06f8d | 132 | if (s.readable()) pc.putc(s.getc()); |
shimniok | 20:043987d06f8d | 133 | } |
shimniok | 20:043987d06f8d | 134 | } |
shimniok | 20:043987d06f8d | 135 | |
shimniok | 24:a7f92dfc5310 | 136 | void reset(int argc, char **argv) |
shimniok | 12:3cd91e150d9c | 137 | { |
shimniok | 12:3cd91e150d9c | 138 | NVIC_SystemReset(); |
shimniok | 9:fc3575d2cbbf | 139 | } |
shimniok | 9:fc3575d2cbbf | 140 | |
shimniok | 24:a7f92dfc5310 | 141 | /////////////////////////////////////////////////////////////////////////////// |
shimniok | 24:a7f92dfc5310 | 142 | // MAIN |
shimniok | 1:7019a60fd585 | 143 | |
shimniok | 0:7e98bbfd102a | 144 | // main() runs in its own thread in the OS |
shimniok | 0:7e98bbfd102a | 145 | int main() |
shimniok | 11:8ec858b7c6d1 | 146 | { |
shimniok | 20:043987d06f8d | 147 | //bridge_uart1(); |
shimniok | 24:a7f92dfc5310 | 148 | |
shimniok | 25:b8176ebb96c6 | 149 | //Kernel::attach_idle_hook(idler); |
shimniok | 20:043987d06f8d | 150 | |
shimniok | 0:7e98bbfd102a | 151 | printf("Bootup...\n"); |
shimniok | 0:7e98bbfd102a | 152 | fflush(stdout); |
shimniok | 1:7019a60fd585 | 153 | |
shimniok | 4:de7feb458652 | 154 | printf("Loading config...\n"); |
shimniok | 7:1f2661b840ed | 155 | config.add("intercept_distance", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 156 | config.add("waypoint_threshold", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 157 | config.add("minimum_turning_radius", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 158 | config.add("wheelbase", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 159 | config.add("track_width", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 160 | config.add("tire_circumference", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 161 | config.add("encoder_stripes", Config::INT); |
shimniok | 7:1f2661b840ed | 162 | config.add("esc_brake", Config::INT); |
shimniok | 7:1f2661b840ed | 163 | config.add("esc_off", Config::INT); |
shimniok | 7:1f2661b840ed | 164 | config.add("esc_max", Config::INT); |
shimniok | 7:1f2661b840ed | 165 | config.add("turn_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 166 | config.add("turn_distance", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 167 | config.add("start_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 168 | config.add("cruise_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 169 | config.add("speed_kp", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 170 | config.add("speed_ki", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 171 | config.add("speed_kd", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 172 | config.add("steer_center", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 173 | config.add("steer_scale", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 174 | config.add("gyro_scale", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 175 | config.add("gps_valid_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 176 | |
shimniok | 4:de7feb458652 | 177 | if (config.load("/etc/2018cfg.txt")) { |
shimniok | 4:de7feb458652 | 178 | printf("error loading config\n"); |
shimniok | 4:de7feb458652 | 179 | } |
shimniok | 10:9fb3feb38746 | 180 | |
shimniok | 24:a7f92dfc5310 | 181 | printf("Starting updater...\n"); |
shimniok | 25:b8176ebb96c6 | 182 | u->attach(updater_callback); |
shimniok | 24:a7f92dfc5310 | 183 | Thread updaterThread(osPriorityRealtime, 512, 0, "updater"); |
shimniok | 26:2dc31a801cc8 | 184 | updaterQueue.call_every(20, u, &Updater::update); |
shimniok | 26:2dc31a801cc8 | 185 | updaterThread.start(callback(&updaterQueue, &EventQueue::dispatch_forever)); |
shimniok | 24:a7f92dfc5310 | 186 | |
shimniok | 24:a7f92dfc5310 | 187 | printf("Starting gps...\n"); |
shimniok | 24:a7f92dfc5310 | 188 | Thread gpsThread(osPriorityHigh, 256, 0, "gps"); |
shimniok | 24:a7f92dfc5310 | 189 | gpsThread.start(callback(&gpsQueue, &EventQueue::dispatch_forever)); |
shimniok | 24:a7f92dfc5310 | 190 | ublox.subscribe(gps_callback); |
shimniok | 24:a7f92dfc5310 | 191 | s.attach(gps_handler); |
shimniok | 24:a7f92dfc5310 | 192 | |
shimniok | 24:a7f92dfc5310 | 193 | printf("Starting logging...\n"); |
shimniok | 24:a7f92dfc5310 | 194 | Thread logThread(osPriorityNormal, 256, 0, "log"); |
shimniok | 24:a7f92dfc5310 | 195 | logThread.start(callback(&logQueue, &EventQueue::dispatch_forever)); |
shimniok | 24:a7f92dfc5310 | 196 | |
shimniok | 13:5566df1250f1 | 197 | printf("Starting shell...\n"); |
shimniok | 1:7019a60fd585 | 198 | sh.attach(test, "test"); |
shimniok | 9:fc3575d2cbbf | 199 | sh.attach(read_gyro, "gyro"); |
shimniok | 14:1dd83e626153 | 200 | sh.attach(read_enc, "enc"); |
shimniok | 18:3f8a8f6e3cc1 | 201 | sh.attach(read_gps, "gps"); |
shimniok | 12:3cd91e150d9c | 202 | sh.attach(reset, "reset"); |
shimniok | 24:a7f92dfc5310 | 203 | sh.attach(stats, "stats"); |
shimniok | 24:a7f92dfc5310 | 204 | sh.run(); |
shimniok | 22:4d62bd16f037 | 205 | |
shimniok | 0:7e98bbfd102a | 206 | while (true) { |
shimniok | 0:7e98bbfd102a | 207 | // Blink LED and wait 0.5 seconds |
shimniok | 0:7e98bbfd102a | 208 | led1 = !led1; |
shimniok | 0:7e98bbfd102a | 209 | wait(0.5f); |
shimniok | 0:7e98bbfd102a | 210 | } |
shimniok | 0:7e98bbfd102a | 211 | } |