r
Dependencies: Encoder HIDScope TextLCD mbed-dsp mbed
Fork of Main-script_groep7 by
main.cpp@5:4842219cb77c, 2014-10-31 (annotated)
- Committer:
- phgbartels
- Date:
- Fri Oct 31 08:36:46 2014 +0000
- Revision:
- 5:4842219cb77c
- Parent:
- 4:377ddd65e4a6
- Child:
- 6:a7379a681adf
Main_script_groep7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
phgbartels | 0:2386012c6594 | 1 | /********************************************/ |
phgbartels | 0:2386012c6594 | 2 | /* */ |
phgbartels | 0:2386012c6594 | 3 | /* BRONCODE GROEP 7, MODULE 9, 2014 */ |
phgbartels | 0:2386012c6594 | 4 | /* *-THE SLAP-* */ |
phgbartels | 0:2386012c6594 | 5 | /* */ |
phgbartels | 0:2386012c6594 | 6 | /* -Anne ten Dam */ |
phgbartels | 0:2386012c6594 | 7 | /* -Laura de Heus */ |
phgbartels | 0:2386012c6594 | 8 | /* -Moniek Strijdveen */ |
phgbartels | 0:2386012c6594 | 9 | /* -Bart Arendshorst */ |
phgbartels | 0:2386012c6594 | 10 | /* -Peter Bartels */ |
phgbartels | 0:2386012c6594 | 11 | /********************************************/ |
phgbartels | 0:2386012c6594 | 12 | |
phgbartels | 0:2386012c6594 | 13 | /* |
phgbartels | 0:2386012c6594 | 14 | #include voor alle libraries |
phgbartels | 0:2386012c6594 | 15 | */ |
phgbartels | 0:2386012c6594 | 16 | #include "TextLCD.h" |
phgbartels | 0:2386012c6594 | 17 | #include "mbed.h" |
phgbartels | 0:2386012c6594 | 18 | #include "encoder.h" |
phgbartels | 1:b08ac32d1ddc | 19 | //#include "HIDScope.h" |
phgbartels | 0:2386012c6594 | 20 | #include "PwmOut.h" |
phgbartels | 0:2386012c6594 | 21 | |
phgbartels | 0:2386012c6594 | 22 | /* |
phgbartels | 0:2386012c6594 | 23 | #define vaste waarden |
phgbartels | 0:2386012c6594 | 24 | */ |
phgbartels | 0:2386012c6594 | 25 | /*definieren pinnen Motor 1*/ |
phgbartels | 0:2386012c6594 | 26 | #define M1_PWM PTA5 |
phgbartels | 0:2386012c6594 | 27 | #define M1_DIR PTA4 |
phgbartels | 0:2386012c6594 | 28 | #define M2_PWM PTC8 |
phgbartels | 0:2386012c6594 | 29 | #define M2_DIR PTC9 |
phgbartels | 0:2386012c6594 | 30 | /*Definieren minimale waarden PWM per motor*/ |
phgbartels | 0:2386012c6594 | 31 | #define PWM1_min_50 0.529 /*met koppelstuk!*/ |
phgbartels | 0:2386012c6594 | 32 | #define PWM2_min_30 0.529 /*aanpassen! Klopt nog niet met koppelstuk*/ |
phgbartels | 0:2386012c6594 | 33 | /*Definieren om de hoeveel seconden er gegevens naar de HID-scope gestuurd worden.*/ |
phgbartels | 0:2386012c6594 | 34 | #define TSAMP 0.005 |
phgbartels | 5:4842219cb77c | 35 | #define K_P (5) |
phgbartels | 5:4842219cb77c | 36 | #define K_I (0.1 * TSAMP) |
phgbartels | 5:4842219cb77c | 37 | #define K_D (0) |
phgbartels | 0:2386012c6594 | 38 | //#define K_D (0.0005 /TSAMP) |
phgbartels | 0:2386012c6594 | 39 | #define I_LIMIT 100. |
phgbartels | 0:2386012c6594 | 40 | #define PI 3.1415926535897 |
phgbartels | 0:2386012c6594 | 41 | #define lengte_arm 0.5 |
phgbartels | 0:2386012c6594 | 42 | |
phgbartels | 0:2386012c6594 | 43 | /* |
phgbartels | 0:2386012c6594 | 44 | Geef een naam aan een actie en vertel welke pinnen hiervoor gebruikt worden. |
phgbartels | 0:2386012c6594 | 45 | */ |
phgbartels | 0:2386012c6594 | 46 | TextLCD lcd(PTD2, PTA12, PTB2, PTB3, PTC2, PTA13, TextLCD::LCD16x2); // rs, e, d4-d7 |
phgbartels | 0:2386012c6594 | 47 | Encoder motor1(PTD3,PTD1); |
phgbartels | 0:2386012c6594 | 48 | Encoder motor2(PTD5, PTD0); |
phgbartels | 0:2386012c6594 | 49 | PwmOut pwm_motor1(M1_PWM); |
phgbartels | 0:2386012c6594 | 50 | PwmOut pwm_motor2(M2_PWM); |
phgbartels | 0:2386012c6594 | 51 | DigitalOut motordir1(M1_DIR); |
phgbartels | 0:2386012c6594 | 52 | DigitalOut motordir2(M2_DIR); |
phgbartels | 0:2386012c6594 | 53 | DigitalOut LEDGREEN(LED_GREEN); |
phgbartels | 0:2386012c6594 | 54 | DigitalOut LEDRED(LED_RED); |
phgbartels | 4:377ddd65e4a6 | 55 | Serial pc(USBTX,USBRX); |
phgbartels | 0:2386012c6594 | 56 | /* |
phgbartels | 0:2386012c6594 | 57 | definieer namen aan var, float, int, static float, uint8_t, uint16_t etc. en geef ze eventueel een waarde |
phgbartels | 0:2386012c6594 | 58 | */ |
phgbartels | 0:2386012c6594 | 59 | Ticker statemachine; |
phgbartels | 0:2386012c6594 | 60 | Ticker screen; |
phgbartels | 0:2386012c6594 | 61 | int previous_herhalingen = 0; |
phgbartels | 1:b08ac32d1ddc | 62 | int current_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 63 | int PWM2_percentage = 100; |
phgbartels | 0:2386012c6594 | 64 | int aantal_rotaties_1 = 10; |
phgbartels | 0:2386012c6594 | 65 | int aantalcr_1 = 1600; |
phgbartels | 0:2386012c6594 | 66 | int aantalcr_2 = 960; |
phgbartels | 0:2386012c6594 | 67 | int beginpos_motor1; |
phgbartels | 0:2386012c6594 | 68 | int beginpos_motor1_new; |
phgbartels | 0:2386012c6594 | 69 | int beginpos_motor2; |
phgbartels | 0:2386012c6594 | 70 | int beginpos_motor2_new; |
phgbartels | 0:2386012c6594 | 71 | int previous_pos_motor1 = 0; |
phgbartels | 0:2386012c6594 | 72 | int current_pos_motor1; |
phgbartels | 5:4842219cb77c | 73 | int EMG = 1; |
phgbartels | 0:2386012c6594 | 74 | int delta_pos_motor1_puls; |
phgbartels | 0:2386012c6594 | 75 | void clamp(float * in, float min, float max); |
phgbartels | 0:2386012c6594 | 76 | volatile bool looptimerflag; |
phgbartels | 2:de7b6c1d67c4 | 77 | int16_t gewenste_snelheid = 2; |
phgbartels | 2:de7b6c1d67c4 | 78 | int16_t gewenste_snelheid_rad = 4; |
phgbartels | 2:de7b6c1d67c4 | 79 | int16_t gewenste_snelheid_rad_return = 1; |
phgbartels | 0:2386012c6594 | 80 | float pid(float setpoint, float measurement); |
phgbartels | 0:2386012c6594 | 81 | float pos_motor1_rad; |
phgbartels | 0:2386012c6594 | 82 | float PWM1_percentage = 0; |
phgbartels | 0:2386012c6594 | 83 | float delta_pos_motor1_rad; |
phgbartels | 0:2386012c6594 | 84 | float snelheid_motor1_rad; |
phgbartels | 0:2386012c6594 | 85 | float snelheid_arm_ms; |
phgbartels | 0:2386012c6594 | 86 | float PWM1; |
phgbartels | 0:2386012c6594 | 87 | float PWM2; |
phgbartels | 0:2386012c6594 | 88 | float Speed_motor1; |
phgbartels | 0:2386012c6594 | 89 | float Speed_motor1rad; |
phgbartels | 1:b08ac32d1ddc | 90 | float setpoint = 0; |
phgbartels | 1:b08ac32d1ddc | 91 | float prev_setpoint = 0; |
phgbartels | 0:2386012c6594 | 92 | |
phgbartels | 1:b08ac32d1ddc | 93 | //HIDScope scope(6); |
phgbartels | 0:2386012c6594 | 94 | |
phgbartels | 0:2386012c6594 | 95 | enum state_t {RUST, ARM_KALIBRATIE, EMG_KALIBRATIE, METEN_RICHTING, METEN_HOOGTE, INSTELLEN_RICHTING, SLAAN, RETURN2RUST}; //verschillende stadia definieren voor gebruik in CASES |
phgbartels | 0:2386012c6594 | 96 | state_t state = RUST; |
phgbartels | 0:2386012c6594 | 97 | |
phgbartels | 0:2386012c6594 | 98 | //functies die vanuit de statemachinefunction aangeroepen kunnen worden |
phgbartels | 0:2386012c6594 | 99 | void rust() { |
phgbartels | 0:2386012c6594 | 100 | current_herhalingen = previous_herhalingen + 1; previous_herhalingen = current_herhalingen; |
phgbartels | 0:2386012c6594 | 101 | } |
phgbartels | 0:2386012c6594 | 102 | |
phgbartels | 0:2386012c6594 | 103 | void arm_kalibratie() { |
phgbartels | 0:2386012c6594 | 104 | //voor nu om de loop te doorlopen wordt onderstaande code gebruikt. Nogmaal gesproken moet er gewacht worden op een 'hoog' signaal van een knop |
phgbartels | 0:2386012c6594 | 105 | current_herhalingen = previous_herhalingen + 1; previous_herhalingen = current_herhalingen; |
phgbartels | 0:2386012c6594 | 106 | motor1.setPosition(0); |
phgbartels | 1:b08ac32d1ddc | 107 | motor2.setPosition(0); |
phgbartels | 0:2386012c6594 | 108 | } |
phgbartels | 0:2386012c6594 | 109 | |
phgbartels | 0:2386012c6594 | 110 | void emg_kalibratie() { |
phgbartels | 0:2386012c6594 | 111 | current_herhalingen = previous_herhalingen + 1; previous_herhalingen = current_herhalingen; |
phgbartels | 0:2386012c6594 | 112 | } |
phgbartels | 0:2386012c6594 | 113 | |
phgbartels | 0:2386012c6594 | 114 | void meten_richting() { |
phgbartels | 0:2386012c6594 | 115 | current_herhalingen = previous_herhalingen + 1; previous_herhalingen = current_herhalingen; |
phgbartels | 0:2386012c6594 | 116 | } |
phgbartels | 0:2386012c6594 | 117 | |
phgbartels | 0:2386012c6594 | 118 | void meten_hoogte() { |
phgbartels | 0:2386012c6594 | 119 | current_herhalingen = previous_herhalingen + 1; previous_herhalingen = current_herhalingen; |
phgbartels | 0:2386012c6594 | 120 | } |
phgbartels | 0:2386012c6594 | 121 | |
phgbartels | 0:2386012c6594 | 122 | void instellen_richting() { |
phgbartels | 0:2386012c6594 | 123 | current_herhalingen = previous_herhalingen + 1; previous_herhalingen = current_herhalingen; |
phgbartels | 0:2386012c6594 | 124 | } |
phgbartels | 0:2386012c6594 | 125 | |
phgbartels | 3:156c3e536ed4 | 126 | void GotoPosition (float position_setpoint_rad, float speed_radpersecond) { |
phgbartels | 5:4842219cb77c | 127 | |
phgbartels | 0:2386012c6594 | 128 | current_pos_motor1 = motor1.getPosition(); //bekijk na elke 0.005s wat de huidige 'waarde' van de encoder is |
phgbartels | 5:4842219cb77c | 129 | //delta_pos_motor1_puls = current_pos_motor1 - previous_pos_motor1; //Bereken wat het verschil in waarde is voor het berekenen van de snelheid(puls) |
phgbartels | 0:2386012c6594 | 130 | pos_motor1_rad = current_pos_motor1/(1600/(2*PI)); //echte waarde omrekenen naar rad voor (positie) PID regelaar |
phgbartels | 5:4842219cb77c | 131 | //delta_pos_motor1_rad = delta_pos_motor1_puls/(1600/(2*PI)); //delta waarde omrekenen naar rad voor snelheidsbepaling (rad) |
phgbartels | 2:de7b6c1d67c4 | 132 | //snelheid_motor1_rad = delta_pos_motor1_rad / TSAMP; //rad delen door TSAMP om rad/s te verkrijgen |
phgbartels | 2:de7b6c1d67c4 | 133 | //snelheid_arm_ms = snelheid_motor1_rad * lengte_arm; //deze waarde maar lengte van de arm om de snelheid van het uiteinde van die arm te verkrijgen |
phgbartels | 1:b08ac32d1ddc | 134 | //scope.set(0, snelheid_motor1_rad); |
phgbartels | 0:2386012c6594 | 135 | |
phgbartels | 0:2386012c6594 | 136 | previous_pos_motor1 = current_pos_motor1; //sla de huidige waarde op als vorige waarde. |
phgbartels | 0:2386012c6594 | 137 | |
phgbartels | 0:2386012c6594 | 138 | //nu gaan we positie regelen i.p.v. snelheid. |
phgbartels | 5:4842219cb77c | 139 | |
phgbartels | 5:4842219cb77c | 140 | if (fabs(pos_motor1_rad - position_setpoint_rad) <= 0.05) //if position error < ...rad, then stop. |
phgbartels | 0:2386012c6594 | 141 | { |
phgbartels | 3:156c3e536ed4 | 142 | speed_radpersecond = 0; |
phgbartels | 4:377ddd65e4a6 | 143 | setpoint = pos_motor1_rad; |
phgbartels | 2:de7b6c1d67c4 | 144 | current_herhalingen = previous_herhalingen + 1; |
phgbartels | 2:de7b6c1d67c4 | 145 | previous_herhalingen = current_herhalingen; |
phgbartels | 4:377ddd65e4a6 | 146 | pc.printf("stop\n\r"); |
phgbartels | 5:4842219cb77c | 147 | PWM1_percentage = 0; |
phgbartels | 0:2386012c6594 | 148 | } |
phgbartels | 4:377ddd65e4a6 | 149 | else if(pos_motor1_rad - position_setpoint_rad < 0) |
phgbartels | 4:377ddd65e4a6 | 150 | { |
phgbartels | 5:4842219cb77c | 151 | setpoint = prev_setpoint +( TSAMP * speed_radpersecond); |
phgbartels | 5:4842219cb77c | 152 | PWM1_percentage = pid(setpoint, pos_motor1_rad); |
phgbartels | 4:377ddd65e4a6 | 153 | } |
phgbartels | 3:156c3e536ed4 | 154 | else |
phgbartels | 4:377ddd65e4a6 | 155 | { |
phgbartels | 5:4842219cb77c | 156 | setpoint = prev_setpoint -( TSAMP * speed_radpersecond); |
phgbartels | 5:4842219cb77c | 157 | PWM1_percentage = pid(setpoint, pos_motor1_rad); |
phgbartels | 4:377ddd65e4a6 | 158 | } |
phgbartels | 0:2386012c6594 | 159 | /*new_pwm = (setpoint - motor1.getPosition())*.001; -> P action*/ |
phgbartels | 5:4842219cb77c | 160 | pc.printf("%f\n\r",PWM1_percentage); |
phgbartels | 4:377ddd65e4a6 | 161 | |
phgbartels | 5:4842219cb77c | 162 | |
phgbartels | 1:b08ac32d1ddc | 163 | //scope.set(1, setpoint-pos_motor1_rad); |
phgbartels | 0:2386012c6594 | 164 | |
phgbartels | 0:2386012c6594 | 165 | if (PWM1_percentage < -100) |
phgbartels | 0:2386012c6594 | 166 | { |
phgbartels | 0:2386012c6594 | 167 | PWM1_percentage = -100; |
phgbartels | 0:2386012c6594 | 168 | } |
phgbartels | 0:2386012c6594 | 169 | else if (PWM1_percentage >100) |
phgbartels | 0:2386012c6594 | 170 | { |
phgbartels | 0:2386012c6594 | 171 | PWM1_percentage =100; |
phgbartels | 0:2386012c6594 | 172 | } |
phgbartels | 0:2386012c6594 | 173 | |
phgbartels | 4:377ddd65e4a6 | 174 | if(PWM1_percentage < 0) |
phgbartels | 0:2386012c6594 | 175 | { |
phgbartels | 0:2386012c6594 | 176 | motordir1 = 1; |
phgbartels | 0:2386012c6594 | 177 | } |
phgbartels | 0:2386012c6594 | 178 | else |
phgbartels | 0:2386012c6594 | 179 | { |
phgbartels | 0:2386012c6594 | 180 | motordir1 = 0; |
phgbartels | 0:2386012c6594 | 181 | } |
phgbartels | 0:2386012c6594 | 182 | |
phgbartels | 4:377ddd65e4a6 | 183 | pwm_motor1.write(abs(PWM1_percentage/100.));//abs(((1-PWM1_min_50)/100)*PWM1_percentage + PWM1_min_50)); |
phgbartels | 1:b08ac32d1ddc | 184 | //scope.set(5, abs(((1-PWM1_min_50)/100)*PWM1_percentage + PWM1_min_50)); |
phgbartels | 0:2386012c6594 | 185 | prev_setpoint = setpoint; |
phgbartels | 2:de7b6c1d67c4 | 186 | //scope.send(); |
phgbartels | 2:de7b6c1d67c4 | 187 | } |
phgbartels | 2:de7b6c1d67c4 | 188 | |
phgbartels | 3:156c3e536ed4 | 189 | |
phgbartels | 0:2386012c6594 | 190 | |
phgbartels | 0:2386012c6594 | 191 | float pid(float setpoint, float measurement) |
phgbartels | 0:2386012c6594 | 192 | { |
phgbartels | 0:2386012c6594 | 193 | float error; |
phgbartels | 0:2386012c6594 | 194 | static float prev_error = 0; |
phgbartels | 0:2386012c6594 | 195 | float out_p = 0; |
phgbartels | 0:2386012c6594 | 196 | static float out_i = 0; |
phgbartels | 0:2386012c6594 | 197 | float out_d = 0; |
phgbartels | 0:2386012c6594 | 198 | error = (setpoint-measurement); |
phgbartels | 0:2386012c6594 | 199 | out_p = error*K_P; |
phgbartels | 0:2386012c6594 | 200 | out_i += error*K_I; |
phgbartels | 0:2386012c6594 | 201 | out_d = (error-prev_error)*K_D; |
phgbartels | 0:2386012c6594 | 202 | clamp(&out_i,-I_LIMIT,I_LIMIT); |
phgbartels | 0:2386012c6594 | 203 | prev_error = error; |
phgbartels | 1:b08ac32d1ddc | 204 | //scope.set(2, out_p); |
phgbartels | 1:b08ac32d1ddc | 205 | //scope.set(3, out_i); |
phgbartels | 1:b08ac32d1ddc | 206 | //scope.set(4, out_d); |
phgbartels | 0:2386012c6594 | 207 | return out_p + out_i + out_d; |
phgbartels | 0:2386012c6594 | 208 | } |
phgbartels | 0:2386012c6594 | 209 | |
phgbartels | 0:2386012c6594 | 210 | void clamp(float* in, float min, float max) // "*" is een pointer (verwijst naar het adres waar een variebele instaat). Dus je slaat niet de variabele op |
phgbartels | 0:2386012c6594 | 211 | // maar de locatie van de variabele. |
phgbartels | 0:2386012c6594 | 212 | { |
phgbartels | 0:2386012c6594 | 213 | *in > min ? /*(*/*in < max? /*niets doen*/ : *in = max/*)*/: *in = min; // a ? b : c --> als a waar is, dan doe je b, en anders c |
phgbartels | 0:2386012c6594 | 214 | // *in = het getal dat staat op locatie van in --> waarde van new_pwm |
phgbartels | 0:2386012c6594 | 215 | } |
phgbartels | 0:2386012c6594 | 216 | |
phgbartels | 0:2386012c6594 | 217 | void statemachinefunction() |
phgbartels | 0:2386012c6594 | 218 | { |
phgbartels | 0:2386012c6594 | 219 | switch(state) { |
phgbartels | 0:2386012c6594 | 220 | case RUST: { |
phgbartels | 0:2386012c6594 | 221 | rust(); |
phgbartels | 0:2386012c6594 | 222 | /*voorwaarde wanneer hij door kan naar de volgende case*/ |
phgbartels | 5:4842219cb77c | 223 | if (current_herhalingen == 100 && EMG == 1) |
phgbartels | 0:2386012c6594 | 224 | { |
phgbartels | 0:2386012c6594 | 225 | current_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 226 | previous_herhalingen = 0; |
phgbartels | 1:b08ac32d1ddc | 227 | state = ARM_KALIBRATIE; |
phgbartels | 5:4842219cb77c | 228 | EMG = 0; |
phgbartels | 0:2386012c6594 | 229 | } |
phgbartels | 1:b08ac32d1ddc | 230 | break; |
phgbartels | 0:2386012c6594 | 231 | /*bepalen hoe je zorgt dat je maar 1x de kalibraties uitvoerd! (Of eventueel de arm kalibratie elke keer!!)*/ |
phgbartels | 0:2386012c6594 | 232 | //if (metingstatus<5); |
phgbartels | 0:2386012c6594 | 233 | // state = ARMKALIBRATIE; |
phgbartels | 0:2386012c6594 | 234 | //if (metingstatus==5); |
phgbartels | 0:2386012c6594 | 235 | // state = METEN_RICHTING; |
phgbartels | 0:2386012c6594 | 236 | //break; |
phgbartels | 0:2386012c6594 | 237 | //} |
phgbartels | 0:2386012c6594 | 238 | } |
phgbartels | 0:2386012c6594 | 239 | |
phgbartels | 0:2386012c6594 | 240 | case ARM_KALIBRATIE: |
phgbartels | 0:2386012c6594 | 241 | { |
phgbartels | 0:2386012c6594 | 242 | arm_kalibratie(); |
phgbartels | 5:4842219cb77c | 243 | if (current_herhalingen == 100) |
phgbartels | 0:2386012c6594 | 244 | { |
phgbartels | 0:2386012c6594 | 245 | current_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 246 | previous_herhalingen = 0; |
phgbartels | 1:b08ac32d1ddc | 247 | state = EMG_KALIBRATIE; |
phgbartels | 2:de7b6c1d67c4 | 248 | motor1.setPosition(0); |
phgbartels | 2:de7b6c1d67c4 | 249 | motor2.setPosition(0); |
phgbartels | 2:de7b6c1d67c4 | 250 | pwm_motor1.period_us(100); |
phgbartels | 2:de7b6c1d67c4 | 251 | pwm_motor2.period_us(100); |
phgbartels | 0:2386012c6594 | 252 | } |
phgbartels | 1:b08ac32d1ddc | 253 | break; |
phgbartels | 0:2386012c6594 | 254 | } |
phgbartels | 0:2386012c6594 | 255 | |
phgbartels | 0:2386012c6594 | 256 | case EMG_KALIBRATIE: |
phgbartels | 0:2386012c6594 | 257 | { |
phgbartels | 0:2386012c6594 | 258 | emg_kalibratie(); |
phgbartels | 5:4842219cb77c | 259 | if (current_herhalingen == 100) |
phgbartels | 0:2386012c6594 | 260 | { |
phgbartels | 0:2386012c6594 | 261 | state = METEN_RICHTING; |
phgbartels | 0:2386012c6594 | 262 | current_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 263 | previous_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 264 | } |
phgbartels | 1:b08ac32d1ddc | 265 | break; |
phgbartels | 0:2386012c6594 | 266 | } |
phgbartels | 0:2386012c6594 | 267 | |
phgbartels | 0:2386012c6594 | 268 | case METEN_RICHTING: |
phgbartels | 0:2386012c6594 | 269 | { |
phgbartels | 0:2386012c6594 | 270 | meten_richting(); |
phgbartels | 5:4842219cb77c | 271 | if (current_herhalingen == 100) |
phgbartels | 0:2386012c6594 | 272 | { |
phgbartels | 0:2386012c6594 | 273 | state = METEN_HOOGTE; |
phgbartels | 0:2386012c6594 | 274 | current_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 275 | previous_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 276 | } |
phgbartels | 1:b08ac32d1ddc | 277 | break; |
phgbartels | 0:2386012c6594 | 278 | } |
phgbartels | 0:2386012c6594 | 279 | |
phgbartels | 0:2386012c6594 | 280 | case METEN_HOOGTE: |
phgbartels | 0:2386012c6594 | 281 | { |
phgbartels | 0:2386012c6594 | 282 | meten_hoogte(); |
phgbartels | 5:4842219cb77c | 283 | if (current_herhalingen == 100) |
phgbartels | 0:2386012c6594 | 284 | { |
phgbartels | 0:2386012c6594 | 285 | state = INSTELLEN_RICHTING; |
phgbartels | 0:2386012c6594 | 286 | current_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 287 | previous_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 288 | } |
phgbartels | 1:b08ac32d1ddc | 289 | break; |
phgbartels | 0:2386012c6594 | 290 | } |
phgbartels | 0:2386012c6594 | 291 | |
phgbartels | 0:2386012c6594 | 292 | case INSTELLEN_RICHTING: |
phgbartels | 0:2386012c6594 | 293 | { |
phgbartels | 0:2386012c6594 | 294 | instellen_richting(); |
phgbartels | 5:4842219cb77c | 295 | if (current_herhalingen == 100) |
phgbartels | 0:2386012c6594 | 296 | { |
phgbartels | 0:2386012c6594 | 297 | state = SLAAN; |
phgbartels | 0:2386012c6594 | 298 | current_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 299 | previous_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 300 | } |
phgbartels | 2:de7b6c1d67c4 | 301 | break; |
phgbartels | 1:b08ac32d1ddc | 302 | |
phgbartels | 0:2386012c6594 | 303 | } |
phgbartels | 0:2386012c6594 | 304 | |
phgbartels | 0:2386012c6594 | 305 | case SLAAN: |
phgbartels | 0:2386012c6594 | 306 | { |
phgbartels | 5:4842219cb77c | 307 | GotoPosition(1.5 ,8); |
phgbartels | 5:4842219cb77c | 308 | if (current_herhalingen == 100) |
phgbartels | 0:2386012c6594 | 309 | { |
phgbartels | 0:2386012c6594 | 310 | state = RETURN2RUST; |
phgbartels | 0:2386012c6594 | 311 | current_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 312 | previous_herhalingen = 0; |
phgbartels | 2:de7b6c1d67c4 | 313 | prev_setpoint =0; |
phgbartels | 2:de7b6c1d67c4 | 314 | setpoint =0; |
phgbartels | 2:de7b6c1d67c4 | 315 | |
phgbartels | 0:2386012c6594 | 316 | } |
phgbartels | 2:de7b6c1d67c4 | 317 | break; |
phgbartels | 0:2386012c6594 | 318 | } |
phgbartels | 0:2386012c6594 | 319 | |
phgbartels | 0:2386012c6594 | 320 | case RETURN2RUST: |
phgbartels | 0:2386012c6594 | 321 | { |
phgbartels | 5:4842219cb77c | 322 | GotoPosition(0,2); |
phgbartels | 5:4842219cb77c | 323 | if (current_herhalingen == 100) |
phgbartels | 0:2386012c6594 | 324 | { |
phgbartels | 0:2386012c6594 | 325 | state = RUST; |
phgbartels | 0:2386012c6594 | 326 | current_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 327 | previous_herhalingen = 0; |
phgbartels | 0:2386012c6594 | 328 | } |
phgbartels | 5:4842219cb77c | 329 | |
phgbartels | 1:b08ac32d1ddc | 330 | break; |
phgbartels | 0:2386012c6594 | 331 | } |
phgbartels | 0:2386012c6594 | 332 | |
phgbartels | 0:2386012c6594 | 333 | default: { |
phgbartels | 0:2386012c6594 | 334 | state = RUST; |
phgbartels | 0:2386012c6594 | 335 | } |
phgbartels | 0:2386012c6594 | 336 | |
phgbartels | 0:2386012c6594 | 337 | }//switch(state) |
phgbartels | 0:2386012c6594 | 338 | }//void statemachinefunction |
phgbartels | 0:2386012c6594 | 339 | |
phgbartels | 0:2386012c6594 | 340 | |
phgbartels | 0:2386012c6594 | 341 | void screenupdate(){ |
phgbartels | 0:2386012c6594 | 342 | if(state==RUST){ |
phgbartels | 0:2386012c6594 | 343 | lcd.cls(); |
phgbartels | 0:2386012c6594 | 344 | lcd.locate(0,0); |
phgbartels | 5:4842219cb77c | 345 | lcd.printf("V.I.C.T.O.R.Y."); //regel 1 LCD scherm |
phgbartels | 0:2386012c6594 | 346 | lcd.locate(0,1); |
phgbartels | 0:2386012c6594 | 347 | lcd.printf(" GROEP 7 "); |
phgbartels | 0:2386012c6594 | 348 | } |
phgbartels | 0:2386012c6594 | 349 | else{ |
phgbartels | 0:2386012c6594 | 350 | lcd.cls(); |
phgbartels | 0:2386012c6594 | 351 | lcd.printf("state %d", state); //hier nog aan toevoegen hoe je de 'waarde', dus eigenlijk tekst, die opgeslagen staat in state kan printen. |
phgbartels | 0:2386012c6594 | 352 | } |
phgbartels | 0:2386012c6594 | 353 | } |
phgbartels | 0:2386012c6594 | 354 | |
phgbartels | 0:2386012c6594 | 355 | int main() { |
phgbartels | 4:377ddd65e4a6 | 356 | pc.baud(115200); |
phgbartels | 4:377ddd65e4a6 | 357 | statemachine.attach(&statemachinefunction, TSAMP); // the address of the function to be attached (flip) and the interval (2 seconds) |
phgbartels | 0:2386012c6594 | 358 | screen.attach(&screenupdate, 0.2); |
phgbartels | 4:377ddd65e4a6 | 359 | while(1); |
phgbartels | 0:2386012c6594 | 360 | } |