repurposed void update_encoder copy

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Fork of EMG_controlled_Inv_Kin_PID_Control by Marlowe Noll

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "stdio.h"
00002 #include "math.h"
00003 #include "mbed.h"
00004 #include "QEI.h"
00005 #include "MODSERIAL.h"
00006 #include "BiQuad.h"
00007 #include "HIDScope.h"
00008 
00009 MODSERIAL pc(USBTX, USBRX);
00010 QEI wheel_M1 (D13, D12, NC, 32);
00011 QEI wheel_M2 (D10, D11, NC, 32);
00012 PwmOut pwm_M1 (D6);
00013 PwmOut pwm_M2 (D5);
00014 DigitalOut dir_M1 (D7);
00015 DigitalOut dir_M2 (D4);
00016 
00017 Ticker emgticker;
00018 AnalogIn emgB(A0);
00019 AnalogIn emgT(A1);
00020 AnalogIn emgS(A2);
00021 HIDScope scope(4);
00022 
00023 DigitalOut ledg (LED_GREEN);
00024 DigitalOut ledr (LED_RED);
00025 DigitalOut ledb (LED_BLUE);
00026 InterruptIn knop_biceps(SW2);
00027 InterruptIn knop_triceps(SW3);
00028 InterruptIn knop_switch(D9);
00029 InterruptIn knop_calibrate(PTC12);
00030 BiQuadChain filter1b;
00031 BiQuadChain filter2b;
00032 BiQuadChain filter1t;
00033 BiQuadChain filter2t;
00034 BiQuadChain filter1s;
00035 BiQuadChain filter2s;
00036 
00037 BiQuad bq1b(8.5977e-01, -1.7195e+00, 8.5977e-01, -1.7347e+00,  7.6601e-01);   // Notch + HP
00038 BiQuad bq2b(1.0000e+00, -1.6182e+00, 1.0000e+00, -1.5933e+00,  9.8217e-01);   // Notch + HP
00039 BiQuad bq3b(1.0000e+00, -1.6182e+00, 1.0000e+00, -1.6143e+00 , 9.8260e-01);   // Notch + HP
00040 BiQuad bq4b(3.4604e-04,  6.9208e-04, 3.4604e-04, -1.9467e+00,  9.4808e-01);   // LP
00041 
00042 BiQuad bq1t(8.5977e-01, -1.7195e+00, 8.5977e-01, -1.7347e+00,  7.6601e-01);   // Notch + HP
00043 BiQuad bq2t(1.0000e+00, -1.6182e+00, 1.0000e+00, -1.5933e+00,  9.8217e-01);   // Notch + HP
00044 BiQuad bq3t(1.0000e+00, -1.6182e+00, 1.0000e+00, -1.6143e+00 , 9.8260e-01);   // Notch + HP
00045 BiQuad bq4t(3.4604e-04,  6.9208e-04, 3.4604e-04, -1.9467e+00,  9.4808e-01);   // LP
00046 
00047 BiQuad bq1s(8.5977e-01, -1.7195e+00, 8.5977e-01, -1.7347e+00,  7.6601e-01);   // Notch + HP
00048 BiQuad bq2s(1.0000e+00, -1.6182e+00, 1.0000e+00, -1.5933e+00,  9.8217e-01);   // Notch + HP
00049 BiQuad bq3s(1.0000e+00, -1.6182e+00, 1.0000e+00, -1.6143e+00 , 9.8260e-01);   // Notch + HP
00050 BiQuad bq4s(3.4604e-04,  6.9208e-04, 3.4604e-04, -1.9467e+00,  9.4808e-01);   // LP
00051 
00052 volatile double bEMG_max = 0;
00053 volatile double tEMG_max = 0;
00054 volatile double sEMG_max = 0;
00055 const double percentage_threshold_biceps = 0.09/0.171536;           // 0.171536 is max aanspanning
00056 const double percentage_threshold_triceps = 0.07/0.203654;          // 0.203654 is max aanspanning
00057 const double percentage_threshold_switch = 0.09/0.171536;           // gekopieerd van andere biceps
00058 volatile double threshold_biceps = 0;
00059 volatile double threshold_triceps = 0;
00060 volatile double threshold_switch = 0;
00061 volatile bool calibrate_biceps = false;
00062 volatile bool calibrate_triceps = false;
00063 volatile bool calibrate_switch = false;
00064 volatile bool calibration_finished = false;
00065 
00066 const double pi = 3.14159265359;
00067 
00068 volatile double q1 = 0;
00069 volatile double q2 = 0;
00070 const double l1 = 0.3626;
00071 const double l2 = (0.420-0.065);        // middelpunt swiffer
00072 volatile double q1_v;
00073 volatile double q2_v;
00074 volatile double q1_ref = 0;
00075 volatile double q2_ref = 0;
00076 volatile double q1_ref_prev = 0;
00077 volatile double q2_ref_prev = 0;
00078 volatile double q1_error = 0;
00079 volatile double q2_error = 0;
00080 volatile double q1_error_prev = 0;
00081 volatile double q2_error_prev = 0;
00082 volatile double q1DerivativeError = 0;
00083 volatile double q2DerivativeError = 0;
00084 volatile double q1IntError = 0;
00085 volatile double q2IntError = 0;
00086 volatile double q1_total_error= 0;
00087 volatile double q2_total_error= 0;
00088 double ctrlOutput_M1 = 0;
00089 double ctrlOutput_M2 = 0;
00090 volatile double vx;
00091 volatile double vy;
00092 volatile bool translatie_richting = true;  //true is verticaal, false is horizontaal
00093 
00094 const double TS = 0.02;
00095 const double MotorGain_M1 = 4.3;         // bij pwm = 1 draait (losse) motor met 4.3 rad/s -> gemeten
00096 const double MotorGain_M2 = 4.7;         // gemeten
00097 
00098 Ticker update_encoder_ticker;
00099 volatile bool go_flag_update_encoder = false;
00100 void flag_update_encoder()
00101 {
00102     go_flag_update_encoder = true;
00103 }
00104 
00105 void update_encoder()
00106 {
00107     pc.printf("vx = %f \tvy = %f \tq1_r = %f \tq2_r = %f \tq1 = %f \tq2 = %f \tpwm_M1 = %f \tpwm_M2 = %f\n\r",vx,vy,q1_ref,q2_ref,q1,q2,pwm_M1.read(),pwm_M2.read());
00108 }
00109 
00110 Ticker end_calibration_biceps_ticker;
00111 void end_calibration_biceps()
00112 {
00113     ledr = 1;
00114     calibrate_biceps = false;
00115     end_calibration_biceps_ticker.detach();
00116 }
00117 
00118 Ticker end_calibration_triceps_ticker;
00119 void end_calibration_triceps()
00120 {
00121     ledg = 1;
00122     calibrate_triceps = false;
00123     end_calibration_triceps_ticker.detach();
00124 }
00125 
00126 Ticker end_calibration_switch_ticker;
00127 void end_calibration_switch()
00128 {
00129     ledb = 1;
00130     calibrate_switch = false;
00131     end_calibration_switch_ticker.detach();
00132     calibration_finished = true;
00133 }
00134 
00135 volatile int n = 0;
00136 void start_calibration()
00137 {
00138     calibration_finished = false;
00139     n++;
00140     if (n == 1) {
00141         ledr = 0;
00142         bEMG_max = 0;
00143         calibrate_biceps = true;
00144         end_calibration_biceps_ticker.attach(&end_calibration_biceps, 10);
00145     }
00146     if (n == 2) {
00147         ledg = 0;
00148         tEMG_max = 0;
00149         calibrate_triceps = true;
00150         end_calibration_triceps_ticker.attach(&end_calibration_triceps, 10);
00151     }
00152     if (n == 3) {
00153         ledb = 0;
00154         sEMG_max = 0;
00155         calibrate_switch = true;
00156         end_calibration_switch_ticker.attach(&end_calibration_switch, 10);
00157         n = 0;
00158     }    
00159 }
00160 
00161 
00162 Ticker PIDcontrol;
00163 volatile bool go_flag_controller = false;
00164 
00165 void flag_controller()
00166 {
00167     go_flag_controller = true;
00168 }
00169 
00170 volatile bool active_PID_ticker = false;
00171 
00172 void begin_hoeken()
00173 {
00174     wait(1);
00175     q1_ref = wheel_M1.getPulses()/(1334.355/2);
00176     q2_ref = wheel_M2.getPulses()/(1334.355/2);
00177     active_PID_ticker = true;
00178 }
00179 
00180 void initialize()
00181 {
00182     dir_M1 = 0; //ccw
00183     dir_M2 = 1; //cw
00184     while ( (q1 < 20*2*pi/360) || (q2 > -45*2*pi/360) ) {
00185         q1 = wheel_M1.getPulses()/(1334.355/2);
00186         q2 = wheel_M2.getPulses()/(1334.355/2);
00187         if (q1 < 20*2*pi/360) {
00188             pwm_M1 = 0.05;
00189         } else {
00190             pwm_M1 = 0;
00191         }
00192         if (q2 > -45*2*pi/360) {
00193             pwm_M2 = 0.06;
00194         } else {
00195             pwm_M2 = 0;
00196         }
00197         wait(0.005f);
00198     }
00199     pwm_M1 = 0;
00200     pwm_M2 = 0;
00201     begin_hoeken();
00202 }
00203 
00204 void biceps()
00205 {
00206     q1_ref_prev = 0;
00207     q2_ref_prev = 0;
00208     q1IntError = 0;
00209     q2IntError = 0;
00210     q1_error_prev = 0;
00211     q2_error_prev = 0;
00212     if (translatie_richting == true) {      // verticaal / up
00213         vx = 0;
00214         vy = 0.1;
00215     } else {                        // horizontaal / right
00216         vx = 0.1;
00217         vy = 0;
00218     }
00219 }
00220 
00221 void triceps()
00222 {
00223     q1_ref_prev = 0;
00224     q2_ref_prev = 0;
00225     q1IntError = 0;
00226     q2IntError = 0;
00227     q1_error_prev = 0;
00228     q2_error_prev = 0;
00229     if (translatie_richting == true) {      // verticaal / down
00230         vx = 0;
00231         vy = -0.1;
00232     } else {                        // horizontaal / left
00233         vx = -0.1;
00234         vy = 0;
00235     }
00236 
00237 }
00238 
00239 void switcher()
00240 {
00241     if ( (vx == 0) && (vy == 0) && (translatie_richting == true) ) {
00242         translatie_richting = false;
00243     } else if ( (vx == 0) && (vy == 0) && (translatie_richting == false) ) {
00244         translatie_richting = true;
00245     } else {
00246         vx = 0;
00247         vy = 0;
00248         q1_ref = q1;
00249         q2_ref = q2;
00250         q1_error = 0;
00251         q2_error = 0;
00252         q1IntError = 0;
00253         q2IntError = 0;
00254         q1_error_prev = 0;
00255         q2_error_prev = 0;
00256         q1_total_error = 0;
00257         q2_total_error = 0;
00258     }
00259 
00260     if (translatie_richting == 1) {
00261         ledr = 1;                   // blauw - verticaal
00262         ledg = 1;
00263         ledb = 0;
00264     } else {
00265         ledr = 0;                   // rood - horizontaal
00266         ledg = 1;
00267         ledb = 1;
00268     }
00269 }
00270 
00271 Ticker switch_activate_ticker;
00272 volatile bool switch_active = true;
00273 void switch_activate()
00274 {
00275     switch_active = true;
00276 }
00277 
00278 volatile bool go_flag_emgsample = false;
00279 void flag_emgsample()
00280 {
00281     go_flag_emgsample = true;
00282 }
00283 
00284 void emgsample()
00285 {
00286     double bEMG_raw = emgB.read();
00287     double bEMG_HPfilt = filter1b.step( bEMG_raw );
00288     double bEMG_rect = abs(bEMG_HPfilt);
00289     double bEMG_filt = filter2b.step(bEMG_rect);
00290 
00291     double tEMG_raw = emgT.read();
00292     double tEMG_HPfilt = filter1t.step( tEMG_raw );
00293     double tEMG_rect = abs(tEMG_HPfilt);
00294     double tEMG_filt = filter2t.step(tEMG_rect);
00295 
00296     double sEMG_raw = emgS.read();
00297     double sEMG_HPfilt = filter1s.step( sEMG_raw );
00298     double sEMG_rect = abs(sEMG_HPfilt);
00299     double sEMG_filt = filter2s.step(sEMG_rect);
00300     
00301     if ((bEMG_filt > bEMG_max) && (calibrate_biceps == true) ) {
00302         bEMG_max = bEMG_filt;
00303         threshold_biceps = bEMG_max*percentage_threshold_biceps;
00304         }
00305     
00306     if ((tEMG_filt > tEMG_max) && (calibrate_triceps == true) ) {
00307         tEMG_max = tEMG_filt;
00308         threshold_triceps = tEMG_max*percentage_threshold_triceps;
00309         }
00310     
00311     if ((sEMG_filt > sEMG_max) && (calibrate_switch == true) ) {
00312         sEMG_max = sEMG_filt;
00313         threshold_switch = sEMG_max*percentage_threshold_switch;
00314         }
00315         
00316     scope.set(0, bEMG_filt);
00317     scope.set(1, tEMG_filt);
00318     scope.set(2, threshold_biceps);
00319     scope.set(3, threshold_triceps);
00320     scope.send();
00321     
00322     // motor aansturing, pas uitvoeren wanneer kalibratie klaar is
00323     if ( calibration_finished == true ) {
00324         if (sEMG_filt > threshold_switch) {
00325             if (switch_active == true) {
00326                 switcher();
00327                 switch_active = false;
00328                 switch_activate_ticker.attach(&switch_activate, 0.5f);
00329             }
00330         } else if (tEMG_filt > threshold_triceps) {
00331             triceps();
00332         } else if (bEMG_filt > threshold_biceps) {
00333             biceps();
00334         }
00335     }
00336 }
00337 
00338 Ticker update_ref_ticker;
00339 volatile double J_1;
00340 volatile double J_2;
00341 volatile double J_3;
00342 volatile double J_4;
00343 volatile bool go_flag_update_ref = false;
00344 void flag_update_ref()
00345 {
00346     go_flag_update_ref = true;
00347 }
00348 
00349 void update_ref()
00350 {
00351     q1 = wheel_M1.getPulses() / (1334.355/2);     // rad
00352     q2 = wheel_M2.getPulses() / (1334.355/2);
00353 
00354     J_1 = -(l2*sin(q1 + q2))/(l2*sin(q1 + q2)*(l2*cos(q1 + q2) + l1*cos(q1)) - l2*cos(q1 + q2)*(l2*sin(q1 + q2) + l1*sin(q1)));
00355     J_2 = (l2*cos(q1 + q2))/(l2*sin(q1 + q2)*(l2*cos(q1 + q2) + l1*cos(q1)) - l2*cos(q1 + q2)*(l2*sin(q1 + q2) + l1*sin(q1)));
00356     J_3 = (l2*sin(q1 + q2) + l1*sin(q1))/(l2*sin(q1 + q2)*(l2*cos(q1 + q2) + l1*cos(q1)) - l2*cos(q1 + q2)*(l2*sin(q1 + q2) + l1*sin(q1)));
00357     J_4 = -(l2*cos(q1 + q2) + l1*cos(q1))/(l2*sin(q1 + q2)*(l2*cos(q1 + q2) + l1*cos(q1)) - l2*cos(q1 + q2)*(l2*sin(q1 + q2) + l1*sin(q1)));
00358 
00359     q1_v = J_1 * vx + J_2 * vy;
00360     q2_v = J_3 * vx + J_4 * vy;
00361 
00362     if ( (q1 > (135*2*pi/360)) && (q1_v > 0 ) ) {                // WAARDES VINDEN 0.8726 (50 graden)
00363         q1_v = 0;
00364         q2_v = 0;
00365         q1_ref = q1;
00366         q2_ref = q2;
00367         q1IntError = 0;
00368         q2IntError = 0;
00369         q1_error_prev = 0;
00370         q2_error_prev = 0;
00371     } else if ( (q1 < -(135*2*pi/360)) && (q1_v < 0) ) {
00372         q1_v = 0;
00373         q2_v = 0;
00374         q1_ref = q1;
00375         q2_ref = q2;
00376         q1IntError = 0;
00377         q2IntError = 0;
00378         q1_error_prev = 0;
00379         q2_error_prev = 0;
00380     } else if ( (q2 < (-2.6*2*pi/360)) && (q2_v < 0) ) {        // WAARDES VINDEN -2.4434 (-140 graden) --> werkelijke max -2.672452
00381         q1_v = 0;
00382         q2_v = 0;
00383         q1_ref = q1;
00384         q2_ref = q2;
00385         q1IntError = 0;
00386         q2IntError = 0;
00387         q1_error_prev = 0;
00388         q2_error_prev = 0;
00389     } else if ( (q2 >= 0) && (q2_v > 0) ) {
00390         q1_v = 0;
00391         q2_v = 0;
00392         q1_ref = q1;
00393         q2_ref = q2;
00394         q1IntError = 0;
00395         q2IntError = 0;
00396         q1_error_prev = 0;
00397         q2_error_prev = 0;
00398     }
00399     
00400 
00401     q1_ref_prev = q1_ref;
00402     q2_ref_prev = q2_ref;
00403 
00404     q1_ref = q1_ref_prev + q1_v*TS;
00405     q2_ref = q2_ref_prev + q2_v*TS;
00406 }
00407 
00408 void PID(double q1,double q1_ref,double q2,double q2_ref,double TS,double &ctrlOutput_M1, double &ctrlOutput_M2)
00409 {
00410     // linear feedback control
00411     q1_error = q1_ref - q1; //referencePosition1 - Position1;             // proportional angular error in radians
00412     q2_error = q2_ref - q2; //referencePosition1 - Position1;             // proportional angular error in radians
00413     double Kp = 10;
00414 
00415     q1IntError = q1IntError + q1_error*TS;             // integrated error in radians
00416     q2IntError = q2IntError + q2_error*TS;             // integrated error in radians
00417     double Ki = 1;
00418 
00419     q1DerivativeError = (q1_error - q1_error_prev)/TS;  // derivative of error in radians
00420     q2DerivativeError = (q2_error - q2_error_prev)/TS;  // derivative of error in radians
00421     double Kd = 0;
00422 
00423     q1_total_error = (q1_error * Kp) + (q1IntError * Ki) + (q1DerivativeError * Kd);         //total controller output = motor input
00424     q2_total_error = (q2_error * Kp) + (q2IntError * Ki) + (q2DerivativeError * Kd);         //total controller output = motor input
00425 
00426     ctrlOutput_M1 = q1_total_error/MotorGain_M1;
00427     ctrlOutput_M2 = q2_total_error/MotorGain_M2;
00428 
00429     q1_error_prev = q1_error;
00430     q2_error_prev = q2_error;
00431 }
00432 
00433 void Controller()
00434 {
00435     PID(q1,q1_ref,q2,q2_ref,TS,ctrlOutput_M1,ctrlOutput_M2);
00436 
00437     if (ctrlOutput_M1 < 0) {
00438         dir_M1 = 1;
00439     } else {
00440         dir_M1 = 0;
00441     }
00442     pwm_M1 = abs(ctrlOutput_M1);
00443     if (pwm_M1 <= 0) {
00444         pwm_M1 = 0;
00445     } else {
00446         pwm_M1 = pwm_M1 + 0.05;
00447     }
00448 
00449     if (ctrlOutput_M2 < 0) {
00450         dir_M2 = 1;
00451     } else {
00452         dir_M2 = 0;
00453     }
00454     pwm_M2 = abs(ctrlOutput_M2);
00455     if (pwm_M2 <= 0) {
00456         pwm_M2 = 0;
00457     } else {
00458         pwm_M2 = pwm_M2 + 0.05;
00459     }
00460 }
00461 
00462 int main()
00463 {
00464     ledr = 1;
00465     ledg = 1;
00466     ledb = 1;
00467     pc.baud(115200);
00468     wheel_M1.reset();
00469     wheel_M2.reset();
00470     filter1b.add(&bq1b).add(&bq2b).add(&bq3b);
00471     filter2b.add(&bq4b);
00472     filter1t.add(&bq1t).add(&bq2t).add(&bq3t);
00473     filter2t.add(&bq4t);
00474     filter1s.add(&bq1s).add(&bq2s).add(&bq3s);
00475     filter2s.add(&bq4s);
00476     knop_biceps.rise(&biceps);
00477     knop_triceps.rise(&triceps);
00478     knop_switch.rise(&switcher);
00479     knop_calibrate.rise(&start_calibration);
00480 
00481     // initialize -> beginposities
00482     initialize();
00483 
00484     // flag functions/tickers
00485     emgticker.attach(&emgsample, 0.002f); // 500 Hz --> moet kloppen met frequentie gebruikt voor filter coefficienten
00486     update_encoder_ticker.attach(&flag_update_encoder, TS);
00487     update_ref_ticker.attach(&flag_update_ref, TS);
00488 
00489     if (active_PID_ticker == true) {
00490         PIDcontrol.attach(&flag_controller, TS);
00491     }
00492 
00493     while(1) {
00494         // sample EMG
00495         if (go_flag_emgsample == true) {
00496             go_flag_emgsample = false;
00497             emgsample();
00498         }
00499         // print variables v, q, qref, pwm
00500         if (go_flag_update_encoder == true) {
00501             go_flag_update_encoder = false;
00502             update_encoder();
00503         }
00504         // update joint positions/velocities
00505         if (go_flag_update_ref == true) {
00506             go_flag_update_ref = false;
00507             update_ref();
00508         }
00509         // controller M1+M2
00510         if (go_flag_controller == true) {
00511             go_flag_controller = false;
00512             Controller();
00513         }
00514     }
00515 }