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
StateMachine/StateMachine.hpp@80:4e5d306d695b, 2018-10-24 (annotated)
- Committer:
- tnhnrl
- Date:
- Wed Oct 24 16:07:10 2018 +0000
- Revision:
- 80:4e5d306d695b
- Parent:
- 74:d281aaef9766
- Child:
- 82:0981b9ada820
Tilde brings up menu to print log size, and asks whether or not you want to erase the MBED local log.; ; Tested code with SD logger, working on PCB 2.4, commented out.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tnhnrl | 73:f6f378311c8d | 1 | #ifndef STATEMACHINE_HPP |
tnhnrl | 73:f6f378311c8d | 2 | #define STATEMACHINE_HPP |
tnhnrl | 73:f6f378311c8d | 3 | |
tnhnrl | 73:f6f378311c8d | 4 | #include "mbed.h" |
tnhnrl | 73:f6f378311c8d | 5 | #include <vector> |
tnhnrl | 73:f6f378311c8d | 6 | |
tnhnrl | 73:f6f378311c8d | 7 | extern "C" void mbed_reset(); // utilized to reset the mbed |
tnhnrl | 73:f6f378311c8d | 8 | |
tnhnrl | 73:f6f378311c8d | 9 | // main finite state enumerations |
tnhnrl | 73:f6f378311c8d | 10 | enum { |
tnhnrl | 73:f6f378311c8d | 11 | SIT_IDLE, // stops both motors, exits after a keyboard input |
tnhnrl | 73:f6f378311c8d | 12 | CHECK_TUNING, // runs the system to the positions specified in the files |
tnhnrl | 73:f6f378311c8d | 13 | FIND_NEUTRAL, // dives to depth at zero pitch, exits when stable |
tnhnrl | 73:f6f378311c8d | 14 | DIVE, // dives to depth at negative pitch, exits when crossing a defined depth |
tnhnrl | 73:f6f378311c8d | 15 | RISE, // rises to surface at positive pitch, exits when near surface |
tnhnrl | 73:f6f378311c8d | 16 | POSITION_DIVE, // NEW POSITION ONLY COMMANDS (inner loop) |
tnhnrl | 73:f6f378311c8d | 17 | POSITION_RISE, // NEW POSITION ONLY COMMANDS (inner loop) |
tnhnrl | 73:f6f378311c8d | 18 | FLOAT_LEVEL, // bce position to float, pitch loop active at zero, exits when stable near zero pitch |
tnhnrl | 73:f6f378311c8d | 19 | FLOAT_BROADCAST, // bce position to float, batt position forward to hold tail up, exits when actuators done |
tnhnrl | 73:f6f378311c8d | 20 | EMERGENCY_CLIMB, // bce position to full rise, batt position to full aft, exits when at surface |
tnhnrl | 73:f6f378311c8d | 21 | MULTI_DIVE, // multi-dive sequence |
tnhnrl | 73:f6f378311c8d | 22 | MULTI_RISE, // multi-rise sequence |
tnhnrl | 73:f6f378311c8d | 23 | KEYBOARD, // "state" for tracking only |
tnhnrl | 73:f6f378311c8d | 24 | TX_MBED_LOG, |
tnhnrl | 74:d281aaef9766 | 25 | RX_SEQUENCE |
tnhnrl | 73:f6f378311c8d | 26 | }; |
tnhnrl | 73:f6f378311c8d | 27 | |
tnhnrl | 73:f6f378311c8d | 28 | // find_neutral finite state machine enumerations |
tnhnrl | 73:f6f378311c8d | 29 | enum { |
tnhnrl | 73:f6f378311c8d | 30 | NEUTRAL_SINKING, // increment the bce until really start sinking |
tnhnrl | 73:f6f378311c8d | 31 | NEUTRAL_SLOWLY_RISE, // once sinking, arrest the sink |
tnhnrl | 73:f6f378311c8d | 32 | NEUTRAL_CHECK_PITCH, // find level again, then save the data and exit |
tnhnrl | 73:f6f378311c8d | 33 | NEUTRAL_EXIT, // sub-FSM has completed all checks |
tnhnrl | 73:f6f378311c8d | 34 | }; |
tnhnrl | 73:f6f378311c8d | 35 | |
tnhnrl | 73:f6f378311c8d | 36 | // test idea |
tnhnrl | 73:f6f378311c8d | 37 | |
tnhnrl | 73:f6f378311c8d | 38 | //struct for saving the data |
tnhnrl | 73:f6f378311c8d | 39 | struct currentSequenceStruct { |
tnhnrl | 73:f6f378311c8d | 40 | int state; //for the current StateMachine, states are ID-ed with enumeration |
tnhnrl | 73:f6f378311c8d | 41 | float timeout; |
tnhnrl | 73:f6f378311c8d | 42 | float depth; |
tnhnrl | 73:f6f378311c8d | 43 | float pitch; |
tnhnrl | 73:f6f378311c8d | 44 | }; |
tnhnrl | 73:f6f378311c8d | 45 | |
tnhnrl | 73:f6f378311c8d | 46 | class StateMachine { |
tnhnrl | 73:f6f378311c8d | 47 | public: |
tnhnrl | 73:f6f378311c8d | 48 | StateMachine(); |
tnhnrl | 73:f6f378311c8d | 49 | |
tnhnrl | 73:f6f378311c8d | 50 | int runStateMachine(); |
tnhnrl | 73:f6f378311c8d | 51 | |
tnhnrl | 73:f6f378311c8d | 52 | void printSimpleMenu(); // simple menu |
tnhnrl | 73:f6f378311c8d | 53 | void printDebugMenu(); // debug menu |
tnhnrl | 73:f6f378311c8d | 54 | |
tnhnrl | 73:f6f378311c8d | 55 | void keyboard(); |
tnhnrl | 73:f6f378311c8d | 56 | |
tnhnrl | 73:f6f378311c8d | 57 | void keyboard_menu_MANUAL_TUNING(); |
tnhnrl | 73:f6f378311c8d | 58 | void keyboard_menu_STREAM_STATUS(); |
tnhnrl | 73:f6f378311c8d | 59 | void keyboard_menu_CHANNEL_READINGS(); |
tnhnrl | 73:f6f378311c8d | 60 | void keyboard_menu_POSITION_READINGS(); |
tnhnrl | 73:f6f378311c8d | 61 | void keyboard_menu_RUDDER_SERVO_settings(); |
tnhnrl | 73:f6f378311c8d | 62 | void keyboard_menu_HEADING_PID_settings(); |
tnhnrl | 73:f6f378311c8d | 63 | void keyboard_menu_COUNTS_STATUS(); //remove? |
tnhnrl | 73:f6f378311c8d | 64 | |
tnhnrl | 73:f6f378311c8d | 65 | void keyboard_menu_BCE_PID_settings(); |
tnhnrl | 73:f6f378311c8d | 66 | void keyboard_menu_BATT_PID_settings(); |
tnhnrl | 73:f6f378311c8d | 67 | void keyboard_menu_DEPTH_PID_settings(); |
tnhnrl | 73:f6f378311c8d | 68 | void keyboard_menu_PITCH_PID_settings(); |
tnhnrl | 73:f6f378311c8d | 69 | |
tnhnrl | 73:f6f378311c8d | 70 | float getDepthCommand(); |
tnhnrl | 73:f6f378311c8d | 71 | float getPitchCommand(); |
tnhnrl | 73:f6f378311c8d | 72 | float getDepthReading(); |
tnhnrl | 73:f6f378311c8d | 73 | float getPitchReading(); |
tnhnrl | 73:f6f378311c8d | 74 | float getTimerReading(); |
tnhnrl | 73:f6f378311c8d | 75 | |
tnhnrl | 73:f6f378311c8d | 76 | int runNeutralStateMachine(); //substate returns the state (which is used in overall FSM) |
tnhnrl | 73:f6f378311c8d | 77 | |
tnhnrl | 73:f6f378311c8d | 78 | int getState(); |
tnhnrl | 73:f6f378311c8d | 79 | void setState(int input_state); |
tnhnrl | 73:f6f378311c8d | 80 | |
tnhnrl | 73:f6f378311c8d | 81 | void setTimeout(float input_timeout); |
tnhnrl | 73:f6f378311c8d | 82 | void setDepthCommand(float input_depth_command); |
tnhnrl | 73:f6f378311c8d | 83 | void setPitchCommand(float input_pitch_command); |
tnhnrl | 73:f6f378311c8d | 84 | |
tnhnrl | 73:f6f378311c8d | 85 | void setNeutralPositions(float batt_pos_mm, float bce_pos_mm); |
tnhnrl | 73:f6f378311c8d | 86 | |
tnhnrl | 73:f6f378311c8d | 87 | void getDiveSequence(); //used in multi-dive sequence with public variables for now |
tnhnrl | 73:f6f378311c8d | 88 | |
tnhnrl | 73:f6f378311c8d | 89 | void runActiveNeutralStateMachine(); //new neutral substate returns the state (which is used in overall FSM) |
tnhnrl | 73:f6f378311c8d | 90 | |
tnhnrl | 73:f6f378311c8d | 91 | float * getLoggerArray(); //delete soon |
tnhnrl | 73:f6f378311c8d | 92 | |
tnhnrl | 73:f6f378311c8d | 93 | void printDirectory(); |
tnhnrl | 73:f6f378311c8d | 94 | void printCurrentSdLog(); //more tricky for SD card, work in progress |
tnhnrl | 73:f6f378311c8d | 95 | |
tnhnrl | 73:f6f378311c8d | 96 | void createNewFile(); |
tnhnrl | 73:f6f378311c8d | 97 | |
tnhnrl | 73:f6f378311c8d | 98 | void transmitData(); |
tnhnrl | 73:f6f378311c8d | 99 | |
tnhnrl | 73:f6f378311c8d | 100 | float * dataArray(); |
tnhnrl | 73:f6f378311c8d | 101 | |
tnhnrl | 73:f6f378311c8d | 102 | //GUI UPDATE FUNCTIONS |
tnhnrl | 73:f6f378311c8d | 103 | float getTimerValue(); |
tnhnrl | 73:f6f378311c8d | 104 | |
tnhnrl | 80:4e5d306d695b | 105 | void logFileMenu(); //instead of immediately erasing log files, NRL Stennis suggests a confirmation |
tnhnrl | 80:4e5d306d695b | 106 | |
tnhnrl | 73:f6f378311c8d | 107 | private: |
tnhnrl | 73:f6f378311c8d | 108 | bool _debug_menu_on; // default is false to show simple menu, debug allows more tuning and has a lot of keyboard commands |
tnhnrl | 73:f6f378311c8d | 109 | |
tnhnrl | 73:f6f378311c8d | 110 | int _timeout; // generic timeout for every state, seconds |
tnhnrl | 73:f6f378311c8d | 111 | float _pitchTolerance; // pitch angle tolerance for neutral finding exit criteria |
tnhnrl | 73:f6f378311c8d | 112 | float _bceFloatPosition; // bce position for "float" states |
tnhnrl | 73:f6f378311c8d | 113 | float _battFloatPosition; // batt position for "broadcast" state |
tnhnrl | 73:f6f378311c8d | 114 | |
tnhnrl | 73:f6f378311c8d | 115 | float _depth_command; // user keyboard depth |
tnhnrl | 73:f6f378311c8d | 116 | float _pitch_command; // user keyboard pitch |
tnhnrl | 73:f6f378311c8d | 117 | float _heading_command; // user keyboard heading |
tnhnrl | 73:f6f378311c8d | 118 | |
tnhnrl | 73:f6f378311c8d | 119 | float _depth_reading; // depth reading (to get the readings at the same time) |
tnhnrl | 73:f6f378311c8d | 120 | float _pitch_reading; // pitch reading (to get the readings at the same time) |
tnhnrl | 73:f6f378311c8d | 121 | float _timer_reading; // pitch reading (to get the readings at the same time) |
tnhnrl | 73:f6f378311c8d | 122 | |
tnhnrl | 73:f6f378311c8d | 123 | Timer _fsm_timer; //timing variable used in class |
tnhnrl | 73:f6f378311c8d | 124 | |
tnhnrl | 73:f6f378311c8d | 125 | volatile int _state; // current state of Finite State Machine (FSM) |
tnhnrl | 73:f6f378311c8d | 126 | int _previous_state; // record previous state |
tnhnrl | 73:f6f378311c8d | 127 | int _sub_state; // substate on find_neutral function |
tnhnrl | 73:f6f378311c8d | 128 | int _previous_sub_state; // previous substate so that what goes into the sub-state is not being changed as it is processed |
tnhnrl | 73:f6f378311c8d | 129 | float _neutral_timer; // keep time for rise/sink/level timer incremnets |
tnhnrl | 73:f6f378311c8d | 130 | |
tnhnrl | 73:f6f378311c8d | 131 | bool _isTimeoutRunning; |
tnhnrl | 73:f6f378311c8d | 132 | |
tnhnrl | 73:f6f378311c8d | 133 | bool _isSubStateTimerRunning; |
tnhnrl | 73:f6f378311c8d | 134 | |
tnhnrl | 73:f6f378311c8d | 135 | float _neutral_bce_pos_mm; |
tnhnrl | 73:f6f378311c8d | 136 | float _neutral_batt_pos_mm; |
tnhnrl | 73:f6f378311c8d | 137 | |
tnhnrl | 73:f6f378311c8d | 138 | int _multi_dive_counter; |
tnhnrl | 73:f6f378311c8d | 139 | |
tnhnrl | 73:f6f378311c8d | 140 | currentSequenceStruct currentStateStruct; //type_of_struct struct_name |
tnhnrl | 73:f6f378311c8d | 141 | |
tnhnrl | 73:f6f378311c8d | 142 | float _depth_KP; |
tnhnrl | 73:f6f378311c8d | 143 | float _depth_KI; |
tnhnrl | 73:f6f378311c8d | 144 | float _depth_KD; |
tnhnrl | 73:f6f378311c8d | 145 | |
tnhnrl | 73:f6f378311c8d | 146 | float _pitch_KP; |
tnhnrl | 73:f6f378311c8d | 147 | float _pitch_KI; |
tnhnrl | 73:f6f378311c8d | 148 | float _pitch_KD; |
tnhnrl | 73:f6f378311c8d | 149 | |
tnhnrl | 73:f6f378311c8d | 150 | int _state_array[256]; //used to print out the states |
tnhnrl | 73:f6f378311c8d | 151 | int _state_array_counter; //used to iterate through state records |
tnhnrl | 73:f6f378311c8d | 152 | int _substate_array[256]; //used to print out the sub-states |
tnhnrl | 73:f6f378311c8d | 153 | int _substate_array_counter; //used to iterate through sub-state records |
tnhnrl | 73:f6f378311c8d | 154 | |
tnhnrl | 73:f6f378311c8d | 155 | int _substate; |
tnhnrl | 73:f6f378311c8d | 156 | int _previous_substate; |
tnhnrl | 73:f6f378311c8d | 157 | |
tnhnrl | 73:f6f378311c8d | 158 | float _max_recorded_depth_neutral; |
tnhnrl | 73:f6f378311c8d | 159 | float _max_recorded_depth_dive; |
tnhnrl | 73:f6f378311c8d | 160 | |
tnhnrl | 73:f6f378311c8d | 161 | float _neutral_sink_command_mm; //defaults for neutral finding sub-FSM |
tnhnrl | 73:f6f378311c8d | 162 | float _neutral_rise_command_mm; |
tnhnrl | 73:f6f378311c8d | 163 | float _neutral_pitch_command_mm; |
tnhnrl | 73:f6f378311c8d | 164 | |
tnhnrl | 73:f6f378311c8d | 165 | float _max_recorded_auto_neutral_depth; |
tnhnrl | 73:f6f378311c8d | 166 | |
tnhnrl | 73:f6f378311c8d | 167 | bool _is_log_timer_running; |
tnhnrl | 73:f6f378311c8d | 168 | float _log_timer; |
tnhnrl | 73:f6f378311c8d | 169 | |
tnhnrl | 73:f6f378311c8d | 170 | float _BCE_dive_offset; // NEW COMMANDS FOR POSITION CONTROLLER |
tnhnrl | 73:f6f378311c8d | 171 | float _BMM_dive_offset; |
tnhnrl | 73:f6f378311c8d | 172 | |
tnhnrl | 73:f6f378311c8d | 173 | float getFloatUserInput(); |
tnhnrl | 73:f6f378311c8d | 174 | |
tnhnrl | 73:f6f378311c8d | 175 | //new |
tnhnrl | 73:f6f378311c8d | 176 | float _batt_filter_freq; |
tnhnrl | 73:f6f378311c8d | 177 | float _bce_filter_freq; |
tnhnrl | 73:f6f378311c8d | 178 | float _pitch_filter_freq; |
tnhnrl | 73:f6f378311c8d | 179 | float _depth_filter_freq; |
tnhnrl | 73:f6f378311c8d | 180 | float _heading_filter_freq; |
tnhnrl | 73:f6f378311c8d | 181 | |
tnhnrl | 73:f6f378311c8d | 182 | float _batt_deadband; |
tnhnrl | 73:f6f378311c8d | 183 | float _bce_deadband; |
tnhnrl | 73:f6f378311c8d | 184 | float _pitch_deadband; |
tnhnrl | 73:f6f378311c8d | 185 | float _depth_deadband; |
tnhnrl | 73:f6f378311c8d | 186 | float _heading_deadband; |
tnhnrl | 73:f6f378311c8d | 187 | }; |
tnhnrl | 73:f6f378311c8d | 188 | |
tnhnrl | 73:f6f378311c8d | 189 | #endif |