![](/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:
- 85:a7493d3dd4ce
- Parent:
- 84:8b3f18260431
- Child:
- 86:9d781e7aa893
diff -r 8b3f18260431 -r a7493d3dd4ce main.cpp --- a/main.cpp Mon Jun 22 22:53:27 2015 +0000 +++ b/main.cpp Thu Jun 25 08:22:00 2015 +0000 @@ -1,5 +1,5 @@ /*Code by Jesse Kaiser, s1355783 for control of the 2DOF Planar Table -Some variables are also numbered at the end. The numbers stand for the muscle that controls it. +Some variables are also numbered at the end. The numbers stands for the muscle that controls it. Biceps = 1 Triceps = 2 Pectoralis Major = 3 @@ -31,8 +31,8 @@ #define error_tresh 0.03 //Motor control -DigitalOut Dirx(p21); -PwmOut Stepx(p22); +DigitalOut Dirx(p25); +PwmOut Stepx(p26); DigitalOut Diry(p23); PwmOut Stepy(p24); @@ -40,21 +40,21 @@ Serial pc(USBTX, USBRX); //Position sensors -AnalogIn Posx(p19); -AnalogIn Posy(p20); -DigitalOut Enablex(p25); -DigitalOut Enabley(p26); +AnalogIn Posx(p20); +AnalogIn Posy(p19); +DigitalOut Enablex(p27); +DigitalOut Enabley(p39); //Microstepping -DigitalOut MS1(p27); -DigitalOut MS2(p28); -DigitalOut MS3(p29); +DigitalOut MS1(p29); +DigitalOut MS2(p30); +DigitalOut MS3(p31); //EMG inputs -AnalogIn emg1(p15); //biceps +AnalogIn emg1(p15); //EMG bordje bovenop, biceps AnalogIn emg2(p16); //triceps -AnalogIn emg3(p17); //pectoralis major -AnalogIn emg4(p18); //deltoid +AnalogIn emg3(p17); +AnalogIn emg4(p18); //HIDScope scope(4); //Ticker scopeTimer; @@ -80,8 +80,7 @@ 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}; +float highnotch_const[] = {0.8370879899975344, -1.6741759799950688, 0.8370879899975344, 1.6474576182593796, -0.7008943417307579, 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, 1.1429772843080923, -0.41279762014290533}; //state values float lowpass_biceps_states[4]; @@ -95,11 +94,11 @@ //global variabels float filtered_biceps, filtered_triceps, filtered_pect, filtered_deltoid; -float speed_old1, speed_old2; -float acc1, acc2; -float force1, force2; -float speed1, speed2; -float damping1, damping2; +float speed_old1, speed_old2, speed_old3, speed_old4; +float acc1, acc2, acc3, acc4; +float force1, force2, force3, force4; +float speed1, speed2, speed3, speed4; +float damping1, damping2, damping3, damping4; float emg_x, emg_y; float cx = 0; float cy = 0; @@ -122,7 +121,7 @@ //process emg biceps arm_biquad_cascade_df1_f32(&highnotch_biceps, &emg_value1_f32, &filtered_biceps, 1 ); //High pass and notch filter - filtered_biceps = fabs(filtered_biceps); //Rectifier + filtered_biceps = fabs(filtered_biceps); //Rectifier, The Gain is already implemented. arm_biquad_cascade_df1_f32(&lowpass_biceps, &filtered_biceps, &filtered_biceps, 1 ); //low pass filter //process emg triceps @@ -147,9 +146,9 @@ scope.set(3,filtered_deltoid);*/ } -void looper_motory() +/*void looper_motory() { - //Motor control + emg_y = (filtered_biceps - filtered_triceps); emg_y_abs = fabs(emg_y); force1 = emg_y_abs*K_Gain; @@ -161,11 +160,10 @@ Stepy.period(1.0/step_freq1); speed_old1 = speed1; - //Direction control. 1 is up. if (emg_y > 0) { Diry = 1; } - //Direction control. 0 is down. + if (emg_y < 0) { Diry = 0; } @@ -181,11 +179,11 @@ Enabley = 0; } -} +}*/ void looper_motorx() { - //Motor control. + emg_x = (filtered_pect - filtered_deltoid); emg_x_abs = fabs(emg_x); force2 = emg_x_abs*K_Gain; @@ -196,11 +194,10 @@ step_freq2 = setpoint * speed2; Stepx.period(1.0/step_freq2); speed_old2 = speed2; - //Direction control. 0 is to the left. + if (emg_x > 0) { Dirx = 0; } - //Direction control. 1 is to the right. if (emg_x < 0) { Dirx = 1; } @@ -222,7 +219,7 @@ { // 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); - //Microstepping control, now configured as half stepping (MS1=1,MS2=0,MS3=0) + MS1 = 1; MS2 = 0; MS3 = 0; @@ -239,7 +236,6 @@ wait(1); Enablex = 0; Enabley = 0; - //Homing loop. while(errorx > error_tresh || errory > error_tresh) { Ps_x = Posx.read(); @@ -247,20 +243,24 @@ errorx = fabs(Pt_x - Ps_x); errory = fabs(Ps_y - Pt_y); lcd.printf("%.2f %.2f \n", errorx, errory); - + + + if (Ps_x < 0.50 && errorx > error_tresh) { + Dirx = 0; + //errorx = Pt_x - Ps_x; + cx = errorx * H_Gain; - if (Ps_x < Pt_x && 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 > Pt_y && errory > error_tresh) { + if (Ps_y > 0.50 && errory > error_tresh) { Diry = 0; + //errory = Ps_y - Pt_y; 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; @@ -268,18 +268,22 @@ wait(0.01); } - if (Ps_x > Pt_x && errorx > error_tresh) { + if (Ps_x > 0.50 && errorx > error_tresh) { Dirx = 1; + //errorx = Pt_x - Ps_x; 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 < Pt_y && errory > error_tresh) { + if (Ps_y < 0.50 && errory > error_tresh) { Diry = 1; + //errory = Ps_y - Pt_y; 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; @@ -302,6 +306,12 @@ 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); @@ -317,15 +327,20 @@ emgtimer.attach(looper_emg, 0.01); Ticker looptimer1; - looptimer1.attach(looper_motorx, 0.01); //X-Spindle motor + looptimer1.attach(looper_motorx, 0.01); //X-Spindle motor, why this freq? - Ticker looptimer2; - looptimer2.attach(looper_motory, 0.01); //Y-Spindle motor + //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) { - pc.printf("%.2f %.2f %.2f \n", Posy.read(), emg_y, step_freq1); //Send signal values to the computer. - wait(0.01); + + pc.printf("%.2f %.2f %.2f \n", Posx.read(), emg_x, step_freq2); //Send signal values to the computer. + wait(0.01); } }