Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HIDScope MODSERIAL QEI TextLCD mbed
Fork of TotalControlEmg2 by
main.cpp@25:c97d079e07f3, 2015-10-16 (annotated)
- Committer:
- RemcoDas
- Date:
- Fri Oct 16 08:04:48 2015 +0000
- Revision:
- 25:c97d079e07f3
- Parent:
- 24:ddd69385b55f
- Child:
- 26:d9855716ced7
Potmeter en EMG conflict
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bartvaart | 0:557b1ff83a8a | 1 | #include "mbed.h" |
RemcoDas | 24:ddd69385b55f | 2 | #include "QEI.h" |
RemcoDas | 24:ddd69385b55f | 3 | #include "MODSERIAL.h" |
RemcoDas | 24:ddd69385b55f | 4 | #include "TextLCD.h" |
Bartvaart | 7:040591b3f019 | 5 | #include "HIDScope.h" |
Bartvaart | 5:b400209df739 | 6 | #include "Filterdesigns.h" |
Bartvaart | 17:cfe44346645c | 7 | #include "Kalibratie.h" |
Bartvaart | 18:eec0880fcded | 8 | #include "Mode.h" |
RemcoDas | 24:ddd69385b55f | 9 | //--------------------Classes------------------------ |
RemcoDas | 24:ddd69385b55f | 10 | InterruptIn btnSet(PTC6); // kalibreer knop |
RemcoDas | 25:c97d079e07f3 | 11 | DigitalOut ledR(LED_RED), LedB(LED3); // Led op moederbord |
RemcoDas | 24:ddd69385b55f | 12 | MODSERIAL pc(USBTX, USBRX); // Modserial voor Putty |
RemcoDas | 24:ddd69385b55f | 13 | TextLCD lcd(PTC5, PTC7, PTC0, PTC9, PTC8, PTC1); // LCD scherm op binnenste rij van K64F, rs, e, d4-d7 |
RemcoDas | 24:ddd69385b55f | 14 | PwmOut servo(D3); // PwmOut servo |
RemcoDas | 25:c97d079e07f3 | 15 | AnalogIn emgL(A0), emgR(A1) // Analog input van emg kabels links en rechts |
RemcoDas | 24:ddd69385b55f | 16 | AnalogIn ptmrL(A2), ptmrR(A3); // Intensiteit 'EMG' signaal door potmeter |
RemcoDas | 24:ddd69385b55f | 17 | AnalogIn KS(A4); // Killswitch |
RemcoDas | 25:c97d079e07f3 | 18 | HIDScope scope(6); // 3 scopes |
RemcoDas | 25:c97d079e07f3 | 19 | Ticker EMGticker, tickerRegelaar, tickerBtn, tickerRem; // regelaar button en rem ticker |
Bartvaart | 0:557b1ff83a8a | 20 | |
RemcoDas | 24:ddd69385b55f | 21 | // QEI Encoder(poort 1, poort 2, ,counts/rev |
RemcoDas | 25:c97d079e07f3 | 22 | QEI enc1 (D13, D12, NC, 64), enc2 (D11, D10, NC, 64); |
RemcoDas | 24:ddd69385b55f | 23 | // Motor1 met PWM aansturen en richting aangeven |
RemcoDas | 24:ddd69385b55f | 24 | PwmOut pwmM1(D6); |
RemcoDas | 24:ddd69385b55f | 25 | DigitalOut dirM1(D7); |
RemcoDas | 24:ddd69385b55f | 26 | // Motor2 met PWM aansturen en richting aangeven |
RemcoDas | 24:ddd69385b55f | 27 | PwmOut pwmM2(D5); |
RemcoDas | 24:ddd69385b55f | 28 | DigitalOut dirM2(D4); |
RemcoDas | 24:ddd69385b55f | 29 | enum spelfase {KALIBRATE_EMG1, KALIBRATE_AIM, KALIBRATE_PEND, AIM, REM, FIRE}; // Spelfases, ACKNOWLEDGEMENT: BMT groep 4 2014 |
RemcoDas | 25:c97d079e07f3 | 30 | uint8_t state = KALIBRATE_EMG1; // eerste spelfase |
RemcoDas | 24:ddd69385b55f | 31 | enum aimFase {OFF, CW, CCW}; // Aim motor mogelijkheden |
RemcoDas | 24:ddd69385b55f | 32 | uint8_t aimState = OFF; // mogelijkheid begin |
RemcoDas | 25:c97d079e07f3 | 33 | //-------------------------------Variables--------------------------------------------------------------------- |
RemcoDas | 25:c97d079e07f3 | 34 | const int on = 0, off = 1; // aan / uit |
RemcoDas | 25:c97d079e07f3 | 35 | int maxCounts = 2100; // max richt motor counts Aim motor |
RemcoDas | 25:c97d079e07f3 | 36 | int remPerc = 0; |
RemcoDas | 25:c97d079e07f3 | 37 | volatile int emgModeL = 1, emgModeR = 1 potModeL = 1, potModeR = 1; // emg signaal waarden (modes) |
RemcoDas | 25:c97d079e07f3 | 38 | |
RemcoDas | 24:ddd69385b55f | 39 | const double servoL = 0.001, servoR = 0.0011; // uitwijking servo, bereik tussen L en R (= pulsewidth pwm servo) |
RemcoDas | 24:ddd69385b55f | 40 | const double tRegelaar = 0.005, tBtn = 0.005, tRem = 0.1; // tijd ticker voor regelaar en knoppen/EMG |
RemcoDas | 25:c97d079e07f3 | 41 | const double Fs = 200; //Sample frequentie Hz |
RemcoDas | 25:c97d079e07f3 | 42 | double t = 1/ Fs; // tijd EMGticker |
RemcoDas | 25:c97d079e07f3 | 43 | double ymin = 0, ymax = 0; |
RemcoDas | 25:c97d079e07f3 | 44 | double thresholdlow = 0, thresholdmid = 0, thresholdhigh= 0; |
RemcoDas | 25:c97d079e07f3 | 45 | |
RemcoDas | 24:ddd69385b55f | 46 | volatile bool L = false, R = false; // Booleans voor knop link en knop rechts |
RemcoDas | 25:c97d079e07f3 | 47 | volatile bool btn = false; // boolean om programma te runnen, drukknop ingedrukt |
RemcoDas | 25:c97d079e07f3 | 48 | volatile bool regelaarFlag = false, btnFlag = false, remFlag = false; // Go flags |
RemcoDas | 25:c97d079e07f3 | 49 | volatile bool emgFlag = false; |
Bartvaart | 18:eec0880fcded | 50 | |
RemcoDas | 24:ddd69385b55f | 51 | //----------------------------Functions----------------------------------------------------------------------- |
RemcoDas | 25:c97d079e07f3 | 52 | void flipLed(DigitalOut& led){ //function to flip one LED |
RemcoDas | 25:c97d079e07f3 | 53 | led.write(on); |
RemcoDas | 25:c97d079e07f3 | 54 | wait(0.2); |
RemcoDas | 25:c97d079e07f3 | 55 | led.write(off); |
RemcoDas | 25:c97d079e07f3 | 56 | } |
RemcoDas | 25:c97d079e07f3 | 57 | void PRINT(const char* text){ // putty en lcd print |
RemcoDas | 25:c97d079e07f3 | 58 | lcd.cls(); // clear LCD scherm |
RemcoDas | 25:c97d079e07f3 | 59 | lcd.printf(text); // print text op lcd |
RemcoDas | 25:c97d079e07f3 | 60 | pc.printf(text); // print text op terminal |
RemcoDas | 25:c97d079e07f3 | 61 | } |
RemcoDas | 25:c97d079e07f3 | 62 | void EMGkalibratie(){ // Bepalen thresholds, aangenomen dat spieren links en rechts hetzelfde zijn |
RemcoDas | 25:c97d079e07f3 | 63 | LedB = 1; |
RemcoDas | 24:ddd69385b55f | 64 | Init(); |
RemcoDas | 25:c97d079e07f3 | 65 | ymin = KalibratieMin(emgL); |
Bartvaart | 19:6c0245063b96 | 66 | wait(1); |
RemcoDas | 25:c97d079e07f3 | 67 | ymax = KalibratieMax(emgL); |
Bartvaart | 19:6c0245063b96 | 68 | |
Bartvaart | 18:eec0880fcded | 69 | // bepalen van thresholds voor aan/uit |
Bartvaart | 22:c1811e60bfce | 70 | thresholdlow = 8 * ymin; // standaardwaarde |
Bartvaart | 20:11e1244ad2ad | 71 | thresholdmid = 0.5 * ymax; // afhankelijk van max output gebruiker |
Bartvaart | 21:8fe8419de3e9 | 72 | thresholdhigh = 0.8 * ymax; |
Bartvaart | 22:c1811e60bfce | 73 | |
Bartvaart | 22:c1811e60bfce | 74 | pc.printf("ymax = %f en ymin = %f \n",ymax, ymin); //bugfix |
RemcoDas | 24:ddd69385b55f | 75 | } |
RemcoDas | 25:c97d079e07f3 | 76 | int EMGfilter(AnalogIn& emg, int w){ |
RemcoDas | 25:c97d079e07f3 | 77 | double u = emg.read(); // uitlezen emg signaal |
RemcoDas | 25:c97d079e07f3 | 78 | double y = Filterdesigns(u); // signaal filteren |
RemcoDas | 25:c97d079e07f3 | 79 | int mode = Mode(y, ymax, thresholdlow, thresholdmid, thresholdhigh); //bepaald welk signaal de motor krijgt (1, 2 ,3 ) |
RemcoDas | 25:c97d079e07f3 | 80 | |
RemcoDas | 25:c97d079e07f3 | 81 | scope.set(w,u); //ongefilterde waarde naar scope 1 |
RemcoDas | 25:c97d079e07f3 | 82 | scope.set(w+1,y); //gefilterde waarde naar scope 2 |
RemcoDas | 25:c97d079e07f3 | 83 | scope.set(w+2,emgModeL); |
RemcoDas | 25:c97d079e07f3 | 84 | scope.send(); //stuur de waardes naar HIDscope |
RemcoDas | 25:c97d079e07f3 | 85 | return mode; |
Bartvaart | 22:c1811e60bfce | 86 | } |
RemcoDas | 25:c97d079e07f3 | 87 | void setEmgFlag(){ |
RemcoDas | 25:c97d079e07f3 | 88 | emgFlag = true; |
RemcoDas | 24:ddd69385b55f | 89 | } |
RemcoDas | 24:ddd69385b55f | 90 | void btnSetAction(){ // Set knop K64F |
RemcoDas | 24:ddd69385b55f | 91 | btn = true; // GoFlag setknop |
RemcoDas | 24:ddd69385b55f | 92 | } |
RemcoDas | 24:ddd69385b55f | 93 | void setBtnFlag(){ // Go flag regelaar Aim motor |
RemcoDas | 24:ddd69385b55f | 94 | btnFlag = true; |
RemcoDas | 24:ddd69385b55f | 95 | } |
RemcoDas | 24:ddd69385b55f | 96 | void setRegelaarFlag(){ // Go flag button controle |
RemcoDas | 24:ddd69385b55f | 97 | regelaarFlag = true; |
RemcoDas | 24:ddd69385b55f | 98 | } |
RemcoDas | 24:ddd69385b55f | 99 | void setRemFlag(){ // Go flag rem |
RemcoDas | 24:ddd69385b55f | 100 | remFlag = true; |
RemcoDas | 24:ddd69385b55f | 101 | } |
RemcoDas | 24:ddd69385b55f | 102 | void checkAim(){ // controleer of killer switch geraakt is en of max aantal counts niet bereikt is |
RemcoDas | 24:ddd69385b55f | 103 | double volt = KS.read(); |
RemcoDas | 24:ddd69385b55f | 104 | if(volt< 0.5 /*|| abs(enc2.getPulses()) > maxCounts*/){ |
RemcoDas | 24:ddd69385b55f | 105 | pwmM2.write(0); // Aim motor stilzetten |
RemcoDas | 24:ddd69385b55f | 106 | pc.printf("BOEM! CRASH! KASTUK! \r\n"); |
RemcoDas | 24:ddd69385b55f | 107 | } |
RemcoDas | 24:ddd69385b55f | 108 | } |
RemcoDas | 24:ddd69385b55f | 109 | void motorAim(int dir){ // Aim motor laten draaien met gegeven direction |
RemcoDas | 24:ddd69385b55f | 110 | dirM2.write(dir); // richting |
RemcoDas | 24:ddd69385b55f | 111 | pwmM2.write(0.5); // width aanpassen |
RemcoDas | 24:ddd69385b55f | 112 | } |
RemcoDas | 24:ddd69385b55f | 113 | bool controlAim(){ // Function om aim motor aan te sturen |
RemcoDas | 24:ddd69385b55f | 114 | bool both = false; // boolean of beide knoppen ingedrukt zijn |
RemcoDas | 24:ddd69385b55f | 115 | |
RemcoDas | 24:ddd69385b55f | 116 | if(!(ptmrL.read() >0.3 && ptmrR.read()>0.3) != !(emgModeL!=1 && emgModeR!=1)) { // Potmeters L en R beide aan XOR EMG beide aangespannen |
RemcoDas | 24:ddd69385b55f | 117 | R = false; |
RemcoDas | 24:ddd69385b55f | 118 | L = false; |
RemcoDas | 24:ddd69385b55f | 119 | pwmM2.write(0); // Aim motor stilzetten |
RemcoDas | 24:ddd69385b55f | 120 | aimState = OFF; |
RemcoDas | 24:ddd69385b55f | 121 | } |
RemcoDas | 24:ddd69385b55f | 122 | else if(!(ptmrL.read() >= 0.8 != !(emgModeL == 3)) && L) { // Potmeter L vol ingedrukt XOR emgModeL is 3 |
RemcoDas | 24:ddd69385b55f | 123 | both = true; // Return true |
RemcoDas | 24:ddd69385b55f | 124 | pwmM2.write(0); // Aim motor stilzetten |
RemcoDas | 24:ddd69385b55f | 125 | aimState = OFF; |
RemcoDas | 24:ddd69385b55f | 126 | L = false; |
RemcoDas | 24:ddd69385b55f | 127 | } |
RemcoDas | 24:ddd69385b55f | 128 | else if((!(ptmrR.read() > 0.8) != !(emgModeR == 3)) && aimState!=OFF) { // Potmeter R vol ingedrukt XOR emgModeR == 3 |
RemcoDas | 24:ddd69385b55f | 129 | R = false; |
RemcoDas | 24:ddd69385b55f | 130 | pwmM2.write(0); // Aim motor stilzetten |
RemcoDas | 24:ddd69385b55f | 131 | aimState = OFF; |
RemcoDas | 24:ddd69385b55f | 132 | PRINT("Motor freeze\r\n"); |
RemcoDas | 24:ddd69385b55f | 133 | } |
RemcoDas | 24:ddd69385b55f | 134 | else if((!(ptmrL.read() > 0.3 && ptmrL.read() < 0.6) != !(emgModeL == 2)) && aimState != CCW && L) { // Potmeter L half XOR emgModeL ==2 AND richting is niet CCW |
RemcoDas | 24:ddd69385b55f | 135 | aimState = CCW; // draai CCW |
RemcoDas | 24:ddd69385b55f | 136 | pc.printf("Turn negative (CCW)\r\n"); |
RemcoDas | 24:ddd69385b55f | 137 | motorAim(0); |
RemcoDas | 24:ddd69385b55f | 138 | } |
RemcoDas | 24:ddd69385b55f | 139 | else if((!(ptmrR.read() > 0.3 && ptmrR.read() < 0.6) != !(emgModeR == 2)) && aimState != CW && R) { // Potmeter R half XOR emgModeR == 2 AND richting is niet CW |
RemcoDas | 24:ddd69385b55f | 140 | aimState = CW; // draai CW |
RemcoDas | 24:ddd69385b55f | 141 | PRINT("Turn positive (CW)\r\n"); |
RemcoDas | 24:ddd69385b55f | 142 | motorAim(1); |
RemcoDas | 24:ddd69385b55f | 143 | } |
RemcoDas | 24:ddd69385b55f | 144 | if(ptmrR.read() < 0.1 && emgModeR == 1 && !R){ // R is niet aan |
RemcoDas | 24:ddd69385b55f | 145 | R = true; |
RemcoDas | 24:ddd69385b55f | 146 | } |
RemcoDas | 24:ddd69385b55f | 147 | if(ptmrL.read() < 0.1 && emgModeL == 1 && !L){ // L is niet aan |
RemcoDas | 24:ddd69385b55f | 148 | L = true; |
RemcoDas | 24:ddd69385b55f | 149 | } |
RemcoDas | 24:ddd69385b55f | 150 | return both; |
RemcoDas | 24:ddd69385b55f | 151 | } |
RemcoDas | 24:ddd69385b55f | 152 | int main(){ |
RemcoDas | 24:ddd69385b55f | 153 | flipLed(ledR); // test of code begint |
RemcoDas | 24:ddd69385b55f | 154 | btnSet.mode(PullUp); // Button mode |
RemcoDas | 24:ddd69385b55f | 155 | btnSet.rise(&btnSetAction); // Setknop aan functie koppellen |
RemcoDas | 24:ddd69385b55f | 156 | tickerRegelaar.attach(&setRegelaarFlag,tRegelaar); // ticker regelaar motor |
RemcoDas | 24:ddd69385b55f | 157 | tickerBtn.attach(&setBtnFlag,tBtn); // ticker knop controle |
RemcoDas | 24:ddd69385b55f | 158 | tickerRem.attach(&setRemFlag,tRem); // ticker rem |
RemcoDas | 24:ddd69385b55f | 159 | |
RemcoDas | 25:c97d079e07f3 | 160 | EMGticker.attach(&setEmgFlag, t); // ticker EMG, 500H |
RemcoDas | 25:c97d079e07f3 | 161 | |
RemcoDas | 24:ddd69385b55f | 162 | pc.printf("\n\n\n\n\n"); |
RemcoDas | 25:c97d079e07f3 | 163 | PRINT("--- NEW GAME ---\r\n"); |
RemcoDas | 24:ddd69385b55f | 164 | while(1){ // Programma door laten lopen |
RemcoDas | 24:ddd69385b55f | 165 | switch(state){ |
RemcoDas | 24:ddd69385b55f | 166 | case KALIBRATE_EMG1: { |
RemcoDas | 24:ddd69385b55f | 167 | pc.printf("Kalibrate EMG signal in 5 seconds\r\n"); |
RemcoDas | 24:ddd69385b55f | 168 | lcd.cls(); |
RemcoDas | 24:ddd69385b55f | 169 | lcd.printf("Kalibrate EMG\n in 5 sec."); |
RemcoDas | 24:ddd69385b55f | 170 | |
Bartvaart | 22:c1811e60bfce | 171 | EMGkalibratie(); |
RemcoDas | 24:ddd69385b55f | 172 | |
RemcoDas | 24:ddd69385b55f | 173 | state = KALIBRATE_AIM; |
RemcoDas | 24:ddd69385b55f | 174 | break; |
RemcoDas | 24:ddd69385b55f | 175 | } |
RemcoDas | 24:ddd69385b55f | 176 | case KALIBRATE_AIM: { |
RemcoDas | 24:ddd69385b55f | 177 | pwmM2.period(1/100000); // aanstuurperiode motor 2 |
RemcoDas | 24:ddd69385b55f | 178 | PRINT("Kalibrate aim motor\r\n"); |
RemcoDas | 24:ddd69385b55f | 179 | wait(1); |
RemcoDas | 24:ddd69385b55f | 180 | pc.printf("Use L and R to move pendulum to equilibruim position\r\n L+R = OK\r\n"); |
RemcoDas | 24:ddd69385b55f | 181 | while(state==KALIBRATE_AIM){ |
RemcoDas | 24:ddd69385b55f | 182 | if(regelaarFlag){ // motor regelen op GoFlag |
RemcoDas | 24:ddd69385b55f | 183 | regelaarFlag = false; |
RemcoDas | 24:ddd69385b55f | 184 | checkAim(); // Controleer positie |
RemcoDas | 24:ddd69385b55f | 185 | } |
RemcoDas | 24:ddd69385b55f | 186 | |
RemcoDas | 25:c97d079e07f3 | 187 | if(emgFlag){ // Go flag EMG sampelen |
RemcoDas | 25:c97d079e07f3 | 188 | emgFlag = false; |
RemcoDas | 25:c97d079e07f3 | 189 | emgModeL = EMGfilter(emgL,0); |
RemcoDas | 25:c97d079e07f3 | 190 | emgModeR = EMGfilter(emgR,3); |
RemcoDas | 24:ddd69385b55f | 191 | } |
RemcoDas | 24:ddd69385b55f | 192 | if(btnFlag){ // Go flag button controle? |
RemcoDas | 24:ddd69385b55f | 193 | btnFlag = false; |
RemcoDas | 24:ddd69385b55f | 194 | if(controlAim()){ // Buttons met control, if true = beide knoppen = bevestigen |
RemcoDas | 24:ddd69385b55f | 195 | enc2.reset(); // resetknop = encoder 1 resetten |
RemcoDas | 24:ddd69385b55f | 196 | pc.printf("Positie gekalibreerd, kalibreer nu slinger, encoder counts: %i\r\n", enc2.getPulses()); |
RemcoDas | 24:ddd69385b55f | 197 | state = KALIBRATE_PEND; // volgende state |
RemcoDas | 24:ddd69385b55f | 198 | } |
RemcoDas | 24:ddd69385b55f | 199 | } |
RemcoDas | 24:ddd69385b55f | 200 | } |
RemcoDas | 24:ddd69385b55f | 201 | break; |
RemcoDas | 24:ddd69385b55f | 202 | } |
RemcoDas | 24:ddd69385b55f | 203 | case KALIBRATE_PEND: { |
RemcoDas | 24:ddd69385b55f | 204 | pc.printf("Kalibrate pendulum motor with setknop\r\n"); |
RemcoDas | 24:ddd69385b55f | 205 | pwmM1.period(1/100000); // aanstuurperiode motor 1 |
RemcoDas | 24:ddd69385b55f | 206 | servo.period(0.02); // 20ms period aanstuurperiode |
RemcoDas | 24:ddd69385b55f | 207 | pwmM1.write(0.5); // Motor aanzetten, laag vermogen |
RemcoDas | 24:ddd69385b55f | 208 | btn = false; |
RemcoDas | 24:ddd69385b55f | 209 | while(state==KALIBRATE_PEND){ |
RemcoDas | 24:ddd69385b55f | 210 | if(btnFlag){ |
RemcoDas | 24:ddd69385b55f | 211 | pc.printf(""); // lege regel printen, anders doet setknop het niet |
RemcoDas | 24:ddd69385b55f | 212 | btnFlag = false; |
RemcoDas | 24:ddd69385b55f | 213 | if (btn){ // Als setknop ingedrukt is reset |
RemcoDas | 24:ddd69385b55f | 214 | pwmM1.write(0); // Motor 1 stilzetten |
RemcoDas | 24:ddd69385b55f | 215 | enc1.reset(); // encoder 1 resetten |
RemcoDas | 24:ddd69385b55f | 216 | pc.printf("Pendulum kalibrated\r\n"); |
RemcoDas | 24:ddd69385b55f | 217 | btn = false; // knop op false |
RemcoDas | 24:ddd69385b55f | 218 | state = AIM; // volgende fase |
RemcoDas | 24:ddd69385b55f | 219 | } |
RemcoDas | 24:ddd69385b55f | 220 | } |
RemcoDas | 24:ddd69385b55f | 221 | } |
RemcoDas | 24:ddd69385b55f | 222 | break; |
RemcoDas | 24:ddd69385b55f | 223 | } |
RemcoDas | 24:ddd69385b55f | 224 | case AIM: { |
RemcoDas | 24:ddd69385b55f | 225 | pc.printf("Aim with EMG\r\n"); |
RemcoDas | 24:ddd69385b55f | 226 | while(state == AIM){ |
RemcoDas | 24:ddd69385b55f | 227 | if(regelaarFlag){ // motor regelen op GoFlag |
RemcoDas | 24:ddd69385b55f | 228 | regelaarFlag = false; |
RemcoDas | 24:ddd69385b55f | 229 | checkAim(); // Controleer positie |
RemcoDas | 24:ddd69385b55f | 230 | } |
RemcoDas | 25:c97d079e07f3 | 231 | if(emgFlag){ // Go flag EMG sampelen |
RemcoDas | 25:c97d079e07f3 | 232 | emgFlag = false; |
RemcoDas | 25:c97d079e07f3 | 233 | emgModeL = EMGfilter(emgL,0); |
RemcoDas | 25:c97d079e07f3 | 234 | emgModeR = EMGfilter(emgR,3); |
RemcoDas | 24:ddd69385b55f | 235 | } |
RemcoDas | 24:ddd69385b55f | 236 | if(btnFlag){ // Go flag button controle? |
RemcoDas | 24:ddd69385b55f | 237 | btnFlag = false; |
RemcoDas | 24:ddd69385b55f | 238 | if(controlAim()){ |
RemcoDas | 24:ddd69385b55f | 239 | pc.printf("Position choosen, adjust shoot velocity\r\n"); |
RemcoDas | 24:ddd69385b55f | 240 | state = REM; // volgende state (REM) |
RemcoDas | 24:ddd69385b55f | 241 | } |
RemcoDas | 24:ddd69385b55f | 242 | } |
RemcoDas | 24:ddd69385b55f | 243 | } |
RemcoDas | 24:ddd69385b55f | 244 | break; |
RemcoDas | 24:ddd69385b55f | 245 | } |
RemcoDas | 24:ddd69385b55f | 246 | case REM: { |
RemcoDas | 24:ddd69385b55f | 247 | pc.printf("Set break percentage, current is: %.0f\r\n", remPerc); |
RemcoDas | 24:ddd69385b55f | 248 | L = false; |
RemcoDas | 24:ddd69385b55f | 249 | R = false; |
RemcoDas | 24:ddd69385b55f | 250 | while(state == REM){ |
RemcoDas | 25:c97d079e07f3 | 251 | if(emgFlag){ // Go flag EMG sampelen |
RemcoDas | 25:c97d079e07f3 | 252 | emgFlag = false; |
RemcoDas | 25:c97d079e07f3 | 253 | emgModeL = EMGfilter(emgL,0); |
RemcoDas | 25:c97d079e07f3 | 254 | emgModeR = EMGfilter(emgR,3); |
RemcoDas | 24:ddd69385b55f | 255 | } |
RemcoDas | 24:ddd69385b55f | 256 | if(btnFlag){ // Go flag button controle? == rem aansturen |
RemcoDas | 24:ddd69385b55f | 257 | btnFlag = false; |
RemcoDas | 24:ddd69385b55f | 258 | |
RemcoDas | 24:ddd69385b55f | 259 | if((ptmrR.read() < 0.1)) { // R is niet aan |
RemcoDas | 24:ddd69385b55f | 260 | R = true; |
RemcoDas | 24:ddd69385b55f | 261 | } |
RemcoDas | 24:ddd69385b55f | 262 | if((ptmrL.read() < 0.1)) { // L is niet aan |
RemcoDas | 24:ddd69385b55f | 263 | L = true; |
RemcoDas | 24:ddd69385b55f | 264 | } |
RemcoDas | 24:ddd69385b55f | 265 | if(L && R){ |
RemcoDas | 24:ddd69385b55f | 266 | if(!(ptmrL.read() >0.3 && ptmrR.read()>0.3) != !(emgModeL!=1 && emgModeR!=1)) { // beide aangespannenR = false; |
RemcoDas | 24:ddd69385b55f | 267 | state = FIRE; |
RemcoDas | 24:ddd69385b55f | 268 | } |
RemcoDas | 24:ddd69385b55f | 269 | else if(!(ptmrL.read() >= 0.8 != !(emgModeL == 3))) { // links vol ingedrukt = schieten |
RemcoDas | 24:ddd69385b55f | 270 | state = FIRE; |
RemcoDas | 24:ddd69385b55f | 271 | } |
RemcoDas | 24:ddd69385b55f | 272 | else if(!(ptmrR.read() > 0.8) != !(emgModeR == 3)) { //rechts vol ingedrukt = schieten |
RemcoDas | 24:ddd69385b55f | 273 | state = FIRE; |
RemcoDas | 24:ddd69385b55f | 274 | } |
RemcoDas | 24:ddd69385b55f | 275 | else if((!(ptmrL.read() > 0.3 && ptmrL.read() < 0.6) != !(emgModeL == 2)) && L) { // links ingedrukt = rem verlagen |
RemcoDas | 24:ddd69385b55f | 276 | if(remPerc>1){ |
RemcoDas | 24:ddd69385b55f | 277 | remPerc--; // Rem percentage verlagen met 1 |
RemcoDas | 24:ddd69385b55f | 278 | } |
RemcoDas | 24:ddd69385b55f | 279 | L = false; |
RemcoDas | 24:ddd69385b55f | 280 | |
RemcoDas | 24:ddd69385b55f | 281 | } |
RemcoDas | 24:ddd69385b55f | 282 | else if((!(ptmrR.read() > 0.3 && ptmrR.read() < 0.6) != !(emgModeR == 2)) && R) { // Rechts half ingedrukt = rem verhogen |
RemcoDas | 24:ddd69385b55f | 283 | if(remPerc<9){ |
RemcoDas | 24:ddd69385b55f | 284 | remPerc++; // rem percentage verhogen met 1 |
RemcoDas | 24:ddd69385b55f | 285 | } |
RemcoDas | 24:ddd69385b55f | 286 | R = false; |
RemcoDas | 24:ddd69385b55f | 287 | } |
RemcoDas | 24:ddd69385b55f | 288 | |
RemcoDas | 24:ddd69385b55f | 289 | if((ptmrL.read() > 0.3 || ptmrR.read() > 0.3) || (emgModeL != 1 || emgModeR !=1)){ // Print rem scale als een of beide knoppen aan zijn |
RemcoDas | 24:ddd69385b55f | 290 | pc.printf("Current breaking scale: %i\r\n", abs(remPerc)); |
RemcoDas | 24:ddd69385b55f | 291 | } |
RemcoDas | 24:ddd69385b55f | 292 | } |
RemcoDas | 24:ddd69385b55f | 293 | } |
RemcoDas | 24:ddd69385b55f | 294 | } |
RemcoDas | 24:ddd69385b55f | 295 | L = false; |
RemcoDas | 24:ddd69385b55f | 296 | R = false; |
RemcoDas | 24:ddd69385b55f | 297 | break; |
RemcoDas | 24:ddd69385b55f | 298 | } |
RemcoDas | 24:ddd69385b55f | 299 | case FIRE: { |
RemcoDas | 24:ddd69385b55f | 300 | pc.printf("Shooting\r\n"); |
RemcoDas | 24:ddd69385b55f | 301 | servo.pulsewidth(servoL); // schrijfrem release |
RemcoDas | 24:ddd69385b55f | 302 | pwmM1.write(0.5); // motor aanzetten |
RemcoDas | 24:ddd69385b55f | 303 | bool servoPos = true; |
RemcoDas | 24:ddd69385b55f | 304 | while(state == FIRE){ // Zolang in FIRE state |
RemcoDas | 24:ddd69385b55f | 305 | if(remFlag && (abs(enc1.getPulses())-100)%4200 < (2100*abs(remPerc)/10)){ // Rem goFlag en half rondje = remmen |
RemcoDas | 24:ddd69385b55f | 306 | remFlag = false; |
RemcoDas | 24:ddd69385b55f | 307 | if(servoPos){ // |
RemcoDas | 24:ddd69385b55f | 308 | servoPos = false; |
RemcoDas | 24:ddd69385b55f | 309 | servo.pulsewidth(servoL); // links |
RemcoDas | 24:ddd69385b55f | 310 | } |
RemcoDas | 24:ddd69385b55f | 311 | else{ |
RemcoDas | 24:ddd69385b55f | 312 | servoPos = true; |
RemcoDas | 24:ddd69385b55f | 313 | servo.pulsewidth(servoR); // rechts |
RemcoDas | 24:ddd69385b55f | 314 | } |
RemcoDas | 24:ddd69385b55f | 315 | } |
RemcoDas | 24:ddd69385b55f | 316 | if(regelaarFlag){ |
RemcoDas | 24:ddd69385b55f | 317 | regelaarFlag = false; |
RemcoDas | 24:ddd69385b55f | 318 | //pc.printf("Slinger encoder: %i\r\n", enc1.getPulses()%4200); |
RemcoDas | 24:ddd69385b55f | 319 | if((abs(enc1.getPulses())+50)%4200 < 150){ // Slinger 1 rondje laten draaien |
RemcoDas | 24:ddd69385b55f | 320 | pwmM1.write(0); // motor uitzetten |
RemcoDas | 24:ddd69385b55f | 321 | pc.printf("Pendulum on startposition\r\n"); |
RemcoDas | 24:ddd69385b55f | 322 | state = AIM; // Aim is volgende fase |
RemcoDas | 24:ddd69385b55f | 323 | } |
RemcoDas | 24:ddd69385b55f | 324 | } |
RemcoDas | 24:ddd69385b55f | 325 | } |
RemcoDas | 24:ddd69385b55f | 326 | break; |
RemcoDas | 24:ddd69385b55f | 327 | } |
Bartvaart | 19:6c0245063b96 | 328 | } |
Bartvaart | 17:cfe44346645c | 329 | } |
RemcoDas | 24:ddd69385b55f | 330 | } |