![](/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
Diff: main.cpp
- Revision:
- 73:8cc2826ab1c4
- Parent:
- 72:4d01b79ad332
- Child:
- 74:a9281e4ae9bb
diff -r 4d01b79ad332 -r 8cc2826ab1c4 main.cpp --- a/main.cpp Mon Jun 22 08:41:15 2015 +0000 +++ b/main.cpp Mon Jun 22 08:51:19 2015 +0000 @@ -27,8 +27,6 @@ #define error_tresh 0.01 //Motor control -DigitalOut Dirx(p21); -PwmOut Stepx(p22); DigitalOut Diry(p23); PwmOut Stepy(p24); @@ -36,9 +34,7 @@ Serial pc(USBTX, USBRX); //Position sensors -AnalogIn Posx(p19); AnalogIn Posy(p20); -DigitalOut Enablex(p25); DigitalOut Enabley(p26); //Microstepping @@ -49,8 +45,6 @@ //EMG inputs AnalogIn emg1(p15); AnalogIn emg2(p16); -AnalogIn emg3(p17); -AnalogIn emg4(p18); //HIDScope scope(4); //Ticker scopeTimer; @@ -61,20 +55,15 @@ //Variables for motor control float setpoint = 2000; //Frequentie setpoint float step_freq1 = 1; -float step_freq2 = 1; //EMG filter arm_biquad_casd_df1_inst_f32 lowpass_biceps; arm_biquad_casd_df1_inst_f32 lowpass_triceps; -arm_biquad_casd_df1_inst_f32 lowpass_pect; -arm_biquad_casd_df1_inst_f32 lowpass_deltoid; //lowpass filter settings: Fc = 2 Hz, Fs = 500 Hz float lowpass_const[] = {0.00015514839749793376, 0.00031029679499586753, 0.00015514839749793376, 1.9644602512795832, -0.9650808448695751}; arm_biquad_casd_df1_inst_f32 highnotch_biceps; arm_biquad_casd_df1_inst_f32 highnotch_triceps; -arm_biquad_casd_df1_inst_f32 highnotch_pect; -arm_biquad_casd_df1_inst_f32 highnotch_deltoid; //highpass filter settings: Fc = 20 Hz, Fs = 500 Hz, notch Fc = 50, Fs = 500 Hz float highnotch_const[] = {0.8370879899975344, -1.6741759799950688, 0.8370879899975344, 1.6474576182593796, -0.7008943417307579, 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, 1.1429772843080923, -0.41279762014290533}; @@ -83,13 +72,9 @@ float highnotch_biceps_states[8]; float lowpass_triceps_states[4]; float highnotch_triceps_states[8]; -float lowpass_pect_states[4]; -float highnotch_pect_states[8]; -float lowpass_deltoid_states[4]; -float highnotch_deltoid_states[8]; //global variabels -float filtered_biceps, filtered_triceps, filtered_pect, filtered_deltoid; +float filtered_biceps, filtered_triceps; float speed_old1, speed_old2; float acc1, acc2; float force1, force2; @@ -109,11 +94,10 @@ void looper_emg() { - float emg_value1_f32, emg_value2_f32, emg_value3_f32, emg_value4_f32; + float emg_value1_f32, emg_value2_f32; emg_value1_f32 = emg1.read(); emg_value2_f32 = emg2.read(); - emg_value3_f32 = emg3.read(); - emg_value4_f32 = emg4.read(); + //process emg biceps arm_biquad_cascade_df1_f32(&highnotch_biceps, &emg_value1_f32, &filtered_biceps, 1 ); //High pass and notch filter @@ -125,24 +109,15 @@ filtered_triceps = fabs(filtered_triceps); arm_biquad_cascade_df1_f32(&lowpass_triceps, &filtered_triceps, &filtered_triceps, 1 ); - //process emg pectoralis major - arm_biquad_cascade_df1_f32(&highnotch_pect, &emg_value3_f32, &filtered_pect, 1 ); - filtered_pect = fabs(filtered_pect); - arm_biquad_cascade_df1_f32(&lowpass_pect, &filtered_pect, &filtered_pect, 1 ); - //process emg deltoid - arm_biquad_cascade_df1_f32(&highnotch_deltoid, &emg_value4_f32, &filtered_deltoid, 1 ); - filtered_deltoid = fabs(filtered_deltoid); - arm_biquad_cascade_df1_f32(&lowpass_deltoid, &filtered_deltoid, &filtered_deltoid, 1 ); /*send value to PC. scope.set(0,filtered_biceps); //Filtered EMG signal - scope.set(1,filtered_triceps); - scope.set(2,filtered_pect); - scope.set(3,filtered_deltoid);*/ + scope.set(1,filtered_triceps);*/ + } -/*void looper_motor() +void looper_motory() { emg_y = (filtered_biceps - filtered_triceps); @@ -174,260 +149,101 @@ } else { Enabley = 0; } - wait(0.01); - - emg_x = (filtered_pect - filtered_deltoid); - emg_x_abs = fabs(emg_x); - force2 = emg_x_abs*K_Gain; - force2 = force2 - damping2; - acc2 = force2/Mass; - speed2 = speed_old2 + (acc2 * dt); - damping2 = speed2 * Damp; - step_freq2 = setpoint * speed2; - Stepx.period(1.0/step_freq2); - speed_old2 = speed2; - - if (emg_x > 0) { - Dirx = 0; - } - if (emg_x < 0) { - Dirx = 1; - } - //Speed limit - if (speed2 > 1) { - speed2 = 1; - step_freq2 = setpoint; - } - //EMG treshold - if (filtered_pect < EMG_tresh3 && filtered_deltoid < EMG_tresh4) { - Enablex = 1; //Enable = 1 turns the motor off. - } else { - Enablex = 0; - } - wait(0.01); -} -void looper_motorx() -{ - - emg_x = (filtered_pect - filtered_deltoid); - emg_x_abs = fabs(emg_x); - force2 = emg_x_abs*K_Gain; - force2 = force2 - damping2; - acc2 = force2/Mass; - speed2 = speed_old2 + (acc2 * dt); - damping2 = speed2 * Damp; - step_freq2 = setpoint * speed2; - Stepx.period(1.0/step_freq2); - speed_old2 = speed2; - - if (emg_x > 0) { - Dirx = 0; - } - if (emg_x < 0) { - Dirx = 1; - } - //Speed limit - if (speed2 > 1) { - speed2 = 1; - step_freq2 = setpoint; - } - //EMG treshold - if (filtered_pect < EMG_tresh3 && filtered_deltoid < EMG_tresh4) { - Enablex = 1; //Enable = 1 turns the motor off. - } else { - Enablex = 0; - } - -}*/ +} int main() { // Attach the HIDScope::send method from the scope object to the timer at 500Hz. Hier wordt de sample freq aangegeven. // scopeTimer.attach_us(&scope, &HIDScope::send, 2e3); - - MS1 = 1; - MS2 = 0; - MS3 = 0; - Stepx.write(0.5); // Duty cycle of 50% - Stepy.write(0.5); - - Enablex = 1; - Enabley = 1; - wait(1); - lcd.printf("Start homing"); - wait(2); - lcd.cls(); - wait(1); - Enablex = 0; - Enabley = 0; - - //Homing of the motor, so you start from the same position every time. - while(errorx > error_tresh || errory > error_tresh) { - - Ps_x = Posx.read(); - Ps_y = Posy.read(); - errorx = fabs(Pt_x - Ps_x); - errory = fabs(Ps_y - Pt_y); - lcd.printf("%.2f %.2f \n", Stepx.read(), Stepy.read()); - - - if (Ps_x < 0.50 && errorx > error_tresh) { - Dirx = 0; - cx = errorx * H_Gain; - float hnew_step_freqx; - hnew_step_freqx = ((1-P_Gain)*setpoint*cx) + (P_Gain*hstep_freqx); - hstep_freqx = hnew_step_freqx; - Stepx.period(1.0/hstep_freqx); - wait(0.01); - } - if (Ps_y > 0.50 && errory > error_tresh) { - Diry = 0; - cy = errory * H_Gain; - float hnew_step_freqy; - hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy); - hstep_freqy = hnew_step_freqy; - Stepy.period(1.0/hstep_freqy); - wait(0.01); - } - - if (Ps_x > 0.50 && errorx > error_tresh) { - Dirx = 1; - cx = errorx * H_Gain; - float hnew_step_freqx; - hnew_step_freqx = ((1-P_Gain)*setpoint*cx) + (P_Gain*hstep_freqx); - hstep_freqx = hnew_step_freqx; - Stepx.period(1.0/hstep_freqx); - wait(0.01); - } - if (Ps_y < 0.50 && errory > error_tresh) { - Diry = 1; - cy = errory * H_Gain; - float hnew_step_freqy; - hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy); - hstep_freqy = hnew_step_freqy; - Stepy.period(1.0/hstep_freqy); - wait(0.01); - } - - } - lcd.printf("Done"); - wait(2); - lcd.cls(); - wait(1); - Enablex = 1; - Enabley = 1; - wait(3); - lcd.printf("Start EMG Control"); - wait(2); - lcd.cls(); - wait(1); - Enablex = 0; - Enabley = 0; - MS1 = 1; MS2 = 0; MS3 = 0; - Stepx.write(0.5); // Duty cycle of 50% + + Stepy.write(0.5); - Ticker emgtimer; //biceps - arm_biquad_cascade_df1_init_f32(&lowpass_biceps, 1 , lowpass_const, lowpass_biceps_states); - arm_biquad_cascade_df1_init_f32(&highnotch_biceps, 2 , highnotch_const, highnotch_biceps_states); - //triceps - arm_biquad_cascade_df1_init_f32(&lowpass_triceps, 1 , lowpass_const, lowpass_triceps_states); - arm_biquad_cascade_df1_init_f32(&highnotch_triceps, 2 , highnotch_const, highnotch_triceps_states); - //pectoralis major - arm_biquad_cascade_df1_init_f32(&lowpass_pect, 1 , lowpass_const, lowpass_pect_states); - arm_biquad_cascade_df1_init_f32(&highnotch_pect, 2 , highnotch_const, highnotch_pect_states); - //deltoid - arm_biquad_cascade_df1_init_f32(&lowpass_deltoid, 1 , lowpass_const, lowpass_deltoid_states); - arm_biquad_cascade_df1_init_f32(&highnotch_deltoid, 2 , highnotch_const, highnotch_deltoid_states); - emgtimer.attach(looper_emg, 0.01); + + Enabley = 1; + wait(1); + lcd.printf("Start homing"); + wait(2); + lcd.cls(); + wait(1); + Enabley = 0; + + //Homing of the motor, so you start from the same position every time. + while(errory > error_tresh) { + + Ps_y = Posy.read(); + errory = fabs(Ps_y - Pt_y); + lcd.printf("%.2f \n", Stepy.read()); + + if (Ps_y > 0.50 && errory > error_tresh) { + Diry = 0; + cy = errory * H_Gain; + float hnew_step_freqy; + hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy); + hstep_freqy = hnew_step_freqy; + Stepy.period(1.0/hstep_freqy); + wait(0.01); + } + - //Ticker looptimer1; - //looptimer1.attach(looper_motorx, 0.01); //X-Spindle motor, why this freq? + if (Ps_y < 0.50 && errory > error_tresh) { + Diry = 1; + cy = errory * H_Gain; + float hnew_step_freqy; + hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy); + hstep_freqy = hnew_step_freqy; + Stepy.period(1.0/hstep_freqy); + wait(0.01); + } - //Ticker looptimer2; - //looptimer2.attach(looper_motory, 0.01); //Y-Spindle motor + } + lcd.printf("Done"); + wait(5); + lcd.cls(); + wait(1); + + Enabley = 1; + wait(3); + lcd.printf("Start EMG Control"); + wait(2); + lcd.cls(); + wait(1); + + Enabley = 0; - //Microstepping control, now configured as half stepping (MS1=1,MS2=0,MS3=0) + MS1 = 1; + MS2 = 0; + MS3 = 0; + + Stepy.write(0.5); + + Ticker emgtimer; //biceps + arm_biquad_cascade_df1_init_f32(&lowpass_biceps, 1 , lowpass_const, lowpass_biceps_states); + arm_biquad_cascade_df1_init_f32(&highnotch_biceps, 2 , highnotch_const, highnotch_biceps_states); + //triceps + arm_biquad_cascade_df1_init_f32(&lowpass_triceps, 1 , lowpass_const, lowpass_triceps_states); + arm_biquad_cascade_df1_init_f32(&highnotch_triceps, 2 , highnotch_const, highnotch_triceps_states); + emgtimer.attach(looper_emg, 0.01); + + Ticker looptimer2; + looptimer2.attach(looper_motory, 0.01); //Y-Spindle motor + + //Microstepping control, now configured as half stepping (MS1=1,MS2=0,MS3=0) - while (1) { - - - emg_y = (filtered_biceps - filtered_triceps); - emg_y_abs = fabs(emg_y); - force1 = emg_y_abs*K_Gain; - force1 = force1 - damping1; - acc1 = force1/Mass; - speed1 = speed_old1 + (acc1 * dt); - damping1 = speed1 * Damp; - step_freq1 = setpoint * speed1; - - Stepy.period(1.0/step_freq1); - speed_old1 = speed1; - - if (emg_y > 0) { - Diry = 1; - } + while (1) { - if (emg_y < 0) { - Diry = 0; - } - //Speed limit - if (speed1 > 1) { - speed1 = 1; - step_freq1 = setpoint; - } - //EMG treshold - if (filtered_biceps < EMG_tresh1 && filtered_triceps < EMG_tresh2) { - Enabley = 1; //Enable = 1 turns the motor off. - } else { - Enabley = 0; - } - wait(0.01); - - - emg_x = (filtered_pect - filtered_deltoid); - emg_x_abs = fabs(emg_x); - force2 = emg_x_abs*K_Gain; - force2 = force2 - damping2; - acc2 = force2/Mass; - speed2 = speed_old2 + (acc2 * dt); - damping2 = speed2 * Damp; - step_freq2 = setpoint * speed2; - - Stepx.period(1.0/step_freq2); - speed_old2 = speed2; - - if (emg_x > 0) { - Dirx = 0; - } - if (emg_x < 0) { - Dirx = 1; - } - //Speed limit - if (speed2 > 1) { - speed2 = 1; - step_freq2 = setpoint; - } - //EMG treshold - if (filtered_pect < EMG_tresh3 && filtered_deltoid < EMG_tresh4) { - Enablex = 1; //Enable = 1 turns the motor off. - } else { - Enablex = 0; - } - wait(0.01); + //lcd.printf("x %.2f, y %.2f \n", Posx.read(), Posy.read()); + lcd.printf("%.2f %.2f %.2f %.2f \n", Stepy.read(), step_freq1); + wait(0.01); - //lcd.printf("x %.2f, y %.2f \n", Posx.read(), Posy.read()); - lcd.printf("%.2f %.2f %.2f %.2f \n", Stepx.read(), step_freq1, Stepy.read(), step_freq2); - wait(0.01); - - } -} + } + }