2018 revision to classic DataBus AVC code.

Dependencies:   LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell

Committer:
shimniok
Date:
Wed Dec 12 18:21:57 2018 +0000
Revision:
14:1dd83e626153
Parent:
13:5566df1250f1
Child:
15:35c40765f7c3
Added encoder read to Updater, added documentation, added shell cmd to read encoder

Who changed what in which revision?

UserRevisionLine numberNew 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 11:8ec858b7c6d1 14 #include "Updater.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 0:7e98bbfd102a 23 DigitalOut led1(LED1);
shimniok 10:9fb3feb38746 24 Thread thread;
shimniok 0:7e98bbfd102a 25
shimniok 9:fc3575d2cbbf 26 /******** SHELL COMMANDS ********/
shimniok 9:fc3575d2cbbf 27
shimniok 1:7019a60fd585 28 void test() {
shimniok 1:7019a60fd585 29 printf("Hello world!\n");
shimniok 1:7019a60fd585 30 }
shimniok 1:7019a60fd585 31
shimniok 9:fc3575d2cbbf 32 void read_gyro()
shimniok 9:fc3575d2cbbf 33 {
shimniok 12:3cd91e150d9c 34 int g[3];
shimniok 13:5566df1250f1 35 float dt;
shimniok 12:3cd91e150d9c 36
shimniok 11:8ec858b7c6d1 37 Updater *u = Updater::instance();
shimniok 11:8ec858b7c6d1 38
shimniok 13:5566df1250f1 39 u->gyro(g, dt);
shimniok 11:8ec858b7c6d1 40
shimniok 13:5566df1250f1 41 printf("Gyro: %d, %d, %d - dt: %f\n", g[0], g[1], g[2], dt);
shimniok 12:3cd91e150d9c 42 }
shimniok 12:3cd91e150d9c 43
shimniok 14:1dd83e626153 44 void read_enc()
shimniok 14:1dd83e626153 45 {
shimniok 14:1dd83e626153 46 Updater *u = Updater::instance();
shimniok 14:1dd83e626153 47
shimniok 14:1dd83e626153 48 printf("Encoder: %d\n", u->encoder());
shimniok 14:1dd83e626153 49 }
shimniok 14:1dd83e626153 50
shimniok 12:3cd91e150d9c 51 void reset()
shimniok 12:3cd91e150d9c 52 {
shimniok 12:3cd91e150d9c 53 NVIC_SystemReset();
shimniok 9:fc3575d2cbbf 54 }
shimniok 9:fc3575d2cbbf 55
shimniok 9:fc3575d2cbbf 56 /******** MAIN ********/
shimniok 1:7019a60fd585 57
shimniok 0:7e98bbfd102a 58 // main() runs in its own thread in the OS
shimniok 0:7e98bbfd102a 59 int main()
shimniok 11:8ec858b7c6d1 60 {
shimniok 0:7e98bbfd102a 61 printf("Bootup...\n");
shimniok 0:7e98bbfd102a 62 fflush(stdout);
shimniok 1:7019a60fd585 63
shimniok 4:de7feb458652 64 printf("Loading config...\n");
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 10:9fb3feb38746 90
shimniok 13:5566df1250f1 91 printf("Starting shell...\n");
shimniok 1:7019a60fd585 92 sh.attach(test, "test");
shimniok 9:fc3575d2cbbf 93 sh.attach(read_gyro, "gyro");
shimniok 14:1dd83e626153 94 sh.attach(read_enc, "enc");
shimniok 12:3cd91e150d9c 95 sh.attach(reset, "reset");
shimniok 0:7e98bbfd102a 96 thread.start(callback(&sh, &SimpleShell::run));
shimniok 0:7e98bbfd102a 97
shimniok 13:5566df1250f1 98 printf("Starting updater...\n");
shimniok 11:8ec858b7c6d1 99 Updater *u = Updater::instance();
shimniok 11:8ec858b7c6d1 100 u->start(50);
shimniok 10:9fb3feb38746 101
shimniok 0:7e98bbfd102a 102 /*
shimniok 0:7e98bbfd102a 103 FILE *fp;
shimniok 0:7e98bbfd102a 104 char buf[128];
shimniok 0:7e98bbfd102a 105 printf("Initializing the block device... ");
shimniok 0:7e98bbfd102a 106 fflush(stdout);
shimniok 0:7e98bbfd102a 107 int err = bd.init();
shimniok 0:7e98bbfd102a 108 printf("%s\n", (err ? "Fail :(" : "OK"));
shimniok 0:7e98bbfd102a 109
shimniok 0:7e98bbfd102a 110 printf("Opening sdtest.txt...");
shimniok 0:7e98bbfd102a 111 fp = fopen("/log/sdtest.txt", "r");
shimniok 0:7e98bbfd102a 112 if(fp) {
shimniok 0:7e98bbfd102a 113 while (!feof(fp)) {
shimniok 0:7e98bbfd102a 114 fgets(buf, 127, fp);
shimniok 0:7e98bbfd102a 115 printf(buf);
shimniok 0:7e98bbfd102a 116 }
shimniok 0:7e98bbfd102a 117 fclose(fp);
shimniok 0:7e98bbfd102a 118 }
shimniok 0:7e98bbfd102a 119
shimniok 0:7e98bbfd102a 120 printf("Opening config.txt...");
shimniok 0:7e98bbfd102a 121 fp = fopen("/etc/config.txt", "r");
shimniok 0:7e98bbfd102a 122 if(fp) {
shimniok 0:7e98bbfd102a 123 while (!feof(fp)) {
shimniok 0:7e98bbfd102a 124 fgets(buf, 127, fp);
shimniok 0:7e98bbfd102a 125 printf(buf);
shimniok 0:7e98bbfd102a 126 }
shimniok 0:7e98bbfd102a 127 fclose(fp);
shimniok 0:7e98bbfd102a 128 }
shimniok 0:7e98bbfd102a 129 */
shimniok 0:7e98bbfd102a 130
shimniok 0:7e98bbfd102a 131 //SystemReport sys_state(500);
shimniok 0:7e98bbfd102a 132
shimniok 0:7e98bbfd102a 133 while (true) {
shimniok 0:7e98bbfd102a 134 // Blink LED and wait 0.5 seconds
shimniok 0:7e98bbfd102a 135 led1 = !led1;
shimniok 0:7e98bbfd102a 136 wait(0.5f);
shimniok 0:7e98bbfd102a 137
shimniok 0:7e98bbfd102a 138 // Following the main thread wait, report on the current system status
shimniok 0:7e98bbfd102a 139 //sys_state.report_state();
shimniok 0:7e98bbfd102a 140 }
shimniok 0:7e98bbfd102a 141 }