![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
4 directional EMG control of the XY table. Made during my bachelor end assignment.
Dependencies: C12832_lcd HIDScope mbed-dsp mbed
main.cpp@85:a7493d3dd4ce, 2015-06-25 (annotated)
- Committer:
- jessekaiser
- Date:
- Thu Jun 25 08:22:00 2015 +0000
- Revision:
- 85:a7493d3dd4ce
- Parent:
- 84:8b3f18260431
- Child:
- 86:9d781e7aa893
Poging Y-Motor 1;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jessekaiser | 56:6ea03cce1175 | 1 | /*Code by Jesse Kaiser, s1355783 for control of the 2DOF Planar Table |
jessekaiser | 85:a7493d3dd4ce | 2 | Some variables are also numbered at the end. The numbers stands for the muscle that controls it. |
jessekaiser | 56:6ea03cce1175 | 3 | Biceps = 1 |
jessekaiser | 56:6ea03cce1175 | 4 | Triceps = 2 |
jessekaiser | 56:6ea03cce1175 | 5 | Pectoralis Major = 3 |
jessekaiser | 56:6ea03cce1175 | 6 | Deltoid = 4 |
jessekaiser | 56:6ea03cce1175 | 7 | The "x" and "y" at the end of variables stand for the X-Spindle or Y-Spindle respectivly. |
jessekaiser | 56:6ea03cce1175 | 8 | */ |
jessekaiser | 56:6ea03cce1175 | 9 | |
jessekaiser | 0:3acdd563582f | 10 | #include "mbed.h" |
jessekaiser | 80:6f9ddb8bb335 | 11 | #include "C12832_lcd.h" |
jessekaiser | 56:6ea03cce1175 | 12 | #include "arm_math.h" |
jessekaiser | 57:0a278c60d28b | 13 | //#include "HIDScope.h" |
jessekaiser | 0:3acdd563582f | 14 | |
jessekaiser | 69:a1ba54587b35 | 15 | #define P_Gain 0.99 |
jessekaiser | 82:bcd96c98af2d | 16 | #define K_Gain 150 //Gain of the filtered EMG signal |
jessekaiser | 82:bcd96c98af2d | 17 | #define Damp 5 //Deceleration of the motor |
jessekaiser | 82:bcd96c98af2d | 18 | #define Mass 1 // Mass value |
jessekaiser | 82:bcd96c98af2d | 19 | #define dt 0.01 //Sample frequency |
jessekaiser | 82:bcd96c98af2d | 20 | #define MAX_bi 0.40 //Can be used for normalisation of the EMG signal of the biceps |
jessekaiser | 82:bcd96c98af2d | 21 | #define MAX_tri 0.60 |
jessekaiser | 82:bcd96c98af2d | 22 | #define MAX_pect 0.48 |
jessekaiser | 82:bcd96c98af2d | 23 | #define MAX_delt 1.07 |
jessekaiser | 67:fba5b64bb295 | 24 | #define EMG_tresh1 0.01 |
jessekaiser | 67:fba5b64bb295 | 25 | #define EMG_tresh2 0.01 |
jessekaiser | 60:664f9b907c02 | 26 | #define EMG_tresh3 0.01 |
jessekaiser | 60:664f9b907c02 | 27 | #define EMG_tresh4 0.01 |
jessekaiser | 69:a1ba54587b35 | 28 | #define H_Gain 3.5 |
jessekaiser | 82:bcd96c98af2d | 29 | #define Pt_x 0.50 |
jessekaiser | 82:bcd96c98af2d | 30 | #define Pt_y 0.50 |
jessekaiser | 82:bcd96c98af2d | 31 | #define error_tresh 0.03 |
jessekaiser | 31:372ff8d49430 | 32 | |
jessekaiser | 56:6ea03cce1175 | 33 | //Motor control |
jessekaiser | 85:a7493d3dd4ce | 34 | DigitalOut Dirx(p25); |
jessekaiser | 85:a7493d3dd4ce | 35 | PwmOut Stepx(p26); |
jessekaiser | 80:6f9ddb8bb335 | 36 | DigitalOut Diry(p23); |
jessekaiser | 80:6f9ddb8bb335 | 37 | PwmOut Stepy(p24); |
jessekaiser | 56:6ea03cce1175 | 38 | |
jessekaiser | 56:6ea03cce1175 | 39 | //Signal to and from computer |
jessekaiser | 56:6ea03cce1175 | 40 | Serial pc(USBTX, USBRX); |
jessekaiser | 56:6ea03cce1175 | 41 | |
jessekaiser | 56:6ea03cce1175 | 42 | //Position sensors |
jessekaiser | 85:a7493d3dd4ce | 43 | AnalogIn Posx(p20); |
jessekaiser | 85:a7493d3dd4ce | 44 | AnalogIn Posy(p19); |
jessekaiser | 85:a7493d3dd4ce | 45 | DigitalOut Enablex(p27); |
jessekaiser | 85:a7493d3dd4ce | 46 | DigitalOut Enabley(p39); |
jessekaiser | 56:6ea03cce1175 | 47 | |
jessekaiser | 56:6ea03cce1175 | 48 | //Microstepping |
jessekaiser | 85:a7493d3dd4ce | 49 | DigitalOut MS1(p29); |
jessekaiser | 85:a7493d3dd4ce | 50 | DigitalOut MS2(p30); |
jessekaiser | 85:a7493d3dd4ce | 51 | DigitalOut MS3(p31); |
jessekaiser | 56:6ea03cce1175 | 52 | |
jessekaiser | 57:0a278c60d28b | 53 | //EMG inputs |
jessekaiser | 85:a7493d3dd4ce | 54 | AnalogIn emg1(p15); //EMG bordje bovenop, biceps |
jessekaiser | 82:bcd96c98af2d | 55 | AnalogIn emg2(p16); //triceps |
jessekaiser | 85:a7493d3dd4ce | 56 | AnalogIn emg3(p17); |
jessekaiser | 85:a7493d3dd4ce | 57 | AnalogIn emg4(p18); |
jessekaiser | 56:6ea03cce1175 | 58 | |
jessekaiser | 57:0a278c60d28b | 59 | //HIDScope scope(4); |
jessekaiser | 57:0a278c60d28b | 60 | //Ticker scopeTimer; |
jessekaiser | 56:6ea03cce1175 | 61 | |
jessekaiser | 82:bcd96c98af2d | 62 | //lcd |
jessekaiser | 80:6f9ddb8bb335 | 63 | C12832_LCD lcd; |
jessekaiser | 0:3acdd563582f | 64 | |
jessekaiser | 56:6ea03cce1175 | 65 | //Variables for motor control |
jessekaiser | 60:664f9b907c02 | 66 | float setpoint = 2000; //Frequentie setpoint |
jessekaiser | 77:f3290f86ae4f | 67 | float step_freq1 = 1; |
jessekaiser | 78:9cae6de48b0e | 68 | float step_freq2 = 1; |
jessekaiser | 69:a1ba54587b35 | 69 | |
jessekaiser | 56:6ea03cce1175 | 70 | |
jessekaiser | 56:6ea03cce1175 | 71 | //EMG filter |
jessekaiser | 56:6ea03cce1175 | 72 | arm_biquad_casd_df1_inst_f32 lowpass_biceps; |
jessekaiser | 56:6ea03cce1175 | 73 | arm_biquad_casd_df1_inst_f32 lowpass_triceps; |
jessekaiser | 78:9cae6de48b0e | 74 | arm_biquad_casd_df1_inst_f32 lowpass_pect; |
jessekaiser | 78:9cae6de48b0e | 75 | arm_biquad_casd_df1_inst_f32 lowpass_deltoid; |
jessekaiser | 82:bcd96c98af2d | 76 | //lowpass filter settings: Fc = 2 Hz, Fs = 500 Hz, Gain = -3 dB |
jessekaiser | 56:6ea03cce1175 | 77 | float lowpass_const[] = {0.00015514839749793376, 0.00031029679499586753, 0.00015514839749793376, 1.9644602512795832, -0.9650808448695751}; |
jessekaiser | 56:6ea03cce1175 | 78 | arm_biquad_casd_df1_inst_f32 highnotch_biceps; |
jessekaiser | 56:6ea03cce1175 | 79 | arm_biquad_casd_df1_inst_f32 highnotch_triceps; |
jessekaiser | 78:9cae6de48b0e | 80 | arm_biquad_casd_df1_inst_f32 highnotch_pect; |
jessekaiser | 78:9cae6de48b0e | 81 | arm_biquad_casd_df1_inst_f32 highnotch_deltoid; |
jessekaiser | 56:6ea03cce1175 | 82 | //highpass filter settings: Fc = 20 Hz, Fs = 500 Hz, notch Fc = 50, Fs = 500 Hz |
jessekaiser | 85:a7493d3dd4ce | 83 | float highnotch_const[] = {0.8370879899975344, -1.6741759799950688, 0.8370879899975344, 1.6474576182593796, -0.7008943417307579, 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, 1.1429772843080923, -0.41279762014290533}; |
jessekaiser | 56:6ea03cce1175 | 84 | |
jessekaiser | 56:6ea03cce1175 | 85 | //state values |
jessekaiser | 56:6ea03cce1175 | 86 | float lowpass_biceps_states[4]; |
jessekaiser | 56:6ea03cce1175 | 87 | float highnotch_biceps_states[8]; |
jessekaiser | 56:6ea03cce1175 | 88 | float lowpass_triceps_states[4]; |
jessekaiser | 56:6ea03cce1175 | 89 | float highnotch_triceps_states[8]; |
jessekaiser | 78:9cae6de48b0e | 90 | float lowpass_pect_states[4]; |
jessekaiser | 78:9cae6de48b0e | 91 | float highnotch_pect_states[8]; |
jessekaiser | 78:9cae6de48b0e | 92 | float lowpass_deltoid_states[4]; |
jessekaiser | 78:9cae6de48b0e | 93 | float highnotch_deltoid_states[8]; |
jessekaiser | 56:6ea03cce1175 | 94 | |
jessekaiser | 56:6ea03cce1175 | 95 | //global variabels |
jessekaiser | 78:9cae6de48b0e | 96 | float filtered_biceps, filtered_triceps, filtered_pect, filtered_deltoid; |
jessekaiser | 85:a7493d3dd4ce | 97 | float speed_old1, speed_old2, speed_old3, speed_old4; |
jessekaiser | 85:a7493d3dd4ce | 98 | float acc1, acc2, acc3, acc4; |
jessekaiser | 85:a7493d3dd4ce | 99 | float force1, force2, force3, force4; |
jessekaiser | 85:a7493d3dd4ce | 100 | float speed1, speed2, speed3, speed4; |
jessekaiser | 85:a7493d3dd4ce | 101 | float damping1, damping2, damping3, damping4; |
jessekaiser | 60:664f9b907c02 | 102 | float emg_x, emg_y; |
jessekaiser | 61:99f3ae6e053b | 103 | float cx = 0; |
jessekaiser | 61:99f3ae6e053b | 104 | float cy = 0; |
jessekaiser | 61:99f3ae6e053b | 105 | float errorx = 0.2; |
jessekaiser | 61:99f3ae6e053b | 106 | float errory = 0.2; |
jessekaiser | 61:99f3ae6e053b | 107 | float Ps_x = 0; |
jessekaiser | 61:99f3ae6e053b | 108 | float Ps_y = 0; |
jessekaiser | 62:509622cce1c6 | 109 | float hstep_freqx = 1; |
jessekaiser | 62:509622cce1c6 | 110 | float hstep_freqy = 1; |
jessekaiser | 69:a1ba54587b35 | 111 | float emg_y_abs = 0; |
jessekaiser | 69:a1ba54587b35 | 112 | float emg_x_abs = 0; |
jessekaiser | 56:6ea03cce1175 | 113 | |
jessekaiser | 56:6ea03cce1175 | 114 | void looper_emg() |
jessekaiser | 56:6ea03cce1175 | 115 | { |
jessekaiser | 78:9cae6de48b0e | 116 | float emg_value1_f32, emg_value2_f32, emg_value3_f32, emg_value4_f32; |
jessekaiser | 56:6ea03cce1175 | 117 | emg_value1_f32 = emg1.read(); |
jessekaiser | 56:6ea03cce1175 | 118 | emg_value2_f32 = emg2.read(); |
jessekaiser | 78:9cae6de48b0e | 119 | emg_value3_f32 = emg3.read(); |
jessekaiser | 78:9cae6de48b0e | 120 | emg_value4_f32 = emg4.read(); |
jessekaiser | 77:f3290f86ae4f | 121 | |
jessekaiser | 56:6ea03cce1175 | 122 | //process emg biceps |
jessekaiser | 56:6ea03cce1175 | 123 | arm_biquad_cascade_df1_f32(&highnotch_biceps, &emg_value1_f32, &filtered_biceps, 1 ); //High pass and notch filter |
jessekaiser | 85:a7493d3dd4ce | 124 | filtered_biceps = fabs(filtered_biceps); //Rectifier, The Gain is already implemented. |
jessekaiser | 56:6ea03cce1175 | 125 | arm_biquad_cascade_df1_f32(&lowpass_biceps, &filtered_biceps, &filtered_biceps, 1 ); //low pass filter |
jessekaiser | 56:6ea03cce1175 | 126 | |
jessekaiser | 56:6ea03cce1175 | 127 | //process emg triceps |
jessekaiser | 56:6ea03cce1175 | 128 | arm_biquad_cascade_df1_f32(&highnotch_triceps, &emg_value2_f32, &filtered_triceps, 1 ); |
jessekaiser | 60:664f9b907c02 | 129 | filtered_triceps = fabs(filtered_triceps); |
jessekaiser | 56:6ea03cce1175 | 130 | arm_biquad_cascade_df1_f32(&lowpass_triceps, &filtered_triceps, &filtered_triceps, 1 ); |
jessekaiser | 56:6ea03cce1175 | 131 | |
jessekaiser | 78:9cae6de48b0e | 132 | //process emg pectoralis major |
jessekaiser | 78:9cae6de48b0e | 133 | arm_biquad_cascade_df1_f32(&highnotch_pect, &emg_value3_f32, &filtered_pect, 1 ); |
jessekaiser | 78:9cae6de48b0e | 134 | filtered_pect = fabs(filtered_pect); |
jessekaiser | 78:9cae6de48b0e | 135 | arm_biquad_cascade_df1_f32(&lowpass_pect, &filtered_pect, &filtered_pect, 1 ); |
jessekaiser | 77:f3290f86ae4f | 136 | |
jessekaiser | 78:9cae6de48b0e | 137 | //process emg deltoid |
jessekaiser | 78:9cae6de48b0e | 138 | arm_biquad_cascade_df1_f32(&highnotch_deltoid, &emg_value4_f32, &filtered_deltoid, 1 ); |
jessekaiser | 78:9cae6de48b0e | 139 | filtered_deltoid = fabs(filtered_deltoid); |
jessekaiser | 78:9cae6de48b0e | 140 | arm_biquad_cascade_df1_f32(&lowpass_deltoid, &filtered_deltoid, &filtered_deltoid, 1 ); |
jessekaiser | 77:f3290f86ae4f | 141 | |
jessekaiser | 58:3ea066215c31 | 142 | /*send value to PC. |
jessekaiser | 56:6ea03cce1175 | 143 | scope.set(0,filtered_biceps); //Filtered EMG signal |
jessekaiser | 78:9cae6de48b0e | 144 | scope.set(1,filtered_triceps); |
jessekaiser | 78:9cae6de48b0e | 145 | scope.set(2,filtered_pect); |
jessekaiser | 78:9cae6de48b0e | 146 | scope.set(3,filtered_deltoid);*/ |
jessekaiser | 56:6ea03cce1175 | 147 | } |
jessekaiser | 56:6ea03cce1175 | 148 | |
jessekaiser | 85:a7493d3dd4ce | 149 | /*void looper_motory() |
jessekaiser | 69:a1ba54587b35 | 150 | { |
jessekaiser | 85:a7493d3dd4ce | 151 | |
jessekaiser | 77:f3290f86ae4f | 152 | emg_y = (filtered_biceps - filtered_triceps); |
jessekaiser | 77:f3290f86ae4f | 153 | emg_y_abs = fabs(emg_y); |
jessekaiser | 77:f3290f86ae4f | 154 | force1 = emg_y_abs*K_Gain; |
jessekaiser | 77:f3290f86ae4f | 155 | force1 = force1 - damping1; |
jessekaiser | 77:f3290f86ae4f | 156 | acc1 = force1/Mass; |
jessekaiser | 77:f3290f86ae4f | 157 | speed1 = speed_old1 + (acc1 * dt); |
jessekaiser | 77:f3290f86ae4f | 158 | damping1 = speed1 * Damp; |
jessekaiser | 77:f3290f86ae4f | 159 | step_freq1 = setpoint * speed1; |
jessekaiser | 77:f3290f86ae4f | 160 | Stepy.period(1.0/step_freq1); |
jessekaiser | 77:f3290f86ae4f | 161 | speed_old1 = speed1; |
jessekaiser | 60:664f9b907c02 | 162 | |
jessekaiser | 77:f3290f86ae4f | 163 | if (emg_y > 0) { |
jessekaiser | 77:f3290f86ae4f | 164 | Diry = 1; |
jessekaiser | 60:664f9b907c02 | 165 | } |
jessekaiser | 85:a7493d3dd4ce | 166 | |
jessekaiser | 77:f3290f86ae4f | 167 | if (emg_y < 0) { |
jessekaiser | 77:f3290f86ae4f | 168 | Diry = 0; |
jessekaiser | 60:664f9b907c02 | 169 | } |
jessekaiser | 60:664f9b907c02 | 170 | //Speed limit |
jessekaiser | 77:f3290f86ae4f | 171 | if (speed1 > 1) { |
jessekaiser | 77:f3290f86ae4f | 172 | speed1 = 1; |
jessekaiser | 77:f3290f86ae4f | 173 | step_freq1 = setpoint; |
jessekaiser | 60:664f9b907c02 | 174 | } |
jessekaiser | 60:664f9b907c02 | 175 | //EMG treshold |
jessekaiser | 60:664f9b907c02 | 176 | if (filtered_biceps < EMG_tresh1 && filtered_triceps < EMG_tresh2) { |
jessekaiser | 77:f3290f86ae4f | 177 | Enabley = 1; //Enable = 1 turns the motor off. |
jessekaiser | 60:664f9b907c02 | 178 | } else { |
jessekaiser | 77:f3290f86ae4f | 179 | Enabley = 0; |
jessekaiser | 60:664f9b907c02 | 180 | } |
jessekaiser | 82:bcd96c98af2d | 181 | |
jessekaiser | 85:a7493d3dd4ce | 182 | }*/ |
jessekaiser | 60:664f9b907c02 | 183 | |
jessekaiser | 83:067e07db027c | 184 | void looper_motorx() |
jessekaiser | 78:9cae6de48b0e | 185 | { |
jessekaiser | 85:a7493d3dd4ce | 186 | |
jessekaiser | 78:9cae6de48b0e | 187 | emg_x = (filtered_pect - filtered_deltoid); |
jessekaiser | 78:9cae6de48b0e | 188 | emg_x_abs = fabs(emg_x); |
jessekaiser | 78:9cae6de48b0e | 189 | force2 = emg_x_abs*K_Gain; |
jessekaiser | 78:9cae6de48b0e | 190 | force2 = force2 - damping2; |
jessekaiser | 78:9cae6de48b0e | 191 | acc2 = force2/Mass; |
jessekaiser | 78:9cae6de48b0e | 192 | speed2 = speed_old2 + (acc2 * dt); |
jessekaiser | 78:9cae6de48b0e | 193 | damping2 = speed2 * Damp; |
jessekaiser | 78:9cae6de48b0e | 194 | step_freq2 = setpoint * speed2; |
jessekaiser | 78:9cae6de48b0e | 195 | Stepx.period(1.0/step_freq2); |
jessekaiser | 78:9cae6de48b0e | 196 | speed_old2 = speed2; |
jessekaiser | 85:a7493d3dd4ce | 197 | |
jessekaiser | 78:9cae6de48b0e | 198 | if (emg_x > 0) { |
jessekaiser | 78:9cae6de48b0e | 199 | Dirx = 0; |
jessekaiser | 78:9cae6de48b0e | 200 | } |
jessekaiser | 78:9cae6de48b0e | 201 | if (emg_x < 0) { |
jessekaiser | 78:9cae6de48b0e | 202 | Dirx = 1; |
jessekaiser | 78:9cae6de48b0e | 203 | } |
jessekaiser | 78:9cae6de48b0e | 204 | //Speed limit |
jessekaiser | 78:9cae6de48b0e | 205 | if (speed2 > 1) { |
jessekaiser | 78:9cae6de48b0e | 206 | speed2 = 1; |
jessekaiser | 78:9cae6de48b0e | 207 | step_freq2 = setpoint; |
jessekaiser | 78:9cae6de48b0e | 208 | } |
jessekaiser | 78:9cae6de48b0e | 209 | //EMG treshold |
jessekaiser | 78:9cae6de48b0e | 210 | if (filtered_pect < EMG_tresh3 && filtered_deltoid < EMG_tresh4) { |
jessekaiser | 78:9cae6de48b0e | 211 | Enablex = 1; //Enable = 1 turns the motor off. |
jessekaiser | 78:9cae6de48b0e | 212 | } else { |
jessekaiser | 78:9cae6de48b0e | 213 | Enablex = 0; |
jessekaiser | 78:9cae6de48b0e | 214 | } |
jessekaiser | 78:9cae6de48b0e | 215 | |
jessekaiser | 83:067e07db027c | 216 | } |
jessekaiser | 59:ba22a8c26dee | 217 | |
jessekaiser | 59:ba22a8c26dee | 218 | int main() |
jessekaiser | 56:6ea03cce1175 | 219 | { |
jessekaiser | 59:ba22a8c26dee | 220 | // Attach the HIDScope::send method from the scope object to the timer at 500Hz. Hier wordt de sample freq aangegeven. |
jessekaiser | 59:ba22a8c26dee | 221 | // scopeTimer.attach_us(&scope, &HIDScope::send, 2e3); |
jessekaiser | 85:a7493d3dd4ce | 222 | |
jessekaiser | 82:bcd96c98af2d | 223 | MS1 = 1; |
jessekaiser | 82:bcd96c98af2d | 224 | MS2 = 0; |
jessekaiser | 82:bcd96c98af2d | 225 | MS3 = 0; |
jessekaiser | 82:bcd96c98af2d | 226 | |
jessekaiser | 82:bcd96c98af2d | 227 | Stepx.write(0.5); // Duty cycle of 50% |
jessekaiser | 82:bcd96c98af2d | 228 | Stepy.write(0.5); |
jessekaiser | 78:9cae6de48b0e | 229 | |
jessekaiser | 82:bcd96c98af2d | 230 | Enablex = 1; |
jessekaiser | 82:bcd96c98af2d | 231 | Enabley = 1; |
jessekaiser | 82:bcd96c98af2d | 232 | wait(1); |
jessekaiser | 82:bcd96c98af2d | 233 | lcd.printf("Start homing"); |
jessekaiser | 82:bcd96c98af2d | 234 | wait(2); |
jessekaiser | 82:bcd96c98af2d | 235 | lcd.cls(); |
jessekaiser | 82:bcd96c98af2d | 236 | wait(1); |
jessekaiser | 82:bcd96c98af2d | 237 | Enablex = 0; |
jessekaiser | 82:bcd96c98af2d | 238 | Enabley = 0; |
jessekaiser | 82:bcd96c98af2d | 239 | while(errorx > error_tresh || errory > error_tresh) { |
jessekaiser | 78:9cae6de48b0e | 240 | |
jessekaiser | 82:bcd96c98af2d | 241 | Ps_x = Posx.read(); |
jessekaiser | 82:bcd96c98af2d | 242 | Ps_y = Posy.read(); |
jessekaiser | 82:bcd96c98af2d | 243 | errorx = fabs(Pt_x - Ps_x); |
jessekaiser | 82:bcd96c98af2d | 244 | errory = fabs(Ps_y - Pt_y); |
jessekaiser | 82:bcd96c98af2d | 245 | lcd.printf("%.2f %.2f \n", errorx, errory); |
jessekaiser | 85:a7493d3dd4ce | 246 | |
jessekaiser | 85:a7493d3dd4ce | 247 | |
jessekaiser | 85:a7493d3dd4ce | 248 | if (Ps_x < 0.50 && errorx > error_tresh) { |
jessekaiser | 85:a7493d3dd4ce | 249 | Dirx = 0; |
jessekaiser | 85:a7493d3dd4ce | 250 | //errorx = Pt_x - Ps_x; |
jessekaiser | 85:a7493d3dd4ce | 251 | cx = errorx * H_Gain; |
jessekaiser | 83:067e07db027c | 252 | |
jessekaiser | 82:bcd96c98af2d | 253 | float hnew_step_freqx; |
jessekaiser | 82:bcd96c98af2d | 254 | hnew_step_freqx = ((1-P_Gain)*setpoint*cx) + (P_Gain*hstep_freqx); |
jessekaiser | 82:bcd96c98af2d | 255 | hstep_freqx = hnew_step_freqx; |
jessekaiser | 82:bcd96c98af2d | 256 | Stepx.period(1.0/hstep_freqx); |
jessekaiser | 82:bcd96c98af2d | 257 | wait(0.01); |
jessekaiser | 82:bcd96c98af2d | 258 | } |
jessekaiser | 85:a7493d3dd4ce | 259 | if (Ps_y > 0.50 && errory > error_tresh) { |
jessekaiser | 82:bcd96c98af2d | 260 | Diry = 0; |
jessekaiser | 85:a7493d3dd4ce | 261 | //errory = Ps_y - Pt_y; |
jessekaiser | 82:bcd96c98af2d | 262 | cy = errory * H_Gain; |
jessekaiser | 85:a7493d3dd4ce | 263 | |
jessekaiser | 82:bcd96c98af2d | 264 | float hnew_step_freqy; |
jessekaiser | 82:bcd96c98af2d | 265 | hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy); |
jessekaiser | 82:bcd96c98af2d | 266 | hstep_freqy = hnew_step_freqy; |
jessekaiser | 82:bcd96c98af2d | 267 | Stepy.period(1.0/hstep_freqy); |
jessekaiser | 82:bcd96c98af2d | 268 | wait(0.01); |
jessekaiser | 82:bcd96c98af2d | 269 | } |
jessekaiser | 82:bcd96c98af2d | 270 | |
jessekaiser | 85:a7493d3dd4ce | 271 | if (Ps_x > 0.50 && errorx > error_tresh) { |
jessekaiser | 82:bcd96c98af2d | 272 | Dirx = 1; |
jessekaiser | 85:a7493d3dd4ce | 273 | //errorx = Pt_x - Ps_x; |
jessekaiser | 82:bcd96c98af2d | 274 | cx = errorx * H_Gain; |
jessekaiser | 85:a7493d3dd4ce | 275 | |
jessekaiser | 82:bcd96c98af2d | 276 | float hnew_step_freqx; |
jessekaiser | 82:bcd96c98af2d | 277 | hnew_step_freqx = ((1-P_Gain)*setpoint*cx) + (P_Gain*hstep_freqx); |
jessekaiser | 82:bcd96c98af2d | 278 | hstep_freqx = hnew_step_freqx; |
jessekaiser | 82:bcd96c98af2d | 279 | Stepx.period(1.0/hstep_freqx); |
jessekaiser | 82:bcd96c98af2d | 280 | wait(0.01); |
jessekaiser | 82:bcd96c98af2d | 281 | } |
jessekaiser | 85:a7493d3dd4ce | 282 | if (Ps_y < 0.50 && errory > error_tresh) { |
jessekaiser | 82:bcd96c98af2d | 283 | Diry = 1; |
jessekaiser | 85:a7493d3dd4ce | 284 | //errory = Ps_y - Pt_y; |
jessekaiser | 82:bcd96c98af2d | 285 | cy = errory * H_Gain; |
jessekaiser | 85:a7493d3dd4ce | 286 | |
jessekaiser | 82:bcd96c98af2d | 287 | float hnew_step_freqy; |
jessekaiser | 82:bcd96c98af2d | 288 | hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy); |
jessekaiser | 82:bcd96c98af2d | 289 | hstep_freqy = hnew_step_freqy; |
jessekaiser | 82:bcd96c98af2d | 290 | Stepy.period(1.0/hstep_freqy); |
jessekaiser | 82:bcd96c98af2d | 291 | wait(0.01); |
jessekaiser | 78:9cae6de48b0e | 292 | } |
jessekaiser | 82:bcd96c98af2d | 293 | |
jessekaiser | 82:bcd96c98af2d | 294 | } |
jessekaiser | 82:bcd96c98af2d | 295 | lcd.printf("Done"); |
jessekaiser | 82:bcd96c98af2d | 296 | wait(2); |
jessekaiser | 82:bcd96c98af2d | 297 | lcd.cls(); |
jessekaiser | 82:bcd96c98af2d | 298 | wait(1); |
jessekaiser | 82:bcd96c98af2d | 299 | Enablex = 1; |
jessekaiser | 82:bcd96c98af2d | 300 | Enabley = 1; |
jessekaiser | 82:bcd96c98af2d | 301 | wait(3); |
jessekaiser | 82:bcd96c98af2d | 302 | lcd.printf("Start EMG Control"); |
jessekaiser | 82:bcd96c98af2d | 303 | wait(2); |
jessekaiser | 82:bcd96c98af2d | 304 | lcd.cls(); |
jessekaiser | 82:bcd96c98af2d | 305 | wait(1); |
jessekaiser | 82:bcd96c98af2d | 306 | Enablex = 0; |
jessekaiser | 82:bcd96c98af2d | 307 | Enabley = 0; |
jessekaiser | 82:bcd96c98af2d | 308 | |
jessekaiser | 85:a7493d3dd4ce | 309 | MS1 = 1; |
jessekaiser | 85:a7493d3dd4ce | 310 | MS2 = 0; |
jessekaiser | 85:a7493d3dd4ce | 311 | MS3 = 0; |
jessekaiser | 85:a7493d3dd4ce | 312 | Stepx.write(0.5); // Duty cycle of 50% |
jessekaiser | 85:a7493d3dd4ce | 313 | Stepy.write(0.5); |
jessekaiser | 85:a7493d3dd4ce | 314 | |
jessekaiser | 78:9cae6de48b0e | 315 | Ticker emgtimer; //biceps |
jessekaiser | 78:9cae6de48b0e | 316 | arm_biquad_cascade_df1_init_f32(&lowpass_biceps, 1 , lowpass_const, lowpass_biceps_states); |
jessekaiser | 78:9cae6de48b0e | 317 | arm_biquad_cascade_df1_init_f32(&highnotch_biceps, 2 , highnotch_const, highnotch_biceps_states); |
jessekaiser | 78:9cae6de48b0e | 318 | //triceps |
jessekaiser | 78:9cae6de48b0e | 319 | arm_biquad_cascade_df1_init_f32(&lowpass_triceps, 1 , lowpass_const, lowpass_triceps_states); |
jessekaiser | 78:9cae6de48b0e | 320 | arm_biquad_cascade_df1_init_f32(&highnotch_triceps, 2 , highnotch_const, highnotch_triceps_states); |
jessekaiser | 78:9cae6de48b0e | 321 | //pectoralis major |
jessekaiser | 78:9cae6de48b0e | 322 | arm_biquad_cascade_df1_init_f32(&lowpass_pect, 1 , lowpass_const, lowpass_pect_states); |
jessekaiser | 78:9cae6de48b0e | 323 | arm_biquad_cascade_df1_init_f32(&highnotch_pect, 2 , highnotch_const, highnotch_pect_states); |
jessekaiser | 78:9cae6de48b0e | 324 | //deltoid |
jessekaiser | 78:9cae6de48b0e | 325 | arm_biquad_cascade_df1_init_f32(&lowpass_deltoid, 1 , lowpass_const, lowpass_deltoid_states); |
jessekaiser | 78:9cae6de48b0e | 326 | arm_biquad_cascade_df1_init_f32(&highnotch_deltoid, 2 , highnotch_const, highnotch_deltoid_states); |
jessekaiser | 78:9cae6de48b0e | 327 | emgtimer.attach(looper_emg, 0.01); |
jessekaiser | 59:ba22a8c26dee | 328 | |
jessekaiser | 83:067e07db027c | 329 | Ticker looptimer1; |
jessekaiser | 85:a7493d3dd4ce | 330 | looptimer1.attach(looper_motorx, 0.01); //X-Spindle motor, why this freq? |
jessekaiser | 61:99f3ae6e053b | 331 | |
jessekaiser | 85:a7493d3dd4ce | 332 | //Ticker looptimer2; |
jessekaiser | 85:a7493d3dd4ce | 333 | //looptimer2.attach(looper_motory, 0.01); //Y-Spindle motor |
jessekaiser | 85:a7493d3dd4ce | 334 | |
jessekaiser | 85:a7493d3dd4ce | 335 | //Microstepping control, now configured as half stepping (MS1=1,MS2=0,MS3=0) |
jessekaiser | 85:a7493d3dd4ce | 336 | |
jessekaiser | 85:a7493d3dd4ce | 337 | |
jessekaiser | 69:a1ba54587b35 | 338 | |
jessekaiser | 78:9cae6de48b0e | 339 | while (1) { |
jessekaiser | 82:bcd96c98af2d | 340 | |
jessekaiser | 85:a7493d3dd4ce | 341 | |
jessekaiser | 85:a7493d3dd4ce | 342 | pc.printf("%.2f %.2f %.2f \n", Posx.read(), emg_x, step_freq2); //Send signal values to the computer. |
jessekaiser | 85:a7493d3dd4ce | 343 | wait(0.01); |
jessekaiser | 72:4d01b79ad332 | 344 | |
jessekaiser | 78:9cae6de48b0e | 345 | } |
jessekaiser | 78:9cae6de48b0e | 346 | } |