2018 revision to classic DataBus AVC code.
Dependencies: LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell
main.cpp@16:eb28d0f64a9b, 2018-12-13 (annotated)
- Committer:
- shimniok
- Date:
- Thu Dec 13 17:35:29 2018 +0000
- Revision:
- 16:eb28d0f64a9b
- Parent:
- 15:35c40765f7c3
- Child:
- 18:3f8a8f6e3cc1
Initial gps receive implementation
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 | 0:7e98bbfd102a | 24 | |
shimniok | 0:7e98bbfd102a | 25 | DigitalOut led1(LED1); |
shimniok | 16:eb28d0f64a9b | 26 | DigitalOut led3(LED3); |
shimniok | 16:eb28d0f64a9b | 27 | |
shimniok | 16:eb28d0f64a9b | 28 | void do_gps() { |
shimniok | 16:eb28d0f64a9b | 29 | Ublox6 ublox; |
shimniok | 16:eb28d0f64a9b | 30 | RawSerial s(UART1TX, UART1RX, 38400); |
shimniok | 16:eb28d0f64a9b | 31 | while (1) { |
shimniok | 16:eb28d0f64a9b | 32 | int c = s.getc(); |
shimniok | 16:eb28d0f64a9b | 33 | //int c = 'a'; |
shimniok | 16:eb28d0f64a9b | 34 | ublox.parse(c); |
shimniok | 16:eb28d0f64a9b | 35 | led3 = !led3; |
shimniok | 16:eb28d0f64a9b | 36 | } |
shimniok | 16:eb28d0f64a9b | 37 | } |
shimniok | 16:eb28d0f64a9b | 38 | |
shimniok | 0:7e98bbfd102a | 39 | |
shimniok | 9:fc3575d2cbbf | 40 | /******** SHELL COMMANDS ********/ |
shimniok | 9:fc3575d2cbbf | 41 | |
shimniok | 1:7019a60fd585 | 42 | void test() { |
shimniok | 1:7019a60fd585 | 43 | printf("Hello world!\n"); |
shimniok | 1:7019a60fd585 | 44 | } |
shimniok | 1:7019a60fd585 | 45 | |
shimniok | 9:fc3575d2cbbf | 46 | void read_gyro() |
shimniok | 9:fc3575d2cbbf | 47 | { |
shimniok | 12:3cd91e150d9c | 48 | int g[3]; |
shimniok | 13:5566df1250f1 | 49 | float dt; |
shimniok | 12:3cd91e150d9c | 50 | |
shimniok | 11:8ec858b7c6d1 | 51 | Updater *u = Updater::instance(); |
shimniok | 11:8ec858b7c6d1 | 52 | |
shimniok | 13:5566df1250f1 | 53 | u->gyro(g, dt); |
shimniok | 11:8ec858b7c6d1 | 54 | |
shimniok | 13:5566df1250f1 | 55 | printf("Gyro: %d, %d, %d - dt: %f\n", g[0], g[1], g[2], dt); |
shimniok | 12:3cd91e150d9c | 56 | } |
shimniok | 12:3cd91e150d9c | 57 | |
shimniok | 14:1dd83e626153 | 58 | void read_enc() |
shimniok | 14:1dd83e626153 | 59 | { |
shimniok | 14:1dd83e626153 | 60 | Updater *u = Updater::instance(); |
shimniok | 14:1dd83e626153 | 61 | |
shimniok | 14:1dd83e626153 | 62 | printf("Encoder: %d\n", u->encoder()); |
shimniok | 14:1dd83e626153 | 63 | } |
shimniok | 14:1dd83e626153 | 64 | |
shimniok | 12:3cd91e150d9c | 65 | void reset() |
shimniok | 12:3cd91e150d9c | 66 | { |
shimniok | 12:3cd91e150d9c | 67 | NVIC_SystemReset(); |
shimniok | 9:fc3575d2cbbf | 68 | } |
shimniok | 9:fc3575d2cbbf | 69 | |
shimniok | 9:fc3575d2cbbf | 70 | /******** MAIN ********/ |
shimniok | 1:7019a60fd585 | 71 | |
shimniok | 0:7e98bbfd102a | 72 | // main() runs in its own thread in the OS |
shimniok | 0:7e98bbfd102a | 73 | int main() |
shimniok | 11:8ec858b7c6d1 | 74 | { |
shimniok | 0:7e98bbfd102a | 75 | printf("Bootup...\n"); |
shimniok | 0:7e98bbfd102a | 76 | fflush(stdout); |
shimniok | 1:7019a60fd585 | 77 | |
shimniok | 4:de7feb458652 | 78 | printf("Loading config...\n"); |
shimniok | 7:1f2661b840ed | 79 | config.add("intercept_distance", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 80 | config.add("waypoint_threshold", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 81 | config.add("minimum_turning_radius", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 82 | config.add("wheelbase", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 83 | config.add("track_width", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 84 | config.add("tire_circumference", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 85 | config.add("encoder_stripes", Config::INT); |
shimniok | 7:1f2661b840ed | 86 | config.add("esc_brake", Config::INT); |
shimniok | 7:1f2661b840ed | 87 | config.add("esc_off", Config::INT); |
shimniok | 7:1f2661b840ed | 88 | config.add("esc_max", Config::INT); |
shimniok | 7:1f2661b840ed | 89 | config.add("turn_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 90 | config.add("turn_distance", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 91 | config.add("start_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 92 | config.add("cruise_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 93 | config.add("speed_kp", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 94 | config.add("speed_ki", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 95 | config.add("speed_kd", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 96 | config.add("steer_center", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 97 | config.add("steer_scale", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 98 | config.add("gyro_scale", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 99 | config.add("gps_valid_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 100 | |
shimniok | 4:de7feb458652 | 101 | if (config.load("/etc/2018cfg.txt")) { |
shimniok | 4:de7feb458652 | 102 | printf("error loading config\n"); |
shimniok | 4:de7feb458652 | 103 | } |
shimniok | 10:9fb3feb38746 | 104 | |
shimniok | 13:5566df1250f1 | 105 | printf("Starting shell...\n"); |
shimniok | 1:7019a60fd585 | 106 | sh.attach(test, "test"); |
shimniok | 9:fc3575d2cbbf | 107 | sh.attach(read_gyro, "gyro"); |
shimniok | 14:1dd83e626153 | 108 | sh.attach(read_enc, "enc"); |
shimniok | 12:3cd91e150d9c | 109 | sh.attach(reset, "reset"); |
shimniok | 15:35c40765f7c3 | 110 | Thread shellThread; |
shimniok | 15:35c40765f7c3 | 111 | shellThread.start(callback(&sh, &SimpleShell::run)); |
shimniok | 0:7e98bbfd102a | 112 | |
shimniok | 13:5566df1250f1 | 113 | printf("Starting updater...\n"); |
shimniok | 11:8ec858b7c6d1 | 114 | Updater *u = Updater::instance(); |
shimniok | 15:35c40765f7c3 | 115 | u->setInterval(50); |
shimniok | 15:35c40765f7c3 | 116 | Thread updaterThread; |
shimniok | 15:35c40765f7c3 | 117 | updaterThread.set_priority(osPriorityRealtime); |
shimniok | 15:35c40765f7c3 | 118 | updaterThread.start(callback(u, &Updater::start)); |
shimniok | 16:eb28d0f64a9b | 119 | |
shimniok | 16:eb28d0f64a9b | 120 | printf("Starting gps...\n"); |
shimniok | 16:eb28d0f64a9b | 121 | Thread gpsThread; |
shimniok | 16:eb28d0f64a9b | 122 | gpsThread.start(&do_gps); |
shimniok | 10:9fb3feb38746 | 123 | |
shimniok | 0:7e98bbfd102a | 124 | /* |
shimniok | 0:7e98bbfd102a | 125 | FILE *fp; |
shimniok | 0:7e98bbfd102a | 126 | char buf[128]; |
shimniok | 0:7e98bbfd102a | 127 | printf("Initializing the block device... "); |
shimniok | 0:7e98bbfd102a | 128 | fflush(stdout); |
shimniok | 0:7e98bbfd102a | 129 | int err = bd.init(); |
shimniok | 0:7e98bbfd102a | 130 | printf("%s\n", (err ? "Fail :(" : "OK")); |
shimniok | 0:7e98bbfd102a | 131 | |
shimniok | 0:7e98bbfd102a | 132 | printf("Opening sdtest.txt..."); |
shimniok | 0:7e98bbfd102a | 133 | fp = fopen("/log/sdtest.txt", "r"); |
shimniok | 0:7e98bbfd102a | 134 | if(fp) { |
shimniok | 0:7e98bbfd102a | 135 | while (!feof(fp)) { |
shimniok | 0:7e98bbfd102a | 136 | fgets(buf, 127, fp); |
shimniok | 0:7e98bbfd102a | 137 | printf(buf); |
shimniok | 0:7e98bbfd102a | 138 | } |
shimniok | 0:7e98bbfd102a | 139 | fclose(fp); |
shimniok | 0:7e98bbfd102a | 140 | } |
shimniok | 0:7e98bbfd102a | 141 | |
shimniok | 0:7e98bbfd102a | 142 | printf("Opening config.txt..."); |
shimniok | 0:7e98bbfd102a | 143 | fp = fopen("/etc/config.txt", "r"); |
shimniok | 0:7e98bbfd102a | 144 | if(fp) { |
shimniok | 0:7e98bbfd102a | 145 | while (!feof(fp)) { |
shimniok | 0:7e98bbfd102a | 146 | fgets(buf, 127, fp); |
shimniok | 0:7e98bbfd102a | 147 | printf(buf); |
shimniok | 0:7e98bbfd102a | 148 | } |
shimniok | 0:7e98bbfd102a | 149 | fclose(fp); |
shimniok | 0:7e98bbfd102a | 150 | } |
shimniok | 0:7e98bbfd102a | 151 | */ |
shimniok | 0:7e98bbfd102a | 152 | |
shimniok | 0:7e98bbfd102a | 153 | //SystemReport sys_state(500); |
shimniok | 0:7e98bbfd102a | 154 | |
shimniok | 0:7e98bbfd102a | 155 | while (true) { |
shimniok | 0:7e98bbfd102a | 156 | // Blink LED and wait 0.5 seconds |
shimniok | 0:7e98bbfd102a | 157 | led1 = !led1; |
shimniok | 0:7e98bbfd102a | 158 | wait(0.5f); |
shimniok | 0:7e98bbfd102a | 159 | |
shimniok | 0:7e98bbfd102a | 160 | // Following the main thread wait, report on the current system status |
shimniok | 0:7e98bbfd102a | 161 | //sys_state.report_state(); |
shimniok | 0:7e98bbfd102a | 162 | } |
shimniok | 0:7e98bbfd102a | 163 | } |