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