2nd draft

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed Servo

Fork of robot_mockup by Martijn Kern

Committer:
Vigilance88
Date:
Tue Oct 27 15:28:31 2015 +0000
Revision:
55:726fdab812a9
Parent:
54:4cda9af56bed
Child:
56:5ff9e5c1ed44
control rate naar 200. doet nog niet echt goed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 0:32bb76391d89 1 #include "mbed.h"
vsluiter 11:ce72ec658a95 2 #include "HIDScope.h"
Vigilance88 18:44905b008f44 3 #include "MODSERIAL.h"
Vigilance88 18:44905b008f44 4 #include "biquadFilter.h"
Vigilance88 54:4cda9af56bed 5 #include "Servo.h"
Vigilance88 18:44905b008f44 6 #include "QEI.h"
Vigilance88 21:d6a46315d5f5 7 #include "math.h"
Vigilance88 26:fe3a5469dd6b 8 #include <string>
Vigilance88 21:d6a46315d5f5 9
Vigilance88 21:d6a46315d5f5 10 /*--------------------------------------------------------------------------------------------------------------------
Vigilance88 21:d6a46315d5f5 11 -------------------------------- BIOROBOTICS GROUP 14 ----------------------------------------------------------------
Vigilance88 21:d6a46315d5f5 12 --------------------------------------------------------------------------------------------------------------------*/
vsluiter 0:32bb76391d89 13
Vigilance88 18:44905b008f44 14 //Define important constants in memory
Vigilance88 21:d6a46315d5f5 15 #define PI 3.14159265
Vigilance88 18:44905b008f44 16 #define SAMPLE_RATE 0.002 //500 Hz EMG sample rate
Vigilance88 55:726fdab812a9 17 #define CONTROL_RATE 0.005 //100 Hz Control rate
Vigilance88 49:6515c045cfd6 18 #define ENCODER_CPR 4200 //both motor encoders have 64 (X4), 32 (X2) counts per revolution of motor shaft
Vigilance88 49:6515c045cfd6 19 //gearbox 1:131.25 -> 4200 counts per revolution of the output shaft (X2),
Vigilance88 26:fe3a5469dd6b 20 #define PWM_PERIOD 0.0001 //10k Hz pwm motor frequency. Higher -> too hot, lower -> motor doesnt respond correctly
Vigilance88 21:d6a46315d5f5 21 /*--------------------------------------------------------------------------------------------------------------------
Vigilance88 21:d6a46315d5f5 22 ---- OBJECTS ---------------------------------------------------------------------------------------------------------
Vigilance88 21:d6a46315d5f5 23 --------------------------------------------------------------------------------------------------------------------*/
Vigilance88 21:d6a46315d5f5 24
Vigilance88 18:44905b008f44 25 MODSERIAL pc(USBTX,USBRX); //serial communication
Vigilance88 18:44905b008f44 26
Vigilance88 36:4d4fc200171b 27 //Debug LEDs
Vigilance88 25:49ccdc98639a 28 DigitalOut red(LED_RED);
Vigilance88 25:49ccdc98639a 29 DigitalOut green(LED_GREEN);
Vigilance88 25:49ccdc98639a 30 DigitalOut blue(LED_BLUE);
Vigilance88 25:49ccdc98639a 31
Vigilance88 21:d6a46315d5f5 32 //EMG shields
Vigilance88 18:44905b008f44 33 AnalogIn emg1(A0); //Analog input - Biceps EMG
Vigilance88 18:44905b008f44 34 AnalogIn emg2(A1); //Analog input - Triceps EMG
Vigilance88 18:44905b008f44 35 AnalogIn emg3(A2); //Analog input - Flexor EMG
Vigilance88 18:44905b008f44 36 AnalogIn emg4(A3); //Analog input - Extensor EMG
Vigilance88 18:44905b008f44 37
Vigilance88 18:44905b008f44 38 Ticker sample_timer; //Ticker for EMG sampling
Vigilance88 18:44905b008f44 39 Ticker control_timer; //Ticker for control loop
Vigilance88 55:726fdab812a9 40 //HIDScope scope(4); //Scope 4 channels
Vigilance88 18:44905b008f44 41
Vigilance88 18:44905b008f44 42 // AnalogIn potmeter(A4); //potmeters
Vigilance88 18:44905b008f44 43 // AnalogIn potmeter2(A5); //
Vigilance88 18:44905b008f44 44
Vigilance88 21:d6a46315d5f5 45 //Encoders
Vigilance88 18:44905b008f44 46 QEI Encoder1(D13,D12,NC,32); //channel A and B from encoder, counts = Encoder.getPulses();
Vigilance88 18:44905b008f44 47 QEI Encoder2(D10,D9,NC,32); //channel A and B from encoder,
Vigilance88 21:d6a46315d5f5 48
Vigilance88 21:d6a46315d5f5 49 //Speed and Direction of motors - D4 (dir) and D5(speed) = motor 2, D7(dir) and D6(speed) = motor 1
Vigilance88 21:d6a46315d5f5 50 PwmOut pwm_motor1(D6); //PWM motor 1
Vigilance88 21:d6a46315d5f5 51 PwmOut pwm_motor2(D5); //PWM motor 2
Vigilance88 53:bf0d97487e84 52 Servo servoPwm(D11); //PWM servomotor
Vigilance88 26:fe3a5469dd6b 53
Vigilance88 18:44905b008f44 54 DigitalOut dir_motor1(D7); //Direction motor 1
Vigilance88 18:44905b008f44 55 DigitalOut dir_motor2(D4); //Direction motor 2
Vigilance88 18:44905b008f44 56
Vigilance88 24:56db31267f10 57 //Limit Switches
Vigilance88 28:743485bb51e4 58 InterruptIn shoulder_limit(D2); //using FRDM buttons
Vigilance88 28:743485bb51e4 59 InterruptIn elbow_limit(D3); //using FRDM buttons
Vigilance88 26:fe3a5469dd6b 60
Vigilance88 26:fe3a5469dd6b 61 //Other buttons
Vigilance88 26:fe3a5469dd6b 62 DigitalIn button1(PTA4); //using FRDM buttons
Vigilance88 26:fe3a5469dd6b 63 DigitalIn button2(PTC6); //using FRDM buttons
Vigilance88 26:fe3a5469dd6b 64
Vigilance88 26:fe3a5469dd6b 65 /*Text colors ASCII code: Set Attribute Mode <ESC>[{attr1};...;{attrn}m
Vigilance88 26:fe3a5469dd6b 66
Vigilance88 26:fe3a5469dd6b 67 \ 0 3 3 - ESC
Vigilance88 26:fe3a5469dd6b 68 [ 3 0 m - black
Vigilance88 26:fe3a5469dd6b 69 [ 3 1 m - red
Vigilance88 26:fe3a5469dd6b 70 [ 3 2 m - green
Vigilance88 26:fe3a5469dd6b 71 [ 3 3 m - yellow
Vigilance88 26:fe3a5469dd6b 72 [ 3 4 m - blue
Vigilance88 26:fe3a5469dd6b 73 [ 3 5 m - magenta
Vigilance88 26:fe3a5469dd6b 74 [ 3 6 m - cyan
Vigilance88 26:fe3a5469dd6b 75 [ 3 7 m - white
Vigilance88 26:fe3a5469dd6b 76 [ 0 m - reset attributes
Vigilance88 26:fe3a5469dd6b 77
Vigilance88 26:fe3a5469dd6b 78 Put the text you want to color between GREEN_ and _GREEN
Vigilance88 26:fe3a5469dd6b 79 */
Vigilance88 26:fe3a5469dd6b 80 string GREEN_ = "\033[32m"; //esc - green
Vigilance88 26:fe3a5469dd6b 81 string _GREEN = "\033[0m"; //esc - reset
Vigilance88 24:56db31267f10 82
Vigilance88 21:d6a46315d5f5 83
Vigilance88 21:d6a46315d5f5 84 /*--------------------------------------------------------------------------------------------------------------------
Vigilance88 21:d6a46315d5f5 85 ---- DECLARE VARIABLES -----------------------------------------------------------------------------------------------
Vigilance88 21:d6a46315d5f5 86 --------------------------------------------------------------------------------------------------------------------*/
Vigilance88 21:d6a46315d5f5 87
Vigilance88 47:c52f9b4c90c4 88 //EMG variables: raw EMG - filtered EMG - maximum voluntary contraction - max amplitude during relaxation.
Vigilance88 38:c8ac615d0c8f 89 double emg_biceps; double biceps_power; double bicepsMVC = 0; double bicepsmin=0;
Vigilance88 38:c8ac615d0c8f 90 double emg_triceps; double triceps_power; double tricepsMVC = 0; double tricepsmin=0;
Vigilance88 38:c8ac615d0c8f 91 double emg_flexor; double flexor_power; double flexorMVC = 0; double flexormin=0;
Vigilance88 38:c8ac615d0c8f 92 double emg_extens; double extens_power; double extensMVC = 0; double extensmin=0;
Vigilance88 47:c52f9b4c90c4 93
Vigilance88 39:e77f844d10d9 94 //Normalize and compare variables
Vigilance88 39:e77f844d10d9 95 double biceps, triceps, flexor, extens; //Storage for normalized emg
Vigilance88 39:e77f844d10d9 96 double xdir, ydir; //EMG reference position directions
Vigilance88 39:e77f844d10d9 97 double xpower, ypower; //EMG reference magnitude
Vigilance88 47:c52f9b4c90c4 98 double dx, dy; //Integral
Vigilance88 47:c52f9b4c90c4 99 double emg_control_time; //Elapsed time in EMG control
Vigilance88 46:c8c5c455dd51 100
Vigilance88 44:145827f5b091 101 //Threshold moving average
Vigilance88 47:c52f9b4c90c4 102 const int window=100; //100 samples
Vigilance88 47:c52f9b4c90c4 103 int i=0; //movavg array index
Vigilance88 47:c52f9b4c90c4 104 double movavg1[window]; //moving average arrays with size of window
Vigilance88 44:145827f5b091 105 double movavg2[window];
Vigilance88 44:145827f5b091 106 double movavg3[window];
Vigilance88 44:145827f5b091 107 double movavg4[window];
Vigilance88 47:c52f9b4c90c4 108 double biceps_avg, triceps_avg,flexor_avg, extens_avg; //sum divided by window size
Vigilance88 46:c8c5c455dd51 109
Vigilance88 36:4d4fc200171b 110 int muscle; //Muscle selector for MVC measurement, 1 = first emg etc.
Vigilance88 47:c52f9b4c90c4 111 double calibrate_time; //Elapsed time for each measurement
Vigilance88 25:49ccdc98639a 112
Vigilance88 24:56db31267f10 113 //PID variables
Vigilance88 36:4d4fc200171b 114 double u1; double u2; //Output of PID controller (PWM value for motor 1 and 2)
Vigilance88 47:c52f9b4c90c4 115 double m1_error=0; double m1_e_int=0; double m1_e_prev=0; //Error, integrated error, previous error motor 1
Vigilance88 55:726fdab812a9 116 const double m1_kp=5; const double m1_ki=0.1; const double m1_kd=0.3; //Proportional, integral and derivative gains.
Vigilance88 24:56db31267f10 117
Vigilance88 47:c52f9b4c90c4 118 double m2_error=0; double m2_e_int=0; double m2_e_prev=0; //Error, integrated error, previous error motor 2
Vigilance88 55:726fdab812a9 119 const double m2_kp=5; const double m2_ki=0.1; const double m2_kd=0.3; //Proportional, integral and derivative gains.
Vigilance88 24:56db31267f10 120
Vigilance88 36:4d4fc200171b 121 //Calibration bools, checks if elbow/shoulder limits are hit
Vigilance88 47:c52f9b4c90c4 122 volatile bool done1 = false;
Vigilance88 47:c52f9b4c90c4 123 volatile bool done2 = false;
Vigilance88 47:c52f9b4c90c4 124 volatile bool calibrating = false;
Vigilance88 32:76c4d7bb2022 125
Vigilance88 24:56db31267f10 126 //highpass filter 20 Hz
Vigilance88 24:56db31267f10 127 const double high_b0 = 0.956543225556877;
Vigilance88 24:56db31267f10 128 const double high_b1 = -1.91308645113754;
Vigilance88 24:56db31267f10 129 const double high_b2 = 0.956543225556877;
Vigilance88 24:56db31267f10 130 const double high_a1 = -1.91197067426073;
Vigilance88 24:56db31267f10 131 const double high_a2 = 0.9149758348014341;
Vigilance88 24:56db31267f10 132
Vigilance88 24:56db31267f10 133 //notchfilter 50Hz
Vigilance88 36:4d4fc200171b 134 /*
Vigilance88 24:56db31267f10 135 Method = Butterworth
Vigilance88 24:56db31267f10 136 Biquad = Yes
Vigilance88 24:56db31267f10 137 Stable = Yes
Vigilance88 24:56db31267f10 138 Sampling Frequency = 500Hz
Vigilance88 24:56db31267f10 139 Filter Order = 2
Vigilance88 24:56db31267f10 140
Vigilance88 24:56db31267f10 141 Band Frequencies (Hz) Att/Ripple (dB) Biquad #1 Biquad #2
Vigilance88 24:56db31267f10 142
Vigilance88 24:56db31267f10 143 1 0, 48 0.001 Gain = 1.000000 Gain = 0.973674
Vigilance88 24:56db31267f10 144 2 49, 51 -60.000 B = [ 1.00000000000, -1.61816176147, 1.00000000000] B = [ 1.00000000000, -1.61816176147, 1.00000000000]
Vigilance88 24:56db31267f10 145 3 52, 250 0.001 A = [ 1.00000000000, -1.58071559235, 0.97319685401] A = [ 1.00000000000, -1.61244708381, 0.97415116257]
Vigilance88 24:56db31267f10 146 */
Vigilance88 24:56db31267f10 147
Vigilance88 24:56db31267f10 148 //biquad 1
Vigilance88 24:56db31267f10 149 const double notch1gain = 1.000000;
Vigilance88 24:56db31267f10 150 const double notch1_b0 = 1;
Vigilance88 24:56db31267f10 151 const double notch1_b1 = -1.61816176147 * notch1gain;
Vigilance88 24:56db31267f10 152 const double notch1_b2 = 1.00000000000 * notch1gain;
Vigilance88 24:56db31267f10 153 const double notch1_a1 = -1.58071559235 * notch1gain;
Vigilance88 24:56db31267f10 154 const double notch1_a2 = 0.97319685401 * notch1gain;
Vigilance88 24:56db31267f10 155
Vigilance88 24:56db31267f10 156 //biquad 2
Vigilance88 24:56db31267f10 157 const double notch2gain = 0.973674;
Vigilance88 24:56db31267f10 158 const double notch2_b0 = 1 * notch2gain;
Vigilance88 24:56db31267f10 159 const double notch2_b1 = -1.61816176147 * notch2gain;
Vigilance88 24:56db31267f10 160 const double notch2_b2 = 1.00000000000 * notch2gain;
Vigilance88 24:56db31267f10 161 const double notch2_a1 = -1.61244708381 * notch2gain;
Vigilance88 24:56db31267f10 162 const double notch2_a2 = 0.97415116257 * notch2gain;
Vigilance88 24:56db31267f10 163
Vigilance88 26:fe3a5469dd6b 164 //lowpass filter 7 Hz - envelope
Vigilance88 24:56db31267f10 165 const double low_b0 = 0.000119046743110057;
Vigilance88 24:56db31267f10 166 const double low_b1 = 0.000238093486220118;
Vigilance88 24:56db31267f10 167 const double low_b2 = 0.000119046743110057;
Vigilance88 24:56db31267f10 168 const double low_a1 = -1.968902268531908;
Vigilance88 24:56db31267f10 169 const double low_a2 = 0.9693784555043481;
Vigilance88 21:d6a46315d5f5 170
Vigilance88 51:e4a0ce7ff4b8 171 //Derivative lowpass filter 60 Hz - remove error noise
Vigilance88 51:e4a0ce7ff4b8 172 const double derlow_b0 = 0.027859766117136;
Vigilance88 51:e4a0ce7ff4b8 173 const double derlow_b1 = 0.0557195322342721;
Vigilance88 51:e4a0ce7ff4b8 174 const double derlow_b2 = 0.027859766117136;
Vigilance88 51:e4a0ce7ff4b8 175 const double derlow_a1 = -1.47548044359265;
Vigilance88 51:e4a0ce7ff4b8 176 const double derlow_a2 = 0.58691950806119;
Vigilance88 51:e4a0ce7ff4b8 177
Vigilance88 36:4d4fc200171b 178 //Forward Kinematics
Vigilance88 36:4d4fc200171b 179 const double l1 = 0.25; const double l2 = 0.25; //Arm lengths
Vigilance88 36:4d4fc200171b 180 double current_x; double current_y; //Current position
Vigilance88 54:4cda9af56bed 181 double theta1; double theta2; double theta3; //Current angles
Vigilance88 54:4cda9af56bed 182 double servopos; //servo position in usec
Vigilance88 36:4d4fc200171b 183 double rpc; //Encoder resolution: radians per count
Vigilance88 36:4d4fc200171b 184
Vigilance88 36:4d4fc200171b 185 //Reference position
Vigilance88 28:743485bb51e4 186 double x; double y;
Vigilance88 36:4d4fc200171b 187
Vigilance88 39:e77f844d10d9 188 //Select whether to use Trig or DLS method, emg true or false
Vigilance88 38:c8ac615d0c8f 189 int control_method;
Vigilance88 39:e77f844d10d9 190 bool emg_control;
Vigilance88 38:c8ac615d0c8f 191
Vigilance88 36:4d4fc200171b 192 //Inverse Kinematics - Trig / Gonio method.
Vigilance88 36:4d4fc200171b 193 //First convert reference position to angle needed, then compare that angle to current angle.
Vigilance88 36:4d4fc200171b 194 double dtheta1; double dtheta2; //reference angles
Vigilance88 36:4d4fc200171b 195 double costheta1; double sintheta1; //helper variables
Vigilance88 36:4d4fc200171b 196 double costheta2; double sintheta2; //
Vigilance88 36:4d4fc200171b 197
Vigilance88 36:4d4fc200171b 198 //Inverse Kinematics - Damped least squares method.
Vigilance88 36:4d4fc200171b 199 //Angle error is directly calculated from position error: dq = [DLS matrix] * position_error
Vigilance88 36:4d4fc200171b 200 // |DLS1 DLS2|
Vigilance88 36:4d4fc200171b 201 double dls1, dls2, dls3, dls4; //DLS matrix: |DLS3 DLS4|
Vigilance88 36:4d4fc200171b 202 double q1_error, q2_error; //Angle errors
Vigilance88 47:c52f9b4c90c4 203 double x_error, y_error; //Position errors
Vigilance88 36:4d4fc200171b 204 double lambda=0.1; //Damping constant
Vigilance88 21:d6a46315d5f5 205
Vigilance88 41:55face19e06b 206 //Mechanical Limits
Vigilance88 41:55face19e06b 207 int theta1_cal, theta2_cal; //Pulse counts at mechanical limits.
Vigilance88 41:55face19e06b 208 double theta1_lower=0.698132, theta1_upper=2.35619; //motor1: lower limit 40 degrees, upper limit 135
Vigilance88 41:55face19e06b 209 double theta2_lower=0.750492, theta2_upper=2.40855; //motor2: lower limit 43 degrees, upper limit 138 degrees.
Vigilance88 41:55face19e06b 210
Vigilance88 21:d6a46315d5f5 211 /*--------------------------------------------------------------------------------------------------------------------
Vigilance88 24:56db31267f10 212 ---- Filters ---------------------------------------------------------------------------------------------------------
Vigilance88 21:d6a46315d5f5 213 --------------------------------------------------------------------------------------------------------------------*/
Vigilance88 24:56db31267f10 214
Vigilance88 24:56db31267f10 215 //Using biquadFilter library
Vigilance88 24:56db31267f10 216 //Syntax: biquadFilter filter(a1, a2, b0, b1, b2); coefficients. Call with: filter.step(u), with u signal to be filtered.
Vigilance88 26:fe3a5469dd6b 217 //Biceps
Vigilance88 24:56db31267f10 218 biquadFilter highpass( high_a1 , high_a2 , high_b0 , high_b1 , high_b2 ); // removes DC and movement artefacts
Vigilance88 24:56db31267f10 219 biquadFilter notch1( notch1_a1 , notch1_a2 , notch1_b0 , notch1_b1 , notch1_b2 ); // removes 49-51 Hz power interference
Vigilance88 24:56db31267f10 220 biquadFilter notch2( notch2_a1 , notch2_a2 , notch2_b0 , notch2_b1 , notch2_b2 ); //
Vigilance88 24:56db31267f10 221 biquadFilter lowpass( low_a1 , low_a2 , low_b0 , low_b1 , low_b2 ); // EMG envelope
Vigilance88 25:49ccdc98639a 222
Vigilance88 26:fe3a5469dd6b 223 //Triceps
Vigilance88 25:49ccdc98639a 224 biquadFilter highpass2( high_a1 , high_a2 , high_b0 , high_b1 , high_b2 ); // removes DC and movement artefacts
Vigilance88 26:fe3a5469dd6b 225 biquadFilter notch1_2( notch1_a1 , notch1_a2 , notch1_b0 , notch1_b1 , notch1_b2 ); // removes 49-51 Hz power interference
Vigilance88 26:fe3a5469dd6b 226 biquadFilter notch2_2( notch2_a1 , notch2_a2 , notch2_b0 , notch2_b1 , notch2_b2 ); //
Vigilance88 25:49ccdc98639a 227 biquadFilter lowpass2( low_a1 , low_a2 , low_b0 , low_b1 , low_b2 ); // EMG envelope
Vigilance88 25:49ccdc98639a 228
Vigilance88 26:fe3a5469dd6b 229 //Flexor
Vigilance88 25:49ccdc98639a 230 biquadFilter highpass3( high_a1 , high_a2 , high_b0 , high_b1 , high_b2 ); // removes DC and movement artefacts
Vigilance88 26:fe3a5469dd6b 231 biquadFilter notch1_3( notch1_a1 , notch1_a2 , notch1_b0 , notch1_b1 , notch1_b2 ); // removes 49-51 Hz power interference
Vigilance88 26:fe3a5469dd6b 232 biquadFilter notch2_3( notch2_a1 , notch2_a2 , notch2_b0 , notch2_b1 , notch2_b2 ); //
Vigilance88 25:49ccdc98639a 233 biquadFilter lowpass3( low_a1 , low_a2 , low_b0 , low_b1 , low_b2 ); // EMG envelope
Vigilance88 25:49ccdc98639a 234
Vigilance88 26:fe3a5469dd6b 235 //Extensor
Vigilance88 25:49ccdc98639a 236 biquadFilter highpass4( high_a1 , high_a2 , high_b0 , high_b1 , high_b2 ); // removes DC and movement artefacts
Vigilance88 26:fe3a5469dd6b 237 biquadFilter notch1_4( notch1_a1 , notch1_a2 , notch1_b0 , notch1_b1 , notch1_b2 ); // removes 49-51 Hz power interference
Vigilance88 26:fe3a5469dd6b 238 biquadFilter notch2_4( notch2_a1 , notch2_a2 , notch2_b0 , notch2_b1 , notch2_b2 ); //
Vigilance88 25:49ccdc98639a 239 biquadFilter lowpass4( low_a1 , low_a2 , low_b0 , low_b1 , low_b2 ); // EMG envelope
Vigilance88 25:49ccdc98639a 240
Vigilance88 51:e4a0ce7ff4b8 241 //PID filter (lowpass 60 Hz)
Vigilance88 51:e4a0ce7ff4b8 242 biquadFilter derfilter1( derlow_a1 , derlow_a2 , derlow_b0 , derlow_b1 , derlow_b2 ); // derivative filter
Vigilance88 51:e4a0ce7ff4b8 243 biquadFilter derfilter2( derlow_a1 , derlow_a2 , derlow_b0 , derlow_b1 , derlow_b2 ); // derivative filter
Vigilance88 40:d62f96ed44c0 244
Vigilance88 24:56db31267f10 245 /*--------------------------------------------------------------------------------------------------------------------
Vigilance88 24:56db31267f10 246 ---- DECLARE FUNCTION NAMES ------------------------------------------------------------------------------------------
Vigilance88 24:56db31267f10 247 --------------------------------------------------------------------------------------------------------------------*/
Vigilance88 26:fe3a5469dd6b 248
Vigilance88 26:fe3a5469dd6b 249 void sample_filter(void); //Sampling and filtering
Vigilance88 26:fe3a5469dd6b 250 void control(); //Control - reference -> error -> pid -> motor output
Vigilance88 37:4d7b7ced20ef 251 void dlscontrol(); //Damped Least Squares method
Vigilance88 37:4d7b7ced20ef 252 void calibrate_emg(); //Instructions + measurement of Min and MVC of each muscle
Vigilance88 26:fe3a5469dd6b 253 void emg_mvc(); //Helper funcion for storing MVC value
Vigilance88 37:4d7b7ced20ef 254 void emg_min(); //Helper function for storing Min value
Vigilance88 26:fe3a5469dd6b 255 void calibrate_arm(void); //Calibration of the arm with limit switches
Vigilance88 26:fe3a5469dd6b 256 void start_sampling(void); //Attaches the sample_filter function to a 500Hz ticker
Vigilance88 26:fe3a5469dd6b 257 void stop_sampling(void); //Stops sample_filter
Vigilance88 26:fe3a5469dd6b 258 void start_control(void); //Attaches the control function to a 100Hz ticker
Vigilance88 26:fe3a5469dd6b 259 void stop_control(void); //Stops control function
Vigilance88 37:4d7b7ced20ef 260
Vigilance88 26:fe3a5469dd6b 261 //Keeps the input between min and max value
Vigilance88 24:56db31267f10 262 void keep_in_range(double * in, double min, double max);
Vigilance88 26:fe3a5469dd6b 263
Vigilance88 26:fe3a5469dd6b 264 //Reusable PID controller, previous and integral error need to be passed by reference
Vigilance88 21:d6a46315d5f5 265 double pid(double error, double kp, double ki, double kd,double &e_int, double &e_prev);
Vigilance88 47:c52f9b4c90c4 266 double pid2(double error, double kp, double ki, double kd,double &e_int, double &e_prev);
Vigilance88 18:44905b008f44 267
Vigilance88 26:fe3a5469dd6b 268 //Menu functions
Vigilance88 21:d6a46315d5f5 269 void mainMenu();
Vigilance88 21:d6a46315d5f5 270 void caliMenu();
Vigilance88 28:743485bb51e4 271 void controlMenu();
Vigilance88 29:948b0b14f6be 272 void controlButtons();
Vigilance88 26:fe3a5469dd6b 273 void clearTerminal();
Vigilance88 28:743485bb51e4 274 void emgInstructions();
Vigilance88 28:743485bb51e4 275 void titleBox();
Vigilance88 26:fe3a5469dd6b 276
Vigilance88 32:76c4d7bb2022 277 //Limit switches - power off motors if switches hit (rising edge interrupt)
Vigilance88 47:c52f9b4c90c4 278 void calibrate(void);
Vigilance88 32:76c4d7bb2022 279 void shoulder();
Vigilance88 32:76c4d7bb2022 280 void elbow();
Vigilance88 21:d6a46315d5f5 281
Vigilance88 21:d6a46315d5f5 282 /*--------------------------------------------------------------------------------------------------------------------
Vigilance88 21:d6a46315d5f5 283 ---- MAIN LOOP -------------------------------------------------------------------------------------------------------
Vigilance88 21:d6a46315d5f5 284 --------------------------------------------------------------------------------------------------------------------*/
Vigilance88 55:726fdab812a9 285 void servotest(){
Vigilance88 55:726fdab812a9 286 //Servo alignment
Vigilance88 55:726fdab812a9 287 theta3 = 1.5*PI - (theta1) - theta2;
Vigilance88 55:726fdab812a9 288 servopos = -(2100/PI)*theta3 + 2700;
Vigilance88 55:726fdab812a9 289 servoPwm.SetPosition(servopos);
Vigilance88 55:726fdab812a9 290 }
Vigilance88 55:726fdab812a9 291 Ticker test;
Vigilance88 21:d6a46315d5f5 292 int main()
Vigilance88 21:d6a46315d5f5 293 {
Vigilance88 29:948b0b14f6be 294 pc.baud(115200); //serial baudrate
Vigilance88 30:a9fdd3202ca2 295 red=1; green=1; blue=1; //Make sure debug LEDs are off
Vigilance88 26:fe3a5469dd6b 296
Vigilance88 53:bf0d97487e84 297 servoPwm.Enable(1650,20000); //Start position servo, PWM period in usecs
Vigilance88 53:bf0d97487e84 298
Vigilance88 50:54f71544964c 299 //theta1_cal = floor(theta1_lower * (4200/(2*PI)));
Vigilance88 50:54f71544964c 300 //Encoder1.setPulses(theta1_cal); //edited QEI library: added setPulses()
Vigilance88 46:c8c5c455dd51 301
Vigilance88 46:c8c5c455dd51 302 //Mechanical limit 43 degrees -> 43*(4200/360) = 350
Vigilance88 50:54f71544964c 303 //theta2_cal = floor(theta2_lower * (4200/(2*PI)));
Vigilance88 50:54f71544964c 304 //Encoder2.setPulses(theta2_cal);
Vigilance88 46:c8c5c455dd51 305
Vigilance88 50:54f71544964c 306 //x = 0.2220;
Vigilance88 50:54f71544964c 307 //y = 0.4088;
Vigilance88 46:c8c5c455dd51 308
Vigilance88 26:fe3a5469dd6b 309 //Set PwmOut frequency to 10k Hz
Vigilance88 42:b9d26b1218b0 310 pwm_motor1.period(0.001);
Vigilance88 42:b9d26b1218b0 311 pwm_motor2.period(0.001);
Vigilance88 26:fe3a5469dd6b 312
Vigilance88 26:fe3a5469dd6b 313 clearTerminal(); //Clear the putty window
Vigilance88 26:fe3a5469dd6b 314
Vigilance88 24:56db31267f10 315 // make a menu, user has to initiate next step
Vigilance88 28:743485bb51e4 316 titleBox();
Vigilance88 26:fe3a5469dd6b 317 mainMenu();
Vigilance88 47:c52f9b4c90c4 318
Vigilance88 32:76c4d7bb2022 319 char command=0;
Vigilance88 27:d1814e271a95 320
Vigilance88 28:743485bb51e4 321 while(command != 'Q' && command != 'q')
Vigilance88 28:743485bb51e4 322 {
Vigilance88 28:743485bb51e4 323 if(pc.readable()){
Vigilance88 28:743485bb51e4 324 command = pc.getc();
Vigilance88 28:743485bb51e4 325
Vigilance88 28:743485bb51e4 326 switch(command){
Vigilance88 28:743485bb51e4 327
Vigilance88 28:743485bb51e4 328 case 'c':
Vigilance88 47:c52f9b4c90c4 329 case 'C': {
Vigilance88 28:743485bb51e4 330 pc.printf("\n\r => You chose calibration.\r\n\n");
Vigilance88 28:743485bb51e4 331 caliMenu();
Vigilance88 28:743485bb51e4 332
Vigilance88 28:743485bb51e4 333 char command2=0;
Vigilance88 28:743485bb51e4 334
Vigilance88 28:743485bb51e4 335 while(command2 != 'B' && command2 != 'b'){
Vigilance88 28:743485bb51e4 336 command2 = pc.getc();
Vigilance88 28:743485bb51e4 337 switch(command2){
Vigilance88 28:743485bb51e4 338 case 'a':
Vigilance88 28:743485bb51e4 339 case 'A':
Vigilance88 28:743485bb51e4 340 pc.printf("\n\r => Arm Calibration Starting... please wait \n\r");
Vigilance88 28:743485bb51e4 341 calibrate_arm();
Vigilance88 28:743485bb51e4 342 wait(1);
Vigilance88 28:743485bb51e4 343 caliMenu();
Vigilance88 28:743485bb51e4 344 break;
Vigilance88 28:743485bb51e4 345
Vigilance88 28:743485bb51e4 346 case 'e':
Vigilance88 28:743485bb51e4 347 case 'E':
Vigilance88 28:743485bb51e4 348 pc.printf("\n\r => EMG Calibration Starting... please wait \n\r");
Vigilance88 28:743485bb51e4 349 wait(1);
Vigilance88 28:743485bb51e4 350 emgInstructions();
Vigilance88 28:743485bb51e4 351 calibrate_emg();
Vigilance88 32:76c4d7bb2022 352 pc.printf("\n\r------------------------- \n\r");
Vigilance88 28:743485bb51e4 353 pc.printf("\n\r EMG Calibration complete \n\r");
Vigilance88 32:76c4d7bb2022 354 pc.printf("\n\r------------------------- \n\r");
Vigilance88 28:743485bb51e4 355 caliMenu();
Vigilance88 28:743485bb51e4 356 break;
Vigilance88 28:743485bb51e4 357
Vigilance88 28:743485bb51e4 358 case 'b':
Vigilance88 28:743485bb51e4 359 case 'B':
Vigilance88 28:743485bb51e4 360 pc.printf("\n\r => Going back to main menu.. \n\r");
Vigilance88 28:743485bb51e4 361 mainMenu();
Vigilance88 28:743485bb51e4 362 break;
Vigilance88 28:743485bb51e4 363 }//end switch
Vigilance88 28:743485bb51e4 364
Vigilance88 28:743485bb51e4 365 }//end while
Vigilance88 28:743485bb51e4 366 break;
Vigilance88 47:c52f9b4c90c4 367 }//end case c C
Vigilance88 35:7d9fca0b1545 368 case 't':
Vigilance88 35:7d9fca0b1545 369 case 'T':
Vigilance88 39:e77f844d10d9 370 pc.printf("=> You chose TRIG button control \r\n\n");
Vigilance88 28:743485bb51e4 371 wait(1);
Vigilance88 28:743485bb51e4 372 start_sampling();
Vigilance88 28:743485bb51e4 373 wait(1);
Vigilance88 39:e77f844d10d9 374 emg_control=false;
Vigilance88 38:c8ac615d0c8f 375 control_method=1;
Vigilance88 28:743485bb51e4 376 start_control();
Vigilance88 28:743485bb51e4 377 wait(1);
Vigilance88 29:948b0b14f6be 378 controlButtons();
Vigilance88 28:743485bb51e4 379 break;
Vigilance88 35:7d9fca0b1545 380 case 'd':
Vigilance88 35:7d9fca0b1545 381 case 'D':
Vigilance88 39:e77f844d10d9 382 pc.printf("=> You chose DLS button control \r\n\n");
Vigilance88 35:7d9fca0b1545 383 wait(1);
Vigilance88 35:7d9fca0b1545 384 start_sampling();
Vigilance88 35:7d9fca0b1545 385 wait(1);
Vigilance88 39:e77f844d10d9 386 emg_control=false;
Vigilance88 38:c8ac615d0c8f 387 control_method=2;
Vigilance88 55:726fdab812a9 388
Vigilance88 55:726fdab812a9 389 test.attach(&servotest,0.02);
Vigilance88 38:c8ac615d0c8f 390 start_control();
Vigilance88 35:7d9fca0b1545 391 wait(1);
Vigilance88 35:7d9fca0b1545 392 controlButtons();
Vigilance88 35:7d9fca0b1545 393 break;
Vigilance88 39:e77f844d10d9 394 case 'e':
Vigilance88 39:e77f844d10d9 395 case 'E':
Vigilance88 39:e77f844d10d9 396 pc.printf("=> You chose EMG DLS control \r\n\n");
Vigilance88 39:e77f844d10d9 397 wait(1);
Vigilance88 39:e77f844d10d9 398 start_sampling();
Vigilance88 47:c52f9b4c90c4 399 wait(1);
Vigilance88 46:c8c5c455dd51 400 emg_control_time = 0;
Vigilance88 39:e77f844d10d9 401 emg_control=true;
Vigilance88 55:726fdab812a9 402 test.attach(&servotest,0.02);
Vigilance88 39:e77f844d10d9 403 control_method=2;
Vigilance88 47:c52f9b4c90c4 404
Vigilance88 39:e77f844d10d9 405 start_control();
Vigilance88 39:e77f844d10d9 406 wait(1);
Vigilance88 39:e77f844d10d9 407 controlButtons();
Vigilance88 39:e77f844d10d9 408 break;
Vigilance88 28:743485bb51e4 409 case 'R':
Vigilance88 28:743485bb51e4 410 red=!red;
Vigilance88 28:743485bb51e4 411 pc.printf("=> Red LED triggered \n\r");
Vigilance88 28:743485bb51e4 412 break;
Vigilance88 28:743485bb51e4 413 case 'G':
Vigilance88 28:743485bb51e4 414 green=!green;
Vigilance88 28:743485bb51e4 415 pc.printf("=> Green LED triggered \n\r");
Vigilance88 28:743485bb51e4 416 break;
Vigilance88 28:743485bb51e4 417 case 'B':
Vigilance88 28:743485bb51e4 418 blue=!blue;
Vigilance88 28:743485bb51e4 419 pc.printf("=> Blue LED triggered \n\r");
Vigilance88 28:743485bb51e4 420 break;
Vigilance88 28:743485bb51e4 421 case 'q':
Vigilance88 28:743485bb51e4 422 case 'Q':
Vigilance88 28:743485bb51e4 423
Vigilance88 28:743485bb51e4 424 break;
Vigilance88 28:743485bb51e4 425 default:
Vigilance88 28:743485bb51e4 426 pc.printf("=> Invalid Input \n\r");
Vigilance88 28:743485bb51e4 427 break;
Vigilance88 28:743485bb51e4 428 } //end switch
Vigilance88 28:743485bb51e4 429 } // end if pc readable
Vigilance88 28:743485bb51e4 430
Vigilance88 28:743485bb51e4 431 } // end while loop
Vigilance88 28:743485bb51e4 432
Vigilance88 28:743485bb51e4 433
Vigilance88 28:743485bb51e4 434
Vigilance88 47:c52f9b4c90c4 435 //When end of while loop reached (user chose quit program).
Vigilance88 28:743485bb51e4 436
Vigilance88 28:743485bb51e4 437 pc.printf("\r\n------------------------------ \n\r");
Vigilance88 28:743485bb51e4 438 pc.printf("Program Offline \n\r");
Vigilance88 28:743485bb51e4 439 pc.printf("Reset to start\r\n");
Vigilance88 28:743485bb51e4 440 pc.printf("------------------------------ \n\r");
Vigilance88 28:743485bb51e4 441 }
Vigilance88 28:743485bb51e4 442 //end of main
Vigilance88 28:743485bb51e4 443
Vigilance88 28:743485bb51e4 444 /*--------------------------------------------------------------------------------------------------------------------
Vigilance88 28:743485bb51e4 445 ---- FUNCTIONS -------------------------------------------------------------------------------------------------------
Vigilance88 28:743485bb51e4 446 --------------------------------------------------------------------------------------------------------------------*/
Vigilance88 28:743485bb51e4 447
Vigilance88 47:c52f9b4c90c4 448 //Use WASD keys to change reference position, x is a and d, y is w and s.
Vigilance88 29:948b0b14f6be 449 void controlButtons()
Vigilance88 28:743485bb51e4 450 {
Vigilance88 28:743485bb51e4 451 controlMenu();
Vigilance88 28:743485bb51e4 452 char c=0;
Vigilance88 28:743485bb51e4 453 while(c != 'Q' && c != 'q') {
Vigilance88 50:54f71544964c 454 //Debugging values:
Vigilance88 50:54f71544964c 455 if(c!='q' && c!='Q'){
Vigilance88 55:726fdab812a9 456 /*pc.printf("Reference position: %f and %f \r\n",x,y);
Vigilance88 50:54f71544964c 457 pc.printf("Current position: %f and %f \r\n",current_x,current_y);
Vigilance88 50:54f71544964c 458 pc.printf("Pos Error: %f and %f \r\n",x_error,y_error);
Vigilance88 50:54f71544964c 459 pc.printf("Current angles: %f and %f \r\n",theta1,theta2);
Vigilance88 50:54f71544964c 460 pc.printf("DLS1: %f and DLS2 %f and DLS3 %f and DLS4: %f \r\n",dls1,dls2,dls3,dls4);
Vigilance88 50:54f71544964c 461 pc.printf("Error in angles: %f and %f \r\n",q1_error,q2_error);
Vigilance88 50:54f71544964c 462 pc.printf("PID output: %f and %f \r\n",u1,u2);
Vigilance88 55:726fdab812a9 463 pc.printf("----------------------------------------\r\n");
Vigilance88 50:54f71544964c 464 pc.printf("Buffer 1: %f \r\n",biceps_avg);
Vigilance88 50:54f71544964c 465 pc.printf("Buffer 2: %f \r\n",triceps_avg);
Vigilance88 50:54f71544964c 466 pc.printf("Buffer 3: %f \r\n",flexor_avg);
Vigilance88 50:54f71544964c 467 pc.printf("Buffer 4: %f \r\n",extens_avg);
Vigilance88 55:726fdab812a9 468 pc.printf("----------------------------------------\r\n");
Vigilance88 55:726fdab812a9 469 pc.printf("Theta 3: %f \r\n",theta3);
Vigilance88 55:726fdab812a9 470 pc.printf("Servoposition (us): %f \r\n",servopos);
Vigilance88 55:726fdab812a9 471 pc.printf("----------------------------------------\r\n");
Vigilance88 55:726fdab812a9 472 wait(1); */
Vigilance88 50:54f71544964c 473 }//end if
Vigilance88 27:d1814e271a95 474
Vigilance88 51:e4a0ce7ff4b8 475 if( pc.readable() ){
Vigilance88 27:d1814e271a95 476 c = pc.getc();
Vigilance88 27:d1814e271a95 477 switch (c)
Vigilance88 27:d1814e271a95 478 {
Vigilance88 38:c8ac615d0c8f 479 case 'd' :
Vigilance88 27:d1814e271a95 480 x = x + 0.01;
Vigilance88 32:76c4d7bb2022 481
Vigilance88 27:d1814e271a95 482 break;
Vigilance88 27:d1814e271a95 483
Vigilance88 38:c8ac615d0c8f 484 case 'a' :
Vigilance88 27:d1814e271a95 485 x-=0.01;
Vigilance88 32:76c4d7bb2022 486
Vigilance88 27:d1814e271a95 487 break;
Vigilance88 27:d1814e271a95 488
Vigilance88 38:c8ac615d0c8f 489 case 'w' :
Vigilance88 27:d1814e271a95 490 y+=0.01;
Vigilance88 32:76c4d7bb2022 491
Vigilance88 27:d1814e271a95 492 break;
Vigilance88 27:d1814e271a95 493
Vigilance88 27:d1814e271a95 494
Vigilance88 38:c8ac615d0c8f 495 case 's' :
Vigilance88 27:d1814e271a95 496 y-=0.01;
Vigilance88 32:76c4d7bb2022 497
Vigilance88 27:d1814e271a95 498 break;
Vigilance88 27:d1814e271a95 499
Vigilance88 27:d1814e271a95 500 case 'q' :
Vigilance88 28:743485bb51e4 501 case 'Q' :
Vigilance88 28:743485bb51e4 502 pc.printf("=> Quitting control... \r\n"); wait(1);
Vigilance88 28:743485bb51e4 503 stop_sampling();
Vigilance88 28:743485bb51e4 504 stop_control();
Vigilance88 28:743485bb51e4 505 pc.printf("Sampling and Control detached \n\r"); wait(1);
Vigilance88 28:743485bb51e4 506 pc.printf("Returning to Main Menu \r\n\n"); wait(1);
Vigilance88 28:743485bb51e4 507 mainMenu();
Vigilance88 47:c52f9b4c90c4 508
Vigilance88 27:d1814e271a95 509 break;
Vigilance88 27:d1814e271a95 510 }//end switch
Vigilance88 55:726fdab812a9 511 /* if(c!='q' && c!='Q'){
Vigilance88 55:726fdab812a9 512 pc.printf("Reference position: %f and %f \r\n",x,y);
Vigilance88 55:726fdab812a9 513 pc.printf("Current position: %f and %f \r\n",current_x,current_y);
Vigilance88 55:726fdab812a9 514 pc.printf("Pos Error: %f and %f \r\n",x_error,y_error);
Vigilance88 55:726fdab812a9 515 pc.printf("Current angles: %f and %f \r\n",theta1,theta2);
Vigilance88 55:726fdab812a9 516 pc.printf("DLS1: %f and DLS2 %f and DLS3 %f and DLS4: %f \r\n",dls1,dls2,dls3,dls4);
Vigilance88 55:726fdab812a9 517 pc.printf("Error in angles: %f and %f \r\n",q1_error,q2_error);
Vigilance88 55:726fdab812a9 518 pc.printf("PID output: %f and %f \r\n",u1,u2);
Vigilance88 55:726fdab812a9 519 pc.printf("----------------------------------------\r\n");
Vigilance88 55:726fdab812a9 520 pc.printf("Buffer 1: %f \r\n",biceps_avg);
Vigilance88 55:726fdab812a9 521 pc.printf("Buffer 2: %f \r\n",triceps_avg);
Vigilance88 55:726fdab812a9 522 pc.printf("Buffer 3: %f \r\n",flexor_avg);
Vigilance88 55:726fdab812a9 523 pc.printf("Buffer 4: %f \r\n",extens_avg);
Vigilance88 55:726fdab812a9 524 pc.printf("----------------------------------------\r\n");
Vigilance88 55:726fdab812a9 525 pc.printf("Theta 3: %f \r\n",theta3);
Vigilance88 55:726fdab812a9 526 pc.printf("Servoposition (us): %f \r\n",servopos);
Vigilance88 55:726fdab812a9 527 pc.printf("----------------------------------------\r\n");
Vigilance88 55:726fdab812a9 528 }*/
Vigilance88 28:743485bb51e4 529 }
Vigilance88 47:c52f9b4c90c4 530 //end if pc readable
Vigilance88 51:e4a0ce7ff4b8 531
Vigilance88 50:54f71544964c 532
Vigilance88 21:d6a46315d5f5 533 }
Vigilance88 21:d6a46315d5f5 534 //end of while loop
Vigilance88 30:a9fdd3202ca2 535 }
Vigilance88 18:44905b008f44 536
Vigilance88 21:d6a46315d5f5 537 //Sample and Filter
Vigilance88 21:d6a46315d5f5 538 void sample_filter(void)
Vigilance88 18:44905b008f44 539 {
Vigilance88 32:76c4d7bb2022 540 emg_biceps = emg1.read(); //Biceps
Vigilance88 32:76c4d7bb2022 541 emg_triceps = emg2.read(); //Triceps
Vigilance88 32:76c4d7bb2022 542 emg_flexor = emg3.read(); //Flexor
Vigilance88 32:76c4d7bb2022 543 emg_extens = emg4.read(); //Extensor
Vigilance88 21:d6a46315d5f5 544
Vigilance88 21:d6a46315d5f5 545 //Filter: high-pass -> notch -> rectify -> lowpass or moving average
Vigilance88 22:1ba637601dc3 546 // Can we use same biquadFilter (eg. highpass) for other muscles or does each muscle need its own biquad?
Vigilance88 25:49ccdc98639a 547 biceps_power = highpass.step(emg_biceps); triceps_power = highpass2.step(emg_triceps); flexor_power = highpass3.step(emg_flexor); extens_power = highpass4.step(emg_extens);
Vigilance88 25:49ccdc98639a 548 biceps_power = notch1.step(biceps_power); triceps_power = notch1_2.step(triceps_power); flexor_power = notch1_3.step(flexor_power); extens_power = notch1_4.step(extens_power);
Vigilance88 25:49ccdc98639a 549 biceps_power = notch2.step(biceps_power); triceps_power = notch2_2.step(triceps_power); flexor_power = notch2_3.step(flexor_power); extens_power = notch2_4.step(extens_power);
Vigilance88 21:d6a46315d5f5 550 biceps_power = abs(biceps_power); triceps_power = abs(triceps_power); flexor_power = abs(flexor_power); extens_power = abs(extens_power);
Vigilance88 25:49ccdc98639a 551 biceps_power = lowpass.step(biceps_power); triceps_power = lowpass2.step(triceps_power); flexor_power = lowpass3.step(flexor_power); extens_power = lowpass4.step(extens_power);
Vigilance88 34:d6ec7c634763 552
Vigilance88 52:8a8c53dc8547 553 //send filtered emg to scope
Vigilance88 55:726fdab812a9 554 /*scope.set(0,biceps_power);
Vigilance88 55:726fdab812a9 555 scope.set(1,triceps_power);
Vigilance88 55:726fdab812a9 556 scope.set(2,flexor_power);
Vigilance88 55:726fdab812a9 557 scope.set(3,extens_power);
Vigilance88 55:726fdab812a9 558 scope.send();
Vigilance88 55:726fdab812a9 559 */
Vigilance88 55:726fdab812a9 560 //send normalized emg to scope
Vigilance88 55:726fdab812a9 561 //scope.set(0,biceps);
Vigilance88 55:726fdab812a9 562 //scope.set(1,triceps);
Vigilance88 55:726fdab812a9 563 //scope.set(2,flexor);
Vigilance88 55:726fdab812a9 564 //scope.set(3,extens);
Vigilance88 50:54f71544964c 565 //scope.send();
Vigilance88 44:145827f5b091 566
Vigilance88 18:44905b008f44 567 }
Vigilance88 18:44905b008f44 568
Vigilance88 47:c52f9b4c90c4 569 //Limit switch - if hit: set pulsecount of encoder to angle of lower mechanical limit
Vigilance88 51:e4a0ce7ff4b8 570 void shoulder()
Vigilance88 51:e4a0ce7ff4b8 571 {
Vigilance88 41:55face19e06b 572 pwm_motor1=0;
Vigilance88 32:76c4d7bb2022 573 done1 = true;
Vigilance88 32:76c4d7bb2022 574 pc.printf("Shoulder Limit hit - shutting down motor 1\r\n");
Vigilance88 36:4d4fc200171b 575 //mechanical angle limits -> pulses. If 40 degrees -> counts = floor( 40 * (4200/360) )
Vigilance88 41:55face19e06b 576 theta1_cal = floor(theta1_lower * (4200/(2*PI)));
Vigilance88 51:e4a0ce7ff4b8 577 Encoder1.setPulses(theta1_cal); //edited QEI library: added setPulses(int p)
Vigilance88 32:76c4d7bb2022 578 }
Vigilance88 32:76c4d7bb2022 579
Vigilance88 32:76c4d7bb2022 580 void elbow(){
Vigilance88 41:55face19e06b 581 pwm_motor2=0;
Vigilance88 32:76c4d7bb2022 582 done2 = true;
Vigilance88 32:76c4d7bb2022 583 pc.printf("Elbow Limit hit - shutting down motor 2\r\n");
Vigilance88 47:c52f9b4c90c4 584
Vigilance88 41:55face19e06b 585 //Mechanical limit 43 degrees -> 43*(4200/360) = 350
Vigilance88 41:55face19e06b 586 theta2_cal = floor(theta2_lower * (4200/(2*PI)));
Vigilance88 51:e4a0ce7ff4b8 587 Encoder2.setPulses(theta2_cal); //edited QEI library: added setPulses(int p)
Vigilance88 32:76c4d7bb2022 588 }
Vigilance88 47:c52f9b4c90c4 589
Vigilance88 47:c52f9b4c90c4 590 //Run motors slowly clockwise to mechanical limit. Attached to 100Hz ticker
Vigilance88 47:c52f9b4c90c4 591 void calibrate(void){
Vigilance88 47:c52f9b4c90c4 592 if(done1==false){ //if motor 1 limit has not been hit yet
Vigilance88 55:726fdab812a9 593 pwm_motor1=0.4; //move upper arm slowly cw
Vigilance88 47:c52f9b4c90c4 594 pc.printf("Motor 1 running %f \r\n");
Vigilance88 47:c52f9b4c90c4 595 }
Vigilance88 47:c52f9b4c90c4 596 if(done1==true && done2==false){ //if limit motor 1 has been hit
Vigilance88 47:c52f9b4c90c4 597 pwm_motor1=0; //stop motor1
Vigilance88 55:726fdab812a9 598 pwm_motor2=0.4; //start moving forearm slowly cw
Vigilance88 47:c52f9b4c90c4 599 pc.printf("Motor 2 running %f \r\n");
Vigilance88 47:c52f9b4c90c4 600 }
Vigilance88 47:c52f9b4c90c4 601 }
Vigilance88 32:76c4d7bb2022 602
Vigilance88 18:44905b008f44 603 //Send arm to mechanical limits, and set encoder to 0. Then send arm to starting position.
Vigilance88 19:0a3ee31dcdb4 604 void calibrate_arm(void)
Vigilance88 19:0a3ee31dcdb4 605 {
Vigilance88 28:743485bb51e4 606 pc.printf("Calibrate_arm() entered\r\n");
Vigilance88 47:c52f9b4c90c4 607
Vigilance88 47:c52f9b4c90c4 608 calibrating = true;
Vigilance88 32:76c4d7bb2022 609 done1 = false;
Vigilance88 32:76c4d7bb2022 610 done2 = false;
Vigilance88 32:76c4d7bb2022 611
Vigilance88 27:d1814e271a95 612 pc.printf("To start arm calibration, press any key =>");
Vigilance88 47:c52f9b4c90c4 613 pc.getc();
Vigilance88 27:d1814e271a95 614 pc.printf("\r\n Calibrating... \r\n");
Vigilance88 47:c52f9b4c90c4 615 red=0; blue=0; //Debug light is purple during arm calibration
Vigilance88 47:c52f9b4c90c4 616
Vigilance88 36:4d4fc200171b 617 dir_motor1=0; //cw
Vigilance88 36:4d4fc200171b 618 dir_motor2=1; //cw
Vigilance88 36:4d4fc200171b 619
Vigilance88 47:c52f9b4c90c4 620 control_timer.attach(&calibrate,CONTROL_RATE);
Vigilance88 26:fe3a5469dd6b 621
Vigilance88 26:fe3a5469dd6b 622 while(calibrating){
Vigilance88 47:c52f9b4c90c4 623 shoulder_limit.fall(&shoulder);
Vigilance88 47:c52f9b4c90c4 624 elbow_limit.fall(&elbow);
Vigilance88 47:c52f9b4c90c4 625 if(done1 && done2){
Vigilance88 47:c52f9b4c90c4 626 pwm_motor2=0;
Vigilance88 47:c52f9b4c90c4 627 control_timer.detach(); //Leave while loop when both limits are reached
Vigilance88 47:c52f9b4c90c4 628 red=1; blue=1; //Turn debug light off when calibration complete
Vigilance88 47:c52f9b4c90c4 629 //set reference position to mechanical limits
Vigilance88 47:c52f9b4c90c4 630 calibrating=false;
Vigilance88 27:d1814e271a95 631
Vigilance88 47:c52f9b4c90c4 632 x = l1 * cos(theta1_lower) + l2 * cos(theta1_lower + theta2_lower);
Vigilance88 47:c52f9b4c90c4 633 y = l1 * sin(theta1_lower) + l2 * sin(theta1_lower + theta2_lower);
Vigilance88 47:c52f9b4c90c4 634 //x = 0.2220;
Vigilance88 47:c52f9b4c90c4 635 //y = 0.4088;
Vigilance88 47:c52f9b4c90c4 636 }
Vigilance88 47:c52f9b4c90c4 637 }
Vigilance88 47:c52f9b4c90c4 638 pc.printf("Current pulsecount motor 1: %i, motor 2: %i \r\n",Encoder1.getPulses(),Encoder2.getPulses());
Vigilance88 47:c52f9b4c90c4 639 pc.printf("Current reference. X: %f, Y: %f \r\n",x,y);
Vigilance88 47:c52f9b4c90c4 640 wait(1);
Vigilance88 47:c52f9b4c90c4 641 pc.printf("\n\r-------------------------- \n\r");
Vigilance88 47:c52f9b4c90c4 642 pc.printf(" Arm Calibration Complete\r\n");
Vigilance88 47:c52f9b4c90c4 643 pc.printf("-------------------------- \n\r");
Vigilance88 47:c52f9b4c90c4 644
Vigilance88 19:0a3ee31dcdb4 645 }
Vigilance88 19:0a3ee31dcdb4 646
Vigilance88 21:d6a46315d5f5 647 //EMG Maximum Voluntary Contraction measurement
Vigilance88 25:49ccdc98639a 648 void emg_mvc()
Vigilance88 25:49ccdc98639a 649 {
Vigilance88 24:56db31267f10 650 if(muscle==1){
Vigilance88 24:56db31267f10 651
Vigilance88 24:56db31267f10 652 if(biceps_power>bicepsMVC){
Vigilance88 26:fe3a5469dd6b 653 //printf("+ ");
Vigilance88 26:fe3a5469dd6b 654 printf("%s+ %s",GREEN_,_GREEN);
Vigilance88 21:d6a46315d5f5 655 bicepsMVC=biceps_power;
Vigilance88 24:56db31267f10 656 }
Vigilance88 25:49ccdc98639a 657 else
Vigilance88 25:49ccdc98639a 658 printf("- ");
Vigilance88 24:56db31267f10 659 }
Vigilance88 24:56db31267f10 660
Vigilance88 24:56db31267f10 661 if(muscle==2){
Vigilance88 24:56db31267f10 662
Vigilance88 24:56db31267f10 663 if(triceps_power>tricepsMVC){
Vigilance88 26:fe3a5469dd6b 664 printf("%s+ %s",GREEN_,_GREEN);
Vigilance88 24:56db31267f10 665 tricepsMVC=triceps_power;
Vigilance88 24:56db31267f10 666 }
Vigilance88 26:fe3a5469dd6b 667 else
Vigilance88 26:fe3a5469dd6b 668 printf("- ");
Vigilance88 24:56db31267f10 669 }
Vigilance88 24:56db31267f10 670
Vigilance88 24:56db31267f10 671 if(muscle==3){
Vigilance88 24:56db31267f10 672
Vigilance88 24:56db31267f10 673 if(flexor_power>flexorMVC){
Vigilance88 26:fe3a5469dd6b 674 printf("%s+ %s",GREEN_,_GREEN);
Vigilance88 24:56db31267f10 675 flexorMVC=flexor_power;
Vigilance88 24:56db31267f10 676 }
Vigilance88 26:fe3a5469dd6b 677 else
Vigilance88 26:fe3a5469dd6b 678 printf("- ");
Vigilance88 24:56db31267f10 679 }
Vigilance88 24:56db31267f10 680
Vigilance88 24:56db31267f10 681 if(muscle==4){
Vigilance88 24:56db31267f10 682
Vigilance88 24:56db31267f10 683 if(extens_power>extensMVC){
Vigilance88 26:fe3a5469dd6b 684 printf("%s+ %s",GREEN_,_GREEN);
Vigilance88 24:56db31267f10 685 extensMVC=extens_power;
Vigilance88 24:56db31267f10 686 }
Vigilance88 26:fe3a5469dd6b 687 else
Vigilance88 26:fe3a5469dd6b 688 printf("- ");
Vigilance88 24:56db31267f10 689 }
Vigilance88 25:49ccdc98639a 690
Vigilance88 25:49ccdc98639a 691 //}
Vigilance88 25:49ccdc98639a 692 calibrate_time = calibrate_time + 0.002;
Vigilance88 36:4d4fc200171b 693
Vigilance88 25:49ccdc98639a 694 }
Vigilance88 25:49ccdc98639a 695
Vigilance88 35:7d9fca0b1545 696 void emg_min()
Vigilance88 48:a1f97eb8c020 697 {
Vigilance88 38:c8ac615d0c8f 698 if(biceps_power>bicepsmin){
Vigilance88 35:7d9fca0b1545 699 bicepsmin=biceps_power;
Vigilance88 35:7d9fca0b1545 700 }
Vigilance88 35:7d9fca0b1545 701
Vigilance88 38:c8ac615d0c8f 702 if(triceps_power>tricepsmin){
Vigilance88 35:7d9fca0b1545 703 tricepsmin=triceps_power;
Vigilance88 35:7d9fca0b1545 704 }
Vigilance88 35:7d9fca0b1545 705
Vigilance88 38:c8ac615d0c8f 706 if(flexor_power>flexormin){
Vigilance88 35:7d9fca0b1545 707 flexormin=flexor_power;
Vigilance88 35:7d9fca0b1545 708 }
Vigilance88 35:7d9fca0b1545 709
Vigilance88 38:c8ac615d0c8f 710 if(extens_power > extensmin){
Vigilance88 35:7d9fca0b1545 711 extensmin = extens_power;
Vigilance88 35:7d9fca0b1545 712 }
Vigilance88 35:7d9fca0b1545 713
Vigilance88 35:7d9fca0b1545 714 calibrate_time = calibrate_time + 0.002;
Vigilance88 35:7d9fca0b1545 715
Vigilance88 35:7d9fca0b1545 716 }
Vigilance88 35:7d9fca0b1545 717
Vigilance88 25:49ccdc98639a 718 //EMG calibration
Vigilance88 25:49ccdc98639a 719 void calibrate_emg()
Vigilance88 25:49ccdc98639a 720 {
Vigilance88 25:49ccdc98639a 721 Ticker timer;
Vigilance88 25:49ccdc98639a 722
Vigilance88 38:c8ac615d0c8f 723 pc.printf("Starting sampling, to allow hidscope to normalize\r\n");
Vigilance88 38:c8ac615d0c8f 724 start_sampling();
Vigilance88 25:49ccdc98639a 725 wait(1);
Vigilance88 48:a1f97eb8c020 726
Vigilance88 48:a1f97eb8c020 727 /******************* All muscles: minimum measurement *************************/
Vigilance88 48:a1f97eb8c020 728 pc.printf("Start of minimum measurement, relax all muscles.\r\n");
Vigilance88 35:7d9fca0b1545 729 wait(1);
Vigilance88 35:7d9fca0b1545 730 pc.printf(" Press any key to begin... "); wait(1);
Vigilance88 35:7d9fca0b1545 731 char input;
Vigilance88 35:7d9fca0b1545 732 input=pc.getc();
Vigilance88 35:7d9fca0b1545 733 pc.printf(" \r\n Starting in 3... \r\n"); wait(1);
Vigilance88 35:7d9fca0b1545 734 pc.printf(" \r\n Starting in 2... \r\n"); wait(1);
Vigilance88 35:7d9fca0b1545 735 pc.printf(" \r\n Starting in 1... \r\n"); wait(1);
Vigilance88 35:7d9fca0b1545 736
Vigilance88 35:7d9fca0b1545 737 timer.attach(&emg_min,SAMPLE_RATE);
Vigilance88 35:7d9fca0b1545 738 wait(5);
Vigilance88 35:7d9fca0b1545 739 timer.detach();
Vigilance88 35:7d9fca0b1545 740 pc.printf("\r\n Measurement complete."); wait(1);
Vigilance88 35:7d9fca0b1545 741 pc.printf("\r\n Biceps min = %f \r\n",bicepsmin); wait(1);
Vigilance88 35:7d9fca0b1545 742 pc.printf("\r\n Triceps min = %f \r\n",tricepsmin); wait(1);
Vigilance88 35:7d9fca0b1545 743 pc.printf("\r\n Flexor min = %f \r\n",flexormin); wait(1);
Vigilance88 35:7d9fca0b1545 744 pc.printf("\r\n Extensor min = %f \r\n",extensmin); wait(1);
Vigilance88 48:a1f97eb8c020 745 /******************************** Done ****************************************/
Vigilance88 35:7d9fca0b1545 746
Vigilance88 48:a1f97eb8c020 747 pc.printf("\r\n Now we will measure maximum amplitudes \r\n"); wait(1);
Vigilance88 25:49ccdc98639a 748 pc.printf("+ means current sample is higher than stored MVC\r\n");
Vigilance88 25:49ccdc98639a 749 pc.printf("- means current sample is lower than stored MVC\r\n");
Vigilance88 48:a1f97eb8c020 750 wait(1);
Vigilance88 48:a1f97eb8c020 751 calibrate_time=0;
Vigilance88 48:a1f97eb8c020 752
Vigilance88 48:a1f97eb8c020 753 /********************* 1st channel: MVC measurement ***************************/
Vigilance88 28:743485bb51e4 754 pc.printf("\r\n----------------\r\n ");
Vigilance88 28:743485bb51e4 755 pc.printf(" Biceps is first.\r\n ");
Vigilance88 28:743485bb51e4 756 pc.printf("----------------\r\n ");
Vigilance88 28:743485bb51e4 757 wait(1);
Vigilance88 25:49ccdc98639a 758 pc.printf(" Press any key to begin... "); wait(1);
Vigilance88 25:49ccdc98639a 759 input=pc.getc();
Vigilance88 25:49ccdc98639a 760 pc.putc(input);
Vigilance88 25:49ccdc98639a 761 pc.printf(" \r\n Starting in 3... \r\n"); wait(1);
Vigilance88 25:49ccdc98639a 762 pc.printf(" \r\n Starting in 2... \r\n"); wait(1);
Vigilance88 25:49ccdc98639a 763 pc.printf(" \r\n Starting in 1... \r\n"); wait(1);
Vigilance88 25:49ccdc98639a 764
Vigilance88 25:49ccdc98639a 765 muscle=1;
Vigilance88 27:d1814e271a95 766 timer.attach(&emg_mvc,SAMPLE_RATE);
Vigilance88 25:49ccdc98639a 767 wait(5);
Vigilance88 25:49ccdc98639a 768 timer.detach();
Vigilance88 26:fe3a5469dd6b 769
Vigilance88 26:fe3a5469dd6b 770 pc.printf("\r\n Measurement complete."); wait(1);
Vigilance88 26:fe3a5469dd6b 771 pc.printf("\r\n Biceps MVC = %f \r\n",bicepsMVC); wait(1);
Vigilance88 26:fe3a5469dd6b 772 pc.printf("Calibrate_emg() exited \r\n"); wait(1);
Vigilance88 26:fe3a5469dd6b 773 pc.printf("Measured time: %f seconds \r\n\n",calibrate_time);
Vigilance88 25:49ccdc98639a 774 calibrate_time=0;
Vigilance88 48:a1f97eb8c020 775 /******************************** Done ****************************************/
Vigilance88 25:49ccdc98639a 776
Vigilance88 48:a1f97eb8c020 777 /********************* 2nd channel: MVC measurement ***************************/
Vigilance88 26:fe3a5469dd6b 778 muscle=2;
Vigilance88 28:743485bb51e4 779 pc.printf("\r\n----------------\r\n ");
Vigilance88 28:743485bb51e4 780 pc.printf(" Triceps is next.\r\n ");
Vigilance88 28:743485bb51e4 781 pc.printf("----------------\r\n ");
Vigilance88 28:743485bb51e4 782 wait(1);
Vigilance88 28:743485bb51e4 783
Vigilance88 25:49ccdc98639a 784 pc.printf(" Press any key to begin... "); wait(1);
Vigilance88 25:49ccdc98639a 785 input=pc.getc();
Vigilance88 25:49ccdc98639a 786 pc.putc(input);
Vigilance88 25:49ccdc98639a 787 pc.printf(" \r\n Starting in 3... \r\n"); wait(1);
Vigilance88 25:49ccdc98639a 788 pc.printf(" \r\n Starting in 2... \r\n"); wait(1);
Vigilance88 25:49ccdc98639a 789 pc.printf(" \r\n Starting in 1... \r\n"); wait(1);
Vigilance88 48:a1f97eb8c020 790
Vigilance88 25:49ccdc98639a 791 timer.attach(&emg_mvc,0.002);
Vigilance88 25:49ccdc98639a 792 wait(5);
Vigilance88 25:49ccdc98639a 793 timer.detach();
Vigilance88 25:49ccdc98639a 794 pc.printf("\r\n Triceps MVC = %f \r\n",tricepsMVC);
Vigilance88 25:49ccdc98639a 795
Vigilance88 25:49ccdc98639a 796 pc.printf("Calibrate_emg() exited \r\n");
Vigilance88 25:49ccdc98639a 797 pc.printf("Measured time: %f seconds \r\n",calibrate_time);
Vigilance88 25:49ccdc98639a 798 calibrate_time=0;
Vigilance88 48:a1f97eb8c020 799 /******************************** Done ****************************************/
Vigilance88 48:a1f97eb8c020 800
Vigilance88 48:a1f97eb8c020 801 /********************* 3rd channel: MVC measurement ***************************/
Vigilance88 26:fe3a5469dd6b 802 muscle=3;
Vigilance88 35:7d9fca0b1545 803 pc.printf("\r\n----------------\r\n ");
Vigilance88 35:7d9fca0b1545 804 pc.printf(" Flexor is next.\r\n ");
Vigilance88 35:7d9fca0b1545 805 pc.printf("----------------\r\n ");
Vigilance88 35:7d9fca0b1545 806 wait(1);
Vigilance88 35:7d9fca0b1545 807
Vigilance88 35:7d9fca0b1545 808 pc.printf(" Press any key to begin... "); wait(1);
Vigilance88 35:7d9fca0b1545 809 input=pc.getc();
Vigilance88 35:7d9fca0b1545 810 pc.putc(input);
Vigilance88 35:7d9fca0b1545 811 pc.printf(" \r\n Starting in 3... \r\n"); wait(1);
Vigilance88 35:7d9fca0b1545 812 pc.printf(" \r\n Starting in 2... \r\n"); wait(1);
Vigilance88 35:7d9fca0b1545 813 pc.printf(" \r\n Starting in 1... \r\n"); wait(1);
Vigilance88 48:a1f97eb8c020 814
Vigilance88 35:7d9fca0b1545 815 timer.attach(&emg_mvc,0.002);
Vigilance88 35:7d9fca0b1545 816 wait(5);
Vigilance88 35:7d9fca0b1545 817 timer.detach();
Vigilance88 35:7d9fca0b1545 818 pc.printf("\r\n Flexor MVC = %f \r\n",flexorMVC);
Vigilance88 35:7d9fca0b1545 819
Vigilance88 35:7d9fca0b1545 820 pc.printf("Calibrate_emg() exited \r\n");
Vigilance88 35:7d9fca0b1545 821 pc.printf("Measured time: %f seconds \r\n",calibrate_time);
Vigilance88 35:7d9fca0b1545 822 calibrate_time=0;
Vigilance88 48:a1f97eb8c020 823 /******************************** Done ****************************************/
Vigilance88 35:7d9fca0b1545 824
Vigilance88 48:a1f97eb8c020 825 /********************* 4th channel: MVC measurement ***************************/
Vigilance88 26:fe3a5469dd6b 826 muscle=4;
Vigilance88 35:7d9fca0b1545 827 pc.printf("\r\n----------------\r\n ");
Vigilance88 35:7d9fca0b1545 828 pc.printf(" Extensor is next.\r\n ");
Vigilance88 35:7d9fca0b1545 829 pc.printf("----------------\r\n ");
Vigilance88 35:7d9fca0b1545 830 wait(1);
Vigilance88 35:7d9fca0b1545 831
Vigilance88 35:7d9fca0b1545 832 pc.printf(" Press any key to begin... "); wait(1);
Vigilance88 35:7d9fca0b1545 833 input=pc.getc();
Vigilance88 35:7d9fca0b1545 834 pc.putc(input);
Vigilance88 35:7d9fca0b1545 835 pc.printf(" \r\n Starting in 3... \r\n"); wait(1);
Vigilance88 35:7d9fca0b1545 836 pc.printf(" \r\n Starting in 2... \r\n"); wait(1);
Vigilance88 35:7d9fca0b1545 837 pc.printf(" \r\n Starting in 1... \r\n"); wait(1);
Vigilance88 48:a1f97eb8c020 838
Vigilance88 35:7d9fca0b1545 839 timer.attach(&emg_mvc,0.002);
Vigilance88 35:7d9fca0b1545 840 wait(5);
Vigilance88 35:7d9fca0b1545 841 timer.detach();
Vigilance88 35:7d9fca0b1545 842 pc.printf("\r\n Extensor MVC = %f \r\n",extensMVC);
Vigilance88 25:49ccdc98639a 843
Vigilance88 35:7d9fca0b1545 844 pc.printf("Calibrate_emg() exited \r\n");
Vigilance88 35:7d9fca0b1545 845 pc.printf("Measured time: %f seconds \r\n",calibrate_time);
Vigilance88 35:7d9fca0b1545 846 calibrate_time=0;
Vigilance88 48:a1f97eb8c020 847 /******************************** Done ****************************************/
Vigilance88 48:a1f97eb8c020 848
Vigilance88 48:a1f97eb8c020 849 //Stop sampling: detach ticker
Vigilance88 25:49ccdc98639a 850 stop_sampling();
Vigilance88 24:56db31267f10 851
Vigilance88 18:44905b008f44 852 }
Vigilance88 18:44905b008f44 853
Vigilance88 18:44905b008f44 854
Vigilance88 48:a1f97eb8c020 855 //PID motor 1 - Input error and Kp, Kd, Ki, output control signal
Vigilance88 20:0ede3818e08e 856 double pid(double error, double kp, double ki, double kd,double &e_int, double &e_prev)
vsluiter 2:e314bb3b2d99 857 {
Vigilance88 20:0ede3818e08e 858 // Derivative
Vigilance88 24:56db31267f10 859 double e_der = (error-e_prev)/ CONTROL_RATE;
Vigilance88 48:a1f97eb8c020 860 e_der = derfilter1.step(e_der); //derfilter1 - seperate 7hz low-pass biquad for this PID
Vigilance88 21:d6a46315d5f5 861 e_prev = error;
Vigilance88 20:0ede3818e08e 862 // Integral
Vigilance88 24:56db31267f10 863 e_int = e_int + CONTROL_RATE * error;
Vigilance88 20:0ede3818e08e 864 // PID
Vigilance88 21:d6a46315d5f5 865 return kp*error + ki*e_int + kd * e_der;
Vigilance88 20:0ede3818e08e 866
Vigilance88 18:44905b008f44 867 }
Vigilance88 18:44905b008f44 868
Vigilance88 48:a1f97eb8c020 869 //PID for motor 2 - needed because of biquadfilter memory variables?
Vigilance88 46:c8c5c455dd51 870 double pid2(double error, double kp, double ki, double kd,double &e_int, double &e_prev)
Vigilance88 46:c8c5c455dd51 871 {
Vigilance88 46:c8c5c455dd51 872 // Derivative
Vigilance88 46:c8c5c455dd51 873 double e_der = (error-e_prev)/ CONTROL_RATE;
Vigilance88 48:a1f97eb8c020 874 e_der = derfilter2.step(e_der); //derfilter2 - seperate 7hz low-pass biquad for this PID
Vigilance88 46:c8c5c455dd51 875 e_prev = error;
Vigilance88 46:c8c5c455dd51 876 // Integral
Vigilance88 46:c8c5c455dd51 877 e_int = e_int + CONTROL_RATE * error;
Vigilance88 46:c8c5c455dd51 878 // PID
Vigilance88 46:c8c5c455dd51 879 return kp*error + ki*e_int + kd * e_der;
Vigilance88 46:c8c5c455dd51 880
Vigilance88 46:c8c5c455dd51 881 }
Vigilance88 46:c8c5c455dd51 882
Vigilance88 46:c8c5c455dd51 883
Vigilance88 20:0ede3818e08e 884 //Analyze filtered EMG, calculate reference position from EMG, compare reference position with current position,convert to angles, send error through pid(), send PWM and DIR to motors
Vigilance88 18:44905b008f44 885 void control()
Vigilance88 50:54f71544964c 886 {
Vigilance88 48:a1f97eb8c020 887
Vigilance88 48:a1f97eb8c020 888 /********************* START OF EMG REFERENCE CALCULATION ***************************/
Vigilance88 46:c8c5c455dd51 889 if(emg_control==true){
Vigilance88 50:54f71544964c 890 //TODO some idle time with static reference before EMG kicks in. or go to reference in the first 5 seconds.
Vigilance88 46:c8c5c455dd51 891 emg_control_time += CONTROL_RATE;
Vigilance88 46:c8c5c455dd51 892 //if(emg_control_time < 5){
Vigilance88 48:a1f97eb8c020 893 // x=BLA; y=BLA; starting position maybe
Vigilance88 46:c8c5c455dd51 894 //}
Vigilance88 48:a1f97eb8c020 895
Vigilance88 30:a9fdd3202ca2 896 //normalize emg to value between 0-1
Vigilance88 38:c8ac615d0c8f 897 biceps = (biceps_power - bicepsmin) / (bicepsMVC - bicepsmin);
Vigilance88 38:c8ac615d0c8f 898 triceps = (triceps_power - tricepsmin) / (tricepsMVC - tricepsmin);
Vigilance88 38:c8ac615d0c8f 899 flexor = (flexor_power - flexormin) / (flexorMVC - flexormin);
Vigilance88 38:c8ac615d0c8f 900 extens = (extens_power - extensmin) / (extensMVC - extensmin);
Vigilance88 39:e77f844d10d9 901 //make sure values stay between 0-1 over time
Vigilance88 39:e77f844d10d9 902 keep_in_range(&biceps,0,1);
Vigilance88 39:e77f844d10d9 903 keep_in_range(&triceps,0,1);
Vigilance88 39:e77f844d10d9 904 keep_in_range(&flexor,0,1);
Vigilance88 39:e77f844d10d9 905 keep_in_range(&extens,0,1);
Vigilance88 39:e77f844d10d9 906
Vigilance88 38:c8ac615d0c8f 907
Vigilance88 52:8a8c53dc8547 908 //threshold detection! buffer or two thresholds? If average of 100 samples > threshold, then muscle considered on.
Vigilance88 44:145827f5b091 909 movavg1[i]=biceps; //fill array with 100 normalized samples
Vigilance88 44:145827f5b091 910 movavg2[i]=triceps;
Vigilance88 44:145827f5b091 911 movavg3[i]=flexor;
Vigilance88 44:145827f5b091 912 movavg4[i]=extens;
Vigilance88 44:145827f5b091 913 i++;
Vigilance88 44:145827f5b091 914 if(i==window){ //if array full,set i to 0
Vigilance88 44:145827f5b091 915 i=0;
Vigilance88 44:145827f5b091 916 }
Vigilance88 44:145827f5b091 917
Vigilance88 50:54f71544964c 918
Vigilance88 50:54f71544964c 919 //Sum all values in the array. The sum needs to be overwritten or it will continue to sum the next 100 samples on top it
Vigilance88 50:54f71544964c 920 //and will grow out of control.
Vigilance88 50:54f71544964c 921 //So the variable name for the sum in the for loop is not really correct since the average is calculated after the loop.
Vigilance88 50:54f71544964c 922 for(int j = 0; j < window; j++){
Vigilance88 50:54f71544964c 923 biceps_avg += movavg1[j];
Vigilance88 50:54f71544964c 924 triceps_avg += movavg2[j];
Vigilance88 50:54f71544964c 925 flexor_avg += movavg3[j];
Vigilance88 50:54f71544964c 926 extens_avg += movavg4[j];
Vigilance88 44:145827f5b091 927 }
Vigilance88 50:54f71544964c 928 biceps_avg = biceps_avg/window; //divide sum by number of samples -> average
Vigilance88 50:54f71544964c 929 triceps_avg = triceps_avg/window;
Vigilance88 50:54f71544964c 930 flexor_avg = flexor_avg/window;
Vigilance88 50:54f71544964c 931 extens_avg = extens_avg/window;
Vigilance88 46:c8c5c455dd51 932
Vigilance88 50:54f71544964c 933
Vigilance88 48:a1f97eb8c020 934
Vigilance88 48:a1f97eb8c020 935 //Compare muscle amplitudes and determine their influence on x and y reference position.
Vigilance88 53:bf0d97487e84 936 if (biceps_avg>triceps_avg && biceps_avg > 0.2){
Vigilance88 48:a1f97eb8c020 937 xdir = 0;
Vigilance88 53:bf0d97487e84 938 xpower = biceps_avg;}
Vigilance88 53:bf0d97487e84 939 else if (triceps_avg>biceps_avg && triceps_avg>0.2){
Vigilance88 30:a9fdd3202ca2 940 xdir = 1;
Vigilance88 53:bf0d97487e84 941 xpower = triceps_avg;}
Vigilance88 30:a9fdd3202ca2 942 else
Vigilance88 30:a9fdd3202ca2 943 xpower=0;
Vigilance88 30:a9fdd3202ca2 944
Vigilance88 53:bf0d97487e84 945 if (flexor_avg>extens_avg && flexor_avg > 0.2){
Vigilance88 30:a9fdd3202ca2 946 ydir = 0;
Vigilance88 53:bf0d97487e84 947 ypower = flexor_avg;
Vigilance88 30:a9fdd3202ca2 948 }
Vigilance88 53:bf0d97487e84 949 else if (extens_avg>flexor_avg && extens_avg > 0.2){
Vigilance88 30:a9fdd3202ca2 950 ydir = 1;
Vigilance88 53:bf0d97487e84 951 ypower = extens_avg;
Vigilance88 30:a9fdd3202ca2 952 }
Vigilance88 30:a9fdd3202ca2 953 else
Vigilance88 30:a9fdd3202ca2 954 ypower = 0;
Vigilance88 30:a9fdd3202ca2 955
Vigilance88 38:c8ac615d0c8f 956 //power: the longer a signal is active, the further the reference goes. So integrate to determine reference position
Vigilance88 55:726fdab812a9 957 dx = xpower * CONTROL_RATE * 0.15; //last value is a factor to control how fast the reference (so also end effector) changes
Vigilance88 55:726fdab812a9 958 dy = ypower * CONTROL_RATE * 0.15;
Vigilance88 18:44905b008f44 959
Vigilance88 48:a1f97eb8c020 960 //Direction! Sum dx and dy but make sure xdir and ydir are considered.
Vigilance88 48:a1f97eb8c020 961 if (xdir>0) //if x direction of sample is positive, add it to reference position
Vigilance88 48:a1f97eb8c020 962 x += dx;
Vigilance88 48:a1f97eb8c020 963 else //if x direction of sample is negative, substract it from reference position
Vigilance88 30:a9fdd3202ca2 964 x += -dx;
Vigilance88 30:a9fdd3202ca2 965
Vigilance88 48:a1f97eb8c020 966 if (ydir>0) //if y direction of sample is positive, add it to reference position
Vigilance88 30:a9fdd3202ca2 967 y += dy;
Vigilance88 30:a9fdd3202ca2 968 else
Vigilance88 48:a1f97eb8c020 969 y += -dy; //if y direction of sample is negative, substract it from reference position
Vigilance88 48:a1f97eb8c020 970
Vigilance88 48:a1f97eb8c020 971 //now we have x and y -> reference position.
Vigilance88 30:a9fdd3202ca2 972
Vigilance88 39:e77f844d10d9 973 }//end emg_control if
Vigilance88 48:a1f97eb8c020 974 /******************************** END OF EMG REFERENCE CALCULATION ****************************************/
Vigilance88 48:a1f97eb8c020 975
Vigilance88 30:a9fdd3202ca2 976
Vigilance88 27:d1814e271a95 977 //Current position - Encoder counts -> current angle -> Forward Kinematics
Vigilance88 49:6515c045cfd6 978 rpc=(2*PI)/ENCODER_CPR; //radians per count (resolution) - 2pi divided by 4200
Vigilance88 48:a1f97eb8c020 979 theta1 = Encoder1.getPulses() * rpc; //multiply resolution with number of counts to get current angles
Vigilance88 27:d1814e271a95 980 theta2 = Encoder2.getPulses() * rpc;
Vigilance88 48:a1f97eb8c020 981 current_x = l1 * cos(theta1) + l2 * cos(theta1 + theta2); //Forward kinematics for current position
Vigilance88 27:d1814e271a95 982 current_y = l1 * sin(theta1) + l2 * sin(theta1 + theta2);
Vigilance88 27:d1814e271a95 983
Vigilance88 27:d1814e271a95 984
Vigilance88 48:a1f97eb8c020 985 //calculate error (refpos-currentpos)
Vigilance88 27:d1814e271a95 986 x_error = x - current_x;
Vigilance88 27:d1814e271a95 987 y_error = y - current_y;
Vigilance88 27:d1814e271a95 988
Vigilance88 48:a1f97eb8c020 989 /******************************** START OF TRIG INVERSE KINEMATICS ****************************************/
Vigilance88 38:c8ac615d0c8f 990 if (control_method==1){
Vigilance88 27:d1814e271a95 991 //inverse kinematics (refpos to refangle)
Vigilance88 18:44905b008f44 992
Vigilance88 27:d1814e271a95 993 costheta2 = (pow(x,2) + pow(y,2) - pow(l1,2) - pow(l2,2)) / (2*l1*l2) ;
Vigilance88 50:54f71544964c 994 //absolute in sqrt to avoid imaginary numbers -> bigger steady state error when reference out of workspace
Vigilance88 50:54f71544964c 995 sintheta2 = sqrt( abs( 1 - pow(costheta2,2) ) );
Vigilance88 27:d1814e271a95 996
Vigilance88 48:a1f97eb8c020 997 //Reference angle 2
Vigilance88 27:d1814e271a95 998 dtheta2 = atan2(sintheta2,costheta2);
Vigilance88 27:d1814e271a95 999
Vigilance88 32:76c4d7bb2022 1000 double k1 = l1 + l2*costheta2;
Vigilance88 32:76c4d7bb2022 1001 double k2 = l2*sintheta2;
Vigilance88 32:76c4d7bb2022 1002
Vigilance88 48:a1f97eb8c020 1003 //Reference angle 1
Vigilance88 32:76c4d7bb2022 1004 dtheta1 = atan2(y, x) - atan2(k2, k1);
Vigilance88 32:76c4d7bb2022 1005
Vigilance88 32:76c4d7bb2022 1006 /* alternative:
Vigilance88 27:d1814e271a95 1007 costheta1 = ( x * (l1 + l2 * costheta2) + y * l2 * sintheta2 ) / ( pow(x,2) + pow(y,2) );
Vigilance88 30:a9fdd3202ca2 1008 sintheta1 = sqrt( abs( 1 - pow(costheta1,2) ) );
Vigilance88 27:d1814e271a95 1009
Vigilance88 27:d1814e271a95 1010 dtheta1 = atan2(sintheta1,costheta1);
Vigilance88 32:76c4d7bb2022 1011 */
Vigilance88 27:d1814e271a95 1012
Vigilance88 27:d1814e271a95 1013 //Angle error
Vigilance88 27:d1814e271a95 1014 m1_error = dtheta1-theta1;
Vigilance88 27:d1814e271a95 1015 m2_error = dtheta2-theta2;
Vigilance88 39:e77f844d10d9 1016 }// end control method 1
Vigilance88 48:a1f97eb8c020 1017 /******************************** END OF TRIG INVERSE KINEMATICS ****************************************/
Vigilance88 27:d1814e271a95 1018
Vigilance88 48:a1f97eb8c020 1019
Vigilance88 48:a1f97eb8c020 1020 /******************************** START OF DLS INVERSE KINEMATICS ****************************************/
Vigilance88 38:c8ac615d0c8f 1021 if(control_method==2){
Vigilance88 37:4d7b7ced20ef 1022 //inverse kinematics (error in position to error in angles)
Vigilance88 37:4d7b7ced20ef 1023 dls1= -(l2*pow(lambda,2)*sin(theta1 + theta2) + l1*pow(lambda,2)*sin(theta1) + l1*pow(l2,2)*pow(cos(theta1 + theta2),2)*sin(theta1) - l1*pow(l2,2)*cos(theta1 + theta2)*sin(theta1 + theta2)*cos(theta1))/(pow(lambda,4) + 2*pow(l2,2)*pow(lambda,2)*pow(cos(theta1 + theta2),2) + 2*pow(l2,2)*pow(lambda,2)*pow(sin(theta1 + theta2),2) + pow(l1,2)*pow(lambda,2)*pow(cos(theta1),2) + pow(l1,2)*pow(lambda,2)*pow(sin(theta1),2) + pow(l1,2)*pow(l2,2)*pow(cos(theta1 + theta2),2)*pow(sin(theta1),2) + pow(l1,2)*pow(l2,2)*pow(sin(theta1 + theta2),2)*pow(cos(theta1),2) + 2*l1*l2*pow(lambda,2)*cos(theta1 + theta2)*cos(theta1) + 2*l1*l2*pow(lambda,2)*sin(theta1 + theta2)*sin(theta1) - 2*pow(l1,2)*pow(l2,2)*cos(theta1 + theta2)*sin(theta1 + theta2)*cos(theta1)*sin(theta1));
Vigilance88 37:4d7b7ced20ef 1024 dls2= (l2*pow(lambda,2)*cos(theta1 + theta2) + l1*pow(lambda,2)*cos(theta1) + l1*pow(l2,2)*pow(sin(theta1 + theta2),2)*cos(theta1) - l1*pow(l2,2)*cos(theta1 + theta2)*sin(theta1 + theta2)*sin(theta1))/(pow(lambda,4) + 2*pow(l2,2)*pow(lambda,2)*pow(cos(theta1 + theta2),2) + 2*pow(l2,2)*pow(lambda,2)*pow(sin(theta1 + theta2),2) + pow(l1,2)*pow(lambda,2)*pow(cos(theta1),2) + pow(l1,2)*pow(lambda,2)*pow(sin(theta1),2) + pow(l1,2)*pow(l2,2)*pow(cos(theta1 + theta2),2)*pow(sin(theta1),2) + pow(l1,2)*pow(l2,2)*pow(sin(theta1 + theta2),2)*pow(cos(theta1),2) + 2*l1*l2*pow(lambda,2)*cos(theta1 + theta2)*cos(theta1) + 2*l1*l2*pow(lambda,2)*sin(theta1 + theta2)*sin(theta1) - 2*pow(l1,2)*pow(l2,2)*cos(theta1 + theta2)*sin(theta1 + theta2)*cos(theta1)*sin(theta1));
Vigilance88 37:4d7b7ced20ef 1025 dls3= -(l2*pow(lambda,2)*sin(theta1 + theta2) - l1*pow(l2,2)*pow(cos(theta1 + theta2),2)*sin(theta1) + pow(l1,2)*l2*sin(theta1 + theta2)*pow(cos(theta1),2) - pow(l1,2)*l2*cos(theta1 + theta2)*cos(theta1)*sin(theta1) + l1*pow(l2,2)*cos(theta1 + theta2)*sin(theta1 + theta2)*cos(theta1))/(pow(lambda,4) + 2*pow(l2,2)*pow(lambda,2)*pow(cos(theta1 + theta2),2) + 2*pow(l2,2)*pow(lambda,2)*pow(sin(theta1 + theta2),2) + pow(l1,2)*pow(lambda,2)*pow(cos(theta1),2) + pow(l1,2)*pow(lambda,2)*pow(sin(theta1),2) + pow(l1,2)*pow(l2,2)*pow(cos(theta1 + theta2),2)*pow(sin(theta1),2) + pow(l1,2)*pow(l2,2)*pow(sin(theta1 + theta2),2)*pow(cos(theta1),2) + 2*l1*l2*pow(lambda,2)*cos(theta1 + theta2)*cos(theta1) + 2*l1*l2*pow(lambda,2)*sin(theta1 + theta2)*sin(theta1) - 2*pow(l1,2)*pow(l2,2)*cos(theta1 + theta2)*sin(theta1 + theta2)*cos(theta1)*sin(theta1));
Vigilance88 37:4d7b7ced20ef 1026 dls4= (l2*pow(lambda,2)*cos(theta1 + theta2) - l1*pow(l2,2)*pow(sin(theta1 + theta2),2)*cos(theta1) + pow(l1,2)*l2*cos(theta1 + theta2)*pow(sin(theta1),2) - pow(l1,2)*l2*sin(theta1 + theta2)*cos(theta1)*sin(theta1) + l1*pow(l2,2)*cos(theta1 + theta2)*sin(theta1 + theta2)*sin(theta1))/(pow(lambda,4) + 2*pow(l2,2)*pow(lambda,2)*pow(cos(theta1 + theta2),2) + 2*pow(l2,2)*pow(lambda,2)*pow(sin(theta1 + theta2),2) + pow(l1,2)*pow(lambda,2)*pow(cos(theta1),2) + pow(l1,2)*pow(lambda,2)*pow(sin(theta1),2) + pow(l1,2)*pow(l2,2)*pow(cos(theta1 + theta2),2)*pow(sin(theta1),2) + pow(l1,2)*pow(l2,2)*pow(sin(theta1 + theta2),2)*pow(cos(theta1),2) + 2*l1*l2*pow(lambda,2)*cos(theta1 + theta2)*cos(theta1) + 2*l1*l2*pow(lambda,2)*sin(theta1 + theta2)*sin(theta1) - 2*pow(l1,2)*pow(l2,2)*cos(theta1 + theta2)*sin(theta1 + theta2)*cos(theta1)*sin(theta1));
Vigilance88 37:4d7b7ced20ef 1027
Vigilance88 37:4d7b7ced20ef 1028 q1_error = dls1 * x_error + dls2 * y_error;
Vigilance88 37:4d7b7ced20ef 1029 q2_error = dls3 * x_error + dls4 * y_error;
Vigilance88 37:4d7b7ced20ef 1030
Vigilance88 37:4d7b7ced20ef 1031 //Angle error
Vigilance88 37:4d7b7ced20ef 1032 m1_error = q1_error;
Vigilance88 37:4d7b7ced20ef 1033 m2_error = q2_error;
Vigilance88 39:e77f844d10d9 1034 }//end control method 2
Vigilance88 48:a1f97eb8c020 1035 /******************************** END OF DLS INVERSE KINEMATICS ****************************************/
Vigilance88 39:e77f844d10d9 1036
Vigilance88 48:a1f97eb8c020 1037
Vigilance88 48:a1f97eb8c020 1038 /* Set limits to the error!
Vigilance88 48:a1f97eb8c020 1039 motor1: lower limit 40 degrees, upper limit 135
Vigilance88 48:a1f97eb8c020 1040 motor2: lower 43 degrees, upper limit 138 degrees. */
Vigilance88 41:55face19e06b 1041
Vigilance88 48:a1f97eb8c020 1042 //lower limits: Negative error not allowed to go further. NEEDS MORE TESTING
Vigilance88 41:55face19e06b 1043 if (theta1 < theta1_lower){
Vigilance88 41:55face19e06b 1044 if (m1_error > 0)
Vigilance88 41:55face19e06b 1045 m1_error = m1_error;
Vigilance88 39:e77f844d10d9 1046 else
Vigilance88 41:55face19e06b 1047 m1_error = 0; }
Vigilance88 41:55face19e06b 1048 if (theta2 < theta2_lower){
Vigilance88 41:55face19e06b 1049 if (m2_error > 0)
Vigilance88 41:55face19e06b 1050 m2_error = m2_error;
Vigilance88 41:55face19e06b 1051 else
Vigilance88 41:55face19e06b 1052 m2_error = 0;
Vigilance88 41:55face19e06b 1053 }
Vigilance88 39:e77f844d10d9 1054 //upper limit: Positive error not allowed to go further
Vigilance88 41:55face19e06b 1055 if (theta1 > theta1_upper){
Vigilance88 41:55face19e06b 1056 if (m1_error < 0 )
Vigilance88 41:55face19e06b 1057 m1_error = m1_error;
Vigilance88 39:e77f844d10d9 1058 else
Vigilance88 41:55face19e06b 1059 m1_error = 0;
Vigilance88 41:55face19e06b 1060 }
Vigilance88 41:55face19e06b 1061 if (theta2 > theta2_upper){
Vigilance88 41:55face19e06b 1062 if (m2_error < 0 )
Vigilance88 41:55face19e06b 1063 m2_error = m2_error;
Vigilance88 41:55face19e06b 1064 else
Vigilance88 41:55face19e06b 1065 m2_error = 0;
Vigilance88 41:55face19e06b 1066 }
Vigilance88 39:e77f844d10d9 1067
Vigilance88 18:44905b008f44 1068 //PID controller
Vigilance88 24:56db31267f10 1069 u1=pid(m1_error,m1_kp,m1_ki,m1_kd,m1_e_int,m1_e_prev); //motor 1
Vigilance88 46:c8c5c455dd51 1070 u2=pid2(m2_error,m2_kp,m2_ki,m2_kd,m2_e_int,m2_e_prev); //motor 2
Vigilance88 21:d6a46315d5f5 1071
Vigilance88 48:a1f97eb8c020 1072 //keep u between limits, sign = direction, PWM = 0-1
Vigilance88 55:726fdab812a9 1073 keep_in_range(&u1,-0.7,0.7);
Vigilance88 55:726fdab812a9 1074 keep_in_range(&u2,-0.7,0.7);
Vigilance88 21:d6a46315d5f5 1075
Vigilance88 21:d6a46315d5f5 1076 //send PWM and DIR to motor 1
Vigilance88 21:d6a46315d5f5 1077 dir_motor1 = u1>0 ? 1 : 0; //conditional statement dir_motor1 = [condition] ? [if met 1] : [else 0]], same as if else below.
Vigilance88 21:d6a46315d5f5 1078 pwm_motor1.write(abs(u1));
Vigilance88 21:d6a46315d5f5 1079
Vigilance88 21:d6a46315d5f5 1080 //send PWM and DIR to motor 2
Vigilance88 27:d1814e271a95 1081 dir_motor2 = u2>0 ? 0 : 1; //conditional statement, same as if else below
Vigilance88 25:49ccdc98639a 1082 pwm_motor2.write(abs(u2));
Vigilance88 21:d6a46315d5f5 1083
Vigilance88 53:bf0d97487e84 1084
Vigilance88 55:726fdab812a9 1085
Vigilance88 21:d6a46315d5f5 1086 /*if(u1 > 0)
Vigilance88 21:d6a46315d5f5 1087 {
Vigilance88 21:d6a46315d5f5 1088 dir_motor1 = 0;
Vigilance88 21:d6a46315d5f5 1089 else{
Vigilance88 21:d6a46315d5f5 1090 dir_motor1 = 1;
Vigilance88 21:d6a46315d5f5 1091 }
Vigilance88 21:d6a46315d5f5 1092 }
Vigilance88 21:d6a46315d5f5 1093 pwm_motor1.write(abs(u1));
Vigilance88 21:d6a46315d5f5 1094
Vigilance88 21:d6a46315d5f5 1095
Vigilance88 21:d6a46315d5f5 1096 if(u2 > 0)
Vigilance88 21:d6a46315d5f5 1097 {
Vigilance88 21:d6a46315d5f5 1098 dir_motor1 = 1;
Vigilance88 21:d6a46315d5f5 1099 else{
Vigilance88 21:d6a46315d5f5 1100 dir_motor1 = 0;
Vigilance88 21:d6a46315d5f5 1101 }
Vigilance88 21:d6a46315d5f5 1102 }
Vigilance88 21:d6a46315d5f5 1103 pwm_motor1.write(abs(u2));*/
Vigilance88 21:d6a46315d5f5 1104
Vigilance88 18:44905b008f44 1105 }
Vigilance88 18:44905b008f44 1106
Vigilance88 38:c8ac615d0c8f 1107
Vigilance88 26:fe3a5469dd6b 1108 void mainMenu()
Vigilance88 26:fe3a5469dd6b 1109 {
Vigilance88 38:c8ac615d0c8f 1110 //Title Box
Vigilance88 26:fe3a5469dd6b 1111 pc.putc(201);
Vigilance88 26:fe3a5469dd6b 1112 for(int j=0;j<33;j++){
Vigilance88 26:fe3a5469dd6b 1113 pc.putc(205);
Vigilance88 26:fe3a5469dd6b 1114 }
Vigilance88 26:fe3a5469dd6b 1115 pc.putc(187);
Vigilance88 26:fe3a5469dd6b 1116 pc.printf("\n\r");
Vigilance88 28:743485bb51e4 1117 pc.putc(186); pc.printf(" Main Menu "); pc.putc(186);
Vigilance88 26:fe3a5469dd6b 1118 pc.printf("\n\r");
Vigilance88 26:fe3a5469dd6b 1119 pc.putc(200);
Vigilance88 26:fe3a5469dd6b 1120 for(int k=0;k<33;k++){
Vigilance88 26:fe3a5469dd6b 1121 pc.putc(205);
Vigilance88 26:fe3a5469dd6b 1122 }
Vigilance88 26:fe3a5469dd6b 1123 pc.putc(188);
Vigilance88 26:fe3a5469dd6b 1124
Vigilance88 26:fe3a5469dd6b 1125 pc.printf("\n\r");
Vigilance88 26:fe3a5469dd6b 1126 //endbox
Vigilance88 48:a1f97eb8c020 1127
Vigilance88 28:743485bb51e4 1128 wait(1);
Vigilance88 28:743485bb51e4 1129 pc.printf("[C]alibration\r\n"); wait(0.2);
Vigilance88 40:d62f96ed44c0 1130 pc.printf("[T]RIG Control with WASD\r\n"); wait(0.2);
Vigilance88 40:d62f96ed44c0 1131 pc.printf("[D]LS Control with WASD\r\n"); wait(0.2);
Vigilance88 40:d62f96ed44c0 1132 pc.printf("[E]MG Control - DLS \r\n"); wait(0.2);
Vigilance88 28:743485bb51e4 1133 pc.printf("[Q]uit Robot Program\r\n"); wait(0.2);
Vigilance88 28:743485bb51e4 1134 pc.printf("[R]ed LED\r\n"); wait(0.2);
Vigilance88 28:743485bb51e4 1135 pc.printf("[G]reen LED\r\n"); wait(0.2);
Vigilance88 28:743485bb51e4 1136 pc.printf("[B]lue LED\r\n"); wait(0.2);
Vigilance88 28:743485bb51e4 1137 pc.printf("Please make a choice => \r\n");
Vigilance88 26:fe3a5469dd6b 1138 }
Vigilance88 24:56db31267f10 1139
Vigilance88 24:56db31267f10 1140 //Start sampling
Vigilance88 24:56db31267f10 1141 void start_sampling(void)
Vigilance88 24:56db31267f10 1142 {
Vigilance88 24:56db31267f10 1143 sample_timer.attach(&sample_filter,SAMPLE_RATE); //500 Hz EMG
Vigilance88 26:fe3a5469dd6b 1144
Vigilance88 26:fe3a5469dd6b 1145 //Debug LED will be green when sampling is active
Vigilance88 26:fe3a5469dd6b 1146 green=0;
Vigilance88 25:49ccdc98639a 1147 pc.printf("||- started sampling -|| \r\n");
Vigilance88 24:56db31267f10 1148 }
Vigilance88 24:56db31267f10 1149
Vigilance88 24:56db31267f10 1150 //stop sampling
Vigilance88 24:56db31267f10 1151 void stop_sampling(void)
Vigilance88 24:56db31267f10 1152 {
Vigilance88 24:56db31267f10 1153 sample_timer.detach();
Vigilance88 26:fe3a5469dd6b 1154
Vigilance88 26:fe3a5469dd6b 1155 //Debug LED will be turned off when sampling stops
Vigilance88 26:fe3a5469dd6b 1156 green=1;
Vigilance88 25:49ccdc98639a 1157 pc.printf("||- stopped sampling -|| \r\n");
Vigilance88 24:56db31267f10 1158 }
Vigilance88 24:56db31267f10 1159
Vigilance88 18:44905b008f44 1160 //Start control
Vigilance88 18:44905b008f44 1161 void start_control(void)
Vigilance88 18:44905b008f44 1162 {
Vigilance88 35:7d9fca0b1545 1163 control_timer.attach(&control,CONTROL_RATE); //100 Hz control
Vigilance88 35:7d9fca0b1545 1164
Vigilance88 35:7d9fca0b1545 1165 //Debug LED will be blue when control is on. If sampling and control are on -> blue + green = cyan.
Vigilance88 35:7d9fca0b1545 1166 blue=0;
Vigilance88 35:7d9fca0b1545 1167 pc.printf("||- started control -|| \r\n");
Vigilance88 35:7d9fca0b1545 1168 }
Vigilance88 35:7d9fca0b1545 1169
Vigilance88 18:44905b008f44 1170 //stop control
Vigilance88 18:44905b008f44 1171 void stop_control(void)
Vigilance88 18:44905b008f44 1172 {
Vigilance88 18:44905b008f44 1173 control_timer.detach();
Vigilance88 53:bf0d97487e84 1174 pwm_motor1=0; pwm_motor2=0;
Vigilance88 26:fe3a5469dd6b 1175 //Debug LED will be off when control is off
Vigilance88 26:fe3a5469dd6b 1176 blue=1;
Vigilance88 26:fe3a5469dd6b 1177 pc.printf("||- stopped control -|| \r\n");
vsluiter 2:e314bb3b2d99 1178 }
vsluiter 0:32bb76391d89 1179
Vigilance88 21:d6a46315d5f5 1180
Vigilance88 26:fe3a5469dd6b 1181 //Clears the putty (or other terminal) window
Vigilance88 26:fe3a5469dd6b 1182 void clearTerminal()
Vigilance88 26:fe3a5469dd6b 1183 {
Vigilance88 26:fe3a5469dd6b 1184 pc.putc(27);
Vigilance88 26:fe3a5469dd6b 1185 pc.printf("[2J"); // clear screen
Vigilance88 26:fe3a5469dd6b 1186 pc.putc(27); // ESC
Vigilance88 26:fe3a5469dd6b 1187 pc.printf("[H"); // cursor to home
Vigilance88 26:fe3a5469dd6b 1188 }
Vigilance88 21:d6a46315d5f5 1189
Vigilance88 30:a9fdd3202ca2 1190
Vigilance88 30:a9fdd3202ca2 1191 void controlMenu()
Vigilance88 30:a9fdd3202ca2 1192 {
Vigilance88 48:a1f97eb8c020 1193 //Title Box
Vigilance88 30:a9fdd3202ca2 1194 pc.putc(201);
Vigilance88 30:a9fdd3202ca2 1195 for(int j=0;j<33;j++){
Vigilance88 30:a9fdd3202ca2 1196 pc.putc(205);
Vigilance88 30:a9fdd3202ca2 1197 }
Vigilance88 30:a9fdd3202ca2 1198 pc.putc(187);
Vigilance88 30:a9fdd3202ca2 1199 pc.printf("\n\r");
Vigilance88 30:a9fdd3202ca2 1200 pc.putc(186); pc.printf(" Control Menu "); pc.putc(186);
Vigilance88 30:a9fdd3202ca2 1201 pc.printf("\n\r");
Vigilance88 30:a9fdd3202ca2 1202 pc.putc(200);
Vigilance88 30:a9fdd3202ca2 1203 for(int k=0;k<33;k++){
Vigilance88 30:a9fdd3202ca2 1204 pc.putc(205);
Vigilance88 30:a9fdd3202ca2 1205 }
Vigilance88 30:a9fdd3202ca2 1206 pc.putc(188);
Vigilance88 30:a9fdd3202ca2 1207
Vigilance88 30:a9fdd3202ca2 1208 pc.printf("\n\r");
Vigilance88 30:a9fdd3202ca2 1209 //endbox
Vigilance88 48:a1f97eb8c020 1210
Vigilance88 38:c8ac615d0c8f 1211 pc.printf("A) Move Arm Left\r\n");
Vigilance88 38:c8ac615d0c8f 1212 pc.printf("D) Move Arm Right\r\n");
Vigilance88 38:c8ac615d0c8f 1213 pc.printf("W) Move Arm Up\r\n");
Vigilance88 38:c8ac615d0c8f 1214 pc.printf("S) Move Arm Down\r\n");
Vigilance88 30:a9fdd3202ca2 1215 pc.printf("q) Exit \r\n");
Vigilance88 30:a9fdd3202ca2 1216 pc.printf("Please make a choice => \r\n");
Vigilance88 30:a9fdd3202ca2 1217 }
Vigilance88 30:a9fdd3202ca2 1218
Vigilance88 28:743485bb51e4 1219 void caliMenu(){
Vigilance88 28:743485bb51e4 1220
Vigilance88 48:a1f97eb8c020 1221 //Title Box
Vigilance88 28:743485bb51e4 1222 pc.putc(201);
Vigilance88 28:743485bb51e4 1223 for(int j=0;j<33;j++){
Vigilance88 28:743485bb51e4 1224 pc.putc(205);
Vigilance88 28:743485bb51e4 1225 }
Vigilance88 28:743485bb51e4 1226 pc.putc(187);
Vigilance88 28:743485bb51e4 1227 pc.printf("\n\r");
Vigilance88 28:743485bb51e4 1228 pc.putc(186); pc.printf(" Calibration Menu "); pc.putc(186);
Vigilance88 28:743485bb51e4 1229 pc.printf("\n\r");
Vigilance88 28:743485bb51e4 1230 pc.putc(200);
Vigilance88 28:743485bb51e4 1231 for(int k=0;k<33;k++){
Vigilance88 28:743485bb51e4 1232 pc.putc(205);
Vigilance88 28:743485bb51e4 1233 }
Vigilance88 28:743485bb51e4 1234 pc.putc(188);
Vigilance88 28:743485bb51e4 1235
Vigilance88 28:743485bb51e4 1236 pc.printf("\n\r");
Vigilance88 28:743485bb51e4 1237 //endbox
Vigilance88 28:743485bb51e4 1238
Vigilance88 28:743485bb51e4 1239 pc.printf("[A]rm Calibration\r\n");
Vigilance88 28:743485bb51e4 1240 pc.printf("[E]MG Calibration\r\n");
Vigilance88 28:743485bb51e4 1241 pc.printf("[B]ack to main menu\r\n");
Vigilance88 28:743485bb51e4 1242 pc.printf("Please make a choice => \r\n");
Vigilance88 28:743485bb51e4 1243
Vigilance88 28:743485bb51e4 1244 }
Vigilance88 28:743485bb51e4 1245
Vigilance88 28:743485bb51e4 1246 void titleBox(){
Vigilance88 28:743485bb51e4 1247
Vigilance88 28:743485bb51e4 1248 //Title Box
Vigilance88 28:743485bb51e4 1249 pc.putc(201);
Vigilance88 28:743485bb51e4 1250 for(int j=0;j<33;j++){
Vigilance88 28:743485bb51e4 1251 pc.putc(205);
Vigilance88 28:743485bb51e4 1252 }
Vigilance88 28:743485bb51e4 1253 pc.putc(187);
Vigilance88 28:743485bb51e4 1254 pc.printf("\n\r");
Vigilance88 28:743485bb51e4 1255 pc.putc(186); pc.printf(" BioRobotics M9 - Group 14 "); pc.putc(186);
Vigilance88 28:743485bb51e4 1256 pc.printf("\n\r");
Vigilance88 28:743485bb51e4 1257 pc.putc(186); pc.printf(" Robot powered ON "); pc.putc(186);
Vigilance88 28:743485bb51e4 1258 pc.printf("\n\r");
Vigilance88 28:743485bb51e4 1259 pc.putc(200);
Vigilance88 28:743485bb51e4 1260 for(int k=0;k<33;k++){
Vigilance88 28:743485bb51e4 1261 pc.putc(205);
Vigilance88 28:743485bb51e4 1262 }
Vigilance88 28:743485bb51e4 1263 pc.putc(188);
Vigilance88 28:743485bb51e4 1264
Vigilance88 28:743485bb51e4 1265 pc.printf("\n\r");
Vigilance88 28:743485bb51e4 1266 //endbox
Vigilance88 28:743485bb51e4 1267 }
Vigilance88 28:743485bb51e4 1268
Vigilance88 28:743485bb51e4 1269 void emgInstructions(){
Vigilance88 36:4d4fc200171b 1270 pc.printf("\r\nPrepare the skin before applying electrodes: \n\r");
Vigilance88 28:743485bb51e4 1271 pc.printf("-> Shave electrode locations if needed and clean with alcohol \n\r"); wait(1);
Vigilance88 38:c8ac615d0c8f 1272 pc.printf("\r\n Check whether EMG signal looks normal. \n\r "); wait(1);
Vigilance88 38:c8ac615d0c8f 1273 pc.printf("\r\n To calibrate the EMG signals we will measure: \n\r ");
Vigilance88 38:c8ac615d0c8f 1274 pc.printf("- Minimum amplitude, while relaxing all muscles. \n\r ");
Vigilance88 48:a1f97eb8c020 1275 pc.printf("- Maximum Voluntary Contraction of each muscle. \n\r"); wait(1);
Vigilance88 48:a1f97eb8c020 1276 pc.printf("\r\nFor the MVC you need to flex the mentioned muscle as much as possible for 5 seconds \n\r"); wait(0.5);
Vigilance88 48:a1f97eb8c020 1277 pc.printf("The measurements will begin once you confirm you're ready [Hit any key] \n\r \n\r"); wait(0.5);
Vigilance88 28:743485bb51e4 1278 }
Vigilance88 28:743485bb51e4 1279
Vigilance88 21:d6a46315d5f5 1280 //keeps input limited between min max
Vigilance88 24:56db31267f10 1281 void keep_in_range(double * in, double min, double max)
Vigilance88 18:44905b008f44 1282 {
Vigilance88 18:44905b008f44 1283 *in > min ? *in < max? : *in = max: *in = min;
vsluiter 0:32bb76391d89 1284 }