![](/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@22:4d62bd16f037, 2018-12-17 (annotated)
- Committer:
- shimniok
- Date:
- Mon Dec 17 22:26:34 2018 +0000
- Revision:
- 22:4d62bd16f037
- Parent:
- 21:020f4ebbb13a
- Child:
- 23:5e61cf4a8c34
gps parse into separate thread via EventQueue
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 | 0:7e98bbfd102a | 17 | |
shimniok | 0:7e98bbfd102a | 18 | Serial pc(USBTX, USBRX); |
shimniok | 0:7e98bbfd102a | 19 | //SDBlockDevice bd(p5, p6, p7, p8); // MOSI, MISO, CLK, CS |
shimniok | 0:7e98bbfd102a | 20 | //FATFileSystem ffs("log", &bd); |
shimniok | 0:7e98bbfd102a | 21 | LocalFileSystem lfs("etc"); |
shimniok | 4:de7feb458652 | 22 | Config config; |
shimniok | 0:7e98bbfd102a | 23 | SimpleShell sh; |
shimniok | 18:3f8a8f6e3cc1 | 24 | Ublox6 ublox; |
shimniok | 0:7e98bbfd102a | 25 | |
shimniok | 0:7e98bbfd102a | 26 | DigitalOut led1(LED1); |
shimniok | 16:eb28d0f64a9b | 27 | DigitalOut led3(LED3); |
shimniok | 16:eb28d0f64a9b | 28 | |
shimniok | 20:043987d06f8d | 29 | RawSerial s(UART1TX, UART1RX, 38400); |
shimniok | 20:043987d06f8d | 30 | |
shimniok | 22:4d62bd16f037 | 31 | EventQueue gpsQueue(32 * EVENTS_EVENT_SIZE); |
shimniok | 19:0d1728091519 | 32 | |
shimniok | 22:4d62bd16f037 | 33 | // thread / eventqueue wrapper for gps parse |
shimniok | 22:4d62bd16f037 | 34 | void gps_parse(int c) { |
shimniok | 22:4d62bd16f037 | 35 | if (ublox.parse(c)) { |
shimniok | 22:4d62bd16f037 | 36 | led3 = !led3; |
shimniok | 22:4d62bd16f037 | 37 | } |
shimniok | 22:4d62bd16f037 | 38 | } |
shimniok | 22:4d62bd16f037 | 39 | |
shimniok | 22:4d62bd16f037 | 40 | // ISR for GPS serial, passes off to thread |
shimniok | 20:043987d06f8d | 41 | void gps_handler() { |
shimniok | 22:4d62bd16f037 | 42 | while (s.readable()) { |
shimniok | 22:4d62bd16f037 | 43 | int c = s.getc(); |
shimniok | 22:4d62bd16f037 | 44 | //gpsQueue.call(gps_parse, c); |
shimniok | 22:4d62bd16f037 | 45 | gpsQueue.call(&ublox, &Ublox6::parse, c); |
shimniok | 16:eb28d0f64a9b | 46 | } |
shimniok | 19:0d1728091519 | 47 | } |
shimniok | 16:eb28d0f64a9b | 48 | |
shimniok | 9:fc3575d2cbbf | 49 | /******** SHELL COMMANDS ********/ |
shimniok | 9:fc3575d2cbbf | 50 | |
shimniok | 1:7019a60fd585 | 51 | void test() { |
shimniok | 1:7019a60fd585 | 52 | printf("Hello world!\n"); |
shimniok | 1:7019a60fd585 | 53 | } |
shimniok | 1:7019a60fd585 | 54 | |
shimniok | 18:3f8a8f6e3cc1 | 55 | void read_gps() |
shimniok | 18:3f8a8f6e3cc1 | 56 | { |
shimniok | 18:3f8a8f6e3cc1 | 57 | double lat=0; |
shimniok | 18:3f8a8f6e3cc1 | 58 | double lon=0; |
shimniok | 18:3f8a8f6e3cc1 | 59 | float course=0; |
shimniok | 18:3f8a8f6e3cc1 | 60 | float speed=0; |
shimniok | 18:3f8a8f6e3cc1 | 61 | float hdop=0.0; |
shimniok | 18:3f8a8f6e3cc1 | 62 | int svcount=0; |
shimniok | 18:3f8a8f6e3cc1 | 63 | |
shimniok | 18:3f8a8f6e3cc1 | 64 | ublox.read(lat, lon, course, speed, hdop, svcount); |
shimniok | 18:3f8a8f6e3cc1 | 65 | printf("%3.7f %3.7f\n", lat, lon); |
shimniok | 18:3f8a8f6e3cc1 | 66 | printf("hdg=%03.1f deg spd=%3.1f m/s\n", course, speed); |
shimniok | 18:3f8a8f6e3cc1 | 67 | printf("%d %f\n", svcount, hdop); |
shimniok | 18:3f8a8f6e3cc1 | 68 | } |
shimniok | 18:3f8a8f6e3cc1 | 69 | |
shimniok | 9:fc3575d2cbbf | 70 | void read_gyro() |
shimniok | 9:fc3575d2cbbf | 71 | { |
shimniok | 12:3cd91e150d9c | 72 | int g[3]; |
shimniok | 13:5566df1250f1 | 73 | float dt; |
shimniok | 12:3cd91e150d9c | 74 | |
shimniok | 11:8ec858b7c6d1 | 75 | Updater *u = Updater::instance(); |
shimniok | 11:8ec858b7c6d1 | 76 | |
shimniok | 13:5566df1250f1 | 77 | u->gyro(g, dt); |
shimniok | 11:8ec858b7c6d1 | 78 | |
shimniok | 13:5566df1250f1 | 79 | printf("Gyro: %d, %d, %d - dt: %f\n", g[0], g[1], g[2], dt); |
shimniok | 12:3cd91e150d9c | 80 | } |
shimniok | 12:3cd91e150d9c | 81 | |
shimniok | 14:1dd83e626153 | 82 | void read_enc() |
shimniok | 14:1dd83e626153 | 83 | { |
shimniok | 14:1dd83e626153 | 84 | Updater *u = Updater::instance(); |
shimniok | 14:1dd83e626153 | 85 | |
shimniok | 14:1dd83e626153 | 86 | printf("Encoder: %d\n", u->encoder()); |
shimniok | 14:1dd83e626153 | 87 | } |
shimniok | 14:1dd83e626153 | 88 | |
shimniok | 20:043987d06f8d | 89 | void bridge_uart1() { |
shimniok | 20:043987d06f8d | 90 | RawSerial s(UART1TX, UART1RX, 38400); |
shimniok | 20:043987d06f8d | 91 | while (1) { |
shimniok | 20:043987d06f8d | 92 | if (pc.readable()) s.putc(pc.getc()); |
shimniok | 20:043987d06f8d | 93 | if (s.readable()) pc.putc(s.getc()); |
shimniok | 20:043987d06f8d | 94 | } |
shimniok | 20:043987d06f8d | 95 | } |
shimniok | 20:043987d06f8d | 96 | |
shimniok | 20:043987d06f8d | 97 | |
shimniok | 12:3cd91e150d9c | 98 | void reset() |
shimniok | 12:3cd91e150d9c | 99 | { |
shimniok | 12:3cd91e150d9c | 100 | NVIC_SystemReset(); |
shimniok | 9:fc3575d2cbbf | 101 | } |
shimniok | 9:fc3575d2cbbf | 102 | |
shimniok | 9:fc3575d2cbbf | 103 | /******** MAIN ********/ |
shimniok | 1:7019a60fd585 | 104 | |
shimniok | 0:7e98bbfd102a | 105 | // main() runs in its own thread in the OS |
shimniok | 0:7e98bbfd102a | 106 | int main() |
shimniok | 11:8ec858b7c6d1 | 107 | { |
shimniok | 20:043987d06f8d | 108 | //bridge_uart1(); |
shimniok | 20:043987d06f8d | 109 | |
shimniok | 0:7e98bbfd102a | 110 | printf("Bootup...\n"); |
shimniok | 0:7e98bbfd102a | 111 | fflush(stdout); |
shimniok | 1:7019a60fd585 | 112 | |
shimniok | 4:de7feb458652 | 113 | printf("Loading config...\n"); |
shimniok | 7:1f2661b840ed | 114 | config.add("intercept_distance", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 115 | config.add("waypoint_threshold", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 116 | config.add("minimum_turning_radius", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 117 | config.add("wheelbase", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 118 | config.add("track_width", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 119 | config.add("tire_circumference", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 120 | config.add("encoder_stripes", Config::INT); |
shimniok | 7:1f2661b840ed | 121 | config.add("esc_brake", Config::INT); |
shimniok | 7:1f2661b840ed | 122 | config.add("esc_off", Config::INT); |
shimniok | 7:1f2661b840ed | 123 | config.add("esc_max", Config::INT); |
shimniok | 7:1f2661b840ed | 124 | config.add("turn_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 125 | config.add("turn_distance", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 126 | config.add("start_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 127 | config.add("cruise_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 128 | config.add("speed_kp", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 129 | config.add("speed_ki", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 130 | config.add("speed_kd", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 131 | config.add("steer_center", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 132 | config.add("steer_scale", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 133 | config.add("gyro_scale", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 134 | config.add("gps_valid_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 135 | |
shimniok | 4:de7feb458652 | 136 | if (config.load("/etc/2018cfg.txt")) { |
shimniok | 4:de7feb458652 | 137 | printf("error loading config\n"); |
shimniok | 4:de7feb458652 | 138 | } |
shimniok | 10:9fb3feb38746 | 139 | |
shimniok | 13:5566df1250f1 | 140 | printf("Starting shell...\n"); |
shimniok | 1:7019a60fd585 | 141 | sh.attach(test, "test"); |
shimniok | 9:fc3575d2cbbf | 142 | sh.attach(read_gyro, "gyro"); |
shimniok | 14:1dd83e626153 | 143 | sh.attach(read_enc, "enc"); |
shimniok | 18:3f8a8f6e3cc1 | 144 | sh.attach(read_gps, "gps"); |
shimniok | 12:3cd91e150d9c | 145 | sh.attach(reset, "reset"); |
shimniok | 15:35c40765f7c3 | 146 | Thread shellThread; |
shimniok | 15:35c40765f7c3 | 147 | shellThread.start(callback(&sh, &SimpleShell::run)); |
shimniok | 0:7e98bbfd102a | 148 | |
shimniok | 13:5566df1250f1 | 149 | printf("Starting updater...\n"); |
shimniok | 11:8ec858b7c6d1 | 150 | Updater *u = Updater::instance(); |
shimniok | 21:020f4ebbb13a | 151 | u->setInterval(20); |
shimniok | 22:4d62bd16f037 | 152 | Thread updaterThread(osPriorityRealtime); |
shimniok | 15:35c40765f7c3 | 153 | updaterThread.start(callback(u, &Updater::start)); |
shimniok | 22:4d62bd16f037 | 154 | |
shimniok | 16:eb28d0f64a9b | 155 | printf("Starting gps...\n"); |
shimniok | 22:4d62bd16f037 | 156 | Thread gpsThread(osPriorityHigh); |
shimniok | 22:4d62bd16f037 | 157 | gpsThread.start(callback(&gpsQueue, &EventQueue::dispatch_forever)); |
shimniok | 22:4d62bd16f037 | 158 | s.attach(gps_handler); |
shimniok | 10:9fb3feb38746 | 159 | |
shimniok | 20:043987d06f8d | 160 | /* |
shimniok | 0:7e98bbfd102a | 161 | FILE *fp; |
shimniok | 0:7e98bbfd102a | 162 | char buf[128]; |
shimniok | 0:7e98bbfd102a | 163 | printf("Initializing the block device... "); |
shimniok | 0:7e98bbfd102a | 164 | fflush(stdout); |
shimniok | 0:7e98bbfd102a | 165 | int err = bd.init(); |
shimniok | 0:7e98bbfd102a | 166 | printf("%s\n", (err ? "Fail :(" : "OK")); |
shimniok | 0:7e98bbfd102a | 167 | |
shimniok | 0:7e98bbfd102a | 168 | printf("Opening sdtest.txt..."); |
shimniok | 0:7e98bbfd102a | 169 | fp = fopen("/log/sdtest.txt", "r"); |
shimniok | 0:7e98bbfd102a | 170 | if(fp) { |
shimniok | 0:7e98bbfd102a | 171 | while (!feof(fp)) { |
shimniok | 0:7e98bbfd102a | 172 | fgets(buf, 127, fp); |
shimniok | 0:7e98bbfd102a | 173 | printf(buf); |
shimniok | 0:7e98bbfd102a | 174 | } |
shimniok | 0:7e98bbfd102a | 175 | fclose(fp); |
shimniok | 0:7e98bbfd102a | 176 | } |
shimniok | 0:7e98bbfd102a | 177 | |
shimniok | 0:7e98bbfd102a | 178 | printf("Opening config.txt..."); |
shimniok | 0:7e98bbfd102a | 179 | fp = fopen("/etc/config.txt", "r"); |
shimniok | 0:7e98bbfd102a | 180 | if(fp) { |
shimniok | 0:7e98bbfd102a | 181 | while (!feof(fp)) { |
shimniok | 0:7e98bbfd102a | 182 | fgets(buf, 127, fp); |
shimniok | 0:7e98bbfd102a | 183 | printf(buf); |
shimniok | 0:7e98bbfd102a | 184 | } |
shimniok | 0:7e98bbfd102a | 185 | fclose(fp); |
shimniok | 0:7e98bbfd102a | 186 | } |
shimniok | 0:7e98bbfd102a | 187 | */ |
shimniok | 0:7e98bbfd102a | 188 | |
shimniok | 0:7e98bbfd102a | 189 | //SystemReport sys_state(500); |
shimniok | 0:7e98bbfd102a | 190 | |
shimniok | 0:7e98bbfd102a | 191 | while (true) { |
shimniok | 0:7e98bbfd102a | 192 | // Blink LED and wait 0.5 seconds |
shimniok | 0:7e98bbfd102a | 193 | led1 = !led1; |
shimniok | 0:7e98bbfd102a | 194 | wait(0.5f); |
shimniok | 0:7e98bbfd102a | 195 | |
shimniok | 0:7e98bbfd102a | 196 | // Following the main thread wait, report on the current system status |
shimniok | 0:7e98bbfd102a | 197 | //sys_state.report_state(); |
shimniok | 0:7e98bbfd102a | 198 | } |
shimniok | 0:7e98bbfd102a | 199 | } |