most functionality to splashdwon, find neutral and start mission. short timeouts still in code for testing, will adjust to go directly to sit_idle after splashdown

Dependencies:   mbed MODSERIAL FATFileSystem

Tue Nov 21 22:03:26 2017 +0000
FSG code commit 11/21

Who changed what in which revision?

UserRevisionLine numberNew contents of line
danstrider 10:085ab7328054 1 /*
danstrider 10:085ab7328054 2 Starting from Trent's Linear Actuator code from 2017-10-19, these modifications
danstrider 10:085ab7328054 3 by Dan add an outer loop controller for depth and pitch to command the inner
danstrider 10:085ab7328054 4 linear actuator loops.
danstrider 10:085ab7328054 5 Modified 2017-10-20 revA by Dan
danstrider 10:085ab7328054 6 - added outer loop controller, but it is hanging the mbed. (turns out it was the imu update)
danstrider 10:085ab7328054 7 Modified 2017-10-22 revA by Dan
danstrider 10:085ab7328054 8 - outer loop now works with a call to outerloop.update() in main loop(), not with an attached ticker
danstrider 10:085ab7328054 9 Modified 2017-10-22 revB by Dan
danstrider 10:085ab7328054 10 - enabled both depth and pitch outer loop controllers
danstrider 10:085ab7328054 11 - added ability to keyboard reset
danstrider 10:085ab7328054 12 Modified 2017-10-22 revC by Dan
danstrider 10:085ab7328054 13 - major update to the IMU library processing to make a state machine that doesn't hang
danstrider 10:085ab7328054 14 - added lat/lon/alt and GNSS fix information to the IMU library
danstrider 10:085ab7328054 15 - brought out the pin names into the constructors of IMU, omega, SpiADC
danstrider 10:085ab7328054 16 Modified 2017-10-22 revD by Dan
danstrider 10:085ab7328054 17 - everything seems to be working, shy of re-checking on the hardware
danstrider 10:085ab7328054 18 - Depth sensor call done inside the OuterLoop, but should somehow be set as a callback instead
danstrider 10:085ab7328054 19 - IMU sensor call done inside the OuterLoop, but should somehow be set as a callback instead
danstrider 10:085ab7328054 20 Modified 2017-10-23 revA by Dan/Matt
danstrider 10:085ab7328054 21 - linear actuator hardware works great, limit switches, sensing, etc.
danstrider 10:085ab7328054 22 - outer loops run, but move the BCE in the wrong direction.
danstrider 10:085ab7328054 23 - new IMU code doesn't read from the sensor correctly, but doesn't hang up either.
danstrider 10:085ab7328054 24 - depth sensor worked fine, just needs zero bias adjustment.
danstrider 11:3b241ecb75ed 25 Modified 2017-10-24 by Troy
danstrider 11:3b241ecb75ed 26 - added offset to outerloop
danstrider 11:3b241ecb75ed 27 Modified 2017-10-26 by Dan
danstrider 11:3b241ecb75ed 28 - brought over a state machine and new keyboard controls ... currently just dumped into main.
danstrider 11:3b241ecb75ed 29 Modified 2017-10-26 by Matt
danstrider 11:3b241ecb75ed 30 - new IMU code imported and working well with the hardware.
danstrider 11:3b241ecb75ed 31 Modified 2017-10-26 revB by Dan
danstrider 11:3b241ecb75ed 32 - This version has been in the pool.
danstrider 11:3b241ecb75ed 33 - Get occasional ADC bad string pot batt & piston. On initial sensor reads, see negative positions.
danstrider 11:3b241ecb75ed 34 But after running FLOAT_BROADCAST, string pot positions are normal. Not sure why.
danstrider 11:3b241ecb75ed 35 - Repeatedly got stuck in RISE with a +60s timeout. Battery hit end bell and stalled out.
danstrider 11:3b241ecb75ed 36 - keyboard and state machine are in main, probably shouldn't be, but easier to debug.
danstrider 11:3b241ecb75ed 37 - Really happy with the logic and flow of the state machine. Timeouts work.
danstrider 11:3b241ecb75ed 38 - Need to add a means to drive the linear actuators manually ... toggle out stop() in SIT_IDLE.
danstrider 11:3b241ecb75ed 39 - Need to add keyboard commands to modify the zeroOffset positions.
danstrider 14:85b64a4d08e8 40 Modified 2017-10-30 by Dan, Trent, Matt, Troy
danstrider 14:85b64a4d08e8 41 - changed .stop() to .pause() and many .start() to .unpause() ... fixed the negative ADC and lets
danstrider 14:85b64a4d08e8 42 the PVF's keep running even though the motor isn't moving.
danstrider 14:85b64a4d08e8 43 - changed exit conditions to use filtered depth from the outer loop. less noisy.
danstrider 14:85b64a4d08e8 44 Modified 2017-10-31 by Dan, Matt
danstrider 14:85b64a4d08e8 45 - added oversampling and a tare function to the depth sensor.
danstrider 14:85b64a4d08e8 46 - updated main and keyboard to include tare in setup() and a keyboard tare option.
danstrider 14:85b64a4d08e8 47 - looks like the piston isn't zeroing correctly, unclear why, should be unrelated to depth sensor.
danstrider 15:2a8cfd3f1cf5 48 Modified 2017-10-31 (again) by Dan, Matt, and Troy
danstrider 15:2a8cfd3f1cf5 49 - POOL TESTED!
danstrider 15:2a8cfd3f1cf5 50 - Tuned the PID gains with magic. Works to hold mostly level during BCE runs. Dive is awesome.
danstrider 15:2a8cfd3f1cf5 51 - Neutral won't do what we want ... without large depth P, it won't get to depth.
danstrider 15:2a8cfd3f1cf5 52 - Dive with just P doesn't overshoot depth at 0.0 deg, but does with -20 deg. Timeout should probably be RISE.
danstrider 15:2a8cfd3f1cf5 53 - Files depth/pitch updated with tuned defaults.
tnhnrl 16:3363b9f14913 54 Modified 2017-11-06 by Troy
tnhnrl 16:3363b9f14913 55 - Added acronyms to print statements (when running this with XBee you don't know what mode the
tnhnrl 16:3363b9f14913 56 hardware is in because of signal dropping out)
tnhnrl 16:3363b9f14913 57 - Fixed print error with setpoints by printing out the setpoint variable, not the outerloop getCommand
tnhnrl 16:3363b9f14913 58 (command is sent every time hardware dives, finds neutral, etc.)
tnhnrl 16:3363b9f14913 59 - Set the pitchCommand input to 0 on the "find neutral" command, it was sending a non-zero pitch command
tnhnrl 16:3363b9f14913 60 - Created a class for the StateMachine
tnhnrl 17:7c16b5671d0e 61 Modified 2017-11-14 by Troy
tnhnrl 17:7c16b5671d0e 62 - Changed tare to void function (functions does not return anything when called)
tnhnrl 17:7c16b5671d0e 63 - Added a "subclass" in the cases for the Neutral Finding Sequence (Sinking, Slowly Rise, Check Pitch (and save positions))
tnhnrl 17:7c16b5671d0e 64 Modified 2017-11-20 by Troy
tnhnrl 17:7c16b5671d0e 65 - Modified StateMachine class to separate keyboard inputs from FSM
tnhnrl 17:7c16b5671d0e 66 - Added Neutral Finding sub-statemachine
tnhnrl 17:7c16b5671d0e 67 - Verified both state machines are working with hardware on desktop
tnhnrl 17:7c16b5671d0e 68 - Added class to save neutral battery and buoyancy engine positions to neutral.cfg file
tnhnrl 17:7c16b5671d0e 69 Modified 2017-11-21 by Troy
tnhnrl 17:7c16b5671d0e 70 - Need to check remove ConfigFileIO and place saving functions into config_functions.cpp (rename ConfigFunctions.cpp)
tnhnrl 17:7c16b5671d0e 71 - Added multi-dive (and multi-rise) states to FSM (may incorporate single dive states into states)
tnhnrl 17:7c16b5671d0e 72 - Need to double-check behavior of sub-FSM but bench-top testing showed correct behavior for transition from sinking, to slowly rise, to check pitch
tnhnrl 17:7c16b5671d0e 73 - Need to add a check for the multi-dive sequence file (sequence.cfg) being loaded on the MBED
danstrider 10:085ab7328054 74 */
danstrider 10:085ab7328054 75
tzyoung 0:ea293bbf9717 76 #include "mbed.h"
tzyoung 0:ea293bbf9717 77 #include "StaticDefs.hpp"
tzyoung 2:892b58e56712 78 #include "config_functions.h"
tzyoung 2:892b58e56712 79
tnhnrl 17:7c16b5671d0e 80 volatile bool blocker = true; //volatile used to prevent compiler from optimizing this (assuming the state is constant)
tnhnrl 17:7c16b5671d0e 81 volatile bool keyboard_flag = true;
tnhnrl 17:7c16b5671d0e 82
tnhnrl 17:7c16b5671d0e 83 Ticker blocker_ticker;
tnhnrl 17:7c16b5671d0e 84
tnhnrl 17:7c16b5671d0e 85 //blocker using a ticker
tnhnrl 17:7c16b5671d0e 86 void reset_blocker() {
tnhnrl 17:7c16b5671d0e 87 blocker = false;
tnhnrl 17:7c16b5671d0e 88 };
tnhnrl 17:7c16b5671d0e 89
danstrider 10:085ab7328054 90 void setup() {
tnhnrl 17:7c16b5671d0e 91 pc().printf("\n\n\r\rFSG 2017-11-20\n\r");
danstrider 11:3b241ecb75ed 92 pc().baud(57600);
tzyoung 4:66f13fbb035d 93
danstrider 10:085ab7328054 94 // start up the system timer
tzyoung 0:ea293bbf9717 95 systemTime().start();
tzyoung 0:ea293bbf9717 96
danstrider 10:085ab7328054 97 // set up and start the adc. This runs on a fixed interval and is interrupt driven
tzyoung 0:ea293bbf9717 98 adc().initialize();
tzyoung 0:ea293bbf9717 99 adc().start();
danstrider 10:085ab7328054 100
danstrider 10:085ab7328054 101 // set up and start the imu. This polls in the background
danstrider 10:085ab7328054 102 imu().initialize();
danstrider 10:085ab7328054 103 imu().start();
danstrider 10:085ab7328054 104
danstrider 10:085ab7328054 105 // set up the depth sensor. This is an internal ADC read, but eventually will be on the ltc1298
danstrider 14:85b64a4d08e8 106 depth().init();
danstrider 14:85b64a4d08e8 107 depth().tare();
danstrider 10:085ab7328054 108
danstrider 10:085ab7328054 109 // construct a local file system
danstrider 10:085ab7328054 110 local();
tzyoung 0:ea293bbf9717 111
danstrider 10:085ab7328054 112 // load config data from files
danstrider 10:085ab7328054 113 load_BCE_config(); // load the buoyancy engine parameters from the file "bce.txt"
danstrider 10:085ab7328054 114 load_BATT_config(); // load the battery mass mover parameters from the file "batt.txt"
danstrider 10:085ab7328054 115 load_DEPTH_config(); // load the depth control loop parameters from the file "depth.txt"
danstrider 10:085ab7328054 116 load_PITCH_config(); // load the depth control loop parameters from the file "pitch.txt"
tzyoung 4:66f13fbb035d 117
danstrider 10:085ab7328054 118 // set up the linear actuators. adc has to be running first.
danstrider 10:085ab7328054 119 bce().init();
danstrider 10:085ab7328054 120 bce().start();
mkelly10 12:a0519d11d2b6 121 bce().pause(); // start by not moving
tzyoung 4:66f13fbb035d 122
danstrider 10:085ab7328054 123 batt().init();
tzyoung 2:892b58e56712 124 batt().start();
mkelly10 12:a0519d11d2b6 125 batt().pause(); // start by not moving
danstrider 10:085ab7328054 126
danstrider 10:085ab7328054 127 // set up the depth and pitch outer loop controllers
danstrider 10:085ab7328054 128 depthLoop().init();
tnhnrl 13:84fcbe1dcd62 129 depthLoop().start();
tnhnrl 16:3363b9f14913 130 depthLoop().setCommand(stateMachine().getDepthCommand());
mkelly10 5:15bd96205bb2 131
danstrider 10:085ab7328054 132 pitchLoop().init();
tnhnrl 13:84fcbe1dcd62 133 pitchLoop().start();
tnhnrl 16:3363b9f14913 134 pitchLoop().setCommand(stateMachine().getPitchCommand());
danstrider 10:085ab7328054 135
danstrider 10:085ab7328054 136 // do not leave this in. Check that PID gains are loading
danstrider 11:3b241ecb75ed 137 pc().printf("bce P: %3.2f, I: %3.2f, D %3.2f, zero %3i, limit %3.0f mm, slope %3.3f \r\n", bce().getControllerP(), bce().getControllerI(), bce().getControllerD(), bce().getZeroCounts(), bce().getTravelLimit(), bce().getPotSlope());
danstrider 11:3b241ecb75ed 138 pc().printf("batt P: %3.2f, I: %3.2f, D %3.2f, zero %3i, limit %3.0f mm, slope %3.3f \r\n", batt().getControllerP(), batt().getControllerI(), batt().getControllerD(), batt().getZeroCounts(), batt().getTravelLimit(), batt().getPotSlope());
danstrider 11:3b241ecb75ed 139 pc().printf("depth P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f mm \r\n", depthLoop().getControllerP(), depthLoop().getControllerI(), depthLoop().getControllerD(), depthLoop().getOutputOffset());
danstrider 11:3b241ecb75ed 140 pc().printf("pitch P: %3.2f, I: %3.2f, D %3.2f, offset: %3.1f mm \r\n", pitchLoop().getControllerP(), pitchLoop().getControllerI(), pitchLoop().getControllerD(), pitchLoop().getOutputOffset());
danstrider 10:085ab7328054 141 pc().printf("\n\r");
tnhnrl 17:7c16b5671d0e 142
tnhnrl 17:7c16b5671d0e 143 //set the neutral positions in the FSM
tnhnrl 17:7c16b5671d0e 144 // load neutral motor positions from the neutral.cfg file
tnhnrl 17:7c16b5671d0e 145 configFileIO().loadNeutralPositions();
tnhnrl 17:7c16b5671d0e 146
tnhnrl 17:7c16b5671d0e 147 // in discussion said this was actually in pitch.txt and depth.txt
tnhnrl 17:7c16b5671d0e 148 // fixing on Wednesday (Troy)
tnhnrl 17:7c16b5671d0e 149 pc().printf("\n\rLoading neutral positions from file.\n\r");
tnhnrl 17:7c16b5671d0e 150 stateMachine().setNeutralPositions(configFileIO().getBattPos(), configFileIO().getBCEPos()); //batt, bce
tnhnrl 17:7c16b5671d0e 151
tnhnrl 17:7c16b5671d0e 152 //load sequence from file
tnhnrl 17:7c16b5671d0e 153 sequenceController().loadSequence();
danstrider 10:085ab7328054 154 }
mkelly10 5:15bd96205bb2 155
tnhnrl 17:7c16b5671d0e 156
tnhnrl 17:7c16b5671d0e 157
danstrider 10:085ab7328054 158 int main() {
danstrider 10:085ab7328054 159 setup();
tnhnrl 16:3363b9f14913 160
tnhnrl 17:7c16b5671d0e 161 blocker_ticker.attach(&reset_blocker, 0.01); // 100 Hz loop rate
tnhnrl 17:7c16b5671d0e 162 uint32_t n = 0;
tnhnrl 17:7c16b5671d0e 163
danstrider 10:085ab7328054 164 while(1) {
danstrider 11:3b241ecb75ed 165 led1() = !led1(); // blink
tnhnrl 16:3363b9f14913 166
tnhnrl 17:7c16b5671d0e 167 //"blocker" while loop runs at 100 hz because of the ticker
tnhnrl 17:7c16b5671d0e 168 while(blocker) {
tnhnrl 17:7c16b5671d0e 169 if (keyboard_flag) {
tnhnrl 17:7c16b5671d0e 170 stateMachine().keyboard(); //current concept runs keyboard and state machine in a while loop
tnhnrl 17:7c16b5671d0e 171 stateMachine().runStateMachine();
tnhnrl 17:7c16b5671d0e 172
tnhnrl 17:7c16b5671d0e 173 keyboard_flag = false;
tnhnrl 17:7c16b5671d0e 174 }
tnhnrl 17:7c16b5671d0e 175 }
tnhnrl 17:7c16b5671d0e 176
tnhnrl 17:7c16b5671d0e 177 //this runs at 10 hz
tnhnrl 17:7c16b5671d0e 178 if (n%10 == 0)
tnhnrl 17:7c16b5671d0e 179 {
tnhnrl 17:7c16b5671d0e 180 keyboard_flag = true; //the flag controls the rate at which this operates
tnhnrl 17:7c16b5671d0e 181 }
tnhnrl 17:7c16b5671d0e 182
tnhnrl 17:7c16b5671d0e 183 //keyboard_flag = true; //set the keyboard flag
tnhnrl 17:7c16b5671d0e 184 blocker = true; //reset the blocker while loop when this is completed
danstrider 10:085ab7328054 185 }
danstrider 10:085ab7328054 186 }