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:
joel_ssc
Date:
Fri Feb 15 16:00:17 2019 +0000
Revision:
82:0981b9ada820
Parent:
80:4e5d306d695b
Child:
84:eccd8e837134
intermediate stage of file leg system

Who changed what in which revision?

UserRevisionLine numberNew 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
joel_ssc 82:0981b9ada820 24 LEG_POSITION_DIVE, // leg has heading and min,max depths and timeout
joel_ssc 82:0981b9ada820 25 LEG_POSITION_RISE,
tnhnrl 73:f6f378311c8d 26 TX_MBED_LOG,
joel_ssc 82:0981b9ada820 27 RX_SEQUENCE,
joel_ssc 82:0981b9ada820 28 FB_EXIT
tnhnrl 73:f6f378311c8d 29 };
tnhnrl 73:f6f378311c8d 30
tnhnrl 73:f6f378311c8d 31 // find_neutral finite state machine enumerations
tnhnrl 73:f6f378311c8d 32 enum {
tnhnrl 73:f6f378311c8d 33 NEUTRAL_SINKING, // increment the bce until really start sinking
tnhnrl 73:f6f378311c8d 34 NEUTRAL_SLOWLY_RISE, // once sinking, arrest the sink
tnhnrl 73:f6f378311c8d 35 NEUTRAL_CHECK_PITCH, // find level again, then save the data and exit
tnhnrl 73:f6f378311c8d 36 NEUTRAL_EXIT, // sub-FSM has completed all checks
tnhnrl 73:f6f378311c8d 37 };
tnhnrl 73:f6f378311c8d 38
tnhnrl 73:f6f378311c8d 39 // test idea
tnhnrl 73:f6f378311c8d 40
tnhnrl 73:f6f378311c8d 41 //struct for saving the data
tnhnrl 73:f6f378311c8d 42 struct currentSequenceStruct {
tnhnrl 73:f6f378311c8d 43 int state; //for the current StateMachine, states are ID-ed with enumeration
tnhnrl 73:f6f378311c8d 44 float timeout;
tnhnrl 73:f6f378311c8d 45 float depth;
tnhnrl 73:f6f378311c8d 46 float pitch;
tnhnrl 73:f6f378311c8d 47 };
joel_ssc 82:0981b9ada820 48 //new struct for saving the leg data
joel_ssc 82:0981b9ada820 49 struct currentLegStruct {
joel_ssc 82:0981b9ada820 50 int state; //for the current StateMachine, states are ID-ed with enumeration
joel_ssc 82:0981b9ada820 51 float timeout;
joel_ssc 82:0981b9ada820 52 float yo_time;
joel_ssc 82:0981b9ada820 53 float min_depth;
joel_ssc 82:0981b9ada820 54 float max_depth;
joel_ssc 82:0981b9ada820 55 float heading;
joel_ssc 82:0981b9ada820 56 };
tnhnrl 73:f6f378311c8d 57
tnhnrl 73:f6f378311c8d 58 class StateMachine {
tnhnrl 73:f6f378311c8d 59 public:
tnhnrl 73:f6f378311c8d 60 StateMachine();
tnhnrl 73:f6f378311c8d 61
tnhnrl 73:f6f378311c8d 62 int runStateMachine();
tnhnrl 73:f6f378311c8d 63
tnhnrl 73:f6f378311c8d 64 void printSimpleMenu(); // simple menu
tnhnrl 73:f6f378311c8d 65 void printDebugMenu(); // debug menu
tnhnrl 73:f6f378311c8d 66
tnhnrl 73:f6f378311c8d 67 void keyboard();
tnhnrl 73:f6f378311c8d 68
tnhnrl 73:f6f378311c8d 69 void keyboard_menu_MANUAL_TUNING();
tnhnrl 73:f6f378311c8d 70 void keyboard_menu_STREAM_STATUS();
tnhnrl 73:f6f378311c8d 71 void keyboard_menu_CHANNEL_READINGS();
tnhnrl 73:f6f378311c8d 72 void keyboard_menu_POSITION_READINGS();
tnhnrl 73:f6f378311c8d 73 void keyboard_menu_RUDDER_SERVO_settings();
tnhnrl 73:f6f378311c8d 74 void keyboard_menu_HEADING_PID_settings();
tnhnrl 73:f6f378311c8d 75 void keyboard_menu_COUNTS_STATUS(); //remove?
tnhnrl 73:f6f378311c8d 76
tnhnrl 73:f6f378311c8d 77 void keyboard_menu_BCE_PID_settings();
tnhnrl 73:f6f378311c8d 78 void keyboard_menu_BATT_PID_settings();
tnhnrl 73:f6f378311c8d 79 void keyboard_menu_DEPTH_PID_settings();
tnhnrl 73:f6f378311c8d 80 void keyboard_menu_PITCH_PID_settings();
tnhnrl 73:f6f378311c8d 81
tnhnrl 73:f6f378311c8d 82 float getDepthCommand();
tnhnrl 73:f6f378311c8d 83 float getPitchCommand();
tnhnrl 73:f6f378311c8d 84 float getDepthReading();
tnhnrl 73:f6f378311c8d 85 float getPitchReading();
tnhnrl 73:f6f378311c8d 86 float getTimerReading();
tnhnrl 73:f6f378311c8d 87
tnhnrl 73:f6f378311c8d 88 int runNeutralStateMachine(); //substate returns the state (which is used in overall FSM)
tnhnrl 73:f6f378311c8d 89
tnhnrl 73:f6f378311c8d 90 int getState();
tnhnrl 73:f6f378311c8d 91 void setState(int input_state);
tnhnrl 73:f6f378311c8d 92
tnhnrl 73:f6f378311c8d 93 void setTimeout(float input_timeout);
tnhnrl 73:f6f378311c8d 94 void setDepthCommand(float input_depth_command);
tnhnrl 73:f6f378311c8d 95 void setPitchCommand(float input_pitch_command);
tnhnrl 73:f6f378311c8d 96
tnhnrl 73:f6f378311c8d 97 void setNeutralPositions(float batt_pos_mm, float bce_pos_mm);
tnhnrl 73:f6f378311c8d 98
tnhnrl 73:f6f378311c8d 99 void getDiveSequence(); //used in multi-dive sequence with public variables for now
joel_ssc 82:0981b9ada820 100 void getLegParams(); //used in multi-leg sequence with public variables for now
tnhnrl 73:f6f378311c8d 101
tnhnrl 73:f6f378311c8d 102 void runActiveNeutralStateMachine(); //new neutral substate returns the state (which is used in overall FSM)
tnhnrl 73:f6f378311c8d 103
tnhnrl 73:f6f378311c8d 104 float * getLoggerArray(); //delete soon
tnhnrl 73:f6f378311c8d 105
tnhnrl 73:f6f378311c8d 106 void printDirectory();
tnhnrl 73:f6f378311c8d 107 void printCurrentSdLog(); //more tricky for SD card, work in progress
tnhnrl 73:f6f378311c8d 108
tnhnrl 73:f6f378311c8d 109 void createNewFile();
tnhnrl 73:f6f378311c8d 110
tnhnrl 73:f6f378311c8d 111 void transmitData();
tnhnrl 73:f6f378311c8d 112
tnhnrl 73:f6f378311c8d 113 float * dataArray();
tnhnrl 73:f6f378311c8d 114
tnhnrl 73:f6f378311c8d 115 //GUI UPDATE FUNCTIONS
tnhnrl 73:f6f378311c8d 116 float getTimerValue();
tnhnrl 73:f6f378311c8d 117
tnhnrl 80:4e5d306d695b 118 void logFileMenu(); //instead of immediately erasing log files, NRL Stennis suggests a confirmation
tnhnrl 80:4e5d306d695b 119
tnhnrl 73:f6f378311c8d 120 private:
tnhnrl 73:f6f378311c8d 121 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 122
tnhnrl 73:f6f378311c8d 123 int _timeout; // generic timeout for every state, seconds
joel_ssc 82:0981b9ada820 124 int _yo_time; // time allowed for one up or down in a leg of yo-yos
tnhnrl 73:f6f378311c8d 125 float _pitchTolerance; // pitch angle tolerance for neutral finding exit criteria
tnhnrl 73:f6f378311c8d 126 float _bceFloatPosition; // bce position for "float" states
tnhnrl 73:f6f378311c8d 127 float _battFloatPosition; // batt position for "broadcast" state
tnhnrl 73:f6f378311c8d 128
tnhnrl 73:f6f378311c8d 129 float _depth_command; // user keyboard depth
tnhnrl 73:f6f378311c8d 130 float _pitch_command; // user keyboard pitch
tnhnrl 73:f6f378311c8d 131 float _heading_command; // user keyboard heading
tnhnrl 73:f6f378311c8d 132
tnhnrl 73:f6f378311c8d 133 float _depth_reading; // depth reading (to get the readings at the same time)
tnhnrl 73:f6f378311c8d 134 float _pitch_reading; // pitch reading (to get the readings at the same time)
tnhnrl 73:f6f378311c8d 135 float _timer_reading; // pitch reading (to get the readings at the same time)
tnhnrl 73:f6f378311c8d 136
tnhnrl 73:f6f378311c8d 137 Timer _fsm_timer; //timing variable used in class
joel_ssc 82:0981b9ada820 138 Timer _yotimer; // timer for each yo in a yoyo leg
tnhnrl 73:f6f378311c8d 139
tnhnrl 73:f6f378311c8d 140 volatile int _state; // current state of Finite State Machine (FSM)
tnhnrl 73:f6f378311c8d 141 int _previous_state; // record previous state
tnhnrl 73:f6f378311c8d 142 int _sub_state; // substate on find_neutral function
tnhnrl 73:f6f378311c8d 143 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 144 float _neutral_timer; // keep time for rise/sink/level timer incremnets
tnhnrl 73:f6f378311c8d 145
tnhnrl 73:f6f378311c8d 146 bool _isTimeoutRunning;
tnhnrl 73:f6f378311c8d 147
tnhnrl 73:f6f378311c8d 148 bool _isSubStateTimerRunning;
tnhnrl 73:f6f378311c8d 149
tnhnrl 73:f6f378311c8d 150 float _neutral_bce_pos_mm;
tnhnrl 73:f6f378311c8d 151 float _neutral_batt_pos_mm;
tnhnrl 73:f6f378311c8d 152
tnhnrl 73:f6f378311c8d 153 int _multi_dive_counter;
joel_ssc 82:0981b9ada820 154 int _multi_leg_counter;
tnhnrl 73:f6f378311c8d 155
tnhnrl 73:f6f378311c8d 156 currentSequenceStruct currentStateStruct; //type_of_struct struct_name
joel_ssc 82:0981b9ada820 157 currentLegStruct currentLegStateStruct; //type_of_struct struct_name
tnhnrl 73:f6f378311c8d 158
tnhnrl 73:f6f378311c8d 159 float _depth_KP;
tnhnrl 73:f6f378311c8d 160 float _depth_KI;
tnhnrl 73:f6f378311c8d 161 float _depth_KD;
tnhnrl 73:f6f378311c8d 162
tnhnrl 73:f6f378311c8d 163 float _pitch_KP;
tnhnrl 73:f6f378311c8d 164 float _pitch_KI;
tnhnrl 73:f6f378311c8d 165 float _pitch_KD;
tnhnrl 73:f6f378311c8d 166
tnhnrl 73:f6f378311c8d 167 int _state_array[256]; //used to print out the states
tnhnrl 73:f6f378311c8d 168 int _state_array_counter; //used to iterate through state records
tnhnrl 73:f6f378311c8d 169 int _substate_array[256]; //used to print out the sub-states
tnhnrl 73:f6f378311c8d 170 int _substate_array_counter; //used to iterate through sub-state records
tnhnrl 73:f6f378311c8d 171
tnhnrl 73:f6f378311c8d 172 int _substate;
tnhnrl 73:f6f378311c8d 173 int _previous_substate;
tnhnrl 73:f6f378311c8d 174
tnhnrl 73:f6f378311c8d 175 float _max_recorded_depth_neutral;
tnhnrl 73:f6f378311c8d 176 float _max_recorded_depth_dive;
tnhnrl 73:f6f378311c8d 177
tnhnrl 73:f6f378311c8d 178 float _neutral_sink_command_mm; //defaults for neutral finding sub-FSM
tnhnrl 73:f6f378311c8d 179 float _neutral_rise_command_mm;
tnhnrl 73:f6f378311c8d 180 float _neutral_pitch_command_mm;
tnhnrl 73:f6f378311c8d 181
tnhnrl 73:f6f378311c8d 182 float _max_recorded_auto_neutral_depth;
tnhnrl 73:f6f378311c8d 183
tnhnrl 73:f6f378311c8d 184 bool _is_log_timer_running;
tnhnrl 73:f6f378311c8d 185 float _log_timer;
tnhnrl 73:f6f378311c8d 186
tnhnrl 73:f6f378311c8d 187 float _BCE_dive_offset; // NEW COMMANDS FOR POSITION CONTROLLER
tnhnrl 73:f6f378311c8d 188 float _BMM_dive_offset;
tnhnrl 73:f6f378311c8d 189
tnhnrl 73:f6f378311c8d 190 float getFloatUserInput();
tnhnrl 73:f6f378311c8d 191
tnhnrl 73:f6f378311c8d 192 //new
tnhnrl 73:f6f378311c8d 193 float _batt_filter_freq;
tnhnrl 73:f6f378311c8d 194 float _bce_filter_freq;
tnhnrl 73:f6f378311c8d 195 float _pitch_filter_freq;
tnhnrl 73:f6f378311c8d 196 float _depth_filter_freq;
tnhnrl 73:f6f378311c8d 197 float _heading_filter_freq;
tnhnrl 73:f6f378311c8d 198
tnhnrl 73:f6f378311c8d 199 float _batt_deadband;
tnhnrl 73:f6f378311c8d 200 float _bce_deadband;
tnhnrl 73:f6f378311c8d 201 float _pitch_deadband;
tnhnrl 73:f6f378311c8d 202 float _depth_deadband;
tnhnrl 73:f6f378311c8d 203 float _heading_deadband;
tnhnrl 73:f6f378311c8d 204 };
tnhnrl 73:f6f378311c8d 205
tnhnrl 73:f6f378311c8d 206 #endif