pid gecomment
Dependencies: HIDScope MODSERIAL QEI biquadFilter mbed
Fork of a_pid_kal_end_def by
Diff: main.cpp
- Revision:
- 42:7164ccd2aa14
- Parent:
- 41:9ea3d5921f07
- Child:
- 43:7d0b2dc05b80
--- a/main.cpp Tue Nov 01 09:14:55 2016 +0000 +++ b/main.cpp Tue Nov 01 10:30:56 2016 +0000 @@ -8,13 +8,17 @@ AnalogIn emg_biceps_right_in( A0 ); //analog in to get EMG biceps (r) in to c++ AnalogIn emg_triceps_right_in(A1); //analog in to get EMG triceps (r) in to c++ AnalogIn emg_biceps_left_in (A2); //analog in to get EMG biceps (l) in to c++ + +InterruptIn button_calibration_biceps (SW3); //button to start calibration biceps +InterruptIn button_calibration_triceps (SW2); // button to start calibration tricps + Ticker sample_timer; //ticker Ticker switch_function; //ticker HIDScope scope(5); //open 3 channels in hidscope MODSERIAL pc(USBTX, USBRX); //pc connection DigitalOut red(LED_RED); DigitalOut green(LED_GREEN); - +DigitalOut blue(LED_BLUE); //motors DigitalOut richting_motor1(D4); PwmOut pwm_motor1(D5); @@ -25,12 +29,18 @@ //other int onoffsignal_biceps=0; // on/off signal: 1; biceps activation, 0: nothing, -1, triceps activation int switch_signal_triceps=0; // switching between motors. -double cut_off_value_biceps_right =0.04; //gespecificeerd door floortje -double cut_off_value_biceps_left=-0.04; -double cut_off_value_triceps=0.05; //gespecificeerd door floortje +volatile double cut_off_value_biceps_right =0.04; //gespecificeerd door floortje +volatile double cut_off_value_biceps_left =-0.04; +volatile double cut_off_value_triceps=0.04; //gespecificeerd door floortje double signal_biceps_sum; -int n = 0; //start van de teller wordt op nul gesteld - +int motorswitch= 0; //start van de teller wordt op nul gesteld + +//variables and constants for calibration +const float percentage_max_triceps=0.3; +const float percentage_max_biceps =0.3; +double max_biceps; //calibration maximum biceps +double max_triceps; //calibration maximum triceps + //biceps arm 1, right arm double emg_biceps_right; double emg_filtered_high_biceps_right; @@ -76,40 +86,119 @@ void SwitchN() { // maakt simpele functie die 1 bij n optelt if(switch_signal_triceps==1) { - n++; - green=!green; - red=!red; - if (n%2==0) - {pc.printf("If you contract the biceps of right arm, the robot will go up \r\n"); + motorswitch++; + + if (motorswitch%2==0) + {pc.printf("If you contract the right arm, the robot will go right \r\n"); + pc.printf("If you contract biceps of the left arm, the robot will go left \r\n"); + pc.printf("\r\n"); + green=0; + red=1; + } + + else + {pc.printf("If you contract the biceps of right arm, the robot will go up \r\n"); pc.printf("If you contract the biceps of left arm, the robot will go down \r\n"); pc.printf("\r\n"); - } - - else - {pc.printf("If you contract the right arm, the robot will go right \r\n"); - pc.printf("If you contract biceps of the left rm, the robot will go left \r\n"); - pc.printf("\r\n"); - + green=1; + red=0; + } + } } //functions which are called in ticker to sample the analog signal +//callibration +void calibration_biceps(){ + pc.printf("start of calibration biceps, contract maximal \n"); + red=1; + green=1; + blue=0; + + for(int n =0; n<3000;n++) //read for 2000 samples as calibration + { + emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes + emg_filtered_high_biceps_right= filterhigh_b1.step(emg_biceps_right); + emg_filtered_high_notch_1_biceps_right=filternotch1_b1.step(emg_filtered_high_biceps_right); + emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_biceps_right); //fabs because float + emg_filtered_biceps_right=filterlow_b1.step(emg_abs_biceps_right); + + if (emg_filtered_biceps_right > max_biceps) //determine what the highest reachable emg signal is + { + max_biceps = emg_filtered_biceps_right; + + } + wait(0.001f); //to sample at same freq; 1000Hz + } + cut_off_value_biceps_right=percentage_max_biceps*max_biceps; + cut_off_value_biceps_left=-cut_off_value_biceps_right; + //toggle lights + blue=!blue; + + pc.printf(" end of calibration\r\n",cut_off_value_biceps_right ); + pc.printf(" change of cv biceps: %f ",cut_off_value_biceps_right ); + + wait(0.5f); + + if (motorswitch%2==0) + {green=0; + red=1;} + + else {green=1; + red=0;} + + } + +void calibration_triceps(){ + red=1; + green=1; + blue=0; + + pc.printf("start of calibration triceps\r\n"); + + for(int n =0; n<3000;n++) //read for 2000 samples as calibration + { + emg_triceps_right=emg_triceps_right_in.read(); //read the emg value from the elektrodes + emg_filtered_high_triceps_right= filterhigh_t1.step(emg_triceps_right); + emg_filtered_high_notch_1_triceps_right=filternotch1_t1.step(emg_filtered_high_triceps_right); + emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_triceps_right); //fabs because float + emg_filtered_triceps_right=filterlow_t1.step(emg_abs_triceps_right); + + if (emg_filtered_triceps_right > max_triceps) //determine what the highest reachable emg signal is + { + max_triceps = emg_filtered_triceps_right; + + } + wait(0.001f); //to sample at same freq; 1000Hz + } + cut_off_value_triceps=percentage_max_triceps*max_triceps; + pc.printf(" end of calibration\r\n"); + pc.printf(" change of cv triceps: %f ",cut_off_value_triceps ); + blue=!blue; + wait(0.5f); + if (motorswitch%2==0) + {green=0; + red=1;} + + else {green=1; + red=0;} + + } + void filter(){ //biceps right arm read+filtering emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes emg_filtered_high_biceps_right= filterhigh_b1.step(emg_biceps_right); emg_filtered_high_notch_1_biceps_right=filternotch1_b1.step(emg_filtered_high_biceps_right); - //emg_filtered_high_notch_1_2_biceps_right=filternotch2_b1.step(emg_filtered_high_notch_1_biceps_right); - emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_biceps_right); //fabs because float + emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_biceps_right); //fabs because float emg_filtered_biceps_right=filterlow_b1.step(emg_abs_biceps_right); //triceps right arm read+filtering emg_triceps_right=emg_triceps_right_in.read(); //read the emg value from the elektrodes emg_filtered_high_triceps_right= filterhigh_t1.step(emg_triceps_right); emg_filtered_high_notch_1_triceps_right=filternotch1_t1.step(emg_filtered_high_triceps_right); - //emg_filtered_high_notch_1_2_triceps_right=filternotch2_t1.step(emg_filtered_high_notch_1_triceps_right); emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_triceps_right); //fabs because float emg_filtered_triceps_right=filterlow_t1.step(emg_abs_triceps_right); @@ -160,12 +249,31 @@ int main() { -pc.baud(115200); -green=0; -red=1; +pc.baud(115200); //connect with pc with baudrate 115200 + +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 +switch_function.attach(&SwitchN,1.0); //switch is every second available +button_calibration_biceps.fall (&calibration_biceps); //to call calibration biceps, stop everything else +button_calibration_triceps.fall(&calibration_triceps); //to call calibration triceps, stop everything else -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 -switch_function.attach(&SwitchN,1.5); + if (motorswitch%2==0) + { pc.printf("If you contract the right arm, the robot will go right \r\n"); + pc.printf("If you contract biceps of the left arm, the robot will go left \r\n"); + pc.printf("\r\n"); + green=0; + red=1; + blue=1; + } + + else + {pc.printf("If you contract the biceps of right arm, the robot will go up \r\n"); + pc.printf("If you contract the biceps of left arm, the robot will go down \r\n"); + pc.printf("\r\n"); + green=1; + red=0; + blue=1; + + } //endless loop @@ -174,10 +282,12 @@ if (onoffsignal_biceps==-1) // als s ingedrukt wordt gebeurd het volgende { - if (n%2==0) // als s ingedrukt wordt en het getal is even gebeurd het onderstaande + if (motorswitch%2==0) // als s ingedrukt wordt en het getal is even gebeurd het onderstaande { richting_motor1 = 1; pwm_motor1 = 1; + + } @@ -185,22 +295,24 @@ { richting_motor2 = 1; pwm_motor2 = 1; - + } } else if (onoffsignal_biceps==1) // als d ingedrukt wordt gebeurd het volgende { - if (n%2==0) // als d is ingedrukt en n is even dan gebeurd het volgende + if (motorswitch%2==0) // als d is ingedrukt en n is even dan gebeurd het volgende { richting_motor1 = 0; pwm_motor1 = 1; + } else // als d is ingedrukt maar het getal is niet even (dus oneven) gebeurt het onderstaande { richting_motor2 = 0; pwm_motor2 = 1; + } }