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@40:35c7c60d7262, 2015-10-21 (annotated)
- Committer:
- RemcoDas
- Date:
- Wed Oct 21 14:17:22 2015 +0000
- Revision:
- 40:35c7c60d7262
- Parent:
- 39:f0f9b3432f43
- Child:
- 41:91c8c39d7a33
Rem aangepast (servo waarden bepaald), krachtleverend programma ipv trillend
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RemcoDas | 33:b4757132437e | 1 | //--------------------Include files and libraries------- |
Bartvaart | 0:557b1ff83a8a | 2 | #include "mbed.h" |
RemcoDas | 24:ddd69385b55f | 3 | #include "QEI.h" |
RemcoDas | 24:ddd69385b55f | 4 | #include "MODSERIAL.h" |
RemcoDas | 24:ddd69385b55f | 5 | #include "TextLCD.h" |
Bartvaart | 7:040591b3f019 | 6 | #include "HIDScope.h" |
Bartvaart | 5:b400209df739 | 7 | #include "Filterdesigns.h" |
Bartvaart | 17:cfe44346645c | 8 | #include "Kalibratie.h" |
Bartvaart | 18:eec0880fcded | 9 | #include "Mode.h" |
RemcoDas | 24:ddd69385b55f | 10 | //--------------------Classes------------------------ |
RemcoDas | 33:b4757132437e | 11 | InterruptIn btnSet(PTC6); // Kalibration button |
RemcoDas | 33:b4757132437e | 12 | DigitalOut ledR(LED_RED), LedB(LED3); // Leds on K64F |
RemcoDas | 24:ddd69385b55f | 13 | MODSERIAL pc(USBTX, USBRX); // Modserial voor Putty |
RemcoDas | 33:b4757132437e | 14 | TextLCD lcd(PTC5, PTC7, PTC0, PTC9, PTC8, PTC1); // LCD screen on inner row of K64F, rs, e, d4-d7 |
RemcoDas | 24:ddd69385b55f | 15 | PwmOut servo(D3); // PwmOut servo |
RemcoDas | 33:b4757132437e | 16 | AnalogIn emgL(A0), emgR(A1); // Analog input of EMG, left and right |
RemcoDas | 33:b4757132437e | 17 | AnalogIn potL(A2), potR(A3); // Potential meter left and right |
RemcoDas | 39:f0f9b3432f43 | 18 | AnalogIn ksLeft(A4), ksRight(A5); // Killswitch left and right |
RemcoDas | 39:f0f9b3432f43 | 19 | HIDScope scope(2); // Hidscope, amount of scopes |
RichardRoos | 34:60391fb72629 | 20 | Ticker EMGticker, tickerControl, tickerBreak, tickerLcd; // Ticker for EMG, regulator and break |
RemcoDas | 33:b4757132437e | 21 | // QEI Encoder(port 1, port 2, ,counts/rev |
RemcoDas | 25:c97d079e07f3 | 22 | QEI enc1 (D13, D12, NC, 64), enc2 (D11, D10, NC, 64); |
RemcoDas | 33:b4757132437e | 23 | // Motor1 met PWM power control and direction |
RemcoDas | 24:ddd69385b55f | 24 | PwmOut pwmM1(D6); |
RemcoDas | 24:ddd69385b55f | 25 | DigitalOut dirM1(D7); |
RemcoDas | 33:b4757132437e | 26 | // Motor2 met PWM power control and direction |
RemcoDas | 24:ddd69385b55f | 27 | PwmOut pwmM2(D5); |
RemcoDas | 24:ddd69385b55f | 28 | DigitalOut dirM2(D4); |
RichardRoos | 34:60391fb72629 | 29 | enum spelfase {CALIBRATE_EMG, CALIBRATE_AIM, CALIBRATE_PEND, AIM, BREAK, FIRE}; // Proframstates, ACKNOWLEDGEMENT switch: BMT groep 4 2014 |
RichardRoos | 34:60391fb72629 | 30 | uint8_t state = CALIBRATE_EMG; // first state program |
RemcoDas | 33:b4757132437e | 31 | enum aimFase {OFF, CW, CCW}; // Aim motor possible states |
RemcoDas | 33:b4757132437e | 32 | uint8_t aimState = OFF; // first state aim motor |
RemcoDas | 25:c97d079e07f3 | 33 | //-------------------------------Variables--------------------------------------------------------------------- |
RemcoDas | 33:b4757132437e | 34 | const int on = 0, off = 1; // on off |
RichardRoos | 34:60391fb72629 | 35 | int maxCounts = 13000; // max richt motor counts Aim motor |
RemcoDas | 33:b4757132437e | 36 | int breakPerc = 0; |
RemcoDas | 40:35c7c60d7262 | 37 | const double servoBreak = 0.0017, servoFree = 0.002; // Range servo,between servoL en servoR (= pulsewidth pwm servo) |
RemcoDas | 38:08c8fd55a3bb | 38 | const double tControl = 0.1, tBreak = 0.1, tLcd = 2; // tickers |
RemcoDas | 39:f0f9b3432f43 | 39 | const double Fs = 200; //Sample frequency Hz |
RemcoDas | 33:b4757132437e | 40 | double t = 1/ Fs; // time EMGticker |
RemcoDas | 27:f62e450bb411 | 41 | double thresholdlowL= 0, thresholdmidL = 0, thresholdhighL= 0; |
RemcoDas | 27:f62e450bb411 | 42 | double thresholdlowR= 0, thresholdmidR = 0, thresholdhighR= 0; |
RemcoDas | 33:b4757132437e | 43 | double yL = 0, yR = 0; // y values EMG left and right |
RemcoDas | 33:b4757132437e | 44 | volatile bool L = false, R = false; // Booleans for checking if mode. has been 1? |
RemcoDas | 33:b4757132437e | 45 | volatile bool btn = false; // Button is pressed? |
RichardRoos | 34:60391fb72629 | 46 | volatile bool controlFlag = false, btnFlag = false, breakFlag = false, emgFlag = false, lcdFlag = false; // Go flags |
RemcoDas | 24:ddd69385b55f | 47 | //----------------------------Functions----------------------------------------------------------------------- |
RemcoDas | 25:c97d079e07f3 | 48 | void flipLed(DigitalOut& led){ //function to flip one LED |
RemcoDas | 25:c97d079e07f3 | 49 | led.write(on); |
RemcoDas | 25:c97d079e07f3 | 50 | wait(0.2); |
RemcoDas | 25:c97d079e07f3 | 51 | led.write(off); |
RemcoDas | 25:c97d079e07f3 | 52 | } |
RemcoDas | 33:b4757132437e | 53 | void PRINT(const char* text){ |
RemcoDas | 25:c97d079e07f3 | 54 | lcd.cls(); // clear LCD scherm |
RemcoDas | 33:b4757132437e | 55 | lcd.printf(text); // print text op lcd |
RemcoDas | 25:c97d079e07f3 | 56 | } |
RichardRoos | 34:60391fb72629 | 57 | void EMGkalibratieL(){ // Determine thresholds left |
RichardRoos | 34:60391fb72629 | 58 | PRINT("EMG LEFT relax muscle"); |
RichardRoos | 34:60391fb72629 | 59 | double ymin = KalibratieMin(emgL, true); // Minimum value left EMG, boolean indicates left |
RemcoDas | 27:f62e450bb411 | 60 | wait(1); |
RichardRoos | 34:60391fb72629 | 61 | PRINT("EMG LEFT flex muscle"); // LCD |
RichardRoos | 34:60391fb72629 | 62 | double ymax = KalibratieMax(emgL, true); // Maxium value left EMG, boolean indicates left |
RichardRoos | 34:60391fb72629 | 63 | PRINT("EMG LEFT well done!"); // LCD |
RemcoDas | 27:f62e450bb411 | 64 | |
RemcoDas | 33:b4757132437e | 65 | if((ymax-ymin) < 0.05){ // No EMG connected |
RemcoDas | 27:f62e450bb411 | 66 | ymin = 10; |
RemcoDas | 27:f62e450bb411 | 67 | ymax = 10; |
RemcoDas | 27:f62e450bb411 | 68 | } |
RemcoDas | 33:b4757132437e | 69 | thresholdlowL = 4 * ymin; // Lowest threshold |
RemcoDas | 33:b4757132437e | 70 | thresholdmidL = 0.5 * ymax; // Midi threshold |
RemcoDas | 33:b4757132437e | 71 | thresholdhighL = 0.8 * ymax; // Highest threshold |
RemcoDas | 27:f62e450bb411 | 72 | |
RemcoDas | 33:b4757132437e | 73 | pc.printf("ymaxL = %f en yminL = %f \r\n",ymax, ymin); |
RemcoDas | 27:f62e450bb411 | 74 | } |
RemcoDas | 33:b4757132437e | 75 | void EMGkalibratieR(){ // Determine thresholds right EMG, same as left |
RichardRoos | 34:60391fb72629 | 76 | PRINT("EMG RIGHT relax muscle"); |
RemcoDas | 30:8ae855348d22 | 77 | double ymin = KalibratieMin(emgR, false); |
Bartvaart | 19:6c0245063b96 | 78 | wait(1); |
RichardRoos | 34:60391fb72629 | 79 | PRINT("EMG RIGHT flex muscle"); |
RemcoDas | 30:8ae855348d22 | 80 | double ymax = KalibratieMax(emgR, false); |
RemcoDas | 39:f0f9b3432f43 | 81 | PRINT("EMG LEFT well done!"); |
RemcoDas | 27:f62e450bb411 | 82 | |
RemcoDas | 33:b4757132437e | 83 | if((ymax-ymin) < 0.05){ |
RemcoDas | 27:f62e450bb411 | 84 | ymin = 10; |
RemcoDas | 27:f62e450bb411 | 85 | ymax = 10; |
RemcoDas | 29:9610df479f89 | 86 | } |
RemcoDas | 33:b4757132437e | 87 | thresholdlowR = 4 * ymin; |
RemcoDas | 33:b4757132437e | 88 | thresholdmidR = 0.5 * ymax; |
RemcoDas | 27:f62e450bb411 | 89 | thresholdhighR = 0.8 * ymax; |
Bartvaart | 22:c1811e60bfce | 90 | |
RemcoDas | 33:b4757132437e | 91 | pc.printf("ymaxR = %f en yminR = %f \r\n",ymax, ymin); // terminal |
RemcoDas | 24:ddd69385b55f | 92 | } |
RemcoDas | 27:f62e450bb411 | 93 | int EMGfilter(AnalogIn& emg, bool side){ |
RemcoDas | 33:b4757132437e | 94 | double u = emg.read(); // read emg signal (left or right EMG) |
RemcoDas | 27:f62e450bb411 | 95 | int mode = 1; |
RemcoDas | 30:8ae855348d22 | 96 | if(side){ |
RemcoDas | 33:b4757132437e | 97 | double y = FilterdesignsLeft(u); // filter signal left EMG |
RemcoDas | 33:b4757132437e | 98 | mode = Mode(y, thresholdlowL, thresholdmidL, thresholdhighL); // Determine mode with thresholds (1, 2, 3) |
RemcoDas | 27:f62e450bb411 | 99 | } |
RemcoDas | 30:8ae855348d22 | 100 | else { |
RemcoDas | 33:b4757132437e | 101 | double y = FilterdesignsRight(u); |
RemcoDas | 33:b4757132437e | 102 | mode = Mode(y, thresholdlowR, thresholdmidR, thresholdhighR); // right EMG |
RemcoDas | 27:f62e450bb411 | 103 | } |
RemcoDas | 25:c97d079e07f3 | 104 | return mode; |
RemcoDas | 26:d9855716ced7 | 105 | } |
RemcoDas | 33:b4757132437e | 106 | int PotReader(AnalogIn& pot){ // read potentialmeter and determine its mode (1 = default, 2, 3) |
RemcoDas | 26:d9855716ced7 | 107 | double volt = pot.read(); |
RemcoDas | 26:d9855716ced7 | 108 | int mode = 1; |
RemcoDas | 26:d9855716ced7 | 109 | if(volt > 0.8){ |
RemcoDas | 26:d9855716ced7 | 110 | mode = 3; |
RemcoDas | 26:d9855716ced7 | 111 | } |
RemcoDas | 26:d9855716ced7 | 112 | else if(volt>0.35 && volt<0.65){ |
RemcoDas | 26:d9855716ced7 | 113 | mode = 2; |
RemcoDas | 26:d9855716ced7 | 114 | } |
RemcoDas | 26:d9855716ced7 | 115 | return mode; |
RemcoDas | 26:d9855716ced7 | 116 | } |
RemcoDas | 33:b4757132437e | 117 | int defMode(AnalogIn& emg, AnalogIn& pot, bool side){ // Determine mode both from EMG and Potentialmeter, ONE of them must be ONE! |
RemcoDas | 27:f62e450bb411 | 118 | int emgMode = EMGfilter(emg, side); |
RemcoDas | 26:d9855716ced7 | 119 | int potMode = PotReader(pot); |
RemcoDas | 26:d9855716ced7 | 120 | int mode = 1; |
RemcoDas | 33:b4757132437e | 121 | if(!(emgMode==1) != !(potMode==1)){ // emgMode = 1 XOR potMode = 1 |
RemcoDas | 33:b4757132437e | 122 | if(emgMode > potMode){ // maximum of emg and pot |
RemcoDas | 26:d9855716ced7 | 123 | mode = emgMode; |
RemcoDas | 26:d9855716ced7 | 124 | } |
RemcoDas | 26:d9855716ced7 | 125 | else{ |
RemcoDas | 26:d9855716ced7 | 126 | mode = potMode; |
RemcoDas | 26:d9855716ced7 | 127 | } |
RemcoDas | 27:f62e450bb411 | 128 | } |
RemcoDas | 26:d9855716ced7 | 129 | return mode; |
RemcoDas | 26:d9855716ced7 | 130 | } |
RemcoDas | 33:b4757132437e | 131 | void setEmgFlag(){ // Goflag EMG |
RemcoDas | 25:c97d079e07f3 | 132 | emgFlag = true; |
RichardRoos | 34:60391fb72629 | 133 | } |
RichardRoos | 34:60391fb72629 | 134 | void setLcdFlag(){ // Goflag EMG |
RichardRoos | 34:60391fb72629 | 135 | lcdFlag = true; |
RichardRoos | 34:60391fb72629 | 136 | } |
RemcoDas | 24:ddd69385b55f | 137 | void btnSetAction(){ // Set knop K64F |
RemcoDas | 24:ddd69385b55f | 138 | btn = true; // GoFlag setknop |
RemcoDas | 26:d9855716ced7 | 139 | } |
RemcoDas | 33:b4757132437e | 140 | void setControlFlag(){ // Go flag setButton |
RemcoDas | 33:b4757132437e | 141 | controlFlag = true; |
RemcoDas | 24:ddd69385b55f | 142 | } |
RemcoDas | 33:b4757132437e | 143 | void setBreakFlag(){ // Go flag Break |
RemcoDas | 33:b4757132437e | 144 | breakFlag = true; |
RemcoDas | 24:ddd69385b55f | 145 | } |
RemcoDas | 33:b4757132437e | 146 | void checkAim(){ // check if Killswitch is on or max counts is reached |
RemcoDas | 39:f0f9b3432f43 | 147 | double volt = ksRight.read(); |
RemcoDas | 39:f0f9b3432f43 | 148 | if(ksRight.read() > 0.8 || ksLeft.read() > 0.8){ |
RemcoDas | 39:f0f9b3432f43 | 149 | if(pwmM1.read()>0){ |
RemcoDas | 39:f0f9b3432f43 | 150 | pwmM2.write(0); // Aim motor freeze |
RemcoDas | 39:f0f9b3432f43 | 151 | pc.printf("BOEM! CRASH! KASTUK!\r\n"); // Terminal |
RemcoDas | 39:f0f9b3432f43 | 152 | PRINT("BOEM! CRASH!"); // LCD |
RemcoDas | 39:f0f9b3432f43 | 153 | } |
RemcoDas | 33:b4757132437e | 154 | } |
RemcoDas | 24:ddd69385b55f | 155 | } |
RemcoDas | 33:b4757132437e | 156 | void motorAim(int dir){ // Rotate Aim motor with given direction |
RemcoDas | 33:b4757132437e | 157 | dirM2.write(dir); |
RemcoDas | 38:08c8fd55a3bb | 158 | pwmM2.write(0.1); |
RemcoDas | 24:ddd69385b55f | 159 | } |
RemcoDas | 33:b4757132437e | 160 | bool controlAim(){ // Function to control aim motor with modes |
RemcoDas | 33:b4757132437e | 161 | bool both = false; // boolean if both modes are 3 |
RemcoDas | 27:f62e450bb411 | 162 | int modeL = defMode(emgL, potL, true); |
RemcoDas | 27:f62e450bb411 | 163 | int modeR = defMode(emgR, potR, false); |
RemcoDas | 27:f62e450bb411 | 164 | |
RemcoDas | 27:f62e450bb411 | 165 | scope.set(0, modeL); |
RemcoDas | 27:f62e450bb411 | 166 | scope.set(1, modeR); |
RemcoDas | 33:b4757132437e | 167 | scope.send(); //send values to HIDScope |
RemcoDas | 27:f62e450bb411 | 168 | |
RemcoDas | 33:b4757132437e | 169 | if(modeR < 2 && !R){ // control if mode has been 1 |
RemcoDas | 27:f62e450bb411 | 170 | R = true; |
RemcoDas | 27:f62e450bb411 | 171 | } |
RemcoDas | 28:e6d2fe0e593e | 172 | if(modeL < 2 && !L){ |
RemcoDas | 28:e6d2fe0e593e | 173 | L = true; |
RemcoDas | 24:ddd69385b55f | 174 | } |
RemcoDas | 28:e6d2fe0e593e | 175 | |
RemcoDas | 33:b4757132437e | 176 | if((modeL>2) && (modeR >2 && R && L)) { // mode L and mode R both 3, and both has been 1 herefore |
RemcoDas | 26:d9855716ced7 | 177 | both = true; // Return true |
RemcoDas | 33:b4757132437e | 178 | pwmM2.write(0); // Aim motor freeze |
RemcoDas | 33:b4757132437e | 179 | aimState = OFF; // next state |
RemcoDas | 28:e6d2fe0e593e | 180 | } |
RemcoDas | 33:b4757132437e | 181 | else if((modeR == 2) && (modeL == 2)) { // modes are both 2 |
RemcoDas | 33:b4757132437e | 182 | if(aimState!=OFF){ // only if aim motor is rotating |
RemcoDas | 33:b4757132437e | 183 | pwmM2.write(0); // Aim motor freeze |
RemcoDas | 33:b4757132437e | 184 | aimState = OFF; // motor state is off |
RichardRoos | 35:012b2e045579 | 185 | pc.printf("Motor freeze\r\n"); // LCD |
RemcoDas | 33:b4757132437e | 186 | L = false; // Modes must be first 1 for another action |
RemcoDas | 33:b4757132437e | 187 | R = false; // "" |
RemcoDas | 28:e6d2fe0e593e | 188 | } |
RemcoDas | 24:ddd69385b55f | 189 | } |
RemcoDas | 33:b4757132437e | 190 | else if((modeL == 2) && (aimState != CCW && (modeR == 1))) { // modeL ==2 AND rotation is not CCW AND modeR has been 1 |
RemcoDas | 28:e6d2fe0e593e | 191 | if(L){ |
RemcoDas | 33:b4757132437e | 192 | aimState = CCW; // Rotate CCW |
RichardRoos | 34:60391fb72629 | 193 | pc.printf("Rotate -, CCW\r\n"); |
RemcoDas | 28:e6d2fe0e593e | 194 | motorAim(0); |
RemcoDas | 28:e6d2fe0e593e | 195 | } |
RemcoDas | 24:ddd69385b55f | 196 | } |
RemcoDas | 33:b4757132437e | 197 | else if((modeR == 2) && (aimState != CW && (modeL == 1))) { // modeR == 2 AND rotation is not CW AND modeL has been 1 |
RemcoDas | 28:e6d2fe0e593e | 198 | if(R){ |
RemcoDas | 33:b4757132437e | 199 | aimState = CW; // Rotate CW |
RichardRoos | 34:60391fb72629 | 200 | pc.printf("Rotate +, CW\r\n"); |
RemcoDas | 28:e6d2fe0e593e | 201 | motorAim(1); |
RemcoDas | 28:e6d2fe0e593e | 202 | } |
RemcoDas | 27:f62e450bb411 | 203 | } |
RemcoDas | 24:ddd69385b55f | 204 | return both; |
RemcoDas | 24:ddd69385b55f | 205 | } |
RemcoDas | 24:ddd69385b55f | 206 | int main(){ |
RemcoDas | 33:b4757132437e | 207 | flipLed(ledR); // test if code begins |
RemcoDas | 24:ddd69385b55f | 208 | btnSet.mode(PullUp); // Button mode |
RemcoDas | 33:b4757132437e | 209 | btnSet.rise(&btnSetAction); // Connect button to function |
RemcoDas | 33:b4757132437e | 210 | tickerControl.attach(&setControlFlag,tControl); // ticker control motor |
RemcoDas | 33:b4757132437e | 211 | tickerBreak.attach(&setBreakFlag,tBreak); // ticker break |
RemcoDas | 26:d9855716ced7 | 212 | EMGticker.attach(&setEmgFlag, t); // ticker EMG, 500H |
RichardRoos | 34:60391fb72629 | 213 | tickerLcd.attach(&setLcdFlag,tLcd); // ticker lcd |
RemcoDas | 25:c97d079e07f3 | 214 | |
RemcoDas | 33:b4757132437e | 215 | pc.printf("\n\n\n\n\n"); // Terminal |
RichardRoos | 34:60391fb72629 | 216 | pc.printf("---NEW GAME---\r\n"); |
RemcoDas | 33:b4757132437e | 217 | PRINT("--- NEW GAME ---"); // LCD |
RemcoDas | 33:b4757132437e | 218 | while(1){ // Run program |
RemcoDas | 24:ddd69385b55f | 219 | switch(state){ |
RichardRoos | 34:60391fb72629 | 220 | case CALIBRATE_EMG: { |
RichardRoos | 34:60391fb72629 | 221 | EMGkalibratieL(); // calibrate left EMG, determine thresholds |
RichardRoos | 34:60391fb72629 | 222 | EMGkalibratieR(); // calibrate right EMG, determine thresholds |
RichardRoos | 34:60391fb72629 | 223 | state = CALIBRATE_AIM; // Next state |
RemcoDas | 24:ddd69385b55f | 224 | break; |
RemcoDas | 24:ddd69385b55f | 225 | } |
RichardRoos | 34:60391fb72629 | 226 | case CALIBRATE_AIM: { |
RemcoDas | 33:b4757132437e | 227 | pwmM2.period(1/100000); // period motor 2 |
RemcoDas | 33:b4757132437e | 228 | printf("Position is kalibrating\r\n"); // terminal |
RichardRoos | 37:090ba5b1e655 | 229 | PRINT("Wait a moment, please"); // LCD |
RemcoDas | 33:b4757132437e | 230 | dirM2.write(0); // direction aim motor |
RemcoDas | 39:f0f9b3432f43 | 231 | pwmM2.write(0.15); // percentage motor power |
RichardRoos | 34:60391fb72629 | 232 | bool calibrated = false; // |
RemcoDas | 38:08c8fd55a3bb | 233 | while(state==CALIBRATE_AIM){ |
RemcoDas | 38:08c8fd55a3bb | 234 | if(controlFlag){ // motor regelen op GoFlag |
RemcoDas | 38:08c8fd55a3bb | 235 | controlFlag = false; |
RemcoDas | 39:f0f9b3432f43 | 236 | if(!calibrated){ // Not calibrated |
RemcoDas | 39:f0f9b3432f43 | 237 | if((ksRight.read() > 0.8)){ // Killswitch? |
RemcoDas | 39:f0f9b3432f43 | 238 | pwmM2.write(0); // Aim motor freeze |
RemcoDas | 39:f0f9b3432f43 | 239 | enc2.reset(); // Reset encoder |
RemcoDas | 39:f0f9b3432f43 | 240 | PRINT("Aim calibrated"); // LCD |
RemcoDas | 39:f0f9b3432f43 | 241 | pc.printf("Aim calibrated\r\n");// Terminal |
RemcoDas | 39:f0f9b3432f43 | 242 | calibrated = true; // |
RemcoDas | 39:f0f9b3432f43 | 243 | //state = CALIBRATE_PEND; // next state |
RemcoDas | 39:f0f9b3432f43 | 244 | wait(1); |
RemcoDas | 39:f0f9b3432f43 | 245 | dirM2.write(1); // direction aim motor |
RemcoDas | 39:f0f9b3432f43 | 246 | pwmM2.write(0.15); // percentage motor power, turn it on |
RemcoDas | 39:f0f9b3432f43 | 247 | } |
RemcoDas | 39:f0f9b3432f43 | 248 | } |
RemcoDas | 39:f0f9b3432f43 | 249 | else { // Calibrated |
RemcoDas | 39:f0f9b3432f43 | 250 | controlFlag = false; |
RemcoDas | 39:f0f9b3432f43 | 251 | checkAim(); |
RemcoDas | 39:f0f9b3432f43 | 252 | if(enc1.getPulses() > maxCounts/2){ // rotate aim motor to midi position |
RemcoDas | 39:f0f9b3432f43 | 253 | pwmM2.write(0); // Aim motor freeze |
RemcoDas | 39:f0f9b3432f43 | 254 | state = CALIBRATE_PEND; // next state |
RemcoDas | 39:f0f9b3432f43 | 255 | } |
RemcoDas | 39:f0f9b3432f43 | 256 | } |
RemcoDas | 39:f0f9b3432f43 | 257 | } |
RemcoDas | 24:ddd69385b55f | 258 | } |
RemcoDas | 24:ddd69385b55f | 259 | break; |
RemcoDas | 24:ddd69385b55f | 260 | } |
RichardRoos | 34:60391fb72629 | 261 | case CALIBRATE_PEND: { |
RemcoDas | 39:f0f9b3432f43 | 262 | pc.printf("Calibrate beam motor with setknop\r\n"); // Terminal |
RemcoDas | 39:f0f9b3432f43 | 263 | dirM1.write(0); // direction beam motor |
RichardRoos | 34:60391fb72629 | 264 | pwmM1.period(1/100000); // period beam motor |
RemcoDas | 33:b4757132437e | 265 | servo.period(0.02); // 20ms period servo |
RemcoDas | 39:f0f9b3432f43 | 266 | //pwmM1.write(0.2); // Turn motor on, low power |
RichardRoos | 34:60391fb72629 | 267 | btn = false; // Button is unpressed |
RichardRoos | 34:60391fb72629 | 268 | R = false; // modeR must become 1 |
RichardRoos | 34:60391fb72629 | 269 | L = false; // modeL must become 1 |
RichardRoos | 36:549d1ce7b96b | 270 | PRINT("Calibrate beam to 10 o'clock"); |
RichardRoos | 36:549d1ce7b96b | 271 | wait(1); |
RichardRoos | 34:60391fb72629 | 272 | PRINT("Flex right half to swing beam"); |
RichardRoos | 34:60391fb72629 | 273 | while(state==CALIBRATE_PEND){ |
RemcoDas | 31:074b9d03d816 | 274 | if(emgFlag){ |
RemcoDas | 24:ddd69385b55f | 275 | pc.printf(""); // lege regel printen, anders doet setknop het niet |
RemcoDas | 31:074b9d03d816 | 276 | emgFlag = false; |
RemcoDas | 30:8ae855348d22 | 277 | |
RichardRoos | 34:60391fb72629 | 278 | int modeL = defMode(emgL, potL, true); // determine modeL |
RichardRoos | 34:60391fb72629 | 279 | int modeR = defMode(emgR, potR, false); // determine modeR |
RemcoDas | 33:b4757132437e | 280 | |
RemcoDas | 33:b4757132437e | 281 | if(modeR < 2) { // modeR == 1 |
RemcoDas | 33:b4757132437e | 282 | R = true; |
RemcoDas | 33:b4757132437e | 283 | } |
RemcoDas | 33:b4757132437e | 284 | if(modeL < 2) { // modeL == 1 |
RemcoDas | 33:b4757132437e | 285 | L = true; |
RichardRoos | 34:60391fb72629 | 286 | } |
RemcoDas | 33:b4757132437e | 287 | if (btn || (modeL == 3 && L) || (modeR == 3 && R)){ // If setbutton is on or one mode is 3, and has been 1 |
RemcoDas | 33:b4757132437e | 288 | pwmM1.write(0); // Motor 1 freeze |
RemcoDas | 33:b4757132437e | 289 | enc1.reset(); // encoder 1 reset |
RichardRoos | 34:60391fb72629 | 290 | PRINT("Beam calibrated"); |
RemcoDas | 38:08c8fd55a3bb | 291 | pc.printf("Beam calibrated\r\n"); |
RemcoDas | 33:b4757132437e | 292 | btn = false; // button op false |
RemcoDas | 33:b4757132437e | 293 | state = AIM; // next state |
RemcoDas | 24:ddd69385b55f | 294 | } |
RemcoDas | 31:074b9d03d816 | 295 | else if(modeL == 2){ |
RichardRoos | 34:60391fb72629 | 296 | pwmM1.write(0); // beam freeze |
RichardRoos | 34:60391fb72629 | 297 | PRINT("Flex both full to continue"); // LCD |
RemcoDas | 38:08c8fd55a3bb | 298 | pc.printf("Beam freeze\r\n"); |
RemcoDas | 31:074b9d03d816 | 299 | } |
RichardRoos | 34:60391fb72629 | 300 | else if(modeR == 2){ |
RichardRoos | 34:60391fb72629 | 301 | pwmM1.write(0.025); // beam rotate |
RichardRoos | 34:60391fb72629 | 302 | PRINT("Flex left half to stop beam"); // LCD |
RemcoDas | 38:08c8fd55a3bb | 303 | pc.printf("Beam move\r\n"); |
RemcoDas | 31:074b9d03d816 | 304 | } |
RemcoDas | 24:ddd69385b55f | 305 | } |
RemcoDas | 24:ddd69385b55f | 306 | } |
RichardRoos | 35:012b2e045579 | 307 | lcd.cls(); |
RemcoDas | 24:ddd69385b55f | 308 | break; |
RemcoDas | 24:ddd69385b55f | 309 | } |
RemcoDas | 24:ddd69385b55f | 310 | case AIM: { |
RichardRoos | 34:60391fb72629 | 311 | pc.printf("Aim with EMG\r\n"); // terminal |
RemcoDas | 39:f0f9b3432f43 | 312 | int i = 0; // counter for lcd |
RemcoDas | 39:f0f9b3432f43 | 313 | R = false; |
RemcoDas | 39:f0f9b3432f43 | 314 | L = false; |
RichardRoos | 34:60391fb72629 | 315 | while(state == AIM){ |
RichardRoos | 36:549d1ce7b96b | 316 | if(lcdFlag){ // LCD loopje to project 3 texts on lcd |
RichardRoos | 35:012b2e045579 | 317 | lcdFlag = false; |
RichardRoos | 34:60391fb72629 | 318 | if(i%3 == 0){ |
RichardRoos | 34:60391fb72629 | 319 | PRINT("Flex left or right half to aim"); |
RichardRoos | 34:60391fb72629 | 320 | } |
RichardRoos | 34:60391fb72629 | 321 | else if(i%3 == 1){ |
RichardRoos | 34:60391fb72629 | 322 | PRINT("Flex both half to freeze"); |
RichardRoos | 34:60391fb72629 | 323 | } |
RichardRoos | 34:60391fb72629 | 324 | else { |
RichardRoos | 34:60391fb72629 | 325 | PRINT("Flex both full to continue"); |
RichardRoos | 34:60391fb72629 | 326 | } |
RichardRoos | 34:60391fb72629 | 327 | i++; |
RichardRoos | 34:60391fb72629 | 328 | } |
RichardRoos | 34:60391fb72629 | 329 | |
RemcoDas | 33:b4757132437e | 330 | if(controlFlag){ // motor control on GoFlag |
RemcoDas | 33:b4757132437e | 331 | controlFlag = false; |
RemcoDas | 33:b4757132437e | 332 | checkAim(); // Check position |
RemcoDas | 24:ddd69385b55f | 333 | } |
RemcoDas | 33:b4757132437e | 334 | if(emgFlag){ // Go flag EMG sampel |
RemcoDas | 26:d9855716ced7 | 335 | emgFlag = false; |
RemcoDas | 24:ddd69385b55f | 336 | if(controlAim()){ |
RichardRoos | 35:012b2e045579 | 337 | pc.printf("Position chosen\r\n"); // terminal |
RichardRoos | 34:60391fb72629 | 338 | state = BREAK; // Next state (BREAK) |
RemcoDas | 24:ddd69385b55f | 339 | } |
RemcoDas | 24:ddd69385b55f | 340 | } |
RichardRoos | 35:012b2e045579 | 341 | } |
RichardRoos | 35:012b2e045579 | 342 | lcd.cls(); |
RemcoDas | 24:ddd69385b55f | 343 | break; |
RemcoDas | 24:ddd69385b55f | 344 | } |
RemcoDas | 33:b4757132437e | 345 | case BREAK: { |
RemcoDas | 33:b4757132437e | 346 | pc.printf("Set break percentage, current is: %.0f\r\n", breakPerc); |
RemcoDas | 24:ddd69385b55f | 347 | L = false; |
RemcoDas | 24:ddd69385b55f | 348 | R = false; |
RichardRoos | 35:012b2e045579 | 349 | int i = 0; // counter for lcd |
RichardRoos | 35:012b2e045579 | 350 | while(state == BREAK){ |
RichardRoos | 36:549d1ce7b96b | 351 | if(lcdFlag){ // LCD loop to project text on LCD |
RichardRoos | 35:012b2e045579 | 352 | lcdFlag = false; |
RichardRoos | 35:012b2e045579 | 353 | if(i%2 == 0){ |
RichardRoos | 35:012b2e045579 | 354 | PRINT("Flex L or R to adjust break"); |
RichardRoos | 35:012b2e045579 | 355 | } |
RichardRoos | 35:012b2e045579 | 356 | else { |
RichardRoos | 35:012b2e045579 | 357 | PRINT("Flex both to continue"); |
RichardRoos | 35:012b2e045579 | 358 | } |
RichardRoos | 35:012b2e045579 | 359 | i++; |
RichardRoos | 35:012b2e045579 | 360 | } |
RemcoDas | 26:d9855716ced7 | 361 | if(emgFlag){ // Go flag EMG sampelen |
RemcoDas | 26:d9855716ced7 | 362 | emgFlag = false; |
RemcoDas | 27:f62e450bb411 | 363 | int modeL = defMode(emgL, potL, true); |
RemcoDas | 27:f62e450bb411 | 364 | int modeR = defMode(emgR, potR, false); |
RemcoDas | 27:f62e450bb411 | 365 | |
RemcoDas | 33:b4757132437e | 366 | if(modeR < 2) { // modeR is 1 |
RemcoDas | 24:ddd69385b55f | 367 | R = true; |
RemcoDas | 24:ddd69385b55f | 368 | } |
RemcoDas | 33:b4757132437e | 369 | if(modeL < 2) { // modeL is 1 |
RemcoDas | 24:ddd69385b55f | 370 | L = true; |
RemcoDas | 24:ddd69385b55f | 371 | } |
RemcoDas | 39:f0f9b3432f43 | 372 | |
RemcoDas | 39:f0f9b3432f43 | 373 | if((modeL > 1) && modeR > 1) { // both modes > 1 |
RemcoDas | 39:f0f9b3432f43 | 374 | state = FIRE; |
RemcoDas | 24:ddd69385b55f | 375 | } |
RemcoDas | 33:b4757132437e | 376 | else if(modeL > 2 || modeR > 2) { // left of right mode 3 = fire |
RemcoDas | 39:f0f9b3432f43 | 377 | state = FIRE; |
RemcoDas | 39:f0f9b3432f43 | 378 | } |
RemcoDas | 39:f0f9b3432f43 | 379 | if(L && R){ |
RemcoDas | 33:b4757132437e | 380 | else if((modeL == 2) && L) { // modeL = BREAK lower with one |
RemcoDas | 33:b4757132437e | 381 | if(breakPerc>1){ |
RemcoDas | 33:b4757132437e | 382 | breakPerc--; |
RemcoDas | 24:ddd69385b55f | 383 | } |
RemcoDas | 26:d9855716ced7 | 384 | L = false; |
RemcoDas | 24:ddd69385b55f | 385 | } |
RemcoDas | 33:b4757132437e | 386 | else if((modeR == 2) && R) { // modeR = Break +1 |
RemcoDas | 33:b4757132437e | 387 | if(breakPerc<10){ |
RemcoDas | 33:b4757132437e | 388 | breakPerc++; |
RemcoDas | 24:ddd69385b55f | 389 | } |
RemcoDas | 24:ddd69385b55f | 390 | R = false; |
RemcoDas | 26:d9855716ced7 | 391 | } |
RemcoDas | 33:b4757132437e | 392 | if(modeL > 1 || modeR > 1){ // Print BREAK scale if one of both modes > 1 |
RemcoDas | 33:b4757132437e | 393 | pc.printf("Current breaking scale: %i\r\n", breakPerc); |
RichardRoos | 34:60391fb72629 | 394 | lcd.cls(); |
RichardRoos | 36:549d1ce7b96b | 395 | lcd.printf("Break scale: %i", breakPerc); |
RemcoDas | 26:d9855716ced7 | 396 | } |
RemcoDas | 26:d9855716ced7 | 397 | } |
RemcoDas | 24:ddd69385b55f | 398 | } |
RemcoDas | 24:ddd69385b55f | 399 | } |
RemcoDas | 33:b4757132437e | 400 | L = false; // modeL must be one for another action can take place |
RichardRoos | 35:012b2e045579 | 401 | R = false; // modeR "" |
RichardRoos | 35:012b2e045579 | 402 | lcd.cls(); |
RemcoDas | 24:ddd69385b55f | 403 | break; |
RemcoDas | 24:ddd69385b55f | 404 | } |
RemcoDas | 24:ddd69385b55f | 405 | case FIRE: { |
RemcoDas | 24:ddd69385b55f | 406 | pc.printf("Shooting\r\n"); |
RichardRoos | 35:012b2e045579 | 407 | PRINT("FIRING"); |
RemcoDas | 33:b4757132437e | 408 | servo.pulsewidth(servoL); // BREAK release |
RemcoDas | 40:35c7c60d7262 | 409 | pwmM1.write(0.25); // beam motor on |
RemcoDas | 40:35c7c60d7262 | 410 | servo.pulsewidth(servoBreak); // Servo to break position |
RemcoDas | 40:35c7c60d7262 | 411 | bool breaking = true; // boolean breaking? |
RemcoDas | 40:35c7c60d7262 | 412 | while(state == FIRE){ // while in FIRE state |
RemcoDas | 40:35c7c60d7262 | 413 | if(controlFlag{ // BREAK goFlag half rotation beam = breaking |
RemcoDas | 40:35c7c60d7262 | 414 | controlFlag = false; |
RemcoDas | 40:35c7c60d7262 | 415 | if(breaking){ // encoder is 0 on 100 counts before 12 o'clock |
RemcoDas | 40:35c7c60d7262 | 416 | if((abs(enc1.getPulses())-100)%4200 > (2100*abs(breakPerc)/10)){ |
RemcoDas | 40:35c7c60d7262 | 417 | servo.pulsewidth(servoFree); // Servo to free position |
RemcoDas | 40:35c7c60d7262 | 418 | breaking = false; |
RemcoDas | 40:35c7c60d7262 | 419 | } |
RemcoDas | 24:ddd69385b55f | 420 | } |
RemcoDas | 40:35c7c60d7262 | 421 | else{ // Not breaking |
RemcoDas | 40:35c7c60d7262 | 422 | if((abs(enc1.getPulses())+150)%4200 < 150){ // rotated 1 rev? |
RemcoDas | 40:35c7c60d7262 | 423 | pwmM1.write(0); // motor beam off |
RemcoDas | 40:35c7c60d7262 | 424 | pc.printf("Beam on startposition\r\n"); // terminal |
RemcoDas | 40:35c7c60d7262 | 425 | state = AIM; // Next stage |
RemcoDas | 40:35c7c60d7262 | 426 | } |
RemcoDas | 40:35c7c60d7262 | 427 | } |
RemcoDas | 24:ddd69385b55f | 428 | } |
RemcoDas | 24:ddd69385b55f | 429 | } |
RichardRoos | 36:549d1ce7b96b | 430 | } |
RichardRoos | 36:549d1ce7b96b | 431 | lcd.cls(); |
RemcoDas | 24:ddd69385b55f | 432 | break; |
RemcoDas | 24:ddd69385b55f | 433 | } |
Bartvaart | 19:6c0245063b96 | 434 | } |
Bartvaart | 17:cfe44346645c | 435 | } |
RemcoDas | 24:ddd69385b55f | 436 | } |