groep 16 / Mbed 2 deprecated Project_BioRobotics_12

Dependencies:   mbed QEI HIDScope BiQuad4th_order biquadFilter MODSERIAL FastPWM

Committer:
Mirjam
Date:
Wed Oct 31 14:49:34 2018 +0000
Revision:
18:f36ac3ee081a
Parent:
10:3f93fdb90c29
Parent:
17:e5d9a543157b
Child:
19:2d9421b0316a
Merged with EMG calibration and start of use phase

Who changed what in which revision?

UserRevisionLine numberNew 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.
Duif 10:3f93fdb90c29 86 double emg1_cal = 0.00000; //measured value of the first emg
Duif 10:3f93fdb90c29 87 double emg2_cal = 0.00000; //measured value of the second emg
Mirjam 7:d4090f334ce2 88
AppelSab 6:a02ad75f0333 89 // ----------------------------------------------
AppelSab 6:a02ad75f0333 90 // ------- FUNCTIONS ----------------------------
AppelSab 6:a02ad75f0333 91 // ----------------------------------------------
AppelSab 6:a02ad75f0333 92
AppelSab 6:a02ad75f0333 93 float ReadEncoder1() // Read Encoder of motor 1.
AppelSab 6:a02ad75f0333 94 {
AppelSab 6:a02ad75f0333 95 counts1 = Encoder1.getPulses(); // Counts of outputshaft of motor 1
AppelSab 6:a02ad75f0333 96 theta1 = (float(counts1)/4200) * 2*pi; // Angle of outputshaft of motor 1
AppelSab 6:a02ad75f0333 97 vel_1 = (theta1 - theta1_prev) / tijd; // Velocity, current angle - previous angle, devided by avarage time between encoder read-outs
AppelSab 6:a02ad75f0333 98 theta1_prev = theta1; // Define theta_prev
AppelSab 6:a02ad75f0333 99 return vel_1;
AppelSab 6:a02ad75f0333 100 }
AppelSab 6:a02ad75f0333 101 float ReadEncoder2() // Read encoder of motor 2.
AppelSab 6:a02ad75f0333 102 {
AppelSab 6:a02ad75f0333 103 counts2 = Encoder2.getPulses(); // Counts of outputshaft of motor 2
AppelSab 6:a02ad75f0333 104 theta2 = (float(counts2)/4200) * 2*pi; // Angle of outputshaft of motor 2
AppelSab 6:a02ad75f0333 105 vel_2 = (theta2 - theta2_prev) / tijd; // Velocity, current angle - previous angle, devided by avarage time between encoder read-outs
AppelSab 6:a02ad75f0333 106 theta2_prev = theta2; // Define theta_prev
AppelSab 6:a02ad75f0333 107 return vel_2;
AppelSab 6:a02ad75f0333 108 }
AppelSab 6:a02ad75f0333 109 void MotorAngleCalibrate() // Function that drives motor 1 and 2.
AppelSab 6:a02ad75f0333 110 {
AppelSab 6:a02ad75f0333 111 float U1 = -0.2; // Negative, so arm goes backwards.
AppelSab 6:a02ad75f0333 112 float U2 = -0.2; // Motor 2 is not taken into account yet.
AppelSab 6:a02ad75f0333 113
AppelSab 6:a02ad75f0333 114 motor1_pwm.write(fabs(U1)); // Send PWM values to motor
AppelSab 6:a02ad75f0333 115 motor2_pwm.write(fabs(U2));
AppelSab 6:a02ad75f0333 116
AppelSab 6:a02ad75f0333 117 directionM1 = U1 > 0.0f; // Either true or false, determines direction.
AppelSab 6:a02ad75f0333 118 directionM2 = U2 > 0.0f;
AppelSab 6:a02ad75f0333 119 }
Mirjam 7:d4090f334ce2 120 void sample()
Mirjam 7:d4090f334ce2 121 {
Mirjam 7:d4090f334ce2 122 emg1_filtered = FilterDesign(emg1_raw.read());
Mirjam 7:d4090f334ce2 123 emg2_filtered = FilterDesign2(emg2_raw.read());
Mirjam 7:d4090f334ce2 124
arnouddomhof 8:2afb66572fc4 125 /**
Mirjam 7:d4090f334ce2 126 scope.set(0, emg1_raw.read()); // Raw EMG 1 send to scope 0
Mirjam 7:d4090f334ce2 127 scope.set(1, emg1_filtered); // Filtered EMG 1 send to scope 1
Mirjam 7:d4090f334ce2 128 scope.set(2, emg2_raw.read()); // Raw EMG 2 send to scope 2
Mirjam 7:d4090f334ce2 129 scope.set(3, emg2_filtered); // Filtered EMG 2 send to scope 3
Mirjam 7:d4090f334ce2 130 scope.send(); // Send the data to the computer
arnouddomhof 8:2afb66572fc4 131 */
Mirjam 7:d4090f334ce2 132 }
Mirjam 17:e5d9a543157b 133
Mirjam 17:e5d9a543157b 134 // ---------------------------------------------------
Mirjam 17:e5d9a543157b 135 // --------INVERSE-KINEMATICS-------------------------
Mirjam 17:e5d9a543157b 136 // ---------------------------------------------------
Mirjam 17:e5d9a543157b 137 double makeAngleq1(double x, double y){
Mirjam 17:e5d9a543157b 138 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 139 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 140 return q1_diff;
Mirjam 17:e5d9a543157b 141 }
Mirjam 17:e5d9a543157b 142
Mirjam 17:e5d9a543157b 143 double makeAngleq2(double x, double y){
Mirjam 17:e5d9a543157b 144 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 145 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 146 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 147 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 148 return -q2_diff;
Mirjam 17:e5d9a543157b 149 }
Mirjam 17:e5d9a543157b 150
Mirjam 17:e5d9a543157b 151 // --------------------------------------------------------------------
Mirjam 17:e5d9a543157b 152 // ---------------READ-OUT ENCODERS------------------------------------
Mirjam 17:e5d9a543157b 153 // --------------------------------------------------------------------
Mirjam 17:e5d9a543157b 154 double counts2angle1()
Mirjam 17:e5d9a543157b 155 {
Mirjam 17:e5d9a543157b 156 counts1 = Encoder1.getPulses(); // Counts of outputshaft of motor 1
Mirjam 17:e5d9a543157b 157 theta1 = -(double(counts1)/4200) * 2*pi; // Angle of outputshaft of motor 1
Mirjam 17:e5d9a543157b 158 return theta1;
Mirjam 17:e5d9a543157b 159 }
Mirjam 17:e5d9a543157b 160
Mirjam 17:e5d9a543157b 161 double counts2angle2()
Mirjam 17:e5d9a543157b 162 {
Mirjam 17:e5d9a543157b 163 counts2 = Encoder2.getPulses(); // Counts of outputshaft of motor 2
Mirjam 17:e5d9a543157b 164 theta2 = (double(counts2)/4200) * 2*pi; // Angle of outputshaft of motor 2
Mirjam 17:e5d9a543157b 165 return theta2;
Mirjam 17:e5d9a543157b 166 }
Mirjam 17:e5d9a543157b 167
Mirjam 17:e5d9a543157b 168 // -----------------------------------------------------------------
Mirjam 17:e5d9a543157b 169 // --------------------------- PI controllers ----------------------
Mirjam 17:e5d9a543157b 170 // -----------------------------------------------------------------
Mirjam 17:e5d9a543157b 171 double PI_controller1(double error1)
Mirjam 17:e5d9a543157b 172 {
Mirjam 17:e5d9a543157b 173 static double error_integral1 = 0;
Mirjam 17:e5d9a543157b 174
Mirjam 17:e5d9a543157b 175 // Proportional part
Mirjam 17:e5d9a543157b 176 double Kp1 = 3.95; // Kp (proportionele controller, nu nog een random waarde)
Mirjam 17:e5d9a543157b 177 double u_p1 = Kp1*error1; // Voltage dat naar de motor gestuurd wordt (volgt uit error en Kp)
Mirjam 17:e5d9a543157b 178
Mirjam 17:e5d9a543157b 179 // Integral part
Mirjam 17:e5d9a543157b 180 double Ki1 = 6.0; // Ki (Integrale deel vd controller, nu nog een random waarde)
Mirjam 17:e5d9a543157b 181 double Ts1 = 0.005; // Sample tijd, net zo vaak als de controller wordt aangeroepen (200 Hz, statemachine)
Mirjam 17:e5d9a543157b 182 error_integral1 = error_integral1 + error1 * Ts1;
Mirjam 17:e5d9a543157b 183 double u_i1 = Ki1 * error_integral1;
Mirjam 17:e5d9a543157b 184
Mirjam 17:e5d9a543157b 185 // Sum
Mirjam 17:e5d9a543157b 186 U1 = u_p1 + u_i1;
Mirjam 17:e5d9a543157b 187
Mirjam 17:e5d9a543157b 188 // Return
Mirjam 17:e5d9a543157b 189 return U1;
Mirjam 17:e5d9a543157b 190 }
Mirjam 17:e5d9a543157b 191 double PI_controller2(double error2)
Mirjam 17:e5d9a543157b 192 {
Mirjam 17:e5d9a543157b 193 static double error_integral2 = 0;
Mirjam 17:e5d9a543157b 194
Mirjam 17:e5d9a543157b 195 // Proportional part
Mirjam 17:e5d9a543157b 196 double Kp2 = 3.95; // Kp (proportionele controller, nu nog een random waarde)
Mirjam 17:e5d9a543157b 197 double u_p2 = Kp2*error2; // Voltage dat naar de motor gestuurd wordt (volgt uit error en Kp)
Mirjam 17:e5d9a543157b 198
Mirjam 17:e5d9a543157b 199 // Integral part
Mirjam 17:e5d9a543157b 200 double Ki2 = 6.0; // Ki (Integrale deel vd controller, nu nog een random waarde)
Mirjam 17:e5d9a543157b 201 double Ts2 = 0.005; // Sample tijd, net zo vaak als de controller wordt aangeroepen (200 Hz, statemachine)
Mirjam 17:e5d9a543157b 202 error_integral2 = error_integral2 + error2 * Ts2;
Mirjam 17:e5d9a543157b 203 double u_i2 = Ki2 * error_integral2;
Mirjam 17:e5d9a543157b 204
Mirjam 17:e5d9a543157b 205 // Sum +
Mirjam 17:e5d9a543157b 206 U2 = u_p2 + u_i2;
Mirjam 17:e5d9a543157b 207
Mirjam 17:e5d9a543157b 208 // Return
Mirjam 17:e5d9a543157b 209 return U2;
Mirjam 17:e5d9a543157b 210 }
Mirjam 17:e5d9a543157b 211
Mirjam 17:e5d9a543157b 212 // -----------------------------------------------
Mirjam 17:e5d9a543157b 213 // ------------ RUN MOTORS -----------------------
Mirjam 17:e5d9a543157b 214 // -----------------------------------------------
Mirjam 17:e5d9a543157b 215 void motoraansturing()
Mirjam 17:e5d9a543157b 216 {
Mirjam 17:e5d9a543157b 217 determinedemoset();
Mirjam 17:e5d9a543157b 218 q1_diff = makeAngleq1(setpointx, setpointy);
Mirjam 17:e5d9a543157b 219 q2_diff = makeAngleq2(setpointx, setpointy);
Mirjam 17:e5d9a543157b 220
Mirjam 17:e5d9a543157b 221 theta2 = counts2angle2();
Mirjam 17:e5d9a543157b 222 error2 = q2_diff - theta2;
Mirjam 17:e5d9a543157b 223 theta1 = counts2angle1();
Mirjam 17:e5d9a543157b 224 error1 = q1_diff - theta1; // Setpoint error, te behalen setpoint minus de huidige positie van de as.
Mirjam 17:e5d9a543157b 225
Mirjam 17:e5d9a543157b 226 U1 = PI_controller1(error1); // Voltage dat naar de motor gestuurd wordt.
Mirjam 17:e5d9a543157b 227 U2 = PI_controller2(error2);
Mirjam 17:e5d9a543157b 228
Mirjam 17:e5d9a543157b 229 motor1_pwm.write(fabs(U1)); // Motor aansturen
Mirjam 17:e5d9a543157b 230 directionM1 = U1 > 0.0f; // Richting van de motor bepalen
Mirjam 17:e5d9a543157b 231 motor2_pwm.write(fabs(U2));
Mirjam 17:e5d9a543157b 232 directionM2 = U2 > 0.0f;
Mirjam 17:e5d9a543157b 233 }
Mirjam 17:e5d9a543157b 234
AppelSab 6:a02ad75f0333 235 // ---------------------------------------------------
AppelSab 6:a02ad75f0333 236 // --------STATEMACHINE-------------------------------
AppelSab 6:a02ad75f0333 237 // ---------------------------------------------------
AppelSab 6:a02ad75f0333 238 void ProcessStateMachine(void)
AppelSab 6:a02ad75f0333 239 {
AppelSab 6:a02ad75f0333 240 switch (currentState)
AppelSab 6:a02ad75f0333 241 {
AppelSab 6:a02ad75f0333 242 case WAITING:
Mirjam 17:e5d9a543157b 243 // Description:
Mirjam 17:e5d9a543157b 244 // In this state we do nothing, and wait for a command
Mirjam 17:e5d9a543157b 245
Mirjam 17:e5d9a543157b 246 // Actions
Mirjam 17:e5d9a543157b 247 led_red = 0; led_green = 0; led_blue = 0; // Colouring the led WHITE
Mirjam 17:e5d9a543157b 248
Mirjam 17:e5d9a543157b 249 // State transition logic
Mirjam 17:e5d9a543157b 250 if (button_clbrt_home == 0)
Mirjam 17:e5d9a543157b 251 {
Mirjam 17:e5d9a543157b 252 currentState = MOTOR_ANGLE_CLBRT;
Mirjam 17:e5d9a543157b 253 stateChanged = true;
Mirjam 17:e5d9a543157b 254 pc.printf("Starting Calibration\n\r");
Mirjam 17:e5d9a543157b 255 }
Mirjam 17:e5d9a543157b 256 else if (Fail_button == 0)
Mirjam 17:e5d9a543157b 257 {
Mirjam 17:e5d9a543157b 258 currentState = FAILURE_MODE;
Mirjam 17:e5d9a543157b 259 stateChanged = true;
Mirjam 17:e5d9a543157b 260 }
Mirjam 17:e5d9a543157b 261 break;
AppelSab 6:a02ad75f0333 262
AppelSab 6:a02ad75f0333 263 case MOTOR_ANGLE_CLBRT:
Mirjam 17:e5d9a543157b 264 // Description:
Mirjam 17:e5d9a543157b 265 // In this state the robot moves with low motor PWM to some
Mirjam 17:e5d9a543157b 266 // mechanical limit of motion, in order to calibrate the motors.
Mirjam 17:e5d9a543157b 267
Mirjam 17:e5d9a543157b 268 // Actions
Mirjam 17:e5d9a543157b 269 led_red = 1; led_green = 0; led_blue = 0; // Colouring the led TURQUOISE
Mirjam 17:e5d9a543157b 270 timer.start(); //Start timer to get time in the state "MOTOR_ANGLE_CLRBRT"
Mirjam 17:e5d9a543157b 271 if (stateChanged)
Mirjam 17:e5d9a543157b 272 {
Mirjam 17:e5d9a543157b 273 MotorAngleCalibrate(); // Actuate motor 1 and 2.
Mirjam 17:e5d9a543157b 274 vel_1 = ReadEncoder1(); // Get velocity of motor 1
Mirjam 17:e5d9a543157b 275 vel_2 = ReadEncoder2(); // Get velocity of motor 2
Mirjam 17:e5d9a543157b 276 stateChanged = true; // Keep this loop going, until the transition conditions are satisfied.
Mirjam 17:e5d9a543157b 277 }
Mirjam 17:e5d9a543157b 278
Mirjam 17:e5d9a543157b 279 // State transition logic
Mirjam 17:e5d9a543157b 280 time_in_state = timer.read(); // Determine if this state has run for long enough.
Mirjam 17:e5d9a543157b 281
Mirjam 17:e5d9a543157b 282 if(time_in_state > 2.0f && vel_1 < 1.1f && vel_2 < 1.1f)
Mirjam 17:e5d9a543157b 283 {
Mirjam 17:e5d9a543157b 284 //pc.printf( "Tijd in deze staat = %f \n\r", time_in_state);
Mirjam 17:e5d9a543157b 285 //pc.printf( "Tijd tijdens actions loop (Waarde voor bepalen van snelheid)") = %f \n\r", tijd);
Mirjam 17:e5d9a543157b 286 pc.printf("Motor calibration has ended. \n\r");
Mirjam 17:e5d9a543157b 287 timer.stop(); // Stop timer for this state
Mirjam 17:e5d9a543157b 288 timer.reset(); // Reset timer for this state
Mirjam 17:e5d9a543157b 289 motor1_pwm.write(fabs(0.0)); // Send PWM values to motor
Mirjam 17:e5d9a543157b 290 motor2_pwm.write(fabs(0.0));
Mirjam 17:e5d9a543157b 291 Encoder1.reset(); // Reset Encoders when arrived at zero-position
Mirjam 17:e5d9a543157b 292 Encoder2.reset();
Mirjam 17:e5d9a543157b 293
Mirjam 17:e5d9a543157b 294 currentState = EMG_CLBRT; // Switch to next state (EMG_CLRBRT).
Mirjam 17:e5d9a543157b 295 pc.printf("EMG calibration \r\n");
Mirjam 17:e5d9a543157b 296 stateChanged = true;
Mirjam 17:e5d9a543157b 297 }
Mirjam 17:e5d9a543157b 298 if (Fail_button == 0)
AppelSab 6:a02ad75f0333 299 {
Mirjam 17:e5d9a543157b 300 currentState = FAILURE_MODE;
Mirjam 17:e5d9a543157b 301 stateChanged = true;
Mirjam 17:e5d9a543157b 302 }
Mirjam 17:e5d9a543157b 303 break;
AppelSab 6:a02ad75f0333 304
AppelSab 6:a02ad75f0333 305 case EMG_CLBRT:
Mirjam 17:e5d9a543157b 306 // In this state the person whom is connected to the robot needs
Mirjam 17:e5d9a543157b 307 // to flex his/her muscles as hard as possible, in order to
Mirjam 17:e5d9a543157b 308 // measure the maximum EMG-signal, which can be used to scale
Mirjam 17:e5d9a543157b 309 // the EMG-filter.
Mirjam 18:f36ac3ee081a 310 led_red = 1; led_green = 1; led_blue = 0; // Colouring the led BLUE
Mirjam 18:f36ac3ee081a 311 for (int i = 0; i <= 10; i++) //10 measuring points
Mirjam 18:f36ac3ee081a 312 {
Mirjam 18:f36ac3ee081a 313 if (emg1_cal > EMG_calibrated_max_1){
Mirjam 18:f36ac3ee081a 314 EMG_calibrated_max_1 = emg1_cal;}
AppelSab 6:a02ad75f0333 315
Mirjam 18:f36ac3ee081a 316 if (emg2_cal > EMG_calibrated_max_2){
Mirjam 18:f36ac3ee081a 317 EMG_calibrated_max_2 = emg2_cal;}
Mirjam 18:f36ac3ee081a 318
Mirjam 18:f36ac3ee081a 319 //pc.printf("EMG1 = %f, EMG2 = %f \r\n",EMG_calibrated_max_1,EMG_calibrated_max_2);
Mirjam 18:f36ac3ee081a 320 wait(0.5f);
Mirjam 18:f36ac3ee081a 321 }
Duif 10:3f93fdb90c29 322
Mirjam 17:e5d9a543157b 323 currentState = HOMING;
Mirjam 17:e5d9a543157b 324 if (Fail_button == 0)
Mirjam 17:e5d9a543157b 325 {
Mirjam 17:e5d9a543157b 326 currentState = FAILURE_MODE;
Mirjam 17:e5d9a543157b 327 stateChanged = true;
Mirjam 17:e5d9a543157b 328 }
Mirjam 17:e5d9a543157b 329 break;
AppelSab 6:a02ad75f0333 330
AppelSab 6:a02ad75f0333 331 case HOMING:
Mirjam 17:e5d9a543157b 332 // Description:
Mirjam 17:e5d9a543157b 333 // Robot moves to the home starting configuration
Mirjam 17:e5d9a543157b 334 pc.printf("HOMING \r\n");
Mirjam 17:e5d9a543157b 335
Mirjam 17:e5d9a543157b 336 led_red = 0; led_green = 1; led_red = 0; // Colouring the led PURPLE
Mirjam 17:e5d9a543157b 337
Mirjam 17:e5d9a543157b 338 // Requirements to move to the next state:
Mirjam 17:e5d9a543157b 339 // If we are in the right location, within some margin, we move to the Waiting for
Mirjam 17:e5d9a543157b 340 // signal state.
AppelSab 6:a02ad75f0333 341
Mirjam 17:e5d9a543157b 342 wait(5.0f); // time_in_this_state > 5.0f
Mirjam 17:e5d9a543157b 343 // INSERT MOVEMENT
Mirjam 17:e5d9a543157b 344 currentState = WAITING4SIGNAL;
Mirjam 17:e5d9a543157b 345 if (Fail_button == 0)
Mirjam 17:e5d9a543157b 346 {
Mirjam 17:e5d9a543157b 347 currentState = FAILURE_MODE;
Mirjam 17:e5d9a543157b 348 stateChanged = true;
Mirjam 17:e5d9a543157b 349 }
Mirjam 17:e5d9a543157b 350 break;
AppelSab 6:a02ad75f0333 351
AppelSab 6:a02ad75f0333 352 case WAITING4SIGNAL:
Mirjam 17:e5d9a543157b 353 // Description:
Mirjam 17:e5d9a543157b 354 // In this state the robot waits for an action to occur.
Mirjam 17:e5d9a543157b 355
Mirjam 17:e5d9a543157b 356 led_red = 0; led_green = 0; led_blue = 0; // Colouring the led WHITE
Mirjam 17:e5d9a543157b 357
Mirjam 17:e5d9a543157b 358 // Requirements to move to the next state:
Mirjam 17:e5d9a543157b 359 // If a certain button is pressed we move to the corresponding
Mirjam 17:e5d9a543157b 360 // state (MOVE_W_DEMO, MOVE_W_EMG or SHUTDOWN)
Mirjam 17:e5d9a543157b 361
Mirjam 17:e5d9a543157b 362 if (button_clbrt_home == 0)
Mirjam 17:e5d9a543157b 363 {
Mirjam 17:e5d9a543157b 364 currentState = MOTOR_ANGLE_CLBRT;
Mirjam 17:e5d9a543157b 365 stateChanged = true;
Mirjam 17:e5d9a543157b 366 pc.printf("Starting Calibration \n\r");
Mirjam 17:e5d9a543157b 367 }
Mirjam 17:e5d9a543157b 368 else if (button_Demo == 1)
Mirjam 17:e5d9a543157b 369 {
Mirjam 17:e5d9a543157b 370 currentState = MOVE_W_DEMO;
Mirjam 17:e5d9a543157b 371 pc.printf("DEMO \r\n");
Mirjam 17:e5d9a543157b 372 wait(1.0f);
Mirjam 17:e5d9a543157b 373 }
Mirjam 17:e5d9a543157b 374 else if (button_Emg == 1)
Mirjam 17:e5d9a543157b 375 {
Mirjam 17:e5d9a543157b 376 currentState = MOVE_W_EMG;
Mirjam 17:e5d9a543157b 377 pc.printf("EMG \r\n");
Mirjam 17:e5d9a543157b 378 wait(1.0f);
Mirjam 17:e5d9a543157b 379 }
Mirjam 17:e5d9a543157b 380 else if (Fail_button == 0)
Mirjam 17:e5d9a543157b 381 {
Mirjam 17:e5d9a543157b 382 currentState = FAILURE_MODE;
Mirjam 17:e5d9a543157b 383 stateChanged = true;
Mirjam 17:e5d9a543157b 384 }
Mirjam 17:e5d9a543157b 385
Mirjam 17:e5d9a543157b 386 break;
AppelSab 6:a02ad75f0333 387
AppelSab 6:a02ad75f0333 388 case MOVE_W_DEMO:
AppelSab 6:a02ad75f0333 389 // Description:
AppelSab 6:a02ad75f0333 390 // In this state the robot follows a preprogrammed shape, e.g.
AppelSab 6:a02ad75f0333 391 // a square.
AppelSab 6:a02ad75f0333 392
AppelSab 6:a02ad75f0333 393 led_red = 1; led_green = 1; led_blue = 0; // Colouring the led GREEN
AppelSab 6:a02ad75f0333 394
AppelSab 6:a02ad75f0333 395 // Requirements to move to the next state:
AppelSab 6:a02ad75f0333 396 // When the home button or the failure button is pressed, we
AppelSab 6:a02ad75f0333 397 // will the move to the corresponding state.
AppelSab 6:a02ad75f0333 398
AppelSab 6:a02ad75f0333 399 // BUILD DEMO MODE
arnouddomhof 9:8b2d6ec577e3 400
arnouddomhof 9:8b2d6ec577e3 401 if (button_clbrt_home == 0)
AppelSab 6:a02ad75f0333 402 {
arnouddomhof 9:8b2d6ec577e3 403 currentState = HOMING;
arnouddomhof 9:8b2d6ec577e3 404 stateChanged = true;
arnouddomhof 9:8b2d6ec577e3 405 pc.printf("Moving home\n\r");
AppelSab 6:a02ad75f0333 406 }
arnouddomhof 9:8b2d6ec577e3 407 else if (Fail_button == 0)
AppelSab 6:a02ad75f0333 408 {
AppelSab 6:a02ad75f0333 409 currentState = FAILURE_MODE;
AppelSab 6:a02ad75f0333 410 stateChanged = true;
AppelSab 6:a02ad75f0333 411 }
AppelSab 6:a02ad75f0333 412 break;
AppelSab 6:a02ad75f0333 413
AppelSab 6:a02ad75f0333 414 case MOVE_W_EMG:
Mirjam 17:e5d9a543157b 415 // Description:
Mirjam 17:e5d9a543157b 416 // In this state the robot will be controlled by use of
Mirjam 17:e5d9a543157b 417 // EMG-signals.
Mirjam 17:e5d9a543157b 418
Mirjam 17:e5d9a543157b 419 led_red = 1; led_green = 0; led_blue = 1; // Colouring the led GREEN
Mirjam 17:e5d9a543157b 420
Mirjam 17:e5d9a543157b 421 if (emg1_filtered >= (threshold_EMG*EMG_calibrated_max_1)){
Mirjam 17:e5d9a543157b 422 need_to_move_1 = 1; // The robot does have to move
Mirjam 17:e5d9a543157b 423 }
Mirjam 17:e5d9a543157b 424 else {
Mirjam 17:e5d9a543157b 425 need_to_move_1 = 0; // If the robot does not have to move
Mirjam 17:e5d9a543157b 426 }
Mirjam 17:e5d9a543157b 427
Mirjam 17:e5d9a543157b 428 if(emg2_filtered >= threshold_EMG*EMG_calibrated_max_2){
Mirjam 17:e5d9a543157b 429 need_to_move_2 = 1;
Mirjam 17:e5d9a543157b 430 }
Mirjam 17:e5d9a543157b 431 else {
Mirjam 17:e5d9a543157b 432 need_to_move_2 = 0;
Mirjam 17:e5d9a543157b 433 }
Mirjam 17:e5d9a543157b 434
Mirjam 17:e5d9a543157b 435
Mirjam 17:e5d9a543157b 436 // Requirements to move to the next state:
Mirjam 17:e5d9a543157b 437 // When the home button or the failure button is pressed, we
Mirjam 17:e5d9a543157b 438 // will the move to the corresponding state.
Mirjam 17:e5d9a543157b 439
Mirjam 17:e5d9a543157b 440 if (button_clbrt_home == 0)
Mirjam 17:e5d9a543157b 441 {
Mirjam 17:e5d9a543157b 442 currentState = MOTOR_ANGLE_CLBRT;
Mirjam 17:e5d9a543157b 443 stateChanged = true;
Mirjam 17:e5d9a543157b 444 pc.printf("Starting Calibration \n\r");
Mirjam 17:e5d9a543157b 445 }
Mirjam 17:e5d9a543157b 446 else if (Fail_button == 0)
Mirjam 17:e5d9a543157b 447 {
Mirjam 17:e5d9a543157b 448 currentState = FAILURE_MODE;
Mirjam 17:e5d9a543157b 449 stateChanged = true;
Mirjam 7:d4090f334ce2 450 }
Mirjam 17:e5d9a543157b 451 break;
AppelSab 6:a02ad75f0333 452
AppelSab 6:a02ad75f0333 453 case FAILURE_MODE:
Mirjam 17:e5d9a543157b 454 // Description:
Mirjam 17:e5d9a543157b 455 // This state is reached when the failure button is reached.
Mirjam 17:e5d9a543157b 456 // In this state everything is turned off.
Mirjam 17:e5d9a543157b 457
Mirjam 17:e5d9a543157b 458 led_red = 0; led_green = 1; led_blue = 1; // Colouring the led RED
Mirjam 17:e5d9a543157b 459 // Actions
Mirjam 17:e5d9a543157b 460 if (stateChanged)
Mirjam 17:e5d9a543157b 461 {
Mirjam 17:e5d9a543157b 462 motor1_pwm.write(fabs(0.0)); // Stop all motors!
Mirjam 17:e5d9a543157b 463 motor2_pwm.write(fabs(0.0));
Mirjam 17:e5d9a543157b 464 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 465 stateChanged = false;
Mirjam 17:e5d9a543157b 466 }
Mirjam 17:e5d9a543157b 467 break;
AppelSab 6:a02ad75f0333 468
AppelSab 6:a02ad75f0333 469 // State transition logic
AppelSab 6:a02ad75f0333 470 // No state transition, you need to restart the robot.
AppelSab 6:a02ad75f0333 471
AppelSab 6:a02ad75f0333 472 default:
AppelSab 6:a02ad75f0333 473 // This state is a default state, this state is reached when
AppelSab 6:a02ad75f0333 474 // the program somehow defies all of the other states.
AppelSab 6:a02ad75f0333 475
AppelSab 6:a02ad75f0333 476 pc.printf("Unknown or unimplemented state reached!!! \n\r");
AppelSab 6:a02ad75f0333 477 led_red = 1; led_green = 1; led_blue = 1; // Colouring the led BLACK
AppelSab 6:a02ad75f0333 478 for (int n = 0; n < 50; n++) // Making an SOS signal with the RED led
AppelSab 6:a02ad75f0333 479 {
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.6f);
AppelSab 6:a02ad75f0333 484 }
AppelSab 6:a02ad75f0333 485 wait(0.4f);
AppelSab 6:a02ad75f0333 486 for (int i = 0 ; i < 6; i++)
AppelSab 6:a02ad75f0333 487 {
AppelSab 6:a02ad75f0333 488 led_red = !led_red;
AppelSab 6:a02ad75f0333 489 wait(0.2f);
AppelSab 6:a02ad75f0333 490 }
AppelSab 6:a02ad75f0333 491 wait(0.4f);
AppelSab 6:a02ad75f0333 492 }
arnouddomhof 3:dca57056e5cb 493 }
AppelSab 6:a02ad75f0333 494 }
AppelSab 6:a02ad75f0333 495
AppelSab 6:a02ad75f0333 496 // --------------------------------
AppelSab 6:a02ad75f0333 497 // ----- MAIN LOOP ----------------
AppelSab 6:a02ad75f0333 498 // --------------------------------
AppelSab 6:a02ad75f0333 499
Mirjam 0:46dbc9b620d8 500 int main()
Mirjam 0:46dbc9b620d8 501 {
Mirjam 4:a0c1c021026b 502 // Switch all LEDs off
arnouddomhof 3:dca57056e5cb 503 led_red = 1;
arnouddomhof 3:dca57056e5cb 504 led_green = 1;
arnouddomhof 3:dca57056e5cb 505 led_blue = 1;
AppelSab 6:a02ad75f0333 506
arnouddomhof 3:dca57056e5cb 507 pc.baud(115200);
arnouddomhof 8:2afb66572fc4 508
arnouddomhof 8:2afb66572fc4 509 pc.printf("\r\n _______________ INSERT ROBOT NAME HERE! _______________ \r\n");
arnouddomhof 8:2afb66572fc4 510 wait(0.5f);
arnouddomhof 8:2afb66572fc4 511 pc.printf("WAITING... \r\n");
arnouddomhof 8:2afb66572fc4 512
AppelSab 6:a02ad75f0333 513 StateMachine.attach(&ProcessStateMachine, 0.005f); // Run statemachine 200 times per second
Mirjam 7:d4090f334ce2 514 sample_EMGtoHIDscope.attach(&sample, 0.02f); // Display EMG values 50 times per second
Mirjam 17:e5d9a543157b 515
Mirjam 0:46dbc9b620d8 516 while (true) {
AppelSab 6:a02ad75f0333 517
AppelSab 6:a02ad75f0333 518 }
AppelSab 6:a02ad75f0333 519 }
AppelSab 6:a02ad75f0333 520
arnouddomhof 5:07e401cb251d 521