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@17:e5d9a543157b, 2018-10-31 (annotated)
- Committer:
- Mirjam
- Date:
- Wed Oct 31 14:44:13 2018 +0000
- Revision:
- 17:e5d9a543157b
- Parent:
- 9:8b2d6ec577e3
- Child:
- 18:f36ac3ee081a
Started with importing use phase
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" |
arnouddomhof | 8:2afb66572fc4 | 6 | //#include "HIDScope.h" |
Mirjam | 7:d4090f334ce2 | 7 | #include "BiQuad.h" |
Mirjam | 7:d4090f334ce2 | 8 | #include "BiQuad4.h" |
Mirjam | 7:d4090f334ce2 | 9 | #include "FilterDesign.h" |
Mirjam | 7:d4090f334ce2 | 10 | #include "FilterDesign2.h" |
Mirjam | 7:d4090f334ce2 | 11 | |
Mirjam | 17:e5d9a543157b | 12 | const double pi = 3.14159265359; |
AppelSab | 6:a02ad75f0333 | 13 | // LED's |
arnouddomhof | 3:dca57056e5cb | 14 | DigitalOut led_red(LED_RED); |
arnouddomhof | 3:dca57056e5cb | 15 | DigitalOut led_green(LED_GREEN); |
arnouddomhof | 3:dca57056e5cb | 16 | DigitalOut led_blue(LED_BLUE); |
AppelSab | 6:a02ad75f0333 | 17 | // Buttons |
arnouddomhof | 9:8b2d6ec577e3 | 18 | DigitalIn button_clbrt_home(SW2); |
arnouddomhof | 9:8b2d6ec577e3 | 19 | DigitalIn button_Demo(D5); |
arnouddomhof | 9:8b2d6ec577e3 | 20 | DigitalIn button_Emg(D6); |
AppelSab | 6:a02ad75f0333 | 21 | DigitalIn Fail_button(SW3); |
AppelSab | 6:a02ad75f0333 | 22 | // Modserial |
arnouddomhof | 3:dca57056e5cb | 23 | MODSERIAL pc(USBTX, USBRX); |
AppelSab | 6:a02ad75f0333 | 24 | // Encoders |
AppelSab | 6:a02ad75f0333 | 25 | QEI Encoder1(D11, D10, NC, 4200) ; // Encoder motor 1, (pin 1A, pin 1B, index pin(not used), counts/rev) |
AppelSab | 6:a02ad75f0333 | 26 | QEI Encoder2(D9, D8, NC, 4200) ; // Encoder motor 2, (pin 2A, pin 2B, index pin (not used), counts/rev) |
AppelSab | 6:a02ad75f0333 | 27 | // Motors (direction and PWM) |
AppelSab | 6:a02ad75f0333 | 28 | DigitalOut directionM1(D4); |
AppelSab | 6:a02ad75f0333 | 29 | DigitalOut directionM2(D7); |
AppelSab | 6:a02ad75f0333 | 30 | FastPWM motor1_pwm(D5); |
AppelSab | 6:a02ad75f0333 | 31 | FastPWM motor2_pwm(D6); |
Mirjam | 17:e5d9a543157b | 32 | // Inverse Kinematics |
Mirjam | 17:e5d9a543157b | 33 | volatile double q1_diff; |
Mirjam | 17:e5d9a543157b | 34 | volatile double q2_diff; |
Mirjam | 17:e5d9a543157b | 35 | double sq = 2.0; //to square numbers |
Mirjam | 17:e5d9a543157b | 36 | const double L1 = 250.0; //length of the first link |
Mirjam | 17:e5d9a543157b | 37 | const double L3 = 350.0; //length of the second link |
Mirjam | 17:e5d9a543157b | 38 | int track; |
Mirjam | 17:e5d9a543157b | 39 | const double x0 = 80.0; //zero x position after homing |
Mirjam | 17:e5d9a543157b | 40 | const double y0 = 141.0; //zero y position after homing |
Mirjam | 17:e5d9a543157b | 41 | volatile double setpointx = x0; |
Mirjam | 17:e5d9a543157b | 42 | volatile double setpointy = y0; |
Mirjam | 17:e5d9a543157b | 43 | volatile double U1; |
Mirjam | 17:e5d9a543157b | 44 | volatile double U2; |
Mirjam | 17:e5d9a543157b | 45 | // Reference angles of the starting position |
Mirjam | 17:e5d9a543157b | 46 | double q2_0 = pi + acos((pow(x0,sq)+pow(y0,sq)-pow(L1,sq)-pow(L3,sq))/(2.0*L1*L3)); |
Mirjam | 17:e5d9a543157b | 47 | double q1_0 = atan(y0/x0)+acos((-pow(L3,sq)+pow(L1,sq)+pow(x0,sq)+pow(y0,sq))/(2.0*L1*sqrt(pow(x0,sq)+pow(y0,sq)))); |
Mirjam | 17:e5d9a543157b | 48 | double q2_0_enc = q2_0 + q1_0; |
Mirjam | 7:d4090f334ce2 | 49 | // EMG input en start value of filtered EMG |
Mirjam | 7:d4090f334ce2 | 50 | AnalogIn emg1_raw( A0 ); |
Mirjam | 7:d4090f334ce2 | 51 | AnalogIn emg2_raw( A1 ); |
Mirjam | 7:d4090f334ce2 | 52 | double emg1_filtered = 0.00; |
Mirjam | 7:d4090f334ce2 | 53 | double emg2_filtered = 0.00; |
Mirjam | 7:d4090f334ce2 | 54 | float threshold_EMG = 0.25; // Threshold on 25 percent of the maximum EMG |
Mirjam | 7:d4090f334ce2 | 55 | |
AppelSab | 6:a02ad75f0333 | 56 | // Declare timers and Tickers |
Mirjam | 7:d4090f334ce2 | 57 | Timer timer; // Timer for counting time in this state |
Mirjam | 7:d4090f334ce2 | 58 | Ticker WriteValues; // Ticker to show values of velocity to screen |
Mirjam | 7:d4090f334ce2 | 59 | Ticker StateMachine; |
arnouddomhof | 8:2afb66572fc4 | 60 | //Ticker sample_EMGtoHIDscope; // Ticker to send the EMG signals to screen |
arnouddomhof | 8:2afb66572fc4 | 61 | //HIDScope scope(4); //Number of channels which needs to be send to the HIDScope |
arnouddomhof | 3:dca57056e5cb | 62 | |
AppelSab | 6:a02ad75f0333 | 63 | // Set up ProcessStateMachine |
arnouddomhof | 5:07e401cb251d | 64 | enum states {WAITING, MOTOR_ANGLE_CLBRT, EMG_CLBRT, HOMING, WAITING4SIGNAL, MOVE_W_EMG, MOVE_W_DEMO, FAILURE_MODE}; |
arnouddomhof | 3:dca57056e5cb | 65 | states currentState = WAITING; |
AppelSab | 6:a02ad75f0333 | 66 | bool stateChanged = true; |
AppelSab | 6:a02ad75f0333 | 67 | volatile bool writeVelocityFlag = false; |
AppelSab | 6:a02ad75f0333 | 68 | |
AppelSab | 6:a02ad75f0333 | 69 | // Global variables |
arnouddomhof | 3:dca57056e5cb | 70 | char c; |
AppelSab | 6:a02ad75f0333 | 71 | int counts1; |
AppelSab | 6:a02ad75f0333 | 72 | int counts2; |
AppelSab | 6:a02ad75f0333 | 73 | float theta1; |
AppelSab | 6:a02ad75f0333 | 74 | float theta2; |
AppelSab | 6:a02ad75f0333 | 75 | float vel_1; |
AppelSab | 6:a02ad75f0333 | 76 | float vel_2; |
AppelSab | 6:a02ad75f0333 | 77 | float theta1_prev = 0.0; |
AppelSab | 6:a02ad75f0333 | 78 | float theta2_prev = 0.0; |
AppelSab | 6:a02ad75f0333 | 79 | float tijd = 0.005; |
AppelSab | 6:a02ad75f0333 | 80 | float time_in_state; |
AppelSab | 6:a02ad75f0333 | 81 | |
Mirjam | 7:d4090f334ce2 | 82 | int need_to_move_1; // Does the robot needs to move in the first direction? |
Mirjam | 7:d4090f334ce2 | 83 | int need_to_move_2; // Does the robot needs to move in the second direction? |
Mirjam | 7:d4090f334ce2 | 84 | double EMG_calibrated_max_1 = 2.00000; // Maximum value of the first EMG signal found in the calibration state. |
Mirjam | 7:d4090f334ce2 | 85 | double EMG_calibrated_max_2 = 2.00000; // Maximum value of the second EMG signal found in the calibration state. |
Mirjam | 7:d4090f334ce2 | 86 | |
AppelSab | 6:a02ad75f0333 | 87 | // ---------------------------------------------- |
AppelSab | 6:a02ad75f0333 | 88 | // ------- FUNCTIONS ---------------------------- |
AppelSab | 6:a02ad75f0333 | 89 | // ---------------------------------------------- |
AppelSab | 6:a02ad75f0333 | 90 | |
AppelSab | 6:a02ad75f0333 | 91 | float ReadEncoder1() // Read Encoder of motor 1. |
AppelSab | 6:a02ad75f0333 | 92 | { |
AppelSab | 6:a02ad75f0333 | 93 | counts1 = Encoder1.getPulses(); // Counts of outputshaft of motor 1 |
AppelSab | 6:a02ad75f0333 | 94 | theta1 = (float(counts1)/4200) * 2*pi; // Angle of outputshaft of motor 1 |
AppelSab | 6:a02ad75f0333 | 95 | vel_1 = (theta1 - theta1_prev) / tijd; // Velocity, current angle - previous angle, devided by avarage time between encoder read-outs |
AppelSab | 6:a02ad75f0333 | 96 | theta1_prev = theta1; // Define theta_prev |
AppelSab | 6:a02ad75f0333 | 97 | return vel_1; |
AppelSab | 6:a02ad75f0333 | 98 | } |
AppelSab | 6:a02ad75f0333 | 99 | float ReadEncoder2() // Read encoder of motor 2. |
AppelSab | 6:a02ad75f0333 | 100 | { |
AppelSab | 6:a02ad75f0333 | 101 | counts2 = Encoder2.getPulses(); // Counts of outputshaft of motor 2 |
AppelSab | 6:a02ad75f0333 | 102 | theta2 = (float(counts2)/4200) * 2*pi; // Angle of outputshaft of motor 2 |
AppelSab | 6:a02ad75f0333 | 103 | vel_2 = (theta2 - theta2_prev) / tijd; // Velocity, current angle - previous angle, devided by avarage time between encoder read-outs |
AppelSab | 6:a02ad75f0333 | 104 | theta2_prev = theta2; // Define theta_prev |
AppelSab | 6:a02ad75f0333 | 105 | return vel_2; |
AppelSab | 6:a02ad75f0333 | 106 | } |
AppelSab | 6:a02ad75f0333 | 107 | void MotorAngleCalibrate() // Function that drives motor 1 and 2. |
AppelSab | 6:a02ad75f0333 | 108 | { |
AppelSab | 6:a02ad75f0333 | 109 | float U1 = -0.2; // Negative, so arm goes backwards. |
AppelSab | 6:a02ad75f0333 | 110 | float U2 = -0.2; // Motor 2 is not taken into account yet. |
AppelSab | 6:a02ad75f0333 | 111 | |
AppelSab | 6:a02ad75f0333 | 112 | motor1_pwm.write(fabs(U1)); // Send PWM values to motor |
AppelSab | 6:a02ad75f0333 | 113 | motor2_pwm.write(fabs(U2)); |
AppelSab | 6:a02ad75f0333 | 114 | |
AppelSab | 6:a02ad75f0333 | 115 | directionM1 = U1 > 0.0f; // Either true or false, determines direction. |
AppelSab | 6:a02ad75f0333 | 116 | directionM2 = U2 > 0.0f; |
AppelSab | 6:a02ad75f0333 | 117 | } |
Mirjam | 7:d4090f334ce2 | 118 | void sample() |
Mirjam | 7:d4090f334ce2 | 119 | { |
Mirjam | 7:d4090f334ce2 | 120 | emg1_filtered = FilterDesign(emg1_raw.read()); |
Mirjam | 7:d4090f334ce2 | 121 | emg2_filtered = FilterDesign2(emg2_raw.read()); |
Mirjam | 7:d4090f334ce2 | 122 | |
arnouddomhof | 8:2afb66572fc4 | 123 | /** |
Mirjam | 7:d4090f334ce2 | 124 | scope.set(0, emg1_raw.read()); // Raw EMG 1 send to scope 0 |
Mirjam | 7:d4090f334ce2 | 125 | scope.set(1, emg1_filtered); // Filtered EMG 1 send to scope 1 |
Mirjam | 7:d4090f334ce2 | 126 | scope.set(2, emg2_raw.read()); // Raw EMG 2 send to scope 2 |
Mirjam | 7:d4090f334ce2 | 127 | scope.set(3, emg2_filtered); // Filtered EMG 2 send to scope 3 |
Mirjam | 7:d4090f334ce2 | 128 | scope.send(); // Send the data to the computer |
arnouddomhof | 8:2afb66572fc4 | 129 | */ |
Mirjam | 7:d4090f334ce2 | 130 | } |
Mirjam | 17:e5d9a543157b | 131 | |
Mirjam | 17:e5d9a543157b | 132 | // --------------------------------------------------- |
Mirjam | 17:e5d9a543157b | 133 | // --------INVERSE-KINEMATICS------------------------- |
Mirjam | 17:e5d9a543157b | 134 | // --------------------------------------------------- |
Mirjam | 17:e5d9a543157b | 135 | double makeAngleq1(double x, double y){ |
Mirjam | 17:e5d9a543157b | 136 | double q1 = atan(y/x)+acos((-pow(L3,sq)+pow(L1,sq)+pow(x,sq)+pow(y,sq))/(2.0*L1*sqrt(pow(x,sq)+pow(y,sq)))); //angle of the first joint in the setpoint configuration |
Mirjam | 17:e5d9a543157b | 137 | q1_diff = -2.0*(q1-q1_0); //the actual amount of radians that the motor has to turn in total to reach the setpoint |
Mirjam | 17:e5d9a543157b | 138 | return q1_diff; |
Mirjam | 17:e5d9a543157b | 139 | } |
Mirjam | 17:e5d9a543157b | 140 | |
Mirjam | 17:e5d9a543157b | 141 | double makeAngleq2(double x, double y){ |
Mirjam | 17:e5d9a543157b | 142 | double q2 = -acos((pow(x,sq)+pow(y,sq)-pow(L1,sq)-pow(L3,sq))/(2.0*L1*L3)); //angle of the second joint in setpoint configuration |
Mirjam | 17:e5d9a543157b | 143 | double q1 = atan(y/x)+acos((-pow(L3,sq)+pow(L1,sq)+pow(x,sq)+pow(y,sq))/(2.0*L1*sqrt(pow(x,sq)+pow(y,sq)))); //angle of the first joint in the setpoint configuration |
Mirjam | 17:e5d9a543157b | 144 | double q2_motor = (pi - q2)+q1; //because q2 represents the angle at joint two and not at the motor a calculation has to be done |
Mirjam | 17:e5d9a543157b | 145 | q2_diff = (2.0*(q2_motor - q2_0_enc))/(2.0*pi); //the actual amount of radians that the motor has to turn in total to reach the setpoint |
Mirjam | 17:e5d9a543157b | 146 | return -q2_diff; |
Mirjam | 17:e5d9a543157b | 147 | } |
Mirjam | 17:e5d9a543157b | 148 | |
Mirjam | 17:e5d9a543157b | 149 | // -------------------------------------------------------------------- |
Mirjam | 17:e5d9a543157b | 150 | // ---------------READ-OUT ENCODERS------------------------------------ |
Mirjam | 17:e5d9a543157b | 151 | // -------------------------------------------------------------------- |
Mirjam | 17:e5d9a543157b | 152 | double counts2angle1() |
Mirjam | 17:e5d9a543157b | 153 | { |
Mirjam | 17:e5d9a543157b | 154 | counts1 = Encoder1.getPulses(); // Counts of outputshaft of motor 1 |
Mirjam | 17:e5d9a543157b | 155 | theta1 = -(double(counts1)/4200) * 2*pi; // Angle of outputshaft of motor 1 |
Mirjam | 17:e5d9a543157b | 156 | return theta1; |
Mirjam | 17:e5d9a543157b | 157 | } |
Mirjam | 17:e5d9a543157b | 158 | |
Mirjam | 17:e5d9a543157b | 159 | double counts2angle2() |
Mirjam | 17:e5d9a543157b | 160 | { |
Mirjam | 17:e5d9a543157b | 161 | counts2 = Encoder2.getPulses(); // Counts of outputshaft of motor 2 |
Mirjam | 17:e5d9a543157b | 162 | theta2 = (double(counts2)/4200) * 2*pi; // Angle of outputshaft of motor 2 |
Mirjam | 17:e5d9a543157b | 163 | return theta2; |
Mirjam | 17:e5d9a543157b | 164 | } |
Mirjam | 17:e5d9a543157b | 165 | |
Mirjam | 17:e5d9a543157b | 166 | // ----------------------------------------------------------------- |
Mirjam | 17:e5d9a543157b | 167 | // --------------------------- PI controllers ---------------------- |
Mirjam | 17:e5d9a543157b | 168 | // ----------------------------------------------------------------- |
Mirjam | 17:e5d9a543157b | 169 | double PI_controller1(double error1) |
Mirjam | 17:e5d9a543157b | 170 | { |
Mirjam | 17:e5d9a543157b | 171 | static double error_integral1 = 0; |
Mirjam | 17:e5d9a543157b | 172 | |
Mirjam | 17:e5d9a543157b | 173 | // Proportional part |
Mirjam | 17:e5d9a543157b | 174 | double Kp1 = 3.95; // Kp (proportionele controller, nu nog een random waarde) |
Mirjam | 17:e5d9a543157b | 175 | double u_p1 = Kp1*error1; // Voltage dat naar de motor gestuurd wordt (volgt uit error en Kp) |
Mirjam | 17:e5d9a543157b | 176 | |
Mirjam | 17:e5d9a543157b | 177 | // Integral part |
Mirjam | 17:e5d9a543157b | 178 | double Ki1 = 6.0; // Ki (Integrale deel vd controller, nu nog een random waarde) |
Mirjam | 17:e5d9a543157b | 179 | double Ts1 = 0.005; // Sample tijd, net zo vaak als de controller wordt aangeroepen (200 Hz, statemachine) |
Mirjam | 17:e5d9a543157b | 180 | error_integral1 = error_integral1 + error1 * Ts1; |
Mirjam | 17:e5d9a543157b | 181 | double u_i1 = Ki1 * error_integral1; |
Mirjam | 17:e5d9a543157b | 182 | |
Mirjam | 17:e5d9a543157b | 183 | // Sum |
Mirjam | 17:e5d9a543157b | 184 | U1 = u_p1 + u_i1; |
Mirjam | 17:e5d9a543157b | 185 | |
Mirjam | 17:e5d9a543157b | 186 | // Return |
Mirjam | 17:e5d9a543157b | 187 | return U1; |
Mirjam | 17:e5d9a543157b | 188 | } |
Mirjam | 17:e5d9a543157b | 189 | double PI_controller2(double error2) |
Mirjam | 17:e5d9a543157b | 190 | { |
Mirjam | 17:e5d9a543157b | 191 | static double error_integral2 = 0; |
Mirjam | 17:e5d9a543157b | 192 | |
Mirjam | 17:e5d9a543157b | 193 | // Proportional part |
Mirjam | 17:e5d9a543157b | 194 | double Kp2 = 3.95; // Kp (proportionele controller, nu nog een random waarde) |
Mirjam | 17:e5d9a543157b | 195 | double u_p2 = Kp2*error2; // Voltage dat naar de motor gestuurd wordt (volgt uit error en Kp) |
Mirjam | 17:e5d9a543157b | 196 | |
Mirjam | 17:e5d9a543157b | 197 | // Integral part |
Mirjam | 17:e5d9a543157b | 198 | double Ki2 = 6.0; // Ki (Integrale deel vd controller, nu nog een random waarde) |
Mirjam | 17:e5d9a543157b | 199 | double Ts2 = 0.005; // Sample tijd, net zo vaak als de controller wordt aangeroepen (200 Hz, statemachine) |
Mirjam | 17:e5d9a543157b | 200 | error_integral2 = error_integral2 + error2 * Ts2; |
Mirjam | 17:e5d9a543157b | 201 | double u_i2 = Ki2 * error_integral2; |
Mirjam | 17:e5d9a543157b | 202 | |
Mirjam | 17:e5d9a543157b | 203 | // Sum + |
Mirjam | 17:e5d9a543157b | 204 | U2 = u_p2 + u_i2; |
Mirjam | 17:e5d9a543157b | 205 | |
Mirjam | 17:e5d9a543157b | 206 | // Return |
Mirjam | 17:e5d9a543157b | 207 | return U2; |
Mirjam | 17:e5d9a543157b | 208 | } |
Mirjam | 17:e5d9a543157b | 209 | |
Mirjam | 17:e5d9a543157b | 210 | // ----------------------------------------------- |
Mirjam | 17:e5d9a543157b | 211 | // ------------ RUN MOTORS ----------------------- |
Mirjam | 17:e5d9a543157b | 212 | // ----------------------------------------------- |
Mirjam | 17:e5d9a543157b | 213 | void motoraansturing() |
Mirjam | 17:e5d9a543157b | 214 | { |
Mirjam | 17:e5d9a543157b | 215 | determinedemoset(); |
Mirjam | 17:e5d9a543157b | 216 | q1_diff = makeAngleq1(setpointx, setpointy); |
Mirjam | 17:e5d9a543157b | 217 | q2_diff = makeAngleq2(setpointx, setpointy); |
Mirjam | 17:e5d9a543157b | 218 | |
Mirjam | 17:e5d9a543157b | 219 | theta2 = counts2angle2(); |
Mirjam | 17:e5d9a543157b | 220 | error2 = q2_diff - theta2; |
Mirjam | 17:e5d9a543157b | 221 | theta1 = counts2angle1(); |
Mirjam | 17:e5d9a543157b | 222 | error1 = q1_diff - theta1; // Setpoint error, te behalen setpoint minus de huidige positie van de as. |
Mirjam | 17:e5d9a543157b | 223 | |
Mirjam | 17:e5d9a543157b | 224 | U1 = PI_controller1(error1); // Voltage dat naar de motor gestuurd wordt. |
Mirjam | 17:e5d9a543157b | 225 | U2 = PI_controller2(error2); |
Mirjam | 17:e5d9a543157b | 226 | |
Mirjam | 17:e5d9a543157b | 227 | motor1_pwm.write(fabs(U1)); // Motor aansturen |
Mirjam | 17:e5d9a543157b | 228 | directionM1 = U1 > 0.0f; // Richting van de motor bepalen |
Mirjam | 17:e5d9a543157b | 229 | motor2_pwm.write(fabs(U2)); |
Mirjam | 17:e5d9a543157b | 230 | directionM2 = U2 > 0.0f; |
Mirjam | 17:e5d9a543157b | 231 | } |
Mirjam | 17:e5d9a543157b | 232 | |
AppelSab | 6:a02ad75f0333 | 233 | // --------------------------------------------------- |
AppelSab | 6:a02ad75f0333 | 234 | // --------STATEMACHINE------------------------------- |
AppelSab | 6:a02ad75f0333 | 235 | // --------------------------------------------------- |
AppelSab | 6:a02ad75f0333 | 236 | void ProcessStateMachine(void) |
AppelSab | 6:a02ad75f0333 | 237 | { |
AppelSab | 6:a02ad75f0333 | 238 | switch (currentState) |
AppelSab | 6:a02ad75f0333 | 239 | { |
AppelSab | 6:a02ad75f0333 | 240 | case WAITING: |
Mirjam | 17:e5d9a543157b | 241 | // Description: |
Mirjam | 17:e5d9a543157b | 242 | // In this state we do nothing, and wait for a command |
Mirjam | 17:e5d9a543157b | 243 | |
Mirjam | 17:e5d9a543157b | 244 | // Actions |
Mirjam | 17:e5d9a543157b | 245 | led_red = 0; led_green = 0; led_blue = 0; // Colouring the led WHITE |
Mirjam | 17:e5d9a543157b | 246 | |
Mirjam | 17:e5d9a543157b | 247 | // State transition logic |
Mirjam | 17:e5d9a543157b | 248 | if (button_clbrt_home == 0) |
Mirjam | 17:e5d9a543157b | 249 | { |
Mirjam | 17:e5d9a543157b | 250 | currentState = MOTOR_ANGLE_CLBRT; |
Mirjam | 17:e5d9a543157b | 251 | stateChanged = true; |
Mirjam | 17:e5d9a543157b | 252 | pc.printf("Starting Calibration\n\r"); |
Mirjam | 17:e5d9a543157b | 253 | } |
Mirjam | 17:e5d9a543157b | 254 | else if (Fail_button == 0) |
Mirjam | 17:e5d9a543157b | 255 | { |
Mirjam | 17:e5d9a543157b | 256 | currentState = FAILURE_MODE; |
Mirjam | 17:e5d9a543157b | 257 | stateChanged = true; |
Mirjam | 17:e5d9a543157b | 258 | } |
Mirjam | 17:e5d9a543157b | 259 | break; |
AppelSab | 6:a02ad75f0333 | 260 | |
AppelSab | 6:a02ad75f0333 | 261 | case MOTOR_ANGLE_CLBRT: |
Mirjam | 17:e5d9a543157b | 262 | // Description: |
Mirjam | 17:e5d9a543157b | 263 | // In this state the robot moves with low motor PWM to some |
Mirjam | 17:e5d9a543157b | 264 | // mechanical limit of motion, in order to calibrate the motors. |
Mirjam | 17:e5d9a543157b | 265 | |
Mirjam | 17:e5d9a543157b | 266 | // Actions |
Mirjam | 17:e5d9a543157b | 267 | led_red = 1; led_green = 0; led_blue = 0; // Colouring the led TURQUOISE |
Mirjam | 17:e5d9a543157b | 268 | timer.start(); //Start timer to get time in the state "MOTOR_ANGLE_CLRBRT" |
Mirjam | 17:e5d9a543157b | 269 | if (stateChanged) |
Mirjam | 17:e5d9a543157b | 270 | { |
Mirjam | 17:e5d9a543157b | 271 | MotorAngleCalibrate(); // Actuate motor 1 and 2. |
Mirjam | 17:e5d9a543157b | 272 | vel_1 = ReadEncoder1(); // Get velocity of motor 1 |
Mirjam | 17:e5d9a543157b | 273 | vel_2 = ReadEncoder2(); // Get velocity of motor 2 |
Mirjam | 17:e5d9a543157b | 274 | stateChanged = true; // Keep this loop going, until the transition conditions are satisfied. |
Mirjam | 17:e5d9a543157b | 275 | } |
Mirjam | 17:e5d9a543157b | 276 | |
Mirjam | 17:e5d9a543157b | 277 | // State transition logic |
Mirjam | 17:e5d9a543157b | 278 | time_in_state = timer.read(); // Determine if this state has run for long enough. |
Mirjam | 17:e5d9a543157b | 279 | |
Mirjam | 17:e5d9a543157b | 280 | if(time_in_state > 2.0f && vel_1 < 1.1f && vel_2 < 1.1f) |
Mirjam | 17:e5d9a543157b | 281 | { |
Mirjam | 17:e5d9a543157b | 282 | //pc.printf( "Tijd in deze staat = %f \n\r", time_in_state); |
Mirjam | 17:e5d9a543157b | 283 | //pc.printf( "Tijd tijdens actions loop (Waarde voor bepalen van snelheid)") = %f \n\r", tijd); |
Mirjam | 17:e5d9a543157b | 284 | pc.printf("Motor calibration has ended. \n\r"); |
Mirjam | 17:e5d9a543157b | 285 | timer.stop(); // Stop timer for this state |
Mirjam | 17:e5d9a543157b | 286 | timer.reset(); // Reset timer for this state |
Mirjam | 17:e5d9a543157b | 287 | motor1_pwm.write(fabs(0.0)); // Send PWM values to motor |
Mirjam | 17:e5d9a543157b | 288 | motor2_pwm.write(fabs(0.0)); |
Mirjam | 17:e5d9a543157b | 289 | Encoder1.reset(); // Reset Encoders when arrived at zero-position |
Mirjam | 17:e5d9a543157b | 290 | Encoder2.reset(); |
Mirjam | 17:e5d9a543157b | 291 | |
Mirjam | 17:e5d9a543157b | 292 | currentState = EMG_CLBRT; // Switch to next state (EMG_CLRBRT). |
Mirjam | 17:e5d9a543157b | 293 | pc.printf("EMG calibration \r\n"); |
Mirjam | 17:e5d9a543157b | 294 | stateChanged = true; |
Mirjam | 17:e5d9a543157b | 295 | } |
Mirjam | 17:e5d9a543157b | 296 | if (Fail_button == 0) |
AppelSab | 6:a02ad75f0333 | 297 | { |
Mirjam | 17:e5d9a543157b | 298 | currentState = FAILURE_MODE; |
Mirjam | 17:e5d9a543157b | 299 | stateChanged = true; |
Mirjam | 17:e5d9a543157b | 300 | } |
Mirjam | 17:e5d9a543157b | 301 | break; |
AppelSab | 6:a02ad75f0333 | 302 | |
AppelSab | 6:a02ad75f0333 | 303 | case EMG_CLBRT: |
Mirjam | 17:e5d9a543157b | 304 | // In this state the person whom is connected to the robot needs |
Mirjam | 17:e5d9a543157b | 305 | // to flex his/her muscles as hard as possible, in order to |
Mirjam | 17:e5d9a543157b | 306 | // measure the maximum EMG-signal, which can be used to scale |
Mirjam | 17:e5d9a543157b | 307 | // the EMG-filter. |
Mirjam | 17:e5d9a543157b | 308 | |
Mirjam | 17:e5d9a543157b | 309 | led_red = 1; led_green = 1; led_blue = 0; // Colouring the led BLUE |
AppelSab | 6:a02ad75f0333 | 310 | |
Mirjam | 17:e5d9a543157b | 311 | // Requirements to move to the next state: |
Mirjam | 17:e5d9a543157b | 312 | // If enough time has passed (5 sec), and the EMG-signal drops below 10% |
Mirjam | 17:e5d9a543157b | 313 | // of the maximum measured value, we move to the Homing state. |
Mirjam | 17:e5d9a543157b | 314 | |
Mirjam | 17:e5d9a543157b | 315 | wait(5.0f); // time_in_this_state > 5.0f |
Mirjam | 17:e5d9a543157b | 316 | // INSERT CALIBRATING |
Mirjam | 17:e5d9a543157b | 317 | currentState = HOMING; |
Mirjam | 17:e5d9a543157b | 318 | if (Fail_button == 0) |
Mirjam | 17:e5d9a543157b | 319 | { |
Mirjam | 17:e5d9a543157b | 320 | currentState = FAILURE_MODE; |
Mirjam | 17:e5d9a543157b | 321 | stateChanged = true; |
Mirjam | 17:e5d9a543157b | 322 | } |
Mirjam | 17:e5d9a543157b | 323 | break; |
AppelSab | 6:a02ad75f0333 | 324 | |
AppelSab | 6:a02ad75f0333 | 325 | case HOMING: |
Mirjam | 17:e5d9a543157b | 326 | // Description: |
Mirjam | 17:e5d9a543157b | 327 | // Robot moves to the home starting configuration |
Mirjam | 17:e5d9a543157b | 328 | pc.printf("HOMING \r\n"); |
Mirjam | 17:e5d9a543157b | 329 | |
Mirjam | 17:e5d9a543157b | 330 | led_red = 0; led_green = 1; led_red = 0; // Colouring the led PURPLE |
Mirjam | 17:e5d9a543157b | 331 | |
Mirjam | 17:e5d9a543157b | 332 | // Requirements to move to the next state: |
Mirjam | 17:e5d9a543157b | 333 | // If we are in the right location, within some margin, we move to the Waiting for |
Mirjam | 17:e5d9a543157b | 334 | // signal state. |
AppelSab | 6:a02ad75f0333 | 335 | |
Mirjam | 17:e5d9a543157b | 336 | wait(5.0f); // time_in_this_state > 5.0f |
Mirjam | 17:e5d9a543157b | 337 | // INSERT MOVEMENT |
Mirjam | 17:e5d9a543157b | 338 | currentState = WAITING4SIGNAL; |
Mirjam | 17:e5d9a543157b | 339 | if (Fail_button == 0) |
Mirjam | 17:e5d9a543157b | 340 | { |
Mirjam | 17:e5d9a543157b | 341 | currentState = FAILURE_MODE; |
Mirjam | 17:e5d9a543157b | 342 | stateChanged = true; |
Mirjam | 17:e5d9a543157b | 343 | } |
Mirjam | 17:e5d9a543157b | 344 | break; |
AppelSab | 6:a02ad75f0333 | 345 | |
AppelSab | 6:a02ad75f0333 | 346 | case WAITING4SIGNAL: |
Mirjam | 17:e5d9a543157b | 347 | // Description: |
Mirjam | 17:e5d9a543157b | 348 | // In this state the robot waits for an action to occur. |
Mirjam | 17:e5d9a543157b | 349 | |
Mirjam | 17:e5d9a543157b | 350 | led_red = 0; led_green = 0; led_blue = 0; // Colouring the led WHITE |
Mirjam | 17:e5d9a543157b | 351 | |
Mirjam | 17:e5d9a543157b | 352 | // Requirements to move to the next state: |
Mirjam | 17:e5d9a543157b | 353 | // If a certain button is pressed we move to the corresponding |
Mirjam | 17:e5d9a543157b | 354 | // state (MOVE_W_DEMO, MOVE_W_EMG or SHUTDOWN) |
Mirjam | 17:e5d9a543157b | 355 | |
Mirjam | 17:e5d9a543157b | 356 | if (button_clbrt_home == 0) |
Mirjam | 17:e5d9a543157b | 357 | { |
Mirjam | 17:e5d9a543157b | 358 | currentState = MOTOR_ANGLE_CLBRT; |
Mirjam | 17:e5d9a543157b | 359 | stateChanged = true; |
Mirjam | 17:e5d9a543157b | 360 | pc.printf("Starting Calibration \n\r"); |
Mirjam | 17:e5d9a543157b | 361 | } |
Mirjam | 17:e5d9a543157b | 362 | else if (button_Demo == 1) |
Mirjam | 17:e5d9a543157b | 363 | { |
Mirjam | 17:e5d9a543157b | 364 | currentState = MOVE_W_DEMO; |
Mirjam | 17:e5d9a543157b | 365 | pc.printf("DEMO \r\n"); |
Mirjam | 17:e5d9a543157b | 366 | wait(1.0f); |
Mirjam | 17:e5d9a543157b | 367 | } |
Mirjam | 17:e5d9a543157b | 368 | else if (button_Emg == 1) |
Mirjam | 17:e5d9a543157b | 369 | { |
Mirjam | 17:e5d9a543157b | 370 | currentState = MOVE_W_EMG; |
Mirjam | 17:e5d9a543157b | 371 | pc.printf("EMG \r\n"); |
Mirjam | 17:e5d9a543157b | 372 | wait(1.0f); |
Mirjam | 17:e5d9a543157b | 373 | } |
Mirjam | 17:e5d9a543157b | 374 | else if (Fail_button == 0) |
Mirjam | 17:e5d9a543157b | 375 | { |
Mirjam | 17:e5d9a543157b | 376 | currentState = FAILURE_MODE; |
Mirjam | 17:e5d9a543157b | 377 | stateChanged = true; |
Mirjam | 17:e5d9a543157b | 378 | } |
Mirjam | 17:e5d9a543157b | 379 | |
Mirjam | 17:e5d9a543157b | 380 | break; |
AppelSab | 6:a02ad75f0333 | 381 | |
AppelSab | 6:a02ad75f0333 | 382 | case MOVE_W_DEMO: |
AppelSab | 6:a02ad75f0333 | 383 | // Description: |
AppelSab | 6:a02ad75f0333 | 384 | // In this state the robot follows a preprogrammed shape, e.g. |
AppelSab | 6:a02ad75f0333 | 385 | // a square. |
AppelSab | 6:a02ad75f0333 | 386 | |
AppelSab | 6:a02ad75f0333 | 387 | led_red = 1; led_green = 1; led_blue = 0; // Colouring the led GREEN |
AppelSab | 6:a02ad75f0333 | 388 | |
AppelSab | 6:a02ad75f0333 | 389 | // Requirements to move to the next state: |
AppelSab | 6:a02ad75f0333 | 390 | // When the home button or the failure button is pressed, we |
AppelSab | 6:a02ad75f0333 | 391 | // will the move to the corresponding state. |
AppelSab | 6:a02ad75f0333 | 392 | |
AppelSab | 6:a02ad75f0333 | 393 | // BUILD DEMO MODE |
arnouddomhof | 9:8b2d6ec577e3 | 394 | |
arnouddomhof | 9:8b2d6ec577e3 | 395 | if (button_clbrt_home == 0) |
AppelSab | 6:a02ad75f0333 | 396 | { |
arnouddomhof | 9:8b2d6ec577e3 | 397 | currentState = HOMING; |
arnouddomhof | 9:8b2d6ec577e3 | 398 | stateChanged = true; |
arnouddomhof | 9:8b2d6ec577e3 | 399 | pc.printf("Moving home\n\r"); |
AppelSab | 6:a02ad75f0333 | 400 | } |
arnouddomhof | 9:8b2d6ec577e3 | 401 | else if (Fail_button == 0) |
AppelSab | 6:a02ad75f0333 | 402 | { |
AppelSab | 6:a02ad75f0333 | 403 | currentState = FAILURE_MODE; |
AppelSab | 6:a02ad75f0333 | 404 | stateChanged = true; |
AppelSab | 6:a02ad75f0333 | 405 | } |
AppelSab | 6:a02ad75f0333 | 406 | break; |
AppelSab | 6:a02ad75f0333 | 407 | |
AppelSab | 6:a02ad75f0333 | 408 | case MOVE_W_EMG: |
Mirjam | 17:e5d9a543157b | 409 | // Description: |
Mirjam | 17:e5d9a543157b | 410 | // In this state the robot will be controlled by use of |
Mirjam | 17:e5d9a543157b | 411 | // EMG-signals. |
Mirjam | 17:e5d9a543157b | 412 | |
Mirjam | 17:e5d9a543157b | 413 | led_red = 1; led_green = 0; led_blue = 1; // Colouring the led GREEN |
Mirjam | 17:e5d9a543157b | 414 | |
Mirjam | 17:e5d9a543157b | 415 | if (emg1_filtered >= (threshold_EMG*EMG_calibrated_max_1)){ |
Mirjam | 17:e5d9a543157b | 416 | need_to_move_1 = 1; // The robot does have to move |
Mirjam | 17:e5d9a543157b | 417 | } |
Mirjam | 17:e5d9a543157b | 418 | else { |
Mirjam | 17:e5d9a543157b | 419 | need_to_move_1 = 0; // If the robot does not have to move |
Mirjam | 17:e5d9a543157b | 420 | } |
Mirjam | 17:e5d9a543157b | 421 | |
Mirjam | 17:e5d9a543157b | 422 | if(emg2_filtered >= threshold_EMG*EMG_calibrated_max_2){ |
Mirjam | 17:e5d9a543157b | 423 | need_to_move_2 = 1; |
Mirjam | 17:e5d9a543157b | 424 | } |
Mirjam | 17:e5d9a543157b | 425 | else { |
Mirjam | 17:e5d9a543157b | 426 | need_to_move_2 = 0; |
Mirjam | 17:e5d9a543157b | 427 | } |
Mirjam | 17:e5d9a543157b | 428 | |
Mirjam | 17:e5d9a543157b | 429 | |
Mirjam | 17:e5d9a543157b | 430 | // Requirements to move to the next state: |
Mirjam | 17:e5d9a543157b | 431 | // When the home button or the failure button is pressed, we |
Mirjam | 17:e5d9a543157b | 432 | // will the move to the corresponding state. |
Mirjam | 17:e5d9a543157b | 433 | |
Mirjam | 17:e5d9a543157b | 434 | if (button_clbrt_home == 0) |
Mirjam | 17:e5d9a543157b | 435 | { |
Mirjam | 17:e5d9a543157b | 436 | currentState = MOTOR_ANGLE_CLBRT; |
Mirjam | 17:e5d9a543157b | 437 | stateChanged = true; |
Mirjam | 17:e5d9a543157b | 438 | pc.printf("Starting Calibration \n\r"); |
Mirjam | 17:e5d9a543157b | 439 | } |
Mirjam | 17:e5d9a543157b | 440 | else if (Fail_button == 0) |
Mirjam | 17:e5d9a543157b | 441 | { |
Mirjam | 17:e5d9a543157b | 442 | currentState = FAILURE_MODE; |
Mirjam | 17:e5d9a543157b | 443 | stateChanged = true; |
Mirjam | 7:d4090f334ce2 | 444 | } |
Mirjam | 17:e5d9a543157b | 445 | break; |
AppelSab | 6:a02ad75f0333 | 446 | |
AppelSab | 6:a02ad75f0333 | 447 | case FAILURE_MODE: |
Mirjam | 17:e5d9a543157b | 448 | // Description: |
Mirjam | 17:e5d9a543157b | 449 | // This state is reached when the failure button is reached. |
Mirjam | 17:e5d9a543157b | 450 | // In this state everything is turned off. |
Mirjam | 17:e5d9a543157b | 451 | |
Mirjam | 17:e5d9a543157b | 452 | led_red = 0; led_green = 1; led_blue = 1; // Colouring the led RED |
Mirjam | 17:e5d9a543157b | 453 | // Actions |
Mirjam | 17:e5d9a543157b | 454 | if (stateChanged) |
Mirjam | 17:e5d9a543157b | 455 | { |
Mirjam | 17:e5d9a543157b | 456 | motor1_pwm.write(fabs(0.0)); // Stop all motors! |
Mirjam | 17:e5d9a543157b | 457 | motor2_pwm.write(fabs(0.0)); |
Mirjam | 17:e5d9a543157b | 458 | pc.printf("FAILURE MODE \r\n PLEASE RESTART THE WHOLE ROBOT \r\n (and make sure this does not happen again) \r\n"); |
Mirjam | 17:e5d9a543157b | 459 | stateChanged = false; |
Mirjam | 17:e5d9a543157b | 460 | } |
Mirjam | 17:e5d9a543157b | 461 | break; |
AppelSab | 6:a02ad75f0333 | 462 | |
AppelSab | 6:a02ad75f0333 | 463 | // State transition logic |
AppelSab | 6:a02ad75f0333 | 464 | // No state transition, you need to restart the robot. |
AppelSab | 6:a02ad75f0333 | 465 | |
AppelSab | 6:a02ad75f0333 | 466 | default: |
AppelSab | 6:a02ad75f0333 | 467 | // This state is a default state, this state is reached when |
AppelSab | 6:a02ad75f0333 | 468 | // the program somehow defies all of the other states. |
AppelSab | 6:a02ad75f0333 | 469 | |
AppelSab | 6:a02ad75f0333 | 470 | pc.printf("Unknown or unimplemented state reached!!! \n\r"); |
AppelSab | 6:a02ad75f0333 | 471 | led_red = 1; led_green = 1; led_blue = 1; // Colouring the led BLACK |
AppelSab | 6:a02ad75f0333 | 472 | for (int n = 0; n < 50; n++) // Making an SOS signal with the RED led |
AppelSab | 6:a02ad75f0333 | 473 | { |
AppelSab | 6:a02ad75f0333 | 474 | for (int i = 0; i < 6; i++) |
AppelSab | 6:a02ad75f0333 | 475 | { |
AppelSab | 6:a02ad75f0333 | 476 | led_red = !led_red; |
AppelSab | 6:a02ad75f0333 | 477 | wait(0.6f); |
AppelSab | 6:a02ad75f0333 | 478 | } |
AppelSab | 6:a02ad75f0333 | 479 | wait(0.4f); |
AppelSab | 6:a02ad75f0333 | 480 | for (int i = 0 ; i < 6; i++) |
AppelSab | 6:a02ad75f0333 | 481 | { |
AppelSab | 6:a02ad75f0333 | 482 | led_red = !led_red; |
AppelSab | 6:a02ad75f0333 | 483 | wait(0.2f); |
AppelSab | 6:a02ad75f0333 | 484 | } |
AppelSab | 6:a02ad75f0333 | 485 | wait(0.4f); |
AppelSab | 6:a02ad75f0333 | 486 | } |
arnouddomhof | 3:dca57056e5cb | 487 | } |
AppelSab | 6:a02ad75f0333 | 488 | } |
AppelSab | 6:a02ad75f0333 | 489 | |
AppelSab | 6:a02ad75f0333 | 490 | // -------------------------------- |
AppelSab | 6:a02ad75f0333 | 491 | // ----- MAIN LOOP ---------------- |
AppelSab | 6:a02ad75f0333 | 492 | // -------------------------------- |
AppelSab | 6:a02ad75f0333 | 493 | |
Mirjam | 0:46dbc9b620d8 | 494 | int main() |
Mirjam | 0:46dbc9b620d8 | 495 | { |
Mirjam | 4:a0c1c021026b | 496 | // Switch all LEDs off |
arnouddomhof | 3:dca57056e5cb | 497 | led_red = 1; |
arnouddomhof | 3:dca57056e5cb | 498 | led_green = 1; |
arnouddomhof | 3:dca57056e5cb | 499 | led_blue = 1; |
AppelSab | 6:a02ad75f0333 | 500 | |
arnouddomhof | 3:dca57056e5cb | 501 | pc.baud(115200); |
arnouddomhof | 8:2afb66572fc4 | 502 | |
arnouddomhof | 8:2afb66572fc4 | 503 | pc.printf("\r\n _______________ INSERT ROBOT NAME HERE! _______________ \r\n"); |
arnouddomhof | 8:2afb66572fc4 | 504 | wait(0.5f); |
arnouddomhof | 8:2afb66572fc4 | 505 | pc.printf("WAITING... \r\n"); |
arnouddomhof | 8:2afb66572fc4 | 506 | |
AppelSab | 6:a02ad75f0333 | 507 | StateMachine.attach(&ProcessStateMachine, 0.005f); // Run statemachine 200 times per second |
Mirjam | 7:d4090f334ce2 | 508 | sample_EMGtoHIDscope.attach(&sample, 0.02f); // Display EMG values 50 times per second |
Mirjam | 17:e5d9a543157b | 509 | |
Mirjam | 0:46dbc9b620d8 | 510 | while (true) { |
AppelSab | 6:a02ad75f0333 | 511 | |
AppelSab | 6:a02ad75f0333 | 512 | } |
AppelSab | 6:a02ad75f0333 | 513 | } |
AppelSab | 6:a02ad75f0333 | 514 | |
arnouddomhof | 5:07e401cb251d | 515 |