2nd draft

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed Servo

Fork of robot_mockup by Martijn Kern

Committer:
Vigilance88
Date:
Fri Oct 23 20:13:06 2015 +0000
Revision:
47:c52f9b4c90c4
Parent:
46:c8c5c455dd51
Child:
48:a1f97eb8c020
rewrote calibrate_arm, added ticker. need to test

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