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:
Mon Feb 25 21:26:34 2019 +0000
Revision:
85:dd8176285b6e
Parent:
84:eccd8e837134
Child:
87:6d95f853dab3
tests imu.roll

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