![](/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@9:fc3575d2cbbf, 2018-12-07 (annotated)
- Committer:
- shimniok
- Date:
- Fri Dec 07 15:58:41 2018 +0000
- Revision:
- 9:fc3575d2cbbf
- Parent:
- 7:1f2661b840ed
- Child:
- 10:9fb3feb38746
Test gyro reading from shell.
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 | 0:7e98bbfd102a | 9 | #include "stats_report.h" |
shimniok | 0:7e98bbfd102a | 10 | #include "SDBlockDevice.h" |
shimniok | 0:7e98bbfd102a | 11 | #include "FATFileSystem.h" |
shimniok | 0:7e98bbfd102a | 12 | #include "SimpleShell.h" |
shimniok | 4:de7feb458652 | 13 | #include "Config.h" |
shimniok | 9:fc3575d2cbbf | 14 | #include "L3G4200D.h" |
shimniok | 0:7e98bbfd102a | 15 | |
shimniok | 0:7e98bbfd102a | 16 | Serial pc(USBTX, USBRX); |
shimniok | 0:7e98bbfd102a | 17 | //SDBlockDevice bd(p5, p6, p7, p8); // MOSI, MISO, CLK, CS |
shimniok | 0:7e98bbfd102a | 18 | //FATFileSystem ffs("log", &bd); |
shimniok | 0:7e98bbfd102a | 19 | LocalFileSystem lfs("etc"); |
shimniok | 4:de7feb458652 | 20 | Config config; |
shimniok | 0:7e98bbfd102a | 21 | SimpleShell sh; |
shimniok | 0:7e98bbfd102a | 22 | |
shimniok | 9:fc3575d2cbbf | 23 | L3G4200D gyro(p9, p10); |
shimniok | 0:7e98bbfd102a | 24 | DigitalOut led1(LED1); |
shimniok | 0:7e98bbfd102a | 25 | |
shimniok | 0:7e98bbfd102a | 26 | Thread thread; |
shimniok | 0:7e98bbfd102a | 27 | |
shimniok | 9:fc3575d2cbbf | 28 | /******** SHELL COMMANDS ********/ |
shimniok | 9:fc3575d2cbbf | 29 | |
shimniok | 1:7019a60fd585 | 30 | void test() { |
shimniok | 1:7019a60fd585 | 31 | printf("Hello world!\n"); |
shimniok | 1:7019a60fd585 | 32 | } |
shimniok | 1:7019a60fd585 | 33 | |
shimniok | 9:fc3575d2cbbf | 34 | void read_gyro() |
shimniok | 9:fc3575d2cbbf | 35 | { |
shimniok | 9:fc3575d2cbbf | 36 | int g[3]; |
shimniok | 9:fc3575d2cbbf | 37 | gyro.read(g); |
shimniok | 9:fc3575d2cbbf | 38 | printf("Gyro: %d, %d, %d\n", g[0], g[1], g[2]); |
shimniok | 9:fc3575d2cbbf | 39 | } |
shimniok | 9:fc3575d2cbbf | 40 | |
shimniok | 9:fc3575d2cbbf | 41 | |
shimniok | 9:fc3575d2cbbf | 42 | /******** GYRO UPDATER ********/ |
shimniok | 9:fc3575d2cbbf | 43 | void gyro_update() |
shimniok | 9:fc3575d2cbbf | 44 | { |
shimniok | 9:fc3575d2cbbf | 45 | int g[3]; |
shimniok | 9:fc3575d2cbbf | 46 | |
shimniok | 9:fc3575d2cbbf | 47 | while (1) { |
shimniok | 9:fc3575d2cbbf | 48 | gyro.read(g); |
shimniok | 9:fc3575d2cbbf | 49 | // send message / add to status fifo? |
shimniok | 9:fc3575d2cbbf | 50 | } |
shimniok | 9:fc3575d2cbbf | 51 | } |
shimniok | 9:fc3575d2cbbf | 52 | |
shimniok | 9:fc3575d2cbbf | 53 | |
shimniok | 9:fc3575d2cbbf | 54 | /******** MAIN ********/ |
shimniok | 1:7019a60fd585 | 55 | |
shimniok | 0:7e98bbfd102a | 56 | // main() runs in its own thread in the OS |
shimniok | 0:7e98bbfd102a | 57 | int main() |
shimniok | 0:7e98bbfd102a | 58 | { |
shimniok | 0:7e98bbfd102a | 59 | printf("Bootup...\n"); |
shimniok | 0:7e98bbfd102a | 60 | fflush(stdout); |
shimniok | 1:7019a60fd585 | 61 | |
shimniok | 4:de7feb458652 | 62 | printf("Loading config...\n"); |
shimniok | 7:1f2661b840ed | 63 | |
shimniok | 7:1f2661b840ed | 64 | // Add valid keywords |
shimniok | 7:1f2661b840ed | 65 | config.add("intercept_distance", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 66 | config.add("waypoint_threshold", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 67 | config.add("minimum_turning_radius", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 68 | config.add("wheelbase", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 69 | config.add("track_width", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 70 | config.add("tire_circumference", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 71 | config.add("encoder_stripes", Config::INT); |
shimniok | 7:1f2661b840ed | 72 | config.add("esc_brake", Config::INT); |
shimniok | 7:1f2661b840ed | 73 | config.add("esc_off", Config::INT); |
shimniok | 7:1f2661b840ed | 74 | config.add("esc_max", Config::INT); |
shimniok | 7:1f2661b840ed | 75 | config.add("turn_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 76 | config.add("turn_distance", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 77 | config.add("start_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 78 | config.add("cruise_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 79 | config.add("speed_kp", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 80 | config.add("speed_ki", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 81 | config.add("speed_kd", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 82 | config.add("steer_center", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 83 | config.add("steer_scale", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 84 | config.add("gyro_scale", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 85 | config.add("gps_valid_speed", Config::DOUBLE); |
shimniok | 7:1f2661b840ed | 86 | |
shimniok | 4:de7feb458652 | 87 | if (config.load("/etc/2018cfg.txt")) { |
shimniok | 4:de7feb458652 | 88 | printf("error loading config\n"); |
shimniok | 4:de7feb458652 | 89 | } |
shimniok | 4:de7feb458652 | 90 | |
shimniok | 1:7019a60fd585 | 91 | sh.attach(test, "test"); |
shimniok | 9:fc3575d2cbbf | 92 | sh.attach(read_gyro, "gyro"); |
shimniok | 1:7019a60fd585 | 93 | |
shimniok | 0:7e98bbfd102a | 94 | thread.start(callback(&sh, &SimpleShell::run)); |
shimniok | 0:7e98bbfd102a | 95 | |
shimniok | 0:7e98bbfd102a | 96 | /* |
shimniok | 0:7e98bbfd102a | 97 | FILE *fp; |
shimniok | 0:7e98bbfd102a | 98 | char buf[128]; |
shimniok | 0:7e98bbfd102a | 99 | printf("Initializing the block device... "); |
shimniok | 0:7e98bbfd102a | 100 | fflush(stdout); |
shimniok | 0:7e98bbfd102a | 101 | int err = bd.init(); |
shimniok | 0:7e98bbfd102a | 102 | printf("%s\n", (err ? "Fail :(" : "OK")); |
shimniok | 0:7e98bbfd102a | 103 | |
shimniok | 0:7e98bbfd102a | 104 | printf("Opening sdtest.txt..."); |
shimniok | 0:7e98bbfd102a | 105 | fp = fopen("/log/sdtest.txt", "r"); |
shimniok | 0:7e98bbfd102a | 106 | if(fp) { |
shimniok | 0:7e98bbfd102a | 107 | while (!feof(fp)) { |
shimniok | 0:7e98bbfd102a | 108 | fgets(buf, 127, fp); |
shimniok | 0:7e98bbfd102a | 109 | printf(buf); |
shimniok | 0:7e98bbfd102a | 110 | } |
shimniok | 0:7e98bbfd102a | 111 | fclose(fp); |
shimniok | 0:7e98bbfd102a | 112 | } |
shimniok | 0:7e98bbfd102a | 113 | |
shimniok | 0:7e98bbfd102a | 114 | printf("Opening config.txt..."); |
shimniok | 0:7e98bbfd102a | 115 | fp = fopen("/etc/config.txt", "r"); |
shimniok | 0:7e98bbfd102a | 116 | if(fp) { |
shimniok | 0:7e98bbfd102a | 117 | while (!feof(fp)) { |
shimniok | 0:7e98bbfd102a | 118 | fgets(buf, 127, fp); |
shimniok | 0:7e98bbfd102a | 119 | printf(buf); |
shimniok | 0:7e98bbfd102a | 120 | } |
shimniok | 0:7e98bbfd102a | 121 | fclose(fp); |
shimniok | 0:7e98bbfd102a | 122 | } |
shimniok | 0:7e98bbfd102a | 123 | */ |
shimniok | 0:7e98bbfd102a | 124 | |
shimniok | 0:7e98bbfd102a | 125 | //SystemReport sys_state(500); |
shimniok | 0:7e98bbfd102a | 126 | |
shimniok | 0:7e98bbfd102a | 127 | while (true) { |
shimniok | 0:7e98bbfd102a | 128 | // Blink LED and wait 0.5 seconds |
shimniok | 0:7e98bbfd102a | 129 | led1 = !led1; |
shimniok | 0:7e98bbfd102a | 130 | wait(0.5f); |
shimniok | 0:7e98bbfd102a | 131 | |
shimniok | 0:7e98bbfd102a | 132 | // Following the main thread wait, report on the current system status |
shimniok | 0:7e98bbfd102a | 133 | //sys_state.report_state(); |
shimniok | 0:7e98bbfd102a | 134 | } |
shimniok | 0:7e98bbfd102a | 135 | } |