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@45:16b8162188ca, 2018-02-15 (annotated)
- Committer:
- tnhnrl
- Date:
- Thu Feb 15 02:39:13 2018 +0000
- Revision:
- 45:16b8162188ca
- Parent:
- 38:83d06c294807
- Child:
- 49:47ffa4feb6db
version for USB testing with reduced class sizes
Who changed what in which revision?
User | Revision | Line number | New 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 | 32:f2f8ae34aadc | 5 | #include <vector> |
tnhnrl | 20:8987a9ae2bc7 | 6 | |
tnhnrl | 16:3363b9f14913 | 7 | extern "C" void mbed_reset(); // utilized to reset the mbed |
tnhnrl | 20:8987a9ae2bc7 | 8 | |
tnhnrl | 20:8987a9ae2bc7 | 9 | // main finite state enumerations |
tnhnrl | 16:3363b9f14913 | 10 | enum { |
tnhnrl | 17:7c16b5671d0e | 11 | SIT_IDLE, // stops both motors, exits after a keyboard input |
tnhnrl | 17:7c16b5671d0e | 12 | FIND_NEUTRAL, // dives to depth at zero pitch, exits when stable |
tnhnrl | 17:7c16b5671d0e | 13 | DIVE, // dives to depth at negative pitch, exits when crossing a defined depth |
tnhnrl | 17:7c16b5671d0e | 14 | RISE, // rises to surface at positive pitch, exits when near surface |
tnhnrl | 17:7c16b5671d0e | 15 | FLOAT_LEVEL, // bce position to float, pitch loop active at zero, exits when stable near zero pitch |
tnhnrl | 17:7c16b5671d0e | 16 | FLOAT_BROADCAST, // bce position to float, batt position forward to hold tail up, exits when actuators done |
tnhnrl | 17:7c16b5671d0e | 17 | EMERGENCY_CLIMB, // bce position to full rise, batt position to full aft, exits when at surface |
danstrider | 22:a10ee088403b | 18 | MULTI_DIVE, // multi-dive sequence |
tnhnrl | 28:16c83a2fdefa | 19 | MULTI_RISE, // multi-rise sequence |
tnhnrl | 32:f2f8ae34aadc | 20 | KEYBOARD, // "state" for tracking only |
tnhnrl | 45:16b8162188ca | 21 | TRANSMIT_LOG, |
tnhnrl | 45:16b8162188ca | 22 | RECEIVE_SEQUENCE |
tnhnrl | 20:8987a9ae2bc7 | 23 | }; |
tnhnrl | 20:8987a9ae2bc7 | 24 | |
tnhnrl | 20:8987a9ae2bc7 | 25 | // find_neutral finite state machine enumerations |
tnhnrl | 20:8987a9ae2bc7 | 26 | enum { |
danstrider | 22:a10ee088403b | 27 | NEUTRAL_SINKING, // increment the bce until really start sinking |
danstrider | 22:a10ee088403b | 28 | NEUTRAL_SLOWLY_RISE, // once sinking, arrest the sink |
danstrider | 22:a10ee088403b | 29 | NEUTRAL_CHECK_PITCH, // find level again, then save the data and exit |
tnhnrl | 32:f2f8ae34aadc | 30 | NEUTRAL_EXIT, // sub-FSM has completed all checks |
tnhnrl | 17:7c16b5671d0e | 31 | }; |
tnhnrl | 32:f2f8ae34aadc | 32 | |
tnhnrl | 32:f2f8ae34aadc | 33 | // test idea |
tnhnrl | 20:8987a9ae2bc7 | 34 | |
tnhnrl | 17:7c16b5671d0e | 35 | //struct for saving the data |
tnhnrl | 17:7c16b5671d0e | 36 | struct currentSequenceStruct { |
tnhnrl | 17:7c16b5671d0e | 37 | int state; //for the current StateMachine, states are ID-ed with enumeration |
tnhnrl | 17:7c16b5671d0e | 38 | float timeout; |
tnhnrl | 17:7c16b5671d0e | 39 | float depth; |
tnhnrl | 17:7c16b5671d0e | 40 | float pitch; |
tnhnrl | 16:3363b9f14913 | 41 | }; |
tnhnrl | 20:8987a9ae2bc7 | 42 | |
tnhnrl | 16:3363b9f14913 | 43 | class StateMachine { |
tnhnrl | 16:3363b9f14913 | 44 | public: |
tnhnrl | 16:3363b9f14913 | 45 | StateMachine(); |
tnhnrl | 16:3363b9f14913 | 46 | |
tnhnrl | 34:9b66c5188051 | 47 | int runStateMachine(); |
tnhnrl | 16:3363b9f14913 | 48 | |
tnhnrl | 16:3363b9f14913 | 49 | void showMenu(); |
tnhnrl | 16:3363b9f14913 | 50 | |
tnhnrl | 17:7c16b5671d0e | 51 | void keyboard(); |
tnhnrl | 16:3363b9f14913 | 52 | |
tnhnrl | 16:3363b9f14913 | 53 | void keyboard_menu_BCE_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 54 | void keyboard_menu_BATT_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 55 | void keyboard_menu_DEPTH_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 56 | void keyboard_menu_PITCH_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 57 | |
tnhnrl | 16:3363b9f14913 | 58 | float getDepthCommand(); |
tnhnrl | 16:3363b9f14913 | 59 | float getPitchCommand(); |
tnhnrl | 32:f2f8ae34aadc | 60 | float getDepthReading(); |
tnhnrl | 32:f2f8ae34aadc | 61 | float getPitchReading(); |
tnhnrl | 32:f2f8ae34aadc | 62 | float getTimerReading(); |
tnhnrl | 16:3363b9f14913 | 63 | |
tnhnrl | 32:f2f8ae34aadc | 64 | int runNeutralStateMachine(); //substate returns the state (which is used in overall FSM) |
tnhnrl | 17:7c16b5671d0e | 65 | |
tnhnrl | 28:16c83a2fdefa | 66 | int getState(); |
tnhnrl | 17:7c16b5671d0e | 67 | void setState(int input_state); |
tnhnrl | 28:16c83a2fdefa | 68 | |
tnhnrl | 17:7c16b5671d0e | 69 | void setTimeout(float input_timeout); |
tnhnrl | 17:7c16b5671d0e | 70 | void setDepthCommand(float input_depth_command); |
tnhnrl | 17:7c16b5671d0e | 71 | void setPitchCommand(float input_pitch_command); |
tnhnrl | 17:7c16b5671d0e | 72 | |
tnhnrl | 17:7c16b5671d0e | 73 | void setNeutralPositions(float batt_pos_mm, float bce_pos_mm); |
tnhnrl | 17:7c16b5671d0e | 74 | |
tnhnrl | 17:7c16b5671d0e | 75 | int timeoutRunning(); |
tnhnrl | 17:7c16b5671d0e | 76 | |
tnhnrl | 21:38c8544db6f4 | 77 | void getDiveSequence(); //used in multi-dive sequence with public variables for now |
tnhnrl | 17:7c16b5671d0e | 78 | |
tnhnrl | 32:f2f8ae34aadc | 79 | void runActiveNeutralStateMachine(); //new neutral substate returns the state (which is used in overall FSM) |
tnhnrl | 32:f2f8ae34aadc | 80 | |
tnhnrl | 34:9b66c5188051 | 81 | float * getLoggerArray(); //delete soon |
tnhnrl | 32:f2f8ae34aadc | 82 | |
tnhnrl | 34:9b66c5188051 | 83 | void recordData(int input_state); //record log data to file using MbedLogger class |
tnhnrl | 32:f2f8ae34aadc | 84 | void printDirectory(); |
tnhnrl | 32:f2f8ae34aadc | 85 | void recordState(int input_state); |
tnhnrl | 34:9b66c5188051 | 86 | void printCurrentSdLog(); //more tricky for SD card, work in progress |
tnhnrl | 32:f2f8ae34aadc | 87 | |
tnhnrl | 32:f2f8ae34aadc | 88 | void createNewFile(); |
tnhnrl | 32:f2f8ae34aadc | 89 | |
tnhnrl | 32:f2f8ae34aadc | 90 | void transmitData(); |
tnhnrl | 32:f2f8ae34aadc | 91 | |
tnhnrl | 36:966a86937e17 | 92 | float * dataArray(); |
tnhnrl | 36:966a86937e17 | 93 | |
tnhnrl | 16:3363b9f14913 | 94 | private: |
tnhnrl | 17:7c16b5671d0e | 95 | int _timeout; // generic timeout for every state, seconds |
tnhnrl | 28:16c83a2fdefa | 96 | float _pitchTolerance; // pitch angle tolerance for neutral finding exit criteria |
tnhnrl | 28:16c83a2fdefa | 97 | float _bceFloatPosition; // bce position for "float" states |
tnhnrl | 28:16c83a2fdefa | 98 | float _battFloatPosition; // batt position for "broadcast" state |
tnhnrl | 16:3363b9f14913 | 99 | |
tnhnrl | 32:f2f8ae34aadc | 100 | float _depth_command; // user keyboard depth |
tnhnrl | 32:f2f8ae34aadc | 101 | float _pitch_command; // user keyboard depth |
tnhnrl | 32:f2f8ae34aadc | 102 | float _depth_reading; // depth reading (to get the readings at the same time) |
tnhnrl | 32:f2f8ae34aadc | 103 | float _pitch_reading; // pitch reading (to get the readings at the same time) |
tnhnrl | 32:f2f8ae34aadc | 104 | float _timer_reading; // pitch reading (to get the readings at the same time) |
tnhnrl | 16:3363b9f14913 | 105 | |
tnhnrl | 16:3363b9f14913 | 106 | Timer timer; |
tnhnrl | 17:7c16b5671d0e | 107 | |
danstrider | 22:a10ee088403b | 108 | int _state; // Fine State Machine (FSM) state |
tnhnrl | 28:16c83a2fdefa | 109 | int _previous_state; // record previous state |
danstrider | 22:a10ee088403b | 110 | int _sub_state; // substate on find_neutral function |
danstrider | 22:a10ee088403b | 111 | int _previous_sub_state; // previous substate so that what goes into the sub-state is not being changed as it is processed |
tnhnrl | 24:c7d9b5bf3829 | 112 | float _neutral_timer; // keep time for rise/sink/level timer incremnets |
tnhnrl | 17:7c16b5671d0e | 113 | |
tnhnrl | 28:16c83a2fdefa | 114 | bool _isTimeoutRunning; |
tnhnrl | 17:7c16b5671d0e | 115 | |
tnhnrl | 28:16c83a2fdefa | 116 | bool _isSubStateTimerRunning; |
tnhnrl | 17:7c16b5671d0e | 117 | |
tnhnrl | 21:38c8544db6f4 | 118 | float _neutral_bce_pos_mm; |
tnhnrl | 21:38c8544db6f4 | 119 | float _neutral_batt_pos_mm; |
tnhnrl | 17:7c16b5671d0e | 120 | |
tnhnrl | 24:c7d9b5bf3829 | 121 | int _multi_dive_counter; |
tnhnrl | 28:16c83a2fdefa | 122 | |
tnhnrl | 21:38c8544db6f4 | 123 | currentSequenceStruct currentStateStruct; //type_of_struct struct_name |
tnhnrl | 21:38c8544db6f4 | 124 | |
tnhnrl | 21:38c8544db6f4 | 125 | bool _neutral_sub_state_active; // controls neutral_sub_state |
tnhnrl | 21:38c8544db6f4 | 126 | |
tnhnrl | 21:38c8544db6f4 | 127 | float _depth_KP; |
tnhnrl | 21:38c8544db6f4 | 128 | float _depth_KI; |
tnhnrl | 21:38c8544db6f4 | 129 | float _depth_KD; |
tnhnrl | 21:38c8544db6f4 | 130 | |
tnhnrl | 21:38c8544db6f4 | 131 | float _pitch_KP; |
tnhnrl | 21:38c8544db6f4 | 132 | float _pitch_KI; |
tnhnrl | 21:38c8544db6f4 | 133 | float _pitch_KD; |
tnhnrl | 28:16c83a2fdefa | 134 | |
tnhnrl | 45:16b8162188ca | 135 | int _state_array[256]; //used to print out the states |
tnhnrl | 28:16c83a2fdefa | 136 | int _state_array_counter; //used to iterate through state records |
tnhnrl | 45:16b8162188ca | 137 | int _substate_array[256]; //used to print out the sub-states |
tnhnrl | 28:16c83a2fdefa | 138 | int _substate_array_counter; //used to iterate through sub-state records |
tnhnrl | 23:434f04ef1fad | 139 | |
tnhnrl | 24:c7d9b5bf3829 | 140 | int _substate; |
tnhnrl | 24:c7d9b5bf3829 | 141 | int _previous_substate; |
tnhnrl | 28:16c83a2fdefa | 142 | |
tnhnrl | 28:16c83a2fdefa | 143 | float _max_recorded_depth_neutral; |
tnhnrl | 28:16c83a2fdefa | 144 | float _max_recorded_depth_dive; |
tnhnrl | 32:f2f8ae34aadc | 145 | |
tnhnrl | 32:f2f8ae34aadc | 146 | float _neutral_sink_command_mm; //defaults for neutral finding sub-FSM |
tnhnrl | 32:f2f8ae34aadc | 147 | float _neutral_rise_command_mm; |
tnhnrl | 32:f2f8ae34aadc | 148 | float _neutral_pitch_command_mm; |
tnhnrl | 32:f2f8ae34aadc | 149 | |
tnhnrl | 32:f2f8ae34aadc | 150 | float _max_recorded_auto_neutral_depth; |
tnhnrl | 32:f2f8ae34aadc | 151 | |
tnhnrl | 32:f2f8ae34aadc | 152 | //float _logger_array[1048][5]; //array to hold the input, one log per dive (row/col) |
tnhnrl | 32:f2f8ae34aadc | 153 | bool _is_log_timer_running; |
tnhnrl | 32:f2f8ae34aadc | 154 | float _log_timer; |
tnhnrl | 32:f2f8ae34aadc | 155 | |
tnhnrl | 32:f2f8ae34aadc | 156 | float _data_log[9]; |
tnhnrl | 32:f2f8ae34aadc | 157 | |
tnhnrl | 32:f2f8ae34aadc | 158 | volatile bool _file_closed; |
tnhnrl | 16:3363b9f14913 | 159 | }; |
tnhnrl | 20:8987a9ae2bc7 | 160 | |
tnhnrl | 16:3363b9f14913 | 161 | #endif |