Robotcontrol groep 2
Dependencies: Encoder MODSERIAL mbed HIDScope
main.cpp@11:97759ceeb638, 2014-11-04 (annotated)
- Committer:
- wiesdat
- Date:
- Tue Nov 04 10:53:04 2014 +0000
- Revision:
- 11:97759ceeb638
- Parent:
- 10:e628a87d18aa
- Child:
- 12:e30bc8446379
Alles werkt, LEDjes alleen nog beter
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wiesdat | 4:c22f3095b130 | 1 | //ROBOTCONTROL BMT M9 - GROEP 2 |
wiesdat | 4:c22f3095b130 | 2 | //Alex Overbeek |
wiesdat | 4:c22f3095b130 | 3 | //Tom Baumeister |
wiesdat | 4:c22f3095b130 | 4 | //Bas van Buuren |
wiesdat | 4:c22f3095b130 | 5 | //Bas Mattern |
wiesdat | 4:c22f3095b130 | 6 | //Thijs ruikes |
wiesdat | 4:c22f3095b130 | 7 | |
wiesdat | 4:c22f3095b130 | 8 | /*Het script bestaat uit drie delen: INCLUDE AND DEFINE, FUNCTIONS, MAINSCRIPT. Per gedeelte wordt uitgelegd wat gedaan wordt. |
wiesdat | 4:c22f3095b130 | 9 | We laten de beweging in fases lopen. Fase 1 werkt in dit script. Voor fase 2 kan de positie al worden ingesteld, de slagbeweging |
wiesdat | 5:36df561f3ac1 | 10 | moet nog gemaakt worden. Dit gaan we doen met een PID regelaar die naar een snelheid toe regelt. |
wiesdat | 4:c22f3095b130 | 11 | */ |
wiesdat | 4:c22f3095b130 | 12 | |
wiesdat | 4:c22f3095b130 | 13 | //INCLUDE AND DEFINE ALL |
wiesdat | 0:25a2e7ea29f3 | 14 | #include "mbed.h" |
wiesdat | 0:25a2e7ea29f3 | 15 | #include "MODSERIAL.h" |
wiesdat | 4:c22f3095b130 | 16 | #include <iostream> |
wiesdat | 4:c22f3095b130 | 17 | #include "encoder.h" |
wiesdat | 5:36df561f3ac1 | 18 | #include "HIDScope.h" |
wiesdat | 1:a010e434a360 | 19 | |
wiesdat | 11:97759ceeb638 | 20 | #define THRESHOLD 0.05 |
wiesdat | 5:36df561f3ac1 | 21 | #define NOSAMPL 500 |
wiesdat | 0:25a2e7ea29f3 | 22 | |
wiesdat | 7:2e4eb23700b0 | 23 | #define TSAMP1 0.01 |
wiesdat | 11:97759ceeb638 | 24 | #define K_P1 (0.0015) |
wiesdat | 11:97759ceeb638 | 25 | #define K_I1 (0.0000001*TSAMP1) |
wiesdat | 11:97759ceeb638 | 26 | #define K_D1 (0.0003/TSAMP1) |
wiesdat | 3:611fd72c9d46 | 27 | #define I_LIMIT1 1. |
wiesdat | 7:2e4eb23700b0 | 28 | |
wiesdat | 7:2e4eb23700b0 | 29 | //KPID voor slagfunctie |
wiesdat | 11:97759ceeb638 | 30 | #define KSLA_P (0.06) |
wiesdat | 11:97759ceeb638 | 31 | #define KSLA_I (0.00002 *TSAMP1) |
wiesdat | 7:2e4eb23700b0 | 32 | #define KSLA_D (0.0005 /TSAMP1) |
wiesdat | 7:2e4eb23700b0 | 33 | |
wiesdat | 11:97759ceeb638 | 34 | #define MAXPOS 750 |
wiesdat | 8:83115293e84d | 35 | |
wiesdat | 4:c22f3095b130 | 36 | // Constantes voor de filters definiëren: |
wiesdat | 0:25a2e7ea29f3 | 37 | // Constantes voor de Low Pass filter |
wiesdat | 0:25a2e7ea29f3 | 38 | #define A1LP 0.018180963222803 |
wiesdat | 0:25a2e7ea29f3 | 39 | #define A0LP 0.016544013176248 |
wiesdat | 0:25a2e7ea29f3 | 40 | #define B1LP -1.718913340044714 |
wiesdat | 0:25a2e7ea29f3 | 41 | #define B0LP 0.753638316443765 |
wiesdat | 0:25a2e7ea29f3 | 42 | // Constantes voor de High Pass Filter |
wiesdat | 0:25a2e7ea29f3 | 43 | #define A1HP -1.999801878951505 |
wiesdat | 0:25a2e7ea29f3 | 44 | #define A0HP 0.999801878951505 |
wiesdat | 0:25a2e7ea29f3 | 45 | #define B1HP -1.971717601075000 |
wiesdat | 0:25a2e7ea29f3 | 46 | #define B0HP 0.972111984032897 |
wiesdat | 0:25a2e7ea29f3 | 47 | // Constantes voor het Notch Filter |
wiesdat | 0:25a2e7ea29f3 | 48 | #define A0N 0.99436777112 |
wiesdat | 0:25a2e7ea29f3 | 49 | #define A1N -1.89139989664 |
wiesdat | 0:25a2e7ea29f3 | 50 | #define A2N 0.99436777112 |
wiesdat | 0:25a2e7ea29f3 | 51 | #define B1N 1.89070035439 |
wiesdat | 0:25a2e7ea29f3 | 52 | #define B2N -0.988036 |
wiesdat | 0:25a2e7ea29f3 | 53 | |
wiesdat | 0:25a2e7ea29f3 | 54 | // Sample Time |
wiesdat | 0:25a2e7ea29f3 | 55 | #define TSAMP 0.001 |
wiesdat | 4:c22f3095b130 | 56 | |
wiesdat | 3:611fd72c9d46 | 57 | // Outputs van de motor |
wiesdat | 3:611fd72c9d46 | 58 | Encoder encoderA(PTD5,PTA13); |
wiesdat | 3:611fd72c9d46 | 59 | Encoder encoder1(PTD0,PTD2); |
wiesdat | 0:25a2e7ea29f3 | 60 | |
wiesdat | 3:611fd72c9d46 | 61 | // Outputs naar de motor |
wiesdat | 3:611fd72c9d46 | 62 | PwmOut pwm(PTC8); |
wiesdat | 3:611fd72c9d46 | 63 | DigitalOut dir(PTC9); |
wiesdat | 3:611fd72c9d46 | 64 | PwmOut pwm1(PTA5); |
wiesdat | 3:611fd72c9d46 | 65 | DigitalOut dir1(PTA4); |
wiesdat | 3:611fd72c9d46 | 66 | |
wiesdat | 3:611fd72c9d46 | 67 | //lampjes |
wiesdat | 3:611fd72c9d46 | 68 | DigitalOut rood(LED1); |
wiesdat | 3:611fd72c9d46 | 69 | DigitalOut blauw(LED3); |
wiesdat | 3:611fd72c9d46 | 70 | DigitalOut groen(LED2); |
wiesdat | 11:97759ceeb638 | 71 | |
wiesdat | 11:97759ceeb638 | 72 | //emg input |
wiesdat | 0:25a2e7ea29f3 | 73 | AnalogIn emg1(PTB1); |
wiesdat | 0:25a2e7ea29f3 | 74 | AnalogIn emg2(PTB2); |
wiesdat | 0:25a2e7ea29f3 | 75 | |
wiesdat | 0:25a2e7ea29f3 | 76 | // PC communicatie |
wiesdat | 0:25a2e7ea29f3 | 77 | MODSERIAL pc(USBTX,USBRX); |
wiesdat | 3:611fd72c9d46 | 78 | |
wiesdat | 0:25a2e7ea29f3 | 79 | // Ticker voor de meetgegevens |
wiesdat | 0:25a2e7ea29f3 | 80 | Ticker timer; |
wiesdat | 0:25a2e7ea29f3 | 81 | volatile bool looptimerflag; |
wiesdat | 0:25a2e7ea29f3 | 82 | |
wiesdat | 0:25a2e7ea29f3 | 83 | // Waardes voor de filters reserveren en als float vaststellen |
wiesdat | 3:611fd72c9d46 | 84 | float emg_value2, ylp2, yhp2, yn2,ysum1 = 0, yave1=0; |
wiesdat | 3:611fd72c9d46 | 85 | float emg_value1, ylp1, yhp1, yn1,ysum2 = 0, yave2=0 ; |
wiesdat | 3:611fd72c9d46 | 86 | |
wiesdat | 3:611fd72c9d46 | 87 | //variabvelen voor positie motor1 |
wiesdat | 4:c22f3095b130 | 88 | float v1=0,out_i1 = 0; //out_i1 globaal gedef om reset |
wiesdat | 0:25a2e7ea29f3 | 89 | |
wiesdat | 0:25a2e7ea29f3 | 90 | // 0 of 1 waardes gedefinieerd uit het EMG, met 0 is te lage activiteit, 1 is hoog genoeg, voor de zekerheid toch als int. |
wiesdat | 0:25a2e7ea29f3 | 91 | uint8_t y1, y2; |
wiesdat | 0:25a2e7ea29f3 | 92 | int check = 0; |
wiesdat | 0:25a2e7ea29f3 | 93 | |
wiesdat | 0:25a2e7ea29f3 | 94 | // integers reserveren voor het deel van het regelsysteem als we de boolean emg waardes hebben: hoek van het badje (0,1, of 2) en de gewenste snelheid (0,1, of 2) |
wiesdat | 3:611fd72c9d46 | 95 | int badjestand, badjes=1,badjedone=0,speeding,armstand=0,armspeed=0; |
wiesdat | 0:25a2e7ea29f3 | 96 | bool speeddone=0; |
wiesdat | 3:611fd72c9d46 | 97 | |
wiesdat | 0:25a2e7ea29f3 | 98 | // Teller voor hoeveel metingen er zijn gedaan |
wiesdat | 0:25a2e7ea29f3 | 99 | uint16_t teller=0; |
wiesdat | 0:25a2e7ea29f3 | 100 | |
wiesdat | 8:83115293e84d | 101 | //Definieer alle functies |
wiesdat | 8:83115293e84d | 102 | void clamp(float * in, float min, float max); |
wiesdat | 8:83115293e84d | 103 | void clampint(int * in, int min, int max); |
wiesdat | 8:83115293e84d | 104 | void setlooptimerflag(void); |
wiesdat | 8:83115293e84d | 105 | float readEMG1(); |
wiesdat | 8:83115293e84d | 106 | float readEMG2(); |
wiesdat | 8:83115293e84d | 107 | float notchfilter1(float ylp1); |
wiesdat | 8:83115293e84d | 108 | float notchfilter2(float ylp2); |
wiesdat | 8:83115293e84d | 109 | float hpfilter1(float yn1); |
wiesdat | 8:83115293e84d | 110 | float hpfilter2(float yn2); |
wiesdat | 8:83115293e84d | 111 | float lpfilter1(float yhp1); |
wiesdat | 8:83115293e84d | 112 | float lpfilter2(float yhp2); |
wiesdat | 8:83115293e84d | 113 | float filter1(float emg_value1); |
wiesdat | 8:83115293e84d | 114 | float filter2(float emg_value2); |
wiesdat | 8:83115293e84d | 115 | float threshold1 (float yave1); |
wiesdat | 8:83115293e84d | 116 | float threshold2 (float yave2); |
wiesdat | 8:83115293e84d | 117 | float getv(float delta_t); |
wiesdat | 8:83115293e84d | 118 | float resetarm(); |
wiesdat | 8:83115293e84d | 119 | uint8_t badje (uint8_t y1, uint8_t y2); |
wiesdat | 8:83115293e84d | 120 | void batposition(int y); |
wiesdat | 8:83115293e84d | 121 | void sla(int k); |
wiesdat | 8:83115293e84d | 122 | float pidposition(float setpoint, float measurement); |
wiesdat | 8:83115293e84d | 123 | float pidarm(float rev_value, float mea_value); |
wiesdat | 8:83115293e84d | 124 | uint8_t armposition (uint8_t y1, uint8_t y2); |
wiesdat | 4:c22f3095b130 | 125 | |
wiesdat | 4:c22f3095b130 | 126 | /*FUNCTIES |
wiesdat | 4:c22f3095b130 | 127 | In deze sectie worden de functie geprogrammeerd, hieronder een uitleg per functie. De functies zijn verdeeld in drie groepen. In het script |
wiesdat | 4:c22f3095b130 | 128 | staat achter enkele functies een 1 of een 2. Er worden 2 signalen bewerkt, dus sommige functies staan dubbel in het script. Signaal 1 wordt |
wiesdat | 4:c22f3095b130 | 129 | door de functies met 1, en signaal 2 wordt door de functie met 2, bewerkt. |
wiesdat | 4:c22f3095b130 | 130 | |
wiesdat | 4:c22f3095b130 | 131 | GLOBAL FUNCTIONS |
wiesdat | 4:c22f3095b130 | 132 | Clamp - "clampt" een waarde binnen grenzen, als de geclampte variabele over de grens heen gaat krijgt de variabele de grenswaarde. |
wiesdat | 4:c22f3095b130 | 133 | setLooptimerflag - Hiermee wordt de ticker aangeroepen. |
wiesdat | 0:25a2e7ea29f3 | 134 | |
wiesdat | 4:c22f3095b130 | 135 | EMG FUNCTIONS |
wiesdat | 4:c22f3095b130 | 136 | readEMG - Leest het voltage af van de geselecteerde pin, in ons geval een EMG waarde. |
wiesdat | 4:c22f3095b130 | 137 | (notch/lp/hp)filter -Er worden drie filters gebruikt: lowpass, highpass en notchfilter. De funtie krijgt een input en geeft dan een gefilterde output. |
wiesdat | 4:c22f3095b130 | 138 | filter - Voert de notch,lp,hp filters achter elkaar uit op een input en geeft het gemiddelde van 500 meetpunten als output. |
wiesdat | 4:c22f3095b130 | 139 | threshold - Vergelijkt de input met een treshold, als de waarde boven de treshold komt (zodra een spier wordt aangespannen) wordt de output 1, anders 0. |
wiesdat | 4:c22f3095b130 | 140 | |
wiesdat | 4:c22f3095b130 | 141 | MOTORCONTROLFUNCTIONS |
wiesdat | 4:c22f3095b130 | 142 | resetarm - Brengt arm terug naar de nul positie, zet de encoder op 0. |
wiesdat | 4:c22f3095b130 | 143 | getv(delta_t) - Geeft de huidige draaisnelheid van motor1, delta_t is het tijdsinterval waarover gemeten wordt |
wiesdat | 4:c22f3095b130 | 144 | badje - Vergelijkt met dubbele input, y1 en y2. Er zijn vier situaties, beide = 0, y1=1, y2=1, beide =1. |
wiesdat | 4:c22f3095b130 | 145 | badposition - Leest de output van badje, laat het badje links, recht, niet draaien. |
wiesdat | 4:c22f3095b130 | 146 | pid... - Pid regelaar, de k waardes wordenin de define sectie gedefineerd. |
wiesdat | 4:c22f3095b130 | 147 | armposition - Op basis van de emgsignalen wordt hier een positie ingesteld. |
wiesdat | 4:c22f3095b130 | 148 | armtopos - Regelt de arm naar de opgegeven positie toe. |
wiesdat | 4:c22f3095b130 | 149 | */ |
wiesdat | 5:36df561f3ac1 | 150 | //HIDScope scope(5); |
wiesdat | 4:c22f3095b130 | 151 | //GLOBAL FUNCTIOMS |
wiesdat | 5:36df561f3ac1 | 152 | /*void viewer(){ |
wiesdat | 5:36df561f3ac1 | 153 | scope.set(0,yave1); |
wiesdat | 5:36df561f3ac1 | 154 | scope.set(1,y1); |
wiesdat | 5:36df561f3ac1 | 155 | scope.set(2,yave2); |
wiesdat | 5:36df561f3ac1 | 156 | scope.set(3,y2); |
wiesdat | 5:36df561f3ac1 | 157 | scope.send(); |
wiesdat | 5:36df561f3ac1 | 158 | }*/ |
wiesdat | 5:36df561f3ac1 | 159 | |
wiesdat | 3:611fd72c9d46 | 160 | void clamp(float * in, float min, float max) |
wiesdat | 0:25a2e7ea29f3 | 161 | { |
wiesdat | 3:611fd72c9d46 | 162 | *in > min ? *in < max? : *in = max: *in = min; |
wiesdat | 0:25a2e7ea29f3 | 163 | } |
wiesdat | 0:25a2e7ea29f3 | 164 | |
wiesdat | 5:36df561f3ac1 | 165 | void clampint(int * in, int min, int max) |
wiesdat | 5:36df561f3ac1 | 166 | { |
wiesdat | 5:36df561f3ac1 | 167 | *in > min ? *in < max? : *in = max: *in = min; |
wiesdat | 5:36df561f3ac1 | 168 | } |
wiesdat | 5:36df561f3ac1 | 169 | |
wiesdat | 4:c22f3095b130 | 170 | void setlooptimerflag(void) |
wiesdat | 4:c22f3095b130 | 171 | { |
wiesdat | 4:c22f3095b130 | 172 | looptimerflag = true; |
wiesdat | 4:c22f3095b130 | 173 | } |
wiesdat | 3:611fd72c9d46 | 174 | |
wiesdat | 4:c22f3095b130 | 175 | //EMG FUNCTIONS |
wiesdat | 0:25a2e7ea29f3 | 176 | float readEMG1() |
wiesdat | 0:25a2e7ea29f3 | 177 | { |
wiesdat | 0:25a2e7ea29f3 | 178 | emg_value1=emg1.read(); |
wiesdat | 0:25a2e7ea29f3 | 179 | return emg_value1; |
wiesdat | 0:25a2e7ea29f3 | 180 | } |
wiesdat | 0:25a2e7ea29f3 | 181 | |
wiesdat | 0:25a2e7ea29f3 | 182 | float readEMG2() |
wiesdat | 0:25a2e7ea29f3 | 183 | { |
wiesdat | 0:25a2e7ea29f3 | 184 | emg_value2=emg2.read(); |
wiesdat | 0:25a2e7ea29f3 | 185 | return emg_value2; |
wiesdat | 0:25a2e7ea29f3 | 186 | } |
wiesdat | 0:25a2e7ea29f3 | 187 | |
wiesdat | 0:25a2e7ea29f3 | 188 | float notchfilter1(float ylp1) |
wiesdat | 0:25a2e7ea29f3 | 189 | { |
wiesdat | 0:25a2e7ea29f3 | 190 | static float yn1,x1=0,x2=0,y1=0,y2=0,x; |
wiesdat | 0:25a2e7ea29f3 | 191 | x = ylp1; |
wiesdat | 0:25a2e7ea29f3 | 192 | yn1 = A0N*x + A1N*x1+A2N*x2+B1N*y1+B2N*y2; |
wiesdat | 0:25a2e7ea29f3 | 193 | x2 = x1; |
wiesdat | 0:25a2e7ea29f3 | 194 | x1 = x; |
wiesdat | 0:25a2e7ea29f3 | 195 | y2 = y1; |
wiesdat | 0:25a2e7ea29f3 | 196 | y1 = yn1; |
wiesdat | 0:25a2e7ea29f3 | 197 | return yn1; |
wiesdat | 0:25a2e7ea29f3 | 198 | } |
wiesdat | 0:25a2e7ea29f3 | 199 | |
wiesdat | 0:25a2e7ea29f3 | 200 | float notchfilter2(float ylp2) |
wiesdat | 0:25a2e7ea29f3 | 201 | { |
wiesdat | 0:25a2e7ea29f3 | 202 | static float x1=0,x2=0,y1=0,y2=0,x; |
wiesdat | 0:25a2e7ea29f3 | 203 | x = ylp2; |
wiesdat | 0:25a2e7ea29f3 | 204 | yn2 = A0N*x + A1N*x1+A2N*x2+B1N*y1+B2N*y2; |
wiesdat | 0:25a2e7ea29f3 | 205 | x2 = x1; |
wiesdat | 0:25a2e7ea29f3 | 206 | x1 = x; |
wiesdat | 0:25a2e7ea29f3 | 207 | y2 = y1; |
wiesdat | 0:25a2e7ea29f3 | 208 | y1 = yn2; |
wiesdat | 0:25a2e7ea29f3 | 209 | return yn2; |
wiesdat | 0:25a2e7ea29f3 | 210 | } |
wiesdat | 0:25a2e7ea29f3 | 211 | |
wiesdat | 0:25a2e7ea29f3 | 212 | float hpfilter1(float yn1) |
wiesdat | 0:25a2e7ea29f3 | 213 | { |
wiesdat | 0:25a2e7ea29f3 | 214 | static float x1=0,y1=0,x2=0, y2=0,x; |
wiesdat | 0:25a2e7ea29f3 | 215 | x = yn1; |
wiesdat | 0:25a2e7ea29f3 | 216 | yhp1 = x + A1HP*x1 + A0HP*x2 - B1HP*y1 - B0HP*y2; |
wiesdat | 0:25a2e7ea29f3 | 217 | x2 = x1; |
wiesdat | 0:25a2e7ea29f3 | 218 | x1 = x; |
wiesdat | 0:25a2e7ea29f3 | 219 | y2 = y1; |
wiesdat | 0:25a2e7ea29f3 | 220 | y1 = yhp1; |
wiesdat | 0:25a2e7ea29f3 | 221 | return yhp1; |
wiesdat | 0:25a2e7ea29f3 | 222 | } |
wiesdat | 0:25a2e7ea29f3 | 223 | |
wiesdat | 0:25a2e7ea29f3 | 224 | float hpfilter2(float yn2) |
wiesdat | 0:25a2e7ea29f3 | 225 | { |
wiesdat | 0:25a2e7ea29f3 | 226 | static float x1=0,y1=0,x2=0, y2=0,x; |
wiesdat | 0:25a2e7ea29f3 | 227 | x = yn2; |
wiesdat | 0:25a2e7ea29f3 | 228 | yhp2 = x + A1HP*x1 + A0HP*x2 - B1HP*y1 - B0HP*y2; |
wiesdat | 0:25a2e7ea29f3 | 229 | x2 = x1; |
wiesdat | 0:25a2e7ea29f3 | 230 | x1 = x; |
wiesdat | 0:25a2e7ea29f3 | 231 | y2 = y1; |
wiesdat | 0:25a2e7ea29f3 | 232 | y1 = yhp2; |
wiesdat | 0:25a2e7ea29f3 | 233 | return yhp2; |
wiesdat | 0:25a2e7ea29f3 | 234 | } |
wiesdat | 0:25a2e7ea29f3 | 235 | |
wiesdat | 0:25a2e7ea29f3 | 236 | float lpfilter1(float yhp1) |
wiesdat | 0:25a2e7ea29f3 | 237 | { |
wiesdat | 0:25a2e7ea29f3 | 238 | static float x1=0,y1=0,x2=0, y2=0,x; |
wiesdat | 0:25a2e7ea29f3 | 239 | x = yhp1; |
wiesdat | 0:25a2e7ea29f3 | 240 | ylp1 = A1LP*x1-B1LP*y1+A0LP*x2-B0LP*y2; |
wiesdat | 0:25a2e7ea29f3 | 241 | x2 = x1; |
wiesdat | 0:25a2e7ea29f3 | 242 | x1 = x; |
wiesdat | 0:25a2e7ea29f3 | 243 | y2 = y1; |
wiesdat | 0:25a2e7ea29f3 | 244 | y1 = ylp1; |
wiesdat | 0:25a2e7ea29f3 | 245 | return ylp1; |
wiesdat | 0:25a2e7ea29f3 | 246 | } |
wiesdat | 0:25a2e7ea29f3 | 247 | |
wiesdat | 0:25a2e7ea29f3 | 248 | float lpfilter2(float yhp2) |
wiesdat | 0:25a2e7ea29f3 | 249 | { |
wiesdat | 0:25a2e7ea29f3 | 250 | static float x1=0,y1=0,x2=0, y2=0,x; |
wiesdat | 0:25a2e7ea29f3 | 251 | x = yhp2; |
wiesdat | 0:25a2e7ea29f3 | 252 | ylp2 = A1LP*x1-B1LP*y1+A0LP*x2-B0LP*y2; |
wiesdat | 0:25a2e7ea29f3 | 253 | x2 = x1; |
wiesdat | 0:25a2e7ea29f3 | 254 | x1 = x; |
wiesdat | 0:25a2e7ea29f3 | 255 | y2 = y1; |
wiesdat | 0:25a2e7ea29f3 | 256 | y1 = ylp2; |
wiesdat | 0:25a2e7ea29f3 | 257 | return ylp2; |
wiesdat | 0:25a2e7ea29f3 | 258 | } |
wiesdat | 0:25a2e7ea29f3 | 259 | |
wiesdat | 0:25a2e7ea29f3 | 260 | float filter1(float emg_value1) |
wiesdat | 0:25a2e7ea29f3 | 261 | { |
wiesdat | 0:25a2e7ea29f3 | 262 | static uint16_t n; |
wiesdat | 0:25a2e7ea29f3 | 263 | yn1 = notchfilter1(emg_value1); |
wiesdat | 0:25a2e7ea29f3 | 264 | yhp1 = hpfilter1(yn1); |
wiesdat | 0:25a2e7ea29f3 | 265 | ylp1 = lpfilter1(yhp1); |
wiesdat | 0:25a2e7ea29f3 | 266 | ylp1 = fabs(ylp1); |
wiesdat | 0:25a2e7ea29f3 | 267 | ysum1 = ysum1+ylp1; |
wiesdat | 0:25a2e7ea29f3 | 268 | n++; |
wiesdat | 0:25a2e7ea29f3 | 269 | if(n==500) { |
wiesdat | 0:25a2e7ea29f3 | 270 | yave1 = ysum1/500; |
wiesdat | 0:25a2e7ea29f3 | 271 | ysum1 = 0; |
wiesdat | 0:25a2e7ea29f3 | 272 | n = 0; |
wiesdat | 0:25a2e7ea29f3 | 273 | } |
wiesdat | 0:25a2e7ea29f3 | 274 | return yave1; |
wiesdat | 0:25a2e7ea29f3 | 275 | } |
wiesdat | 0:25a2e7ea29f3 | 276 | |
wiesdat | 0:25a2e7ea29f3 | 277 | float filter2(float emg_value2) |
wiesdat | 0:25a2e7ea29f3 | 278 | { |
wiesdat | 0:25a2e7ea29f3 | 279 | static uint16_t n; |
wiesdat | 0:25a2e7ea29f3 | 280 | yn2 = notchfilter2(emg_value2); |
wiesdat | 0:25a2e7ea29f3 | 281 | yhp2 = hpfilter2(yn2); |
wiesdat | 0:25a2e7ea29f3 | 282 | ylp2 = lpfilter2(yhp2); |
wiesdat | 0:25a2e7ea29f3 | 283 | ylp2 = fabs(ylp2); |
wiesdat | 0:25a2e7ea29f3 | 284 | ysum2 = ysum2 + ylp2; |
wiesdat | 0:25a2e7ea29f3 | 285 | n++; |
wiesdat | 0:25a2e7ea29f3 | 286 | |
wiesdat | 5:36df561f3ac1 | 287 | if(n==NOSAMPL) { |
wiesdat | 5:36df561f3ac1 | 288 | yave2 = ysum2/NOSAMPL; |
wiesdat | 0:25a2e7ea29f3 | 289 | ysum2 = 0; |
wiesdat | 0:25a2e7ea29f3 | 290 | n = 0; |
wiesdat | 0:25a2e7ea29f3 | 291 | } |
wiesdat | 0:25a2e7ea29f3 | 292 | return yave2; |
wiesdat | 0:25a2e7ea29f3 | 293 | } |
wiesdat | 0:25a2e7ea29f3 | 294 | |
wiesdat | 0:25a2e7ea29f3 | 295 | float threshold1 (float yave1) |
wiesdat | 0:25a2e7ea29f3 | 296 | { |
wiesdat | 0:25a2e7ea29f3 | 297 | if(yave1>THRESHOLD) { |
wiesdat | 0:25a2e7ea29f3 | 298 | y1 = 1; |
wiesdat | 0:25a2e7ea29f3 | 299 | } else { |
wiesdat | 0:25a2e7ea29f3 | 300 | y1 = 0; |
wiesdat | 0:25a2e7ea29f3 | 301 | } |
wiesdat | 0:25a2e7ea29f3 | 302 | return y1; |
wiesdat | 0:25a2e7ea29f3 | 303 | } |
wiesdat | 0:25a2e7ea29f3 | 304 | |
wiesdat | 0:25a2e7ea29f3 | 305 | float threshold2 (float yave2) |
wiesdat | 0:25a2e7ea29f3 | 306 | { |
wiesdat | 0:25a2e7ea29f3 | 307 | if(yave2>THRESHOLD) { |
wiesdat | 0:25a2e7ea29f3 | 308 | y2 = 1; |
wiesdat | 0:25a2e7ea29f3 | 309 | } else { |
wiesdat | 0:25a2e7ea29f3 | 310 | y2 = 0; |
wiesdat | 0:25a2e7ea29f3 | 311 | } |
wiesdat | 0:25a2e7ea29f3 | 312 | return y2; |
wiesdat | 0:25a2e7ea29f3 | 313 | } |
wiesdat | 0:25a2e7ea29f3 | 314 | |
wiesdat | 4:c22f3095b130 | 315 | //MOTORCONTROL FUNCTIONS |
wiesdat | 4:c22f3095b130 | 316 | float getv(float delta_t) |
wiesdat | 0:25a2e7ea29f3 | 317 | { |
wiesdat | 4:c22f3095b130 | 318 | int enca1=0,enca2=0,counts=0; |
wiesdat | 3:611fd72c9d46 | 319 | float v; |
wiesdat | 2:f3e8a27d376c | 320 | int n =0 ; |
wiesdat | 2:f3e8a27d376c | 321 | while(n<3) { |
wiesdat | 2:f3e8a27d376c | 322 | wait(delta_t); |
wiesdat | 2:f3e8a27d376c | 323 | enca2 = enca1; |
wiesdat | 4:c22f3095b130 | 324 | enca1 = encoder1.getPosition(); |
wiesdat | 2:f3e8a27d376c | 325 | n++; |
wiesdat | 2:f3e8a27d376c | 326 | } |
wiesdat | 2:f3e8a27d376c | 327 | counts = (enca1 - enca2)/delta_t; |
wiesdat | 4:c22f3095b130 | 328 | v = (counts)*((2*3.14159265359)/1550)*0.5; |
wiesdat | 2:f3e8a27d376c | 329 | return v; |
wiesdat | 2:f3e8a27d376c | 330 | } |
wiesdat | 4:c22f3095b130 | 331 | |
wiesdat | 4:c22f3095b130 | 332 | float resetarm() |
wiesdat | 3:611fd72c9d46 | 333 | { |
wiesdat | 3:611fd72c9d46 | 334 | v1 = 1; |
wiesdat | 3:611fd72c9d46 | 335 | while(v1 !=0) { |
wiesdat | 3:611fd72c9d46 | 336 | dir1 = 0; |
wiesdat | 3:611fd72c9d46 | 337 | pwm1.write(0.1); |
wiesdat | 4:c22f3095b130 | 338 | v1 =getv(0.1); |
wiesdat | 3:611fd72c9d46 | 339 | } |
wiesdat | 3:611fd72c9d46 | 340 | pwm1 = 0; |
wiesdat | 3:611fd72c9d46 | 341 | dir1 =1; |
wiesdat | 3:611fd72c9d46 | 342 | encoder1.setPosition(0); |
wiesdat | 3:611fd72c9d46 | 343 | return pwm1; |
wiesdat | 3:611fd72c9d46 | 344 | } |
wiesdat | 3:611fd72c9d46 | 345 | uint8_t badje (uint8_t y1, uint8_t y2) |
wiesdat | 3:611fd72c9d46 | 346 | { |
wiesdat | 3:611fd72c9d46 | 347 | if (y1>0 && y2>0 && check>0) { |
wiesdat | 3:611fd72c9d46 | 348 | badjedone=1; |
wiesdat | 3:611fd72c9d46 | 349 | check=0; |
wiesdat | 10:e628a87d18aa | 350 | /*rood = 1; |
wiesdat | 10:e628a87d18aa | 351 | groen = 0;*/ |
wiesdat | 3:611fd72c9d46 | 352 | cout<<"ga naar mode 2"<<endl; |
wiesdat | 11:97759ceeb638 | 353 | wait(1); |
wiesdat | 3:611fd72c9d46 | 354 | } else if (y1>0 && y2>0) { |
wiesdat | 3:611fd72c9d46 | 355 | check=1; |
wiesdat | 3:611fd72c9d46 | 356 | } else if (y1>0) { |
wiesdat | 3:611fd72c9d46 | 357 | badjes=0; |
wiesdat | 3:611fd72c9d46 | 358 | check=0; |
wiesdat | 3:611fd72c9d46 | 359 | } else if (y2>0 ) { |
wiesdat | 3:611fd72c9d46 | 360 | badjes=1; |
wiesdat | 3:611fd72c9d46 | 361 | check=0; |
wiesdat | 4:c22f3095b130 | 362 | } else { |
wiesdat | 3:611fd72c9d46 | 363 | check=0; |
wiesdat | 3:611fd72c9d46 | 364 | badjes = 2; |
wiesdat | 3:611fd72c9d46 | 365 | } |
wiesdat | 3:611fd72c9d46 | 366 | return badjes; |
wiesdat | 3:611fd72c9d46 | 367 | } |
wiesdat | 3:611fd72c9d46 | 368 | |
wiesdat | 4:c22f3095b130 | 369 | void batposition(int y) |
wiesdat | 4:c22f3095b130 | 370 | { |
wiesdat | 4:c22f3095b130 | 371 | switch(y) { |
wiesdat | 4:c22f3095b130 | 372 | case 0: |
wiesdat | 4:c22f3095b130 | 373 | |
wiesdat | 4:c22f3095b130 | 374 | dir = 1; |
wiesdat | 4:c22f3095b130 | 375 | pwm.write(0.4); |
wiesdat | 4:c22f3095b130 | 376 | wait(0.04); |
wiesdat | 4:c22f3095b130 | 377 | pwm.write(0); |
wiesdat | 5:36df561f3ac1 | 378 | cout<<"links"<<endl; |
wiesdat | 4:c22f3095b130 | 379 | break; |
wiesdat | 4:c22f3095b130 | 380 | case 1: |
wiesdat | 4:c22f3095b130 | 381 | dir = 0; |
wiesdat | 4:c22f3095b130 | 382 | pwm.write(0.4); |
wiesdat | 4:c22f3095b130 | 383 | wait(0.04); |
wiesdat | 4:c22f3095b130 | 384 | pwm.write(0); |
wiesdat | 5:36df561f3ac1 | 385 | cout<<"rechts"<<endl; |
wiesdat | 4:c22f3095b130 | 386 | break; |
wiesdat | 4:c22f3095b130 | 387 | case 2: |
wiesdat | 4:c22f3095b130 | 388 | pwm.write(0); |
wiesdat | 4:c22f3095b130 | 389 | break; |
wiesdat | 4:c22f3095b130 | 390 | |
wiesdat | 4:c22f3095b130 | 391 | } |
wiesdat | 4:c22f3095b130 | 392 | |
wiesdat | 4:c22f3095b130 | 393 | } |
wiesdat | 4:c22f3095b130 | 394 | |
wiesdat | 8:83115293e84d | 395 | void sla(int k) |
wiesdat | 8:83115293e84d | 396 | { |
wiesdat | 8:83115293e84d | 397 | float maxpwm; |
wiesdat | 8:83115293e84d | 398 | float new_pwm; |
wiesdat | 8:83115293e84d | 399 | switch(k) |
wiesdat | 8:83115293e84d | 400 | { |
wiesdat | 8:83115293e84d | 401 | case 1: |
wiesdat | 11:97759ceeb638 | 402 | maxpwm=0.6; //DE MAX PWM'en AANPASSEN VOOR DE ANDERE CASES!!!! |
wiesdat | 8:83115293e84d | 403 | break; |
wiesdat | 8:83115293e84d | 404 | case 2: |
wiesdat | 11:97759ceeb638 | 405 | maxpwm=0.8; |
wiesdat | 8:83115293e84d | 406 | break; |
wiesdat | 8:83115293e84d | 407 | case 3: |
wiesdat | 11:97759ceeb638 | 408 | maxpwm=1.0; |
wiesdat | 8:83115293e84d | 409 | break; |
wiesdat | 11:97759ceeb638 | 410 | /*case 4: |
wiesdat | 10:e628a87d18aa | 411 | maxpwm=0.2; |
wiesdat | 11:97759ceeb638 | 412 | break;*/ |
wiesdat | 8:83115293e84d | 413 | default: |
wiesdat | 8:83115293e84d | 414 | maxpwm=0; |
wiesdat | 8:83115293e84d | 415 | break; |
wiesdat | 8:83115293e84d | 416 | } |
wiesdat | 11:97759ceeb638 | 417 | while(abs(encoder1.getPosition()<MAXPOS)) |
wiesdat | 8:83115293e84d | 418 | { |
wiesdat | 8:83115293e84d | 419 | new_pwm=pidarm(MAXPOS,encoder1.getPosition()); |
wiesdat | 11:97759ceeb638 | 420 | clamp(&new_pwm,-1,maxpwm); |
wiesdat | 8:83115293e84d | 421 | if (new_pwm>0) |
wiesdat | 8:83115293e84d | 422 | { |
wiesdat | 8:83115293e84d | 423 | dir1=1; |
wiesdat | 8:83115293e84d | 424 | } |
wiesdat | 8:83115293e84d | 425 | else if (new_pwm<0) |
wiesdat | 8:83115293e84d | 426 | { |
wiesdat | 8:83115293e84d | 427 | dir1=0; |
wiesdat | 8:83115293e84d | 428 | } |
wiesdat | 8:83115293e84d | 429 | pwm1.write(fabs(new_pwm)); |
wiesdat | 8:83115293e84d | 430 | cout<<"pwm1: "<<new_pwm<<endl; |
wiesdat | 11:97759ceeb638 | 431 | cout<<"dir: "<<dir<<endl; |
wiesdat | 11:97759ceeb638 | 432 | cout<<"v: "<<getv(0.01)<<endl; |
wiesdat | 8:83115293e84d | 433 | cout<<"pos: "<<encoder1.getPosition()<<endl; |
wiesdat | 8:83115293e84d | 434 | } |
wiesdat | 11:97759ceeb638 | 435 | dir1=0; |
wiesdat | 11:97759ceeb638 | 436 | pwm1.write(.5); |
wiesdat | 11:97759ceeb638 | 437 | wait(.1); |
wiesdat | 11:97759ceeb638 | 438 | pwm1.write(0); |
wiesdat | 11:97759ceeb638 | 439 | wait(1); |
wiesdat | 11:97759ceeb638 | 440 | //cout<< encoder1.getPosition()<<endl; |
wiesdat | 8:83115293e84d | 441 | } |
wiesdat | 8:83115293e84d | 442 | |
wiesdat | 4:c22f3095b130 | 443 | float pidposition(float setpoint, float measurement) |
wiesdat | 3:611fd72c9d46 | 444 | { |
wiesdat | 4:c22f3095b130 | 445 | float error, out, out_p=0,out_d=0; |
wiesdat | 4:c22f3095b130 | 446 | static float prev_error = 0; |
wiesdat | 4:c22f3095b130 | 447 | error = setpoint-measurement; |
wiesdat | 4:c22f3095b130 | 448 | out_p = error*K_P1; |
wiesdat | 4:c22f3095b130 | 449 | out_i1 += error*K_I1; |
wiesdat | 4:c22f3095b130 | 450 | out_d = (error-prev_error)*K_D1; |
wiesdat | 4:c22f3095b130 | 451 | clamp(&out_i1,-I_LIMIT1,I_LIMIT1); |
wiesdat | 4:c22f3095b130 | 452 | prev_error = error; |
wiesdat | 4:c22f3095b130 | 453 | out = out_i1+out_p+out_d; |
wiesdat | 4:c22f3095b130 | 454 | return out; |
wiesdat | 4:c22f3095b130 | 455 | } |
wiesdat | 4:c22f3095b130 | 456 | |
wiesdat | 8:83115293e84d | 457 | float pidarm(float rev_value, float mea_value) |
wiesdat | 8:83115293e84d | 458 | { |
wiesdat | 8:83115293e84d | 459 | float error; |
wiesdat | 8:83115293e84d | 460 | static float prev_error = 0; |
wiesdat | 8:83115293e84d | 461 | float p_out = 0; |
wiesdat | 8:83115293e84d | 462 | static float i_out = 0; |
wiesdat | 8:83115293e84d | 463 | float d_out = 0; |
wiesdat | 8:83115293e84d | 464 | error = rev_value - mea_value; |
wiesdat | 8:83115293e84d | 465 | p_out = error * KSLA_P; |
wiesdat | 8:83115293e84d | 466 | i_out += error * KSLA_I; |
wiesdat | 8:83115293e84d | 467 | d_out = (error - prev_error) * KSLA_D; |
wiesdat | 8:83115293e84d | 468 | clamp(&i_out,-I_LIMIT1,I_LIMIT1); |
wiesdat | 8:83115293e84d | 469 | prev_error=error; |
wiesdat | 8:83115293e84d | 470 | return p_out + i_out + d_out; |
wiesdat | 8:83115293e84d | 471 | } |
wiesdat | 4:c22f3095b130 | 472 | |
wiesdat | 5:36df561f3ac1 | 473 | |
wiesdat | 4:c22f3095b130 | 474 | uint8_t armposition (uint8_t y1, uint8_t y2) |
wiesdat | 4:c22f3095b130 | 475 | { |
wiesdat | 4:c22f3095b130 | 476 | static int stand=0; |
wiesdat | 4:c22f3095b130 | 477 | if (y1>0 && y2>0 && check>0) { |
wiesdat | 5:36df561f3ac1 | 478 | badjedone=2; |
wiesdat | 4:c22f3095b130 | 479 | check=0; |
wiesdat | 10:e628a87d18aa | 480 | /*rood = 0; |
wiesdat | 10:e628a87d18aa | 481 | groen = 1;*/ |
wiesdat | 4:c22f3095b130 | 482 | } else if (y1>0 && y2>0) { |
wiesdat | 4:c22f3095b130 | 483 | check=1; |
wiesdat | 4:c22f3095b130 | 484 | } else if (y1>0) { |
wiesdat | 4:c22f3095b130 | 485 | stand=stand+1; |
wiesdat | 4:c22f3095b130 | 486 | check=0; |
wiesdat | 4:c22f3095b130 | 487 | cout<<"stand "<<stand<<endl; |
wiesdat | 4:c22f3095b130 | 488 | } else if (y2>0 ) { |
wiesdat | 4:c22f3095b130 | 489 | stand=stand-1; |
wiesdat | 4:c22f3095b130 | 490 | check=0; |
wiesdat | 4:c22f3095b130 | 491 | cout<<"stand "<<stand<<endl; |
wiesdat | 4:c22f3095b130 | 492 | } else { |
wiesdat | 5:36df561f3ac1 | 493 | |
wiesdat | 4:c22f3095b130 | 494 | check =0; |
wiesdat | 4:c22f3095b130 | 495 | } |
wiesdat | 6:b69b9597d4fc | 496 | clampint(&stand,1,3); |
wiesdat | 4:c22f3095b130 | 497 | return stand; |
wiesdat | 4:c22f3095b130 | 498 | } |
wiesdat | 4:c22f3095b130 | 499 | |
wiesdat | 6:b69b9597d4fc | 500 | float gotopos(int pos) |
wiesdat | 4:c22f3095b130 | 501 | { |
wiesdat | 6:b69b9597d4fc | 502 | float out1; |
wiesdat | 6:b69b9597d4fc | 503 | |
wiesdat | 6:b69b9597d4fc | 504 | switch(pos) { |
wiesdat | 2:f3e8a27d376c | 505 | case 1: |
wiesdat | 6:b69b9597d4fc | 506 | pos = 100; |
wiesdat | 2:f3e8a27d376c | 507 | break; |
wiesdat | 2:f3e8a27d376c | 508 | |
wiesdat | 2:f3e8a27d376c | 509 | case 2: |
wiesdat | 6:b69b9597d4fc | 510 | pos = 200; |
wiesdat | 2:f3e8a27d376c | 511 | break; |
wiesdat | 2:f3e8a27d376c | 512 | |
wiesdat | 2:f3e8a27d376c | 513 | case 3: |
wiesdat | 11:97759ceeb638 | 514 | pos = 300; |
wiesdat | 11:97759ceeb638 | 515 | break; |
wiesdat | 11:97759ceeb638 | 516 | default: |
wiesdat | 2:f3e8a27d376c | 517 | break; |
wiesdat | 2:f3e8a27d376c | 518 | } |
wiesdat | 6:b69b9597d4fc | 519 | |
wiesdat | 11:97759ceeb638 | 520 | while((abs(pos-encoder1.getPosition()) >15)|| (v1>0.1)) { |
wiesdat | 2:f3e8a27d376c | 521 | |
wiesdat | 2:f3e8a27d376c | 522 | while(!looptimerflag); |
wiesdat | 2:f3e8a27d376c | 523 | looptimerflag = false; |
wiesdat | 11:97759ceeb638 | 524 | cout << "Deltapos: " << abs(pos-encoder1.getPosition()) << endl; |
wiesdat | 6:b69b9597d4fc | 525 | out1 = pidposition(pos,encoder1.getPosition()); |
wiesdat | 11:97759ceeb638 | 526 | clamp(&out1,-1,1); |
wiesdat | 11:97759ceeb638 | 527 | cout <<"out1: " << out1 <<endl; |
wiesdat | 6:b69b9597d4fc | 528 | if(out1>0) { |
wiesdat | 2:f3e8a27d376c | 529 | dir1 = 1; |
wiesdat | 2:f3e8a27d376c | 530 | |
wiesdat | 11:97759ceeb638 | 531 | } else if(out1<0) { |
wiesdat | 2:f3e8a27d376c | 532 | dir1 = 0; |
wiesdat | 2:f3e8a27d376c | 533 | } |
wiesdat | 11:97759ceeb638 | 534 | else{ |
wiesdat | 11:97759ceeb638 | 535 | } |
wiesdat | 6:b69b9597d4fc | 536 | pwm1 = fabs(out1); |
wiesdat | 4:c22f3095b130 | 537 | v1 = getv(0.001); |
wiesdat | 2:f3e8a27d376c | 538 | } |
wiesdat | 2:f3e8a27d376c | 539 | pwm1 =0; |
wiesdat | 2:f3e8a27d376c | 540 | |
wiesdat | 6:b69b9597d4fc | 541 | return pwm1; |
wiesdat | 2:f3e8a27d376c | 542 | } |
wiesdat | 2:f3e8a27d376c | 543 | |
wiesdat | 4:c22f3095b130 | 544 | // MAIN SCRIPT |
wiesdat | 0:25a2e7ea29f3 | 545 | int main() |
wiesdat | 0:25a2e7ea29f3 | 546 | { |
wiesdat | 10:e628a87d18aa | 547 | /*rood = 0; |
wiesdat | 1:a010e434a360 | 548 | blauw = 1; |
wiesdat | 10:e628a87d18aa | 549 | groen = 1;*/ |
wiesdat | 4:c22f3095b130 | 550 | resetarm(); |
wiesdat | 0:25a2e7ea29f3 | 551 | pc.baud(115200); |
wiesdat | 0:25a2e7ea29f3 | 552 | timer.attach(setlooptimerflag,TSAMP); |
wiesdat | 0:25a2e7ea29f3 | 553 | wait(2); |
wiesdat | 5:36df561f3ac1 | 554 | cout<<"Begin programma"<<endl; |
wiesdat | 0:25a2e7ea29f3 | 555 | while(1) { |
wiesdat | 11:97759ceeb638 | 556 | //Per TSAMP word EMG uitgelezen, gefilterd en gemiddeld |
wiesdat | 0:25a2e7ea29f3 | 557 | while(!looptimerflag); |
wiesdat | 0:25a2e7ea29f3 | 558 | looptimerflag = false; |
wiesdat | 0:25a2e7ea29f3 | 559 | emg_value1 = readEMG1(); |
wiesdat | 0:25a2e7ea29f3 | 560 | emg_value2 = readEMG2(); |
wiesdat | 0:25a2e7ea29f3 | 561 | yave1 = filter1(emg_value1); |
wiesdat | 0:25a2e7ea29f3 | 562 | yave2 = filter2(emg_value2); |
wiesdat | 0:25a2e7ea29f3 | 563 | y1=threshold1(yave1); |
wiesdat | 0:25a2e7ea29f3 | 564 | y2=threshold2(yave2); |
wiesdat | 0:25a2e7ea29f3 | 565 | teller++; |
wiesdat | 0:25a2e7ea29f3 | 566 | |
wiesdat | 4:c22f3095b130 | 567 | /*Dit gedeelte voert de bewegingen uit. In de eerste fase kan met de linker spier het batje naar links gedraaid worden, |
wiesdat | 4:c22f3095b130 | 568 | en met de rechter spier het batje naar recht. Als het badje in de juiste positie staat kunnen beide spieren tegelijk |
wiesdat | 4:c22f3095b130 | 569 | aangespannen worden om naar de volgende fase te gaan. In fase 2 wordt de armpositie ingesteld. Hoe groter de afstand van de |
wiesdat | 4:c22f3095b130 | 570 | arm met de bal, hoe harder de arm gaat slaan. Zodra de arm in de goede positie staat kan door beide spieren aan te spannen worden |
wiesdat | 4:c22f3095b130 | 571 | doorgegaan naar fase 3. In fase 3 slaat de arm de bal. De snelheid die ingesteld wordt waarmee de arm gaat slaan wordt bepaald aan de |
wiesdat | 4:c22f3095b130 | 572 | hand van de positie waarin de arm in fase 2 is gezet. Zodra de arm de bal geslagen heeft moet de arm gereset worden en terug gaan naar |
wiesdat | 4:c22f3095b130 | 573 | fase 1.*/ |
wiesdat | 0:25a2e7ea29f3 | 574 | |
wiesdat | 5:36df561f3ac1 | 575 | if (teller==NOSAMPL) { |
wiesdat | 0:25a2e7ea29f3 | 576 | teller=0; |
wiesdat | 5:36df561f3ac1 | 577 | switch(badjedone) { |
wiesdat | 5:36df561f3ac1 | 578 | case 0: |
wiesdat | 10:e628a87d18aa | 579 | rood=0; |
wiesdat | 10:e628a87d18aa | 580 | groen=1; |
wiesdat | 10:e628a87d18aa | 581 | blauw=1; |
wiesdat | 5:36df561f3ac1 | 582 | cout<<"fase1"<<endl; |
wiesdat | 5:36df561f3ac1 | 583 | badjestand=badje(y1,y2); |
wiesdat | 5:36df561f3ac1 | 584 | batposition(badjestand); |
wiesdat | 11:97759ceeb638 | 585 | armstand=1; |
wiesdat | 5:36df561f3ac1 | 586 | break; |
wiesdat | 5:36df561f3ac1 | 587 | |
wiesdat | 5:36df561f3ac1 | 588 | case 1: |
wiesdat | 10:e628a87d18aa | 589 | rood=1; |
wiesdat | 10:e628a87d18aa | 590 | groen=0; |
wiesdat | 10:e628a87d18aa | 591 | blauw=1; |
wiesdat | 5:36df561f3ac1 | 592 | cout<<"fase2"<<endl; |
wiesdat | 4:c22f3095b130 | 593 | armstand=armposition(y1,y2); |
wiesdat | 6:b69b9597d4fc | 594 | gotopos(armstand); |
wiesdat | 3:611fd72c9d46 | 595 | cout<<"armstand "<<armstand<<endl; |
wiesdat | 5:36df561f3ac1 | 596 | |
wiesdat | 5:36df561f3ac1 | 597 | //armtopos(armstand); |
wiesdat | 5:36df561f3ac1 | 598 | //cout<<"badjedont: "<<badjedone<<endl; |
wiesdat | 5:36df561f3ac1 | 599 | break; |
wiesdat | 5:36df561f3ac1 | 600 | |
wiesdat | 5:36df561f3ac1 | 601 | case 2: |
wiesdat | 10:e628a87d18aa | 602 | rood=1; |
wiesdat | 10:e628a87d18aa | 603 | groen=1; |
wiesdat | 10:e628a87d18aa | 604 | blauw=0; |
wiesdat | 10:e628a87d18aa | 605 | resetarm(); |
wiesdat | 9:80693874f9ce | 606 | cout<<"sla"<<endl; |
wiesdat | 9:80693874f9ce | 607 | sla(armstand); |
wiesdat | 9:80693874f9ce | 608 | resetarm(); |
wiesdat | 5:36df561f3ac1 | 609 | badjedone=0; |
wiesdat | 5:36df561f3ac1 | 610 | break; |
wiesdat | 5:36df561f3ac1 | 611 | default: |
wiesdat | 5:36df561f3ac1 | 612 | break; |
wiesdat | 0:25a2e7ea29f3 | 613 | } |
wiesdat | 5:36df561f3ac1 | 614 | |
wiesdat | 0:25a2e7ea29f3 | 615 | } |
wiesdat | 0:25a2e7ea29f3 | 616 | } |
wiesdat | 5:36df561f3ac1 | 617 | |
wiesdat | 5:36df561f3ac1 | 618 | } |
wiesdat | 0:25a2e7ea29f3 | 619 | |
wiesdat | 5:36df561f3ac1 | 620 |