pid gecomment
Dependencies: HIDScope MODSERIAL QEI biquadFilter mbed
Fork of a_pid_kal_end_def by
Diff: main.cpp
- Revision:
- 18:d7695ac04de3
- Parent:
- 17:4548efffe193
- Child:
- 19:fb98ff1d06ed
--- a/main.cpp Mon Oct 24 11:45:07 2016 +0000 +++ b/main.cpp Mon Oct 24 12:09:25 2016 +0000 @@ -4,60 +4,70 @@ #include "BiQuad.h" //Define objects -AnalogIn emg_biceps_right_in( A0 ); //analog in to get EMG from right biceps in to c++ -AnalogIn emg_triceps_right_in (A1); //analog in to get EMG from left biceps in to c++ +AnalogIn emg_biceps_right_in( A0 ); //analog in to get EMG in to c++ Ticker sample_timer; //ticker -HIDScope scope( 3); //open 3 channels in hidscope +HIDScope scope( 3); //open 3 channels in hidscope DigitalOut richting_motor1(D4); //motor1 direction output PwmOut pwm_motor1(D5); //motor1 velocity output DigitalOut led(LED_GREEN); +DigitalIn button (D9); //define variables +double emg_biceps_right; +double emg_filtered_high_biceps_right; +double emg_abs_biceps_right; +double emg_filtered_biceps_right; int onoffsignal=0; -double cut_off_value=0.08; //gespecifeerd door floortje -double signalsum_right; -double signalsum_right_filter_high; -double signalsum_right_filter_high_abs; -double signalsum_right_filtered; -double biceps_right; -double triceps_right; +double cut_off_value=0.20; +double max_right_biceps; + BiQuad filterhigh(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01); BiQuad filterlow (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01); //functions which are called in ticker void filter(){ - - biceps_right = emg_biceps_right_in.read(); //inladen data - triceps_right = emg_triceps_right_in.read(); //inladen data - signalsum_right = biceps_right-triceps_right; // inladen data - - signalsum_right_filter_high = filterhigh.step(signalsum_right); - signalsum_right_filter_high_abs = fabs(signalsum_right_filter_high); - signalsum_right_filtered = filterlow.step(signalsum_right_filtered); + emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes + emg_filtered_high_biceps_right= filterhigh.step(emg_biceps_right); + emg_abs_biceps_right=fabs(emg_filtered_high_biceps_right); //fabs because float + emg_filtered_biceps_right=filterlow.step(emg_abs_biceps_right); + led=!led; - if (signalsum_right_filtered>cut_off_value) + if (emg_filtered_biceps_right>cut_off_value) {onoffsignal=1;} else {onoffsignal=0;} //send signals to scope - scope.set(0, biceps_right ); //set emg signal to scope in channel 1 - scope.set(1, triceps_right); - scope.set(2, signalsum_right_filtered); + scope.set(0, emg_biceps_right ); //set emg signal to scope in channel 1 + scope.set(1, emg_filtered_biceps_right); + scope.set(2, onoffsignal); scope.send(); //send all the signals to the scope } - + +void calibration(){ + for(int n =0; n<5000;n++){ //read for 5000 samples as calibration + emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes + emg_filtered_high_biceps_right= filterhigh.step(emg_biceps_right); //highpass + emg_abs_biceps_right=fabs(emg_filtered_high_biceps_right); //fabs because float + emg_filtered_biceps_right=filterlow.step(emg_abs_biceps_right); //lowpass to envelope + if (emg_filtered_biceps_right > max_right_biceps){ //determine what the highest reachable emg signal is + max_right_biceps = emg_filtered_biceps_right; + } + + cut_off_value=0.2*max_right_biceps; + } //program int main() -{ +{ sample_timer.attach(&filter, 0.001); //continously execute the EMG reader and filter, it ensures that filter and sampling is executed every 1/frequency seconds //endless loop - - while(1) - { + while(1) { + if(button==0){ + &calibri(); } //if button pressed calibration starts again! + if (onoffsignal==1) { richting_motor1 = 0; //motordirection (ccw) @@ -68,9 +78,7 @@ { richting_motor1 = 0; //motordirection (ccw) pwm_motor1 = 0; //motorspeed 0 - - } + } - } - + } } \ No newline at end of file