![](/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:
- 21:dcc0fd9dbb08
- Parent:
- 15:ae8b209e8493
- Child:
- 22:3122a33793f6
diff -r ae8b209e8493 -r dcc0fd9dbb08 main.cpp --- a/main.cpp Wed Apr 29 09:42:09 2015 +0000 +++ b/main.cpp Thu May 07 15:03:15 2015 +0000 @@ -1,44 +1,162 @@ #include "mbed.h" #include "C12832_lcd.h" +#include "arm_math.h" +#include "HIDScope.h" #define P_GAIN 0.998 +//Motor control DigitalOut Dir(p21); PwmOut Step(p22); + +//Signal to and from computer +Serial pc(USBTX, USBRX); + DigitalOut Enable(p14); + +//Microstepping DigitalOut MS1(p27); DigitalOut MS2(p28); DigitalOut MS3(p29); + +//Potmeter and EMG AnalogIn Pot1(p19); -AnalogIn Pot2(p20); -//C12832_LCD lcd; +AnalogIn EMG1(p20); +HIDScope scope(2); +//lcd +C12832_LCD lcd; +//Joystick control (probably not necessary BusIn Joystick(p12,p13,p14,p15,p16); DigitalIn Up(p15); DigitalIn Down(p12); + +//Variables for motor control +float setpoint = 7000; //Frequentie +float step_freq = 1; + + +// Filters +arm_biquad_casd_df1_inst_f32 lowpass_pot; +arm_biquad_casd_df1_inst_f32 lowpass_step; + +//lowpass filter settings: Fc = 2 Hz, Fs = 100 Hz, Gain = 6 dB +float lowpass_const[] = {0.007820199259120319, 0.015640398518240638, 0.007820199259120319, 1.7347238224240125, -0.7660046194604936}; +//lowpass for step_freq: Fc = 2 Hz, Fs = 100, Gain = 6 dB +float lowpass1_const[] = {0.007820199259120319, 0.015640398518240638, 0.007820199259120319, 1.7347238224240125, -0.7660046194604936}; + +//EMG filter +arm_biquad_casd_df1_inst_f32 lowpass_biceps; +//lowpass filter settings biceps: Fc = 2 Hz, Fs = 500 Hz, Gain = -3 dB +float lowpass_const[] = {0.00015514839749793376, 0.00031029679499586753, 0.00015514839749793376, 1.9644602512795832, -0.9650808448695751}; +arm_biquad_casd_df1_inst_f32 highnotch_biceps; +//highpass filter settings: Fc = 10 Hz, Fs = 500 Hz, Gain = -3 dB, notch Fc = 50, Fs =500Hz, Gain = -3 dB +float highnotch_const[] = {0.9149684297741606, -1.8299368595483212, 0.9149684297741606, 1.8226935021735358, -0.8371802169231065 ,0.7063988100714527, -1.1429772843080923, 0.7063988100714527, 1.1429772843080923, -0.41279762014290533}; + + +//state values +float lowpass_biceps_states[4]; +float highnotch_biceps_states[8]; +float lowpass_pot_states[4]; +float lowpass1_step_states[4]; + +//global variabels +float filtered_biceps; +float filtered_pot; +float filtered_average_pot; +float filtered_step; +float pot_value1_f32; + +//Averaging (look if necessary) +/*void average_pot(float filtered_pot,float *average) +{ + static float total=0; + static float number=0; + total = total + filtered_pot; + number = number + 1; + if ( number == 50) { + *average = total/50; + total = 0; + number = 0; + } +}*/ +void looper_emg() +{ + /*variable to store value in*/ + uint16_t emg_value1; + + float emg_value1_f32; + + /*put raw emg value both in red and in emg_value*/ + emg_value1 = emg0.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V) + emg_value1_f32 = emg0.read(); + + //process emg biceps + arm_biquad_cascade_df1_f32(&highnotch_biceps, &emg_value1_f32, &filtered_biceps, 1 ); + filtered_biceps = fabs(filtered_biceps); + arm_biquad_cascade_df1_f32(&lowpass_biceps, &filtered_biceps, &filtered_biceps, 1 ); + + /*send value to PC. */ + scope.set(0,emg_value1); //Raw EMG signal biceps + scope.set(1,filtered_biceps); //Filtered signal + scope.send(); +} +void looper_pot() +{ + + pot_value1_f32 = Pot1.read() - 0.500; + + //process input + arm_biquad_cascade_df1_f32(&lowpass_pot, &pot_value1_f32, &filtered_pot, 1 ); + arm_biquad_cascade_df1_f32(&lowpass_step, &step_freq, &filtered_step, 1); + +} + +void looper_motor() +{ + float new_step_freq; + new_step_freq = (setpoint*pot_value1_f32*2); + step_freq = abs(new_step_freq); //Gives the PWM frequenty to the motor. + if (step_freq < 700) { + Step.period(0); + step_freq = 0; + } else { + Step.period(1.0/step_freq); + } +} int main() { - Enable = 0; - float setpoint = 10000; //Frequentie - float step_freq = 1; + Ticker log_timer; + //set up filters. Use external array for constants + arm_biquad_cascade_df1_init_f32(&lowpass_pot, 1 , lowpass_const, lowpass_pot_states); + log_timer.attach(looper_pot, 0.01); + + Ticker looptimer; + arm_biquad_cascade_df1_init_f32(&lowpass_step, 1, lowpass1_const, lowpass1_step_states); + looptimer.attach(looper_motor, 0.01); + MS1 = 1; - MS2 = 1; + MS2 = 0; MS3 = 0; - float p1; - float p2; - Step.period(1./step_freq); // 1 kHz, vanaf 2,5 kHz doet de motor het niet meer. + //Step.period(1./step_freq); // 1 kHz, vanaf 2,5 kHz doet de motor het niet meer. Step.write(0.5); // Duty cycle van 50% - // Dir = Pot1; // Dir 1 is naar boven, Dir 0 naar onder. - Enable = 1; + + + + while (1) { - p1 = Pot1.read(); - Dir = 0; - float new_step_freq; - new_step_freq = ((1-P_GAIN)*setpoint) + (P_GAIN*step_freq); - step_freq = new_step_freq; - Step.period(1.0/step_freq); - //lcd.printf("Pot1 : %f \n", p1); - wait(0.01); //Hier nog ticker inbouwen + + if (pot_value1_f32 < 0) { //Directie controle. + Dir = 0; + } else { + Dir = 1; + } + + + lcd.printf("Spd %.0f Hz p1 %.4f \n", step_freq, pot_value1_f32); //snelheid meting op lcd + pc.printf("Spd %.0f Hz p1 %.4f \n", step_freq, pot_value1_f32); //snelheid meting op lcd + wait(0.01); + } } \ No newline at end of file