Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed QEI HIDScope BiQuad4th_order biquadFilter MODSERIAL FastPWM
main.cpp@8:2afb66572fc4, 2018-10-31 (annotated)
- Committer:
- arnouddomhof
- Date:
- Wed Oct 31 09:12:59 2018 +0000
- Revision:
- 8:2afb66572fc4
- Parent:
- 7:d4090f334ce2
- Child:
- 9:8b2d6ec577e3
Poging tot het werkend maken van de state machine, met EMG calibration
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mirjam | 0:46dbc9b620d8 | 1 | #include "mbed.h" |
arnouddomhof | 3:dca57056e5cb | 2 | #include "MODSERIAL.h" |
AppelSab | 6:a02ad75f0333 | 3 | #include "QEI.h" |
AppelSab | 6:a02ad75f0333 | 4 | #include "FastPWM.h" |
AppelSab | 6:a02ad75f0333 | 5 | #include "math.h" |
Mirjam | 0:46dbc9b620d8 | 6 | |
Mirjam | 7:d4090f334ce2 | 7 | #include "mbed.h" |
arnouddomhof | 8:2afb66572fc4 | 8 | //#include "HIDScope.h" |
Mirjam | 7:d4090f334ce2 | 9 | #include "BiQuad.h" |
Mirjam | 7:d4090f334ce2 | 10 | #include "BiQuad4.h" |
Mirjam | 7:d4090f334ce2 | 11 | #include "FilterDesign.h" |
Mirjam | 7:d4090f334ce2 | 12 | #include "FilterDesign2.h" |
Mirjam | 7:d4090f334ce2 | 13 | |
AppelSab | 6:a02ad75f0333 | 14 | // LED's |
arnouddomhof | 3:dca57056e5cb | 15 | DigitalOut led_red(LED_RED); |
arnouddomhof | 3:dca57056e5cb | 16 | DigitalOut led_green(LED_GREEN); |
arnouddomhof | 3:dca57056e5cb | 17 | DigitalOut led_blue(LED_BLUE); |
AppelSab | 6:a02ad75f0333 | 18 | // Buttons |
AppelSab | 6:a02ad75f0333 | 19 | DigitalIn button_clbrt(SW2); |
AppelSab | 6:a02ad75f0333 | 20 | DigitalIn Fail_button(SW3); |
AppelSab | 6:a02ad75f0333 | 21 | // Modserial |
arnouddomhof | 3:dca57056e5cb | 22 | MODSERIAL pc(USBTX, USBRX); |
AppelSab | 6:a02ad75f0333 | 23 | // Encoders |
AppelSab | 6:a02ad75f0333 | 24 | QEI Encoder1(D11, D10, NC, 4200) ; // Encoder motor 1, (pin 1A, pin 1B, index pin(not used), counts/rev) |
AppelSab | 6:a02ad75f0333 | 25 | QEI Encoder2(D9, D8, NC, 4200) ; // Encoder motor 2, (pin 2A, pin 2B, index pin (not used), counts/rev) |
AppelSab | 6:a02ad75f0333 | 26 | // Motors (direction and PWM) |
AppelSab | 6:a02ad75f0333 | 27 | DigitalOut directionM1(D4); |
AppelSab | 6:a02ad75f0333 | 28 | DigitalOut directionM2(D7); |
AppelSab | 6:a02ad75f0333 | 29 | FastPWM motor1_pwm(D5); |
AppelSab | 6:a02ad75f0333 | 30 | FastPWM motor2_pwm(D6); |
Mirjam | 7:d4090f334ce2 | 31 | // EMG input en start value of filtered EMG |
Mirjam | 7:d4090f334ce2 | 32 | AnalogIn emg1_raw( A0 ); |
Mirjam | 7:d4090f334ce2 | 33 | AnalogIn emg2_raw( A1 ); |
Mirjam | 7:d4090f334ce2 | 34 | double emg1_filtered = 0.00; |
Mirjam | 7:d4090f334ce2 | 35 | double emg2_filtered = 0.00; |
Mirjam | 7:d4090f334ce2 | 36 | float threshold_EMG = 0.25; // Threshold on 25 percent of the maximum EMG |
Mirjam | 7:d4090f334ce2 | 37 | |
AppelSab | 6:a02ad75f0333 | 38 | // Declare timers and Tickers |
Mirjam | 7:d4090f334ce2 | 39 | Timer timer; // Timer for counting time in this state |
Mirjam | 7:d4090f334ce2 | 40 | Ticker WriteValues; // Ticker to show values of velocity to screen |
Mirjam | 7:d4090f334ce2 | 41 | Ticker StateMachine; |
arnouddomhof | 8:2afb66572fc4 | 42 | //Ticker sample_EMGtoHIDscope; // Ticker to send the EMG signals to screen |
arnouddomhof | 8:2afb66572fc4 | 43 | //HIDScope scope(4); //Number of channels which needs to be send to the HIDScope |
arnouddomhof | 3:dca57056e5cb | 44 | |
AppelSab | 6:a02ad75f0333 | 45 | // Set up ProcessStateMachine |
arnouddomhof | 5:07e401cb251d | 46 | enum states {WAITING, MOTOR_ANGLE_CLBRT, EMG_CLBRT, HOMING, WAITING4SIGNAL, MOVE_W_EMG, MOVE_W_DEMO, FAILURE_MODE}; |
arnouddomhof | 3:dca57056e5cb | 47 | states currentState = WAITING; |
AppelSab | 6:a02ad75f0333 | 48 | bool stateChanged = true; |
AppelSab | 6:a02ad75f0333 | 49 | volatile bool writeVelocityFlag = false; |
AppelSab | 6:a02ad75f0333 | 50 | |
AppelSab | 6:a02ad75f0333 | 51 | // Global variables |
arnouddomhof | 3:dca57056e5cb | 52 | char c; |
AppelSab | 6:a02ad75f0333 | 53 | int counts1; |
AppelSab | 6:a02ad75f0333 | 54 | int counts2; |
AppelSab | 6:a02ad75f0333 | 55 | float theta1; |
AppelSab | 6:a02ad75f0333 | 56 | float theta2; |
AppelSab | 6:a02ad75f0333 | 57 | float vel_1; |
AppelSab | 6:a02ad75f0333 | 58 | float vel_2; |
AppelSab | 6:a02ad75f0333 | 59 | float theta1_prev = 0.0; |
AppelSab | 6:a02ad75f0333 | 60 | float theta2_prev = 0.0; |
AppelSab | 6:a02ad75f0333 | 61 | const float pi = 3.14159265359; |
AppelSab | 6:a02ad75f0333 | 62 | float tijd = 0.005; |
AppelSab | 6:a02ad75f0333 | 63 | float time_in_state; |
AppelSab | 6:a02ad75f0333 | 64 | |
Mirjam | 7:d4090f334ce2 | 65 | int need_to_move_1; // Does the robot needs to move in the first direction? |
Mirjam | 7:d4090f334ce2 | 66 | int need_to_move_2; // Does the robot needs to move in the second direction? |
Mirjam | 7:d4090f334ce2 | 67 | double EMG_calibrated_max_1 = 2.00000; // Maximum value of the first EMG signal found in the calibration state. |
Mirjam | 7:d4090f334ce2 | 68 | double EMG_calibrated_max_2 = 2.00000; // Maximum value of the second EMG signal found in the calibration state. |
Mirjam | 7:d4090f334ce2 | 69 | |
AppelSab | 6:a02ad75f0333 | 70 | // ---------------------------------------------- |
AppelSab | 6:a02ad75f0333 | 71 | // ------- FUNCTIONS ---------------------------- |
AppelSab | 6:a02ad75f0333 | 72 | // ---------------------------------------------- |
AppelSab | 6:a02ad75f0333 | 73 | |
AppelSab | 6:a02ad75f0333 | 74 | float ReadEncoder1() // Read Encoder of motor 1. |
AppelSab | 6:a02ad75f0333 | 75 | { |
AppelSab | 6:a02ad75f0333 | 76 | counts1 = Encoder1.getPulses(); // Counts of outputshaft of motor 1 |
AppelSab | 6:a02ad75f0333 | 77 | theta1 = (float(counts1)/4200) * 2*pi; // Angle of outputshaft of motor 1 |
AppelSab | 6:a02ad75f0333 | 78 | vel_1 = (theta1 - theta1_prev) / tijd; // Velocity, current angle - previous angle, devided by avarage time between encoder read-outs |
AppelSab | 6:a02ad75f0333 | 79 | theta1_prev = theta1; // Define theta_prev |
AppelSab | 6:a02ad75f0333 | 80 | return vel_1; |
AppelSab | 6:a02ad75f0333 | 81 | } |
AppelSab | 6:a02ad75f0333 | 82 | float ReadEncoder2() // Read encoder of motor 2. |
AppelSab | 6:a02ad75f0333 | 83 | { |
AppelSab | 6:a02ad75f0333 | 84 | counts2 = Encoder2.getPulses(); // Counts of outputshaft of motor 2 |
AppelSab | 6:a02ad75f0333 | 85 | theta2 = (float(counts2)/4200) * 2*pi; // Angle of outputshaft of motor 2 |
AppelSab | 6:a02ad75f0333 | 86 | vel_2 = (theta2 - theta2_prev) / tijd; // Velocity, current angle - previous angle, devided by avarage time between encoder read-outs |
AppelSab | 6:a02ad75f0333 | 87 | theta2_prev = theta2; // Define theta_prev |
AppelSab | 6:a02ad75f0333 | 88 | return vel_2; |
AppelSab | 6:a02ad75f0333 | 89 | } |
AppelSab | 6:a02ad75f0333 | 90 | void MotorAngleCalibrate() // Function that drives motor 1 and 2. |
AppelSab | 6:a02ad75f0333 | 91 | { |
AppelSab | 6:a02ad75f0333 | 92 | float U1 = -0.2; // Negative, so arm goes backwards. |
AppelSab | 6:a02ad75f0333 | 93 | float U2 = -0.2; // Motor 2 is not taken into account yet. |
AppelSab | 6:a02ad75f0333 | 94 | |
AppelSab | 6:a02ad75f0333 | 95 | motor1_pwm.write(fabs(U1)); // Send PWM values to motor |
AppelSab | 6:a02ad75f0333 | 96 | motor2_pwm.write(fabs(U2)); |
AppelSab | 6:a02ad75f0333 | 97 | |
AppelSab | 6:a02ad75f0333 | 98 | directionM1 = U1 > 0.0f; // Either true or false, determines direction. |
AppelSab | 6:a02ad75f0333 | 99 | directionM2 = U2 > 0.0f; |
AppelSab | 6:a02ad75f0333 | 100 | } |
Mirjam | 7:d4090f334ce2 | 101 | void sample() |
Mirjam | 7:d4090f334ce2 | 102 | { |
Mirjam | 7:d4090f334ce2 | 103 | emg1_filtered = FilterDesign(emg1_raw.read()); |
Mirjam | 7:d4090f334ce2 | 104 | emg2_filtered = FilterDesign2(emg2_raw.read()); |
Mirjam | 7:d4090f334ce2 | 105 | |
arnouddomhof | 8:2afb66572fc4 | 106 | /** |
Mirjam | 7:d4090f334ce2 | 107 | scope.set(0, emg1_raw.read()); // Raw EMG 1 send to scope 0 |
Mirjam | 7:d4090f334ce2 | 108 | scope.set(1, emg1_filtered); // Filtered EMG 1 send to scope 1 |
Mirjam | 7:d4090f334ce2 | 109 | scope.set(2, emg2_raw.read()); // Raw EMG 2 send to scope 2 |
Mirjam | 7:d4090f334ce2 | 110 | scope.set(3, emg2_filtered); // Filtered EMG 2 send to scope 3 |
Mirjam | 7:d4090f334ce2 | 111 | scope.send(); // Send the data to the computer |
arnouddomhof | 8:2afb66572fc4 | 112 | */ |
Mirjam | 7:d4090f334ce2 | 113 | } |
AppelSab | 6:a02ad75f0333 | 114 | // --------------------------------------------------- |
AppelSab | 6:a02ad75f0333 | 115 | // --------STATEMACHINE------------------------------- |
AppelSab | 6:a02ad75f0333 | 116 | // --------------------------------------------------- |
AppelSab | 6:a02ad75f0333 | 117 | void ProcessStateMachine(void) |
AppelSab | 6:a02ad75f0333 | 118 | { |
AppelSab | 6:a02ad75f0333 | 119 | switch (currentState) |
AppelSab | 6:a02ad75f0333 | 120 | { |
AppelSab | 6:a02ad75f0333 | 121 | case WAITING: |
AppelSab | 6:a02ad75f0333 | 122 | // Description: |
AppelSab | 6:a02ad75f0333 | 123 | // In this state we do nothing, and wait for a command |
AppelSab | 6:a02ad75f0333 | 124 | |
AppelSab | 6:a02ad75f0333 | 125 | // Actions |
AppelSab | 6:a02ad75f0333 | 126 | led_red = 0; led_green = 0; led_blue = 0; // Colouring the led WHITE |
AppelSab | 6:a02ad75f0333 | 127 | |
AppelSab | 6:a02ad75f0333 | 128 | // State transition logic |
AppelSab | 6:a02ad75f0333 | 129 | if (button_clbrt == 0) |
AppelSab | 6:a02ad75f0333 | 130 | { |
AppelSab | 6:a02ad75f0333 | 131 | currentState = MOTOR_ANGLE_CLBRT; |
AppelSab | 6:a02ad75f0333 | 132 | stateChanged = true; |
AppelSab | 6:a02ad75f0333 | 133 | pc.printf("Starting Calibration\n\r"); |
AppelSab | 6:a02ad75f0333 | 134 | } |
AppelSab | 6:a02ad75f0333 | 135 | if (Fail_button == 0) |
AppelSab | 6:a02ad75f0333 | 136 | { |
AppelSab | 6:a02ad75f0333 | 137 | currentState = FAILURE_MODE; |
AppelSab | 6:a02ad75f0333 | 138 | stateChanged = true; |
AppelSab | 6:a02ad75f0333 | 139 | } |
AppelSab | 6:a02ad75f0333 | 140 | break; |
AppelSab | 6:a02ad75f0333 | 141 | |
AppelSab | 6:a02ad75f0333 | 142 | case MOTOR_ANGLE_CLBRT: |
AppelSab | 6:a02ad75f0333 | 143 | // Description: |
AppelSab | 6:a02ad75f0333 | 144 | // In this state the robot moves with low motor PWM to some |
AppelSab | 6:a02ad75f0333 | 145 | // mechanical limit of motion, in order to calibrate the motors. |
AppelSab | 6:a02ad75f0333 | 146 | |
AppelSab | 6:a02ad75f0333 | 147 | // Actions |
AppelSab | 6:a02ad75f0333 | 148 | led_red = 1; led_green = 0; led_blue = 0; // Colouring the led TURQUOISE |
AppelSab | 6:a02ad75f0333 | 149 | timer.start(); //Start timer to get time in the state "MOTOR_ANGLE_CLRBRT" |
AppelSab | 6:a02ad75f0333 | 150 | if (stateChanged) |
AppelSab | 6:a02ad75f0333 | 151 | { |
AppelSab | 6:a02ad75f0333 | 152 | MotorAngleCalibrate(); // Actuate motor 1 and 2. |
AppelSab | 6:a02ad75f0333 | 153 | vel_1 = ReadEncoder1(); // Get velocity of motor 1 |
AppelSab | 6:a02ad75f0333 | 154 | vel_2 = ReadEncoder2(); // Get velocity of motor 2 |
AppelSab | 6:a02ad75f0333 | 155 | stateChanged = true; // Keep this loop going, until the transition conditions are satisfied. |
AppelSab | 6:a02ad75f0333 | 156 | } |
AppelSab | 6:a02ad75f0333 | 157 | |
AppelSab | 6:a02ad75f0333 | 158 | // State transition logic |
AppelSab | 6:a02ad75f0333 | 159 | time_in_state = timer.read(); // Determine if this state has run for long enough. |
arnouddomhof | 3:dca57056e5cb | 160 | |
AppelSab | 6:a02ad75f0333 | 161 | if(time_in_state > 2.0f && vel_1 < 1.1f && vel_2 < 1.1f) |
AppelSab | 6:a02ad75f0333 | 162 | { |
AppelSab | 6:a02ad75f0333 | 163 | //pc.printf( "Tijd in deze staat = %f \n\r", time_in_state); |
AppelSab | 6:a02ad75f0333 | 164 | //pc.printf( "Tijd tijdens actions loop (Waarde voor bepalen van snelheid)") = %f \n\r", tijd); |
arnouddomhof | 8:2afb66572fc4 | 165 | pc.printf("Motor calibration has ended. \n\r"); |
AppelSab | 6:a02ad75f0333 | 166 | timer.stop(); // Stop timer for this state |
AppelSab | 6:a02ad75f0333 | 167 | timer.reset(); // Reset timer for this state |
AppelSab | 6:a02ad75f0333 | 168 | motor1_pwm.write(fabs(0.0)); // Send PWM values to motor |
AppelSab | 6:a02ad75f0333 | 169 | motor2_pwm.write(fabs(0.0)); |
AppelSab | 6:a02ad75f0333 | 170 | Encoder1.reset(); // Reset Encoders when arrived at zero-position |
AppelSab | 6:a02ad75f0333 | 171 | Encoder2.reset(); |
AppelSab | 6:a02ad75f0333 | 172 | |
AppelSab | 6:a02ad75f0333 | 173 | currentState = EMG_CLBRT; // Switch to next state (EMG_CLRBRT). |
arnouddomhof | 8:2afb66572fc4 | 174 | pc.printf("EMG calibration \r\n"); |
AppelSab | 6:a02ad75f0333 | 175 | stateChanged = true; |
AppelSab | 6:a02ad75f0333 | 176 | } |
AppelSab | 6:a02ad75f0333 | 177 | if (Fail_button == 0) |
AppelSab | 6:a02ad75f0333 | 178 | { |
AppelSab | 6:a02ad75f0333 | 179 | currentState = FAILURE_MODE; |
AppelSab | 6:a02ad75f0333 | 180 | stateChanged = true; |
AppelSab | 6:a02ad75f0333 | 181 | } |
AppelSab | 6:a02ad75f0333 | 182 | break; |
AppelSab | 6:a02ad75f0333 | 183 | |
AppelSab | 6:a02ad75f0333 | 184 | case EMG_CLBRT: |
AppelSab | 6:a02ad75f0333 | 185 | // In this state the person whom is connected to the robot needs |
AppelSab | 6:a02ad75f0333 | 186 | // to flex his/her muscles as hard as possible, in order to |
AppelSab | 6:a02ad75f0333 | 187 | // measure the maximum EMG-signal, which can be used to scale |
AppelSab | 6:a02ad75f0333 | 188 | // the EMG-filter. |
AppelSab | 6:a02ad75f0333 | 189 | |
AppelSab | 6:a02ad75f0333 | 190 | led_red = 1; led_green = 1; led_blue = 0; // Colouring the led BLUE |
AppelSab | 6:a02ad75f0333 | 191 | |
AppelSab | 6:a02ad75f0333 | 192 | // Requirements to move to the next state: |
AppelSab | 6:a02ad75f0333 | 193 | // If enough time has passed (5 sec), and the EMG-signal drops below 10% |
AppelSab | 6:a02ad75f0333 | 194 | // of the maximum measured value, we move to the Homing state. |
AppelSab | 6:a02ad75f0333 | 195 | |
AppelSab | 6:a02ad75f0333 | 196 | wait(5.0f); // time_in_this_state > 5.0f |
AppelSab | 6:a02ad75f0333 | 197 | // if moving_average(procd_emg) < 0.1*max_procd_emg_ever |
AppelSab | 6:a02ad75f0333 | 198 | // INSERT CALIBRATING |
AppelSab | 6:a02ad75f0333 | 199 | currentState = HOMING; |
AppelSab | 6:a02ad75f0333 | 200 | if (Fail_button == 0) |
AppelSab | 6:a02ad75f0333 | 201 | { |
AppelSab | 6:a02ad75f0333 | 202 | currentState = FAILURE_MODE; |
AppelSab | 6:a02ad75f0333 | 203 | stateChanged = true; |
AppelSab | 6:a02ad75f0333 | 204 | } |
AppelSab | 6:a02ad75f0333 | 205 | break; |
AppelSab | 6:a02ad75f0333 | 206 | |
AppelSab | 6:a02ad75f0333 | 207 | case HOMING: |
AppelSab | 6:a02ad75f0333 | 208 | // Description: |
AppelSab | 6:a02ad75f0333 | 209 | // Robot moves to the home starting configuration |
arnouddomhof | 8:2afb66572fc4 | 210 | pc.printf("HOMING, moving to the home starting configuration. \r\n"); |
AppelSab | 6:a02ad75f0333 | 211 | |
AppelSab | 6:a02ad75f0333 | 212 | led_red = 0; led_green = 1; led_red = 0; // Colouring the led PURPLE |
AppelSab | 6:a02ad75f0333 | 213 | |
AppelSab | 6:a02ad75f0333 | 214 | // Requirements to move to the next state: |
AppelSab | 6:a02ad75f0333 | 215 | // If we are in the right location, within some margin, we move to the Waiting for |
AppelSab | 6:a02ad75f0333 | 216 | // signal state. |
AppelSab | 6:a02ad75f0333 | 217 | |
AppelSab | 6:a02ad75f0333 | 218 | wait(5.0f); // time_in_this_state > 5.0f |
AppelSab | 6:a02ad75f0333 | 219 | // if ((fabs(angle_error1) < 0.01f) && (fabs(angle_error2) < 0.01f)) { |
AppelSab | 6:a02ad75f0333 | 220 | // INSERT MOVEMENT |
AppelSab | 6:a02ad75f0333 | 221 | currentState = WAITING4SIGNAL; |
AppelSab | 6:a02ad75f0333 | 222 | if (Fail_button == 0) |
AppelSab | 6:a02ad75f0333 | 223 | { |
AppelSab | 6:a02ad75f0333 | 224 | currentState = FAILURE_MODE; |
AppelSab | 6:a02ad75f0333 | 225 | stateChanged = true; |
AppelSab | 6:a02ad75f0333 | 226 | } |
AppelSab | 6:a02ad75f0333 | 227 | break; |
AppelSab | 6:a02ad75f0333 | 228 | |
AppelSab | 6:a02ad75f0333 | 229 | case WAITING4SIGNAL: |
AppelSab | 6:a02ad75f0333 | 230 | // Description: |
AppelSab | 6:a02ad75f0333 | 231 | // In this state the robot waits for an action to occur. |
arnouddomhof | 8:2afb66572fc4 | 232 | pc.printf("Press d to run the demo, Press e to move with EMG, Press c to re-calibrate. \r\n"); |
AppelSab | 6:a02ad75f0333 | 233 | led_red = 0; led_green = 0; led_blue = 0; // Colouring the led WHITE |
AppelSab | 6:a02ad75f0333 | 234 | |
AppelSab | 6:a02ad75f0333 | 235 | // Requirements to move to the next state: |
AppelSab | 6:a02ad75f0333 | 236 | // If a certain button is pressed we move to the corresponding |
AppelSab | 6:a02ad75f0333 | 237 | // state (MOVE_W_DEMO, MOVE_W_EMG or SHUTDOWN) |
AppelSab | 6:a02ad75f0333 | 238 | |
AppelSab | 6:a02ad75f0333 | 239 | // CANNOT USE KEYBOARD. SO LOOK FOR ALTERNATIVE |
AppelSab | 6:a02ad75f0333 | 240 | c = pc.getc(); |
AppelSab | 6:a02ad75f0333 | 241 | if (c == 'd') |
AppelSab | 6:a02ad75f0333 | 242 | { |
arnouddomhof | 8:2afb66572fc4 | 243 | pc.printf("MOVE_W_DEMO, Running the demo \r\n"); |
AppelSab | 6:a02ad75f0333 | 244 | currentState = MOVE_W_DEMO; |
arnouddomhof | 8:2afb66572fc4 | 245 | |
AppelSab | 6:a02ad75f0333 | 246 | } |
AppelSab | 6:a02ad75f0333 | 247 | else if (c == 'e') |
AppelSab | 6:a02ad75f0333 | 248 | { |
arnouddomhof | 8:2afb66572fc4 | 249 | pc.printf("MOVE_W_EMG, Moving with use of EMG \r\n"); |
AppelSab | 6:a02ad75f0333 | 250 | currentState = MOVE_W_EMG; |
AppelSab | 6:a02ad75f0333 | 251 | } |
AppelSab | 6:a02ad75f0333 | 252 | else if (c == 'c') |
AppelSab | 6:a02ad75f0333 | 253 | { |
arnouddomhof | 8:2afb66572fc4 | 254 | pc.printf("Starting Calibration\n\r"); |
arnouddomhof | 8:2afb66572fc4 | 255 | currentState = MOTOR_ANGLE_CLBRT; |
AppelSab | 6:a02ad75f0333 | 256 | } |
AppelSab | 6:a02ad75f0333 | 257 | |
AppelSab | 6:a02ad75f0333 | 258 | if (Fail_button == 0) |
AppelSab | 6:a02ad75f0333 | 259 | { |
AppelSab | 6:a02ad75f0333 | 260 | currentState = FAILURE_MODE; |
AppelSab | 6:a02ad75f0333 | 261 | stateChanged = true; |
AppelSab | 6:a02ad75f0333 | 262 | } |
AppelSab | 6:a02ad75f0333 | 263 | break; |
AppelSab | 6:a02ad75f0333 | 264 | |
AppelSab | 6:a02ad75f0333 | 265 | case MOVE_W_DEMO: |
AppelSab | 6:a02ad75f0333 | 266 | // Description: |
AppelSab | 6:a02ad75f0333 | 267 | // In this state the robot follows a preprogrammed shape, e.g. |
AppelSab | 6:a02ad75f0333 | 268 | // a square. |
AppelSab | 6:a02ad75f0333 | 269 | |
AppelSab | 6:a02ad75f0333 | 270 | led_red = 1; led_green = 1; led_blue = 0; // Colouring the led GREEN |
AppelSab | 6:a02ad75f0333 | 271 | |
AppelSab | 6:a02ad75f0333 | 272 | // Requirements to move to the next state: |
AppelSab | 6:a02ad75f0333 | 273 | // When the home button or the failure button is pressed, we |
AppelSab | 6:a02ad75f0333 | 274 | // will the move to the corresponding state. |
AppelSab | 6:a02ad75f0333 | 275 | |
AppelSab | 6:a02ad75f0333 | 276 | // BUILD DEMO MODE |
AppelSab | 6:a02ad75f0333 | 277 | // FIND ALTERNATIVE FOR KEYBOARD |
AppelSab | 6:a02ad75f0333 | 278 | c = pc.getcNb(); |
AppelSab | 6:a02ad75f0333 | 279 | if (c == 'h') |
AppelSab | 6:a02ad75f0333 | 280 | { |
AppelSab | 6:a02ad75f0333 | 281 | currentState = HOMING; |
AppelSab | 6:a02ad75f0333 | 282 | } |
AppelSab | 6:a02ad75f0333 | 283 | wait(1.0f); |
AppelSab | 6:a02ad75f0333 | 284 | |
AppelSab | 6:a02ad75f0333 | 285 | if (Fail_button == 0) |
AppelSab | 6:a02ad75f0333 | 286 | { |
AppelSab | 6:a02ad75f0333 | 287 | currentState = FAILURE_MODE; |
AppelSab | 6:a02ad75f0333 | 288 | stateChanged = true; |
AppelSab | 6:a02ad75f0333 | 289 | } |
AppelSab | 6:a02ad75f0333 | 290 | break; |
AppelSab | 6:a02ad75f0333 | 291 | |
AppelSab | 6:a02ad75f0333 | 292 | case MOVE_W_EMG: |
AppelSab | 6:a02ad75f0333 | 293 | // Description: |
AppelSab | 6:a02ad75f0333 | 294 | // In this state the robot will be controlled by use of |
AppelSab | 6:a02ad75f0333 | 295 | // EMG-signals. |
AppelSab | 6:a02ad75f0333 | 296 | |
AppelSab | 6:a02ad75f0333 | 297 | led_red = 1; led_green = 0; led_blue = 1; // Colouring the led GREEN |
Mirjam | 7:d4090f334ce2 | 298 | |
Mirjam | 7:d4090f334ce2 | 299 | if (emg1_filtered >= (threshold_EMG*EMG_calibrated_max_1)){ |
Mirjam | 7:d4090f334ce2 | 300 | need_to_move_1 = 1; // The robot does have to move |
Mirjam | 7:d4090f334ce2 | 301 | } |
Mirjam | 7:d4090f334ce2 | 302 | else { |
Mirjam | 7:d4090f334ce2 | 303 | need_to_move_1 = 0; // If the robot does not have to move |
Mirjam | 7:d4090f334ce2 | 304 | } |
Mirjam | 7:d4090f334ce2 | 305 | |
arnouddomhof | 8:2afb66572fc4 | 306 | if(emg2_filtered >= threshold_EMG*EMG_calibrated_max_2){ |
Mirjam | 7:d4090f334ce2 | 307 | need_to_move_2 = 1; |
Mirjam | 7:d4090f334ce2 | 308 | } |
Mirjam | 7:d4090f334ce2 | 309 | else { |
Mirjam | 7:d4090f334ce2 | 310 | need_to_move_2 = 0; |
Mirjam | 7:d4090f334ce2 | 311 | } |
AppelSab | 6:a02ad75f0333 | 312 | // Requirements to move to the next state: |
AppelSab | 6:a02ad75f0333 | 313 | // When the home button or the failure button is pressed, we |
AppelSab | 6:a02ad75f0333 | 314 | // will the move to the corresponding state. |
AppelSab | 6:a02ad75f0333 | 315 | |
AppelSab | 6:a02ad75f0333 | 316 | wait(1); |
AppelSab | 6:a02ad75f0333 | 317 | c = pc.getcNb(); |
AppelSab | 6:a02ad75f0333 | 318 | if (c == 'h') |
AppelSab | 6:a02ad75f0333 | 319 | { |
AppelSab | 6:a02ad75f0333 | 320 | currentState = HOMING; |
AppelSab | 6:a02ad75f0333 | 321 | } |
AppelSab | 6:a02ad75f0333 | 322 | wait(1.0f); |
AppelSab | 6:a02ad75f0333 | 323 | |
AppelSab | 6:a02ad75f0333 | 324 | if (Fail_button == 0) |
AppelSab | 6:a02ad75f0333 | 325 | { |
AppelSab | 6:a02ad75f0333 | 326 | currentState = FAILURE_MODE; |
AppelSab | 6:a02ad75f0333 | 327 | stateChanged = true; |
AppelSab | 6:a02ad75f0333 | 328 | } |
AppelSab | 6:a02ad75f0333 | 329 | break; |
AppelSab | 6:a02ad75f0333 | 330 | |
AppelSab | 6:a02ad75f0333 | 331 | case FAILURE_MODE: |
AppelSab | 6:a02ad75f0333 | 332 | // Description: |
AppelSab | 6:a02ad75f0333 | 333 | // This state is reached when the failure button is reached. |
AppelSab | 6:a02ad75f0333 | 334 | // In this state everything is turned off. |
AppelSab | 6:a02ad75f0333 | 335 | |
AppelSab | 6:a02ad75f0333 | 336 | led_red = 0; led_green = 1; led_blue = 1; // Colouring the led RED |
AppelSab | 6:a02ad75f0333 | 337 | // Actions |
AppelSab | 6:a02ad75f0333 | 338 | if (stateChanged) |
AppelSab | 6:a02ad75f0333 | 339 | { |
AppelSab | 6:a02ad75f0333 | 340 | motor1_pwm.write(fabs(0.0)); // Stop all motors! |
AppelSab | 6:a02ad75f0333 | 341 | motor2_pwm.write(fabs(0.0)); |
AppelSab | 6:a02ad75f0333 | 342 | pc.printf("FAILURE MODE \r\n PLEASE RESTART THE WHOLE ROBOT \r\n (and make sure this does not happen again) \r\n"); |
AppelSab | 6:a02ad75f0333 | 343 | stateChanged = false; |
AppelSab | 6:a02ad75f0333 | 344 | } |
AppelSab | 6:a02ad75f0333 | 345 | break; |
AppelSab | 6:a02ad75f0333 | 346 | |
AppelSab | 6:a02ad75f0333 | 347 | // State transition logic |
AppelSab | 6:a02ad75f0333 | 348 | // No state transition, you need to restart the robot. |
AppelSab | 6:a02ad75f0333 | 349 | |
AppelSab | 6:a02ad75f0333 | 350 | default: |
AppelSab | 6:a02ad75f0333 | 351 | // This state is a default state, this state is reached when |
AppelSab | 6:a02ad75f0333 | 352 | // the program somehow defies all of the other states. |
AppelSab | 6:a02ad75f0333 | 353 | |
AppelSab | 6:a02ad75f0333 | 354 | pc.printf("Unknown or unimplemented state reached!!! \n\r"); |
AppelSab | 6:a02ad75f0333 | 355 | led_red = 1; led_green = 1; led_blue = 1; // Colouring the led BLACK |
AppelSab | 6:a02ad75f0333 | 356 | for (int n = 0; n < 50; n++) // Making an SOS signal with the RED led |
AppelSab | 6:a02ad75f0333 | 357 | { |
AppelSab | 6:a02ad75f0333 | 358 | for (int i = 0; i < 6; i++) |
AppelSab | 6:a02ad75f0333 | 359 | { |
AppelSab | 6:a02ad75f0333 | 360 | led_red = !led_red; |
AppelSab | 6:a02ad75f0333 | 361 | wait(0.6f); |
AppelSab | 6:a02ad75f0333 | 362 | } |
AppelSab | 6:a02ad75f0333 | 363 | wait(0.4f); |
AppelSab | 6:a02ad75f0333 | 364 | for (int i = 0 ; i < 6; i++) |
AppelSab | 6:a02ad75f0333 | 365 | { |
AppelSab | 6:a02ad75f0333 | 366 | led_red = !led_red; |
AppelSab | 6:a02ad75f0333 | 367 | wait(0.2f); |
AppelSab | 6:a02ad75f0333 | 368 | } |
AppelSab | 6:a02ad75f0333 | 369 | wait(0.4f); |
AppelSab | 6:a02ad75f0333 | 370 | } |
arnouddomhof | 3:dca57056e5cb | 371 | } |
AppelSab | 6:a02ad75f0333 | 372 | } |
AppelSab | 6:a02ad75f0333 | 373 | |
AppelSab | 6:a02ad75f0333 | 374 | // -------------------------------- |
AppelSab | 6:a02ad75f0333 | 375 | // ----- MAIN LOOP ---------------- |
AppelSab | 6:a02ad75f0333 | 376 | // -------------------------------- |
AppelSab | 6:a02ad75f0333 | 377 | |
Mirjam | 0:46dbc9b620d8 | 378 | int main() |
Mirjam | 0:46dbc9b620d8 | 379 | { |
Mirjam | 4:a0c1c021026b | 380 | // Switch all LEDs off |
arnouddomhof | 3:dca57056e5cb | 381 | led_red = 1; |
arnouddomhof | 3:dca57056e5cb | 382 | led_green = 1; |
arnouddomhof | 3:dca57056e5cb | 383 | led_blue = 1; |
AppelSab | 6:a02ad75f0333 | 384 | |
arnouddomhof | 3:dca57056e5cb | 385 | pc.baud(115200); |
arnouddomhof | 8:2afb66572fc4 | 386 | |
arnouddomhof | 8:2afb66572fc4 | 387 | pc.printf("\r\n _______________ INSERT ROBOT NAME HERE! _______________ \r\n"); |
arnouddomhof | 8:2afb66572fc4 | 388 | wait(0.5f); |
arnouddomhof | 8:2afb66572fc4 | 389 | pc.printf("WAITING... \r\n"); |
arnouddomhof | 8:2afb66572fc4 | 390 | |
AppelSab | 6:a02ad75f0333 | 391 | StateMachine.attach(&ProcessStateMachine, 0.005f); // Run statemachine 200 times per second |
arnouddomhof | 8:2afb66572fc4 | 392 | /** |
Mirjam | 7:d4090f334ce2 | 393 | sample_EMGtoHIDscope.attach(&sample, 0.02f); // Display EMG values 50 times per second |
arnouddomhof | 8:2afb66572fc4 | 394 | */ |
Mirjam | 0:46dbc9b620d8 | 395 | while (true) { |
AppelSab | 6:a02ad75f0333 | 396 | |
AppelSab | 6:a02ad75f0333 | 397 | } |
AppelSab | 6:a02ad75f0333 | 398 | } |
AppelSab | 6:a02ad75f0333 | 399 | |
arnouddomhof | 5:07e401cb251d | 400 |