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@24:c7d9b5bf3829, 2017-11-29 (annotated)
- Committer:
- tnhnrl
- Date:
- Wed Nov 29 15:19:14 2017 +0000
- Revision:
- 24:c7d9b5bf3829
- Parent:
- 23:434f04ef1fad
- Child:
- 27:0a5b90cd65d6
Xbee / Pool version of code with latest code morning of 11/29/17
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 | 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 |
danstrider | 22:a10ee088403b | 17 | MULTI_DIVE, // multi-dive sequence |
danstrider | 22:a10ee088403b | 18 | MULTI_RISE // 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 { |
danstrider | 22:a10ee088403b | 23 | NEUTRAL_FIRST_PITCH, // NEW first state: find level by moving the battery |
danstrider | 22:a10ee088403b | 24 | NEUTRAL_SINKING, // increment the bce until really start sinking |
danstrider | 22:a10ee088403b | 25 | NEUTRAL_SLOWLY_RISE, // once sinking, arrest the sink |
danstrider | 22:a10ee088403b | 26 | NEUTRAL_CHECK_PITCH, // find level again, then save the data and exit |
danstrider | 22:a10ee088403b | 27 | NEUTRAL_EXIT // sub-FSM has completed all checks |
tnhnrl | 17:7c16b5671d0e | 28 | }; |
tnhnrl | 20:8987a9ae2bc7 | 29 | |
tnhnrl | 17:7c16b5671d0e | 30 | //struct for saving the data |
tnhnrl | 17:7c16b5671d0e | 31 | struct currentSequenceStruct { |
tnhnrl | 17:7c16b5671d0e | 32 | int state; //for the current StateMachine, states are ID-ed with enumeration |
tnhnrl | 17:7c16b5671d0e | 33 | float timeout; |
tnhnrl | 17:7c16b5671d0e | 34 | float depth; |
tnhnrl | 17:7c16b5671d0e | 35 | float pitch; |
tnhnrl | 16:3363b9f14913 | 36 | }; |
tnhnrl | 20:8987a9ae2bc7 | 37 | |
tnhnrl | 16:3363b9f14913 | 38 | class StateMachine { |
tnhnrl | 16:3363b9f14913 | 39 | public: |
tnhnrl | 16:3363b9f14913 | 40 | StateMachine(); |
tnhnrl | 16:3363b9f14913 | 41 | |
tnhnrl | 16:3363b9f14913 | 42 | void runStateMachine(); |
tnhnrl | 16:3363b9f14913 | 43 | |
tnhnrl | 16:3363b9f14913 | 44 | void showMenu(); |
tnhnrl | 16:3363b9f14913 | 45 | |
tnhnrl | 17:7c16b5671d0e | 46 | void keyboard(); |
tnhnrl | 16:3363b9f14913 | 47 | |
tnhnrl | 16:3363b9f14913 | 48 | void keyboard_menu_BCE_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 49 | void keyboard_menu_BATT_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 50 | void keyboard_menu_DEPTH_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 51 | void keyboard_menu_PITCH_PID_settings(); |
tnhnrl | 16:3363b9f14913 | 52 | |
tnhnrl | 16:3363b9f14913 | 53 | float getDepthCommand(); |
tnhnrl | 16:3363b9f14913 | 54 | float getPitchCommand(); |
tnhnrl | 16:3363b9f14913 | 55 | |
tnhnrl | 21:38c8544db6f4 | 56 | int runNeutralStateMachine(); //substate returns the state (which is used in overall FSM) |
tnhnrl | 17:7c16b5671d0e | 57 | |
tnhnrl | 17:7c16b5671d0e | 58 | void setState(int input_state); |
tnhnrl | 17:7c16b5671d0e | 59 | int getState(); |
tnhnrl | 17:7c16b5671d0e | 60 | void setTimeout(float input_timeout); |
tnhnrl | 17:7c16b5671d0e | 61 | void setDepthCommand(float input_depth_command); |
tnhnrl | 17:7c16b5671d0e | 62 | void setPitchCommand(float input_pitch_command); |
tnhnrl | 17:7c16b5671d0e | 63 | |
tnhnrl | 17:7c16b5671d0e | 64 | void setNeutralPositions(float batt_pos_mm, float bce_pos_mm); |
tnhnrl | 17:7c16b5671d0e | 65 | |
tnhnrl | 17:7c16b5671d0e | 66 | int timeoutRunning(); |
tnhnrl | 17:7c16b5671d0e | 67 | |
tnhnrl | 21:38c8544db6f4 | 68 | void getDiveSequence(); //used in multi-dive sequence with public variables for now |
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 | |
danstrider | 22:a10ee088403b | 82 | int _state; // Fine State Machine (FSM) state |
danstrider | 22:a10ee088403b | 83 | int _sub_state; // substate on find_neutral function |
danstrider | 22:a10ee088403b | 84 | 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 | 85 | float _neutral_timer; // keep time for rise/sink/level timer incremnets |
tnhnrl | 17:7c16b5671d0e | 86 | |
tnhnrl | 17:7c16b5671d0e | 87 | bool isTimeoutRunning; |
tnhnrl | 17:7c16b5671d0e | 88 | |
tnhnrl | 20:8987a9ae2bc7 | 89 | bool isSubStateTimerRunning; |
tnhnrl | 17:7c16b5671d0e | 90 | |
tnhnrl | 21:38c8544db6f4 | 91 | float _neutral_bce_pos_mm; |
tnhnrl | 21:38c8544db6f4 | 92 | float _neutral_batt_pos_mm; |
tnhnrl | 17:7c16b5671d0e | 93 | |
tnhnrl | 24:c7d9b5bf3829 | 94 | int _multi_dive_counter; |
tnhnrl | 21:38c8544db6f4 | 95 | currentSequenceStruct currentStateStruct; //type_of_struct struct_name |
tnhnrl | 21:38c8544db6f4 | 96 | |
tnhnrl | 21:38c8544db6f4 | 97 | bool _neutral_sub_state_active; // controls neutral_sub_state |
tnhnrl | 21:38c8544db6f4 | 98 | |
tnhnrl | 21:38c8544db6f4 | 99 | float _depth_KP; |
tnhnrl | 21:38c8544db6f4 | 100 | float _depth_KI; |
tnhnrl | 21:38c8544db6f4 | 101 | float _depth_KD; |
tnhnrl | 21:38c8544db6f4 | 102 | |
tnhnrl | 21:38c8544db6f4 | 103 | float _pitch_KP; |
tnhnrl | 21:38c8544db6f4 | 104 | float _pitch_KI; |
tnhnrl | 21:38c8544db6f4 | 105 | float _pitch_KD; |
tnhnrl | 23:434f04ef1fad | 106 | |
tnhnrl | 24:c7d9b5bf3829 | 107 | int _state_array[1024]; //used to print out the states |
tnhnrl | 24:c7d9b5bf3829 | 108 | int _state_array_counter; //variables used to record NEUTRAL sub-FSM states |
tnhnrl | 24:c7d9b5bf3829 | 109 | int _substate; |
tnhnrl | 24:c7d9b5bf3829 | 110 | int _previous_substate; |
tnhnrl | 16:3363b9f14913 | 111 | }; |
tnhnrl | 20:8987a9ae2bc7 | 112 | |
tnhnrl | 16:3363b9f14913 | 113 | #endif |