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

Committer:
tnhnrl
Date:
Wed Nov 22 14:32:06 2017 +0000
Revision:
20:8987a9ae2bc7
Parent:
17:7c16b5671d0e
Child:
21:38c8544db6f4
Re-Publish of Dan's mods

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tnhnrl 16:3363b9f14913 1 #ifndef STATEMACHINE_HPP
tnhnrl 16:3363b9f14913 2 #define STATEMACHINE_HPP
tnhnrl 20:8987a9ae2bc7 3
tnhnrl 16:3363b9f14913 4 #include "mbed.h"
tnhnrl 20:8987a9ae2bc7 5
tnhnrl 16:3363b9f14913 6 extern "C" void mbed_reset(); // utilized to reset the mbed
tnhnrl 20:8987a9ae2bc7 7
tnhnrl 20:8987a9ae2bc7 8 // main finite state enumerations
tnhnrl 16:3363b9f14913 9 enum {
tnhnrl 17:7c16b5671d0e 10 SIT_IDLE, // stops both motors, exits after a keyboard input
tnhnrl 17:7c16b5671d0e 11 FIND_NEUTRAL, // dives to depth at zero pitch, exits when stable
tnhnrl 17:7c16b5671d0e 12 DIVE, // dives to depth at negative pitch, exits when crossing a defined depth
tnhnrl 17:7c16b5671d0e 13 RISE, // rises to surface at positive pitch, exits when near surface
tnhnrl 17:7c16b5671d0e 14 FLOAT_LEVEL, // bce position to float, pitch loop active at zero, exits when stable near zero pitch
tnhnrl 17:7c16b5671d0e 15 FLOAT_BROADCAST, // bce position to float, batt position forward to hold tail up, exits when actuators done
tnhnrl 17:7c16b5671d0e 16 EMERGENCY_CLIMB, // bce position to full rise, batt position to full aft, exits when at surface
tnhnrl 20:8987a9ae2bc7 17 MULTI_DIVE, // NEW: multi-dive sequence
tnhnrl 20:8987a9ae2bc7 18 MULTI_RISE // NEW: multi-rise sequence
tnhnrl 20:8987a9ae2bc7 19 };
tnhnrl 20:8987a9ae2bc7 20
tnhnrl 20:8987a9ae2bc7 21 // find_neutral finite state machine enumerations
tnhnrl 20:8987a9ae2bc7 22 enum {
tnhnrl 17:7c16b5671d0e 23 NEUTRAL_SINKING, // NEW: FIND_NEUTRAL immediately goes into "sinking"
tnhnrl 17:7c16b5671d0e 24 NEUTRAL_SLOWLY_RISE, // NEW: Once sinking times out, goes into "check pitch"
tnhnrl 17:7c16b5671d0e 25 NEUTRAL_CHECK_PITCH, // NEW: Once pitch rate confirmed, saves the data and exits to RISE state
tnhnrl 20:8987a9ae2bc7 26 NEUTRAL_EXIT // NEW: sub-FSM has completed all checks
tnhnrl 17:7c16b5671d0e 27 };
tnhnrl 20:8987a9ae2bc7 28
tnhnrl 17:7c16b5671d0e 29 //struct for saving the data
tnhnrl 17:7c16b5671d0e 30 struct currentSequenceStruct {
tnhnrl 17:7c16b5671d0e 31 int state; //for the current StateMachine, states are ID-ed with enumeration
tnhnrl 17:7c16b5671d0e 32 float timeout;
tnhnrl 17:7c16b5671d0e 33 float depth;
tnhnrl 17:7c16b5671d0e 34 float pitch;
tnhnrl 16:3363b9f14913 35 };
tnhnrl 20:8987a9ae2bc7 36
tnhnrl 16:3363b9f14913 37 class StateMachine {
tnhnrl 16:3363b9f14913 38 public:
tnhnrl 16:3363b9f14913 39 StateMachine();
tnhnrl 16:3363b9f14913 40
tnhnrl 16:3363b9f14913 41 void runStateMachine();
tnhnrl 16:3363b9f14913 42
tnhnrl 16:3363b9f14913 43 void showMenu();
tnhnrl 16:3363b9f14913 44
tnhnrl 17:7c16b5671d0e 45 void keyboard();
tnhnrl 16:3363b9f14913 46
tnhnrl 16:3363b9f14913 47 void keyboard_menu_BCE_PID_settings();
tnhnrl 16:3363b9f14913 48 void keyboard_menu_BATT_PID_settings();
tnhnrl 16:3363b9f14913 49 void keyboard_menu_DEPTH_PID_settings();
tnhnrl 16:3363b9f14913 50 void keyboard_menu_PITCH_PID_settings();
tnhnrl 16:3363b9f14913 51
tnhnrl 16:3363b9f14913 52 float getDepthCommand();
tnhnrl 16:3363b9f14913 53 float getPitchCommand();
tnhnrl 16:3363b9f14913 54
tnhnrl 17:7c16b5671d0e 55 int findNeutralSubState(int input_state); //substate returns the state (which is used in overall FSM)
tnhnrl 17:7c16b5671d0e 56
tnhnrl 17:7c16b5671d0e 57 void setState(int input_state);
tnhnrl 17:7c16b5671d0e 58 int getState();
tnhnrl 17:7c16b5671d0e 59 void setTimeout(float input_timeout);
tnhnrl 17:7c16b5671d0e 60 void setDepthCommand(float input_depth_command);
tnhnrl 17:7c16b5671d0e 61 void setPitchCommand(float input_pitch_command);
tnhnrl 17:7c16b5671d0e 62
tnhnrl 17:7c16b5671d0e 63 void setNeutralPositions(float batt_pos_mm, float bce_pos_mm);
tnhnrl 17:7c16b5671d0e 64
tnhnrl 17:7c16b5671d0e 65 int timeoutRunning();
tnhnrl 17:7c16b5671d0e 66
tnhnrl 17:7c16b5671d0e 67 //new (quick and dirty version using public variables)
tnhnrl 17:7c16b5671d0e 68 void getDiveSequence();
tnhnrl 17:7c16b5671d0e 69
tnhnrl 16:3363b9f14913 70 private:
tnhnrl 17:7c16b5671d0e 71 int _timeout; // generic timeout for every state, seconds
tnhnrl 16:3363b9f14913 72 float depthTolerance; // depth tolerance for neutral finding exit critera
tnhnrl 16:3363b9f14913 73 float pitchTolerance; // pitch angle tolerance for neutral finding exit criteria
tnhnrl 16:3363b9f14913 74 float bceFloatPosition; // bce position for "float" states
tnhnrl 16:3363b9f14913 75 float battFloatPosition; // batt position for "broadcast" state
tnhnrl 16:3363b9f14913 76
tnhnrl 16:3363b9f14913 77 float depthCommand; // user keyboard depth
tnhnrl 16:3363b9f14913 78 float pitchCommand; // user keyboard depth
tnhnrl 16:3363b9f14913 79
tnhnrl 16:3363b9f14913 80 Timer timer;
tnhnrl 17:7c16b5671d0e 81
tnhnrl 17:7c16b5671d0e 82 int _state; //Fine State Machine (FSM) state
tnhnrl 17:7c16b5671d0e 83 int _sub_state; //substate on find_neutral function
tnhnrl 17:7c16b5671d0e 84 int _previous_sub_state; //previous substate so that what goes into the sub-state is not being changed as it is processed
tnhnrl 17:7c16b5671d0e 85 float _neutral_sink_timer;
tnhnrl 17:7c16b5671d0e 86 float _neutral_rise_timer;
tnhnrl 17:7c16b5671d0e 87
tnhnrl 17:7c16b5671d0e 88 bool isTimeoutRunning;
tnhnrl 17:7c16b5671d0e 89
tnhnrl 17:7c16b5671d0e 90 float previousPosition_mm;
tnhnrl 20:8987a9ae2bc7 91 bool isSubStateTimerRunning;
tnhnrl 17:7c16b5671d0e 92
tnhnrl 17:7c16b5671d0e 93 float _neutral_buoyancy_bce_pos_mm;
tnhnrl 17:7c16b5671d0e 94 float _neutral_buoyancy_batt_pos_mm;
tnhnrl 17:7c16b5671d0e 95
tnhnrl 17:7c16b5671d0e 96 int _keyboard_state;
tnhnrl 17:7c16b5671d0e 97 int _next_state; //next state is used to prevent states from changing as the FSM executes
tnhnrl 17:7c16b5671d0e 98
tnhnrl 17:7c16b5671d0e 99 int _state_counter;
tnhnrl 17:7c16b5671d0e 100 currentSequenceStruct currentStateStruct; //type_of_struct struct_name
tnhnrl 16:3363b9f14913 101 };
tnhnrl 20:8987a9ae2bc7 102
tnhnrl 16:3363b9f14913 103 #endif