r

Dependencies:   Encoder HIDScope TextLCD mbed-dsp mbed

Fork of Main-script_groep7 by Peter Bartels

Committer:
lauradeheus
Date:
Fri Oct 31 12:36:14 2014 +0000
Revision:
6:a7379a681adf
Parent:
5:4842219cb77c
Child:
7:7e3e183bf063
Script motoraansturing (nog geen correcte PID waarden) en emgmeting werkend

Who changed what in which revision?

UserRevisionLine numberNew 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"
lauradeheus 6:a7379a681adf 19 #include "HIDScope.h"
phgbartels 0:2386012c6594 20 #include "PwmOut.h"
lauradeheus 6:a7379a681adf 21 #include "arm_math.h"
phgbartels 0:2386012c6594 22
phgbartels 0:2386012c6594 23 /*
phgbartels 0:2386012c6594 24 #define vaste waarden
phgbartels 0:2386012c6594 25 */
phgbartels 0:2386012c6594 26 /*definieren pinnen Motor 1*/
phgbartels 0:2386012c6594 27 #define M1_PWM PTA5
phgbartels 0:2386012c6594 28 #define M1_DIR PTA4
phgbartels 0:2386012c6594 29 #define M2_PWM PTC8
phgbartels 0:2386012c6594 30 #define M2_DIR PTC9
phgbartels 0:2386012c6594 31 /*Definieren minimale waarden PWM per motor*/
phgbartels 0:2386012c6594 32 #define PWM1_min_50 0.529 /*met koppelstuk!*/
phgbartels 0:2386012c6594 33 #define PWM2_min_30 0.529 /*aanpassen! Klopt nog niet met koppelstuk*/
phgbartels 0:2386012c6594 34 /*Definieren om de hoeveel seconden er gegevens naar de HID-scope gestuurd worden.*/
phgbartels 0:2386012c6594 35 #define TSAMP 0.005
phgbartels 5:4842219cb77c 36 #define K_P (5)
phgbartels 5:4842219cb77c 37 #define K_I (0.1 * TSAMP)
phgbartels 5:4842219cb77c 38 #define K_D (0)
phgbartels 0:2386012c6594 39 //#define K_D (0.0005 /TSAMP)
phgbartels 0:2386012c6594 40 #define I_LIMIT 100.
phgbartels 0:2386012c6594 41 #define PI 3.1415926535897
phgbartels 0:2386012c6594 42 #define lengte_arm 0.5
phgbartels 0:2386012c6594 43
phgbartels 0:2386012c6594 44 /*
phgbartels 0:2386012c6594 45 Geef een naam aan een actie en vertel welke pinnen hiervoor gebruikt worden.
phgbartels 0:2386012c6594 46 */
phgbartels 0:2386012c6594 47 TextLCD lcd(PTD2, PTA12, PTB2, PTB3, PTC2, PTA13, TextLCD::LCD16x2); // rs, e, d4-d7
phgbartels 0:2386012c6594 48 Encoder motor1(PTD3,PTD1);
phgbartels 0:2386012c6594 49 Encoder motor2(PTD5, PTD0);
phgbartels 0:2386012c6594 50 PwmOut pwm_motor1(M1_PWM);
phgbartels 0:2386012c6594 51 PwmOut pwm_motor2(M2_PWM);
phgbartels 0:2386012c6594 52 DigitalOut motordir1(M1_DIR);
phgbartels 0:2386012c6594 53 DigitalOut motordir2(M2_DIR);
phgbartels 0:2386012c6594 54 DigitalOut LEDGREEN(LED_GREEN);
phgbartels 0:2386012c6594 55 DigitalOut LEDRED(LED_RED);
phgbartels 4:377ddd65e4a6 56 Serial pc(USBTX,USBRX);
lauradeheus 6:a7379a681adf 57 HIDScope scope(3);
lauradeheus 6:a7379a681adf 58 AnalogIn emg(PTB1);
phgbartels 0:2386012c6594 59 /*
phgbartels 0:2386012c6594 60 definieer namen aan var, float, int, static float, uint8_t, uint16_t etc. en geef ze eventueel een waarde
phgbartels 0:2386012c6594 61 */
phgbartels 0:2386012c6594 62 Ticker statemachine;
phgbartels 0:2386012c6594 63 Ticker screen;
lauradeheus 6:a7379a681adf 64 arm_biquad_casd_df1_inst_f32 lowpass_1; //2e orde lowpass biquad butterworthfilter 99Hz
lauradeheus 6:a7379a681adf 65 arm_biquad_casd_df1_inst_f32 lowpass_2; //2e orde lowpass biquad butterworthfilter 3Hz
lauradeheus 6:a7379a681adf 66 arm_biquad_casd_df1_inst_f32 highpass; //2e orde highpass biquad butterworthfilter 20Hz
lauradeheus 6:a7379a681adf 67 arm_biquad_casd_df1_inst_f32 notch; //2e orde lowpass biquad butterworthfilter 50Hz
phgbartels 0:2386012c6594 68 int previous_herhalingen = 0;
phgbartels 1:b08ac32d1ddc 69 int current_herhalingen = 0;
phgbartels 0:2386012c6594 70 int PWM2_percentage = 100;
phgbartels 0:2386012c6594 71 int aantal_rotaties_1 = 10;
phgbartels 0:2386012c6594 72 int aantalcr_1 = 1600;
phgbartels 0:2386012c6594 73 int aantalcr_2 = 960;
phgbartels 0:2386012c6594 74 int beginpos_motor1;
phgbartels 0:2386012c6594 75 int beginpos_motor1_new;
phgbartels 0:2386012c6594 76 int beginpos_motor2;
phgbartels 0:2386012c6594 77 int beginpos_motor2_new;
phgbartels 0:2386012c6594 78 int previous_pos_motor1 = 0;
phgbartels 0:2386012c6594 79 int current_pos_motor1;
phgbartels 5:4842219cb77c 80 int EMG = 1;
phgbartels 0:2386012c6594 81 int delta_pos_motor1_puls;
lauradeheus 6:a7379a681adf 82 int aantal_pieken;
lauradeheus 6:a7379a681adf 83 int doel;
lauradeheus 6:a7379a681adf 84 bool aanspan;
phgbartels 0:2386012c6594 85 void clamp(float * in, float min, float max);
phgbartels 0:2386012c6594 86 volatile bool looptimerflag;
phgbartels 2:de7b6c1d67c4 87 int16_t gewenste_snelheid = 2;
phgbartels 2:de7b6c1d67c4 88 int16_t gewenste_snelheid_rad = 4;
phgbartels 2:de7b6c1d67c4 89 int16_t gewenste_snelheid_rad_return = 1;
phgbartels 0:2386012c6594 90 float pid(float setpoint, float measurement);
phgbartels 0:2386012c6594 91 float pos_motor1_rad;
phgbartels 0:2386012c6594 92 float PWM1_percentage = 0;
phgbartels 0:2386012c6594 93 float delta_pos_motor1_rad;
phgbartels 0:2386012c6594 94 float snelheid_motor1_rad;
phgbartels 0:2386012c6594 95 float snelheid_arm_ms;
phgbartels 0:2386012c6594 96 float PWM1;
phgbartels 0:2386012c6594 97 float PWM2;
phgbartels 0:2386012c6594 98 float Speed_motor1;
phgbartels 0:2386012c6594 99 float Speed_motor1rad;
phgbartels 1:b08ac32d1ddc 100 float setpoint = 0;
lauradeheus 6:a7379a681adf 101 float prev_setpoint = 0;
lauradeheus 6:a7379a681adf 102 float lowpass_1_const[] = {0.978030479206560 , 1.956060958413119 , 0.978030479206560 , -1.955578240315036 , -0.956543676511203};
lauradeheus 6:a7379a681adf 103 float lowpass_1_states[4];
lauradeheus 6:a7379a681adf 104 float lowpass_2_const[] = {0.002080567135492 , 0.004161134270985 , 0.002080567135492 , 1.866892279711715 , -0.875214548253684};
lauradeheus 6:a7379a681adf 105 float lowpass_2_states[4];
lauradeheus 6:a7379a681adf 106 float highpass_const[] = {0.638945525159022 , -1.277891050318045 , 0.638945525159022 , 1.142980502539901 , -0.412801598096189};
lauradeheus 6:a7379a681adf 107 float highpass_states[4];
lauradeheus 6:a7379a681adf 108 float notch_const[] = {0.978048948305681 , 0.000000000000000 , 0.978048948305681 , 0.000000000000000 , -0.956097896611362};
lauradeheus 6:a7379a681adf 109 float notch_states[4];
lauradeheus 6:a7379a681adf 110 float emg_filtered;
lauradeheus 6:a7379a681adf 111 float emg_max = 0;
lauradeheus 6:a7379a681adf 112 float emg_treshhold_laag = 0;
lauradeheus 6:a7379a681adf 113 float emg_treshhold_hoog = 0;
phgbartels 0:2386012c6594 114
phgbartels 1:b08ac32d1ddc 115 //HIDScope scope(6);
phgbartels 0:2386012c6594 116
phgbartels 0:2386012c6594 117 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 118 state_t state = RUST;
phgbartels 0:2386012c6594 119
phgbartels 0:2386012c6594 120 //functies die vanuit de statemachinefunction aangeroepen kunnen worden
phgbartels 0:2386012c6594 121 void rust() {
phgbartels 0:2386012c6594 122 current_herhalingen = previous_herhalingen + 1; previous_herhalingen = current_herhalingen;
phgbartels 0:2386012c6594 123 }
phgbartels 0:2386012c6594 124
phgbartels 0:2386012c6594 125 void arm_kalibratie() {
phgbartels 0:2386012c6594 126 //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 127 current_herhalingen = previous_herhalingen + 1; previous_herhalingen = current_herhalingen;
phgbartels 0:2386012c6594 128 motor1.setPosition(0);
phgbartels 1:b08ac32d1ddc 129 motor2.setPosition(0);
phgbartels 0:2386012c6594 130 }
phgbartels 0:2386012c6594 131
lauradeheus 6:a7379a681adf 132 void pieken_tellen(){
lauradeheus 6:a7379a681adf 133 if (emg_filtered>=emg_treshhold_hoog)
lauradeheus 6:a7379a681adf 134 {
lauradeheus 6:a7379a681adf 135 aanspan=true; //maak een variabele waarin je opslaat dat het signaal hoog is.
lauradeheus 6:a7379a681adf 136 }
lauradeheus 6:a7379a681adf 137 if (aanspan==true && emg_filtered<=emg_treshhold_laag)//== ipv =, anders wordt aanspan true gemaakt
lauradeheus 6:a7379a681adf 138 {
lauradeheus 6:a7379a681adf 139 aanspan=false;
lauradeheus 6:a7379a681adf 140 aantal_pieken++;
lauradeheus 6:a7379a681adf 141 doel = aantal_pieken-((aantal_pieken/3)*3)+1;
lauradeheus 6:a7379a681adf 142 }
lauradeheus 6:a7379a681adf 143 }
lauradeheus 6:a7379a681adf 144
lauradeheus 6:a7379a681adf 145 void emg_filtering() {
lauradeheus 6:a7379a681adf 146 uint16_t emg_value;
lauradeheus 6:a7379a681adf 147 float emg_value_f32;
lauradeheus 6:a7379a681adf 148 emg_value = emg.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V)
lauradeheus 6:a7379a681adf 149 emg_value_f32 = emg.read();
lauradeheus 6:a7379a681adf 150
lauradeheus 6:a7379a681adf 151 arm_biquad_cascade_df1_f32(&highpass, &emg_value_f32, &emg_filtered, 1 );
lauradeheus 6:a7379a681adf 152 arm_biquad_cascade_df1_f32(&lowpass_1, &emg_filtered, &emg_filtered, 1 );
lauradeheus 6:a7379a681adf 153 arm_biquad_cascade_df1_f32(&notch, &emg_filtered, &emg_filtered, 1);
lauradeheus 6:a7379a681adf 154 emg_filtered = fabs(emg_filtered);
lauradeheus 6:a7379a681adf 155 arm_biquad_cascade_df1_f32(&lowpass_2, &emg_filtered, &emg_filtered, 1 );
lauradeheus 6:a7379a681adf 156 scope.set(0,emg_value); //uint value
lauradeheus 6:a7379a681adf 157 scope.set(1,emg_filtered); //processed float
lauradeheus 6:a7379a681adf 158 scope.set(2,doel);
lauradeheus 6:a7379a681adf 159 scope.send();
lauradeheus 6:a7379a681adf 160 if(state!=EMG_KALIBRATIE)
lauradeheus 6:a7379a681adf 161 {
lauradeheus 6:a7379a681adf 162 pieken_tellen();
lauradeheus 6:a7379a681adf 163 }
lauradeheus 6:a7379a681adf 164 }
lauradeheus 6:a7379a681adf 165
lauradeheus 6:a7379a681adf 166 void emg_max_meting(){
lauradeheus 6:a7379a681adf 167 emg_filtering();
lauradeheus 6:a7379a681adf 168 if (emg_filtered>=emg_max)
lauradeheus 6:a7379a681adf 169 {
lauradeheus 6:a7379a681adf 170 emg_max=emg_filtered;
lauradeheus 6:a7379a681adf 171 }
lauradeheus 6:a7379a681adf 172 emg_treshhold_laag = 0.3*emg_max;
lauradeheus 6:a7379a681adf 173 emg_treshhold_hoog = 0.7*emg_max;
lauradeheus 6:a7379a681adf 174 }
lauradeheus 6:a7379a681adf 175
phgbartels 0:2386012c6594 176 void emg_kalibratie() {
lauradeheus 6:a7379a681adf 177 if(emg_filtered>=0.05){
lauradeheus 6:a7379a681adf 178 current_herhalingen = previous_herhalingen + 1; previous_herhalingen = current_herhalingen;
lauradeheus 6:a7379a681adf 179 }
lauradeheus 6:a7379a681adf 180 if(current_herhalingen<=1000)
lauradeheus 6:a7379a681adf 181 {
lauradeheus 6:a7379a681adf 182 emg_max_meting();
lauradeheus 6:a7379a681adf 183 }
phgbartels 0:2386012c6594 184 }
phgbartels 0:2386012c6594 185
phgbartels 0:2386012c6594 186 void meten_richting() {
phgbartels 0:2386012c6594 187 current_herhalingen = previous_herhalingen + 1; previous_herhalingen = current_herhalingen;
phgbartels 0:2386012c6594 188 }
phgbartels 0:2386012c6594 189
phgbartels 0:2386012c6594 190 void meten_hoogte() {
phgbartels 0:2386012c6594 191 current_herhalingen = previous_herhalingen + 1; previous_herhalingen = current_herhalingen;
phgbartels 0:2386012c6594 192 }
phgbartels 0:2386012c6594 193
phgbartels 0:2386012c6594 194 void instellen_richting() {
phgbartels 0:2386012c6594 195 current_herhalingen = previous_herhalingen + 1; previous_herhalingen = current_herhalingen;
phgbartels 0:2386012c6594 196 }
phgbartels 0:2386012c6594 197
phgbartels 3:156c3e536ed4 198 void GotoPosition (float position_setpoint_rad, float speed_radpersecond) {
phgbartels 5:4842219cb77c 199
phgbartels 0:2386012c6594 200 current_pos_motor1 = motor1.getPosition(); //bekijk na elke 0.005s wat de huidige 'waarde' van de encoder is
phgbartels 5:4842219cb77c 201 //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 202 pos_motor1_rad = current_pos_motor1/(1600/(2*PI)); //echte waarde omrekenen naar rad voor (positie) PID regelaar
phgbartels 5:4842219cb77c 203 //delta_pos_motor1_rad = delta_pos_motor1_puls/(1600/(2*PI)); //delta waarde omrekenen naar rad voor snelheidsbepaling (rad)
phgbartels 2:de7b6c1d67c4 204 //snelheid_motor1_rad = delta_pos_motor1_rad / TSAMP; //rad delen door TSAMP om rad/s te verkrijgen
phgbartels 2:de7b6c1d67c4 205 //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 206 //scope.set(0, snelheid_motor1_rad);
phgbartels 0:2386012c6594 207
phgbartels 0:2386012c6594 208 previous_pos_motor1 = current_pos_motor1; //sla de huidige waarde op als vorige waarde.
phgbartels 0:2386012c6594 209
phgbartels 0:2386012c6594 210 //nu gaan we positie regelen i.p.v. snelheid.
phgbartels 5:4842219cb77c 211
phgbartels 5:4842219cb77c 212 if (fabs(pos_motor1_rad - position_setpoint_rad) <= 0.05) //if position error < ...rad, then stop.
phgbartels 0:2386012c6594 213 {
phgbartels 3:156c3e536ed4 214 speed_radpersecond = 0;
phgbartels 4:377ddd65e4a6 215 setpoint = pos_motor1_rad;
phgbartels 2:de7b6c1d67c4 216 current_herhalingen = previous_herhalingen + 1;
phgbartels 2:de7b6c1d67c4 217 previous_herhalingen = current_herhalingen;
phgbartels 4:377ddd65e4a6 218 pc.printf("stop\n\r");
phgbartels 5:4842219cb77c 219 PWM1_percentage = 0;
phgbartels 0:2386012c6594 220 }
phgbartels 4:377ddd65e4a6 221 else if(pos_motor1_rad - position_setpoint_rad < 0)
phgbartels 4:377ddd65e4a6 222 {
phgbartels 5:4842219cb77c 223 setpoint = prev_setpoint +( TSAMP * speed_radpersecond);
phgbartels 5:4842219cb77c 224 PWM1_percentage = pid(setpoint, pos_motor1_rad);
phgbartels 4:377ddd65e4a6 225 }
phgbartels 3:156c3e536ed4 226 else
phgbartels 4:377ddd65e4a6 227 {
phgbartels 5:4842219cb77c 228 setpoint = prev_setpoint -( TSAMP * speed_radpersecond);
phgbartels 5:4842219cb77c 229 PWM1_percentage = pid(setpoint, pos_motor1_rad);
phgbartels 4:377ddd65e4a6 230 }
phgbartels 0:2386012c6594 231 /*new_pwm = (setpoint - motor1.getPosition())*.001; -> P action*/
phgbartels 5:4842219cb77c 232 pc.printf("%f\n\r",PWM1_percentage);
phgbartels 4:377ddd65e4a6 233
phgbartels 5:4842219cb77c 234
phgbartels 1:b08ac32d1ddc 235 //scope.set(1, setpoint-pos_motor1_rad);
phgbartels 0:2386012c6594 236
phgbartels 0:2386012c6594 237 if (PWM1_percentage < -100)
phgbartels 0:2386012c6594 238 {
phgbartels 0:2386012c6594 239 PWM1_percentage = -100;
phgbartels 0:2386012c6594 240 }
phgbartels 0:2386012c6594 241 else if (PWM1_percentage >100)
phgbartels 0:2386012c6594 242 {
phgbartels 0:2386012c6594 243 PWM1_percentage =100;
phgbartels 0:2386012c6594 244 }
phgbartels 0:2386012c6594 245
phgbartels 4:377ddd65e4a6 246 if(PWM1_percentage < 0)
phgbartels 0:2386012c6594 247 {
phgbartels 0:2386012c6594 248 motordir1 = 1;
phgbartels 0:2386012c6594 249 }
phgbartels 0:2386012c6594 250 else
phgbartels 0:2386012c6594 251 {
phgbartels 0:2386012c6594 252 motordir1 = 0;
phgbartels 0:2386012c6594 253 }
phgbartels 0:2386012c6594 254
phgbartels 4:377ddd65e4a6 255 pwm_motor1.write(abs(PWM1_percentage/100.));//abs(((1-PWM1_min_50)/100)*PWM1_percentage + PWM1_min_50));
phgbartels 1:b08ac32d1ddc 256 //scope.set(5, abs(((1-PWM1_min_50)/100)*PWM1_percentage + PWM1_min_50));
phgbartels 0:2386012c6594 257 prev_setpoint = setpoint;
phgbartels 2:de7b6c1d67c4 258 //scope.send();
phgbartels 2:de7b6c1d67c4 259 }
phgbartels 2:de7b6c1d67c4 260
phgbartels 3:156c3e536ed4 261
phgbartels 0:2386012c6594 262
phgbartels 0:2386012c6594 263 float pid(float setpoint, float measurement)
phgbartels 0:2386012c6594 264 {
phgbartels 0:2386012c6594 265 float error;
phgbartels 0:2386012c6594 266 static float prev_error = 0;
phgbartels 0:2386012c6594 267 float out_p = 0;
phgbartels 0:2386012c6594 268 static float out_i = 0;
phgbartels 0:2386012c6594 269 float out_d = 0;
phgbartels 0:2386012c6594 270 error = (setpoint-measurement);
phgbartels 0:2386012c6594 271 out_p = error*K_P;
phgbartels 0:2386012c6594 272 out_i += error*K_I;
phgbartels 0:2386012c6594 273 out_d = (error-prev_error)*K_D;
phgbartels 0:2386012c6594 274 clamp(&out_i,-I_LIMIT,I_LIMIT);
phgbartels 0:2386012c6594 275 prev_error = error;
phgbartels 1:b08ac32d1ddc 276 //scope.set(2, out_p);
phgbartels 1:b08ac32d1ddc 277 //scope.set(3, out_i);
phgbartels 1:b08ac32d1ddc 278 //scope.set(4, out_d);
phgbartels 0:2386012c6594 279 return out_p + out_i + out_d;
phgbartels 0:2386012c6594 280 }
phgbartels 0:2386012c6594 281
phgbartels 0:2386012c6594 282 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 283 // maar de locatie van de variabele.
phgbartels 0:2386012c6594 284 {
phgbartels 0:2386012c6594 285 *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 286 // *in = het getal dat staat op locatie van in --> waarde van new_pwm
phgbartels 0:2386012c6594 287 }
phgbartels 0:2386012c6594 288
phgbartels 0:2386012c6594 289 void statemachinefunction()
phgbartels 0:2386012c6594 290 {
phgbartels 0:2386012c6594 291 switch(state) {
phgbartels 0:2386012c6594 292 case RUST: {
phgbartels 0:2386012c6594 293 rust();
phgbartels 0:2386012c6594 294 /*voorwaarde wanneer hij door kan naar de volgende case*/
phgbartels 5:4842219cb77c 295 if (current_herhalingen == 100 && EMG == 1)
phgbartels 0:2386012c6594 296 {
phgbartels 0:2386012c6594 297 current_herhalingen = 0;
phgbartels 0:2386012c6594 298 previous_herhalingen = 0;
phgbartels 1:b08ac32d1ddc 299 state = ARM_KALIBRATIE;
phgbartels 5:4842219cb77c 300 EMG = 0;
phgbartels 0:2386012c6594 301 }
phgbartels 1:b08ac32d1ddc 302 break;
phgbartels 0:2386012c6594 303 /*bepalen hoe je zorgt dat je maar 1x de kalibraties uitvoerd! (Of eventueel de arm kalibratie elke keer!!)*/
phgbartels 0:2386012c6594 304 //if (metingstatus<5);
phgbartels 0:2386012c6594 305 // state = ARMKALIBRATIE;
phgbartels 0:2386012c6594 306 //if (metingstatus==5);
phgbartels 0:2386012c6594 307 // state = METEN_RICHTING;
phgbartels 0:2386012c6594 308 //break;
phgbartels 0:2386012c6594 309 //}
phgbartels 0:2386012c6594 310 }
phgbartels 0:2386012c6594 311
phgbartels 0:2386012c6594 312 case ARM_KALIBRATIE:
phgbartels 0:2386012c6594 313 {
phgbartels 0:2386012c6594 314 arm_kalibratie();
phgbartels 5:4842219cb77c 315 if (current_herhalingen == 100)
phgbartels 0:2386012c6594 316 {
phgbartels 0:2386012c6594 317 current_herhalingen = 0;
phgbartels 0:2386012c6594 318 previous_herhalingen = 0;
phgbartels 1:b08ac32d1ddc 319 state = EMG_KALIBRATIE;
phgbartels 2:de7b6c1d67c4 320 motor1.setPosition(0);
phgbartels 2:de7b6c1d67c4 321 motor2.setPosition(0);
phgbartels 2:de7b6c1d67c4 322 pwm_motor1.period_us(100);
phgbartels 2:de7b6c1d67c4 323 pwm_motor2.period_us(100);
phgbartels 0:2386012c6594 324 }
phgbartels 1:b08ac32d1ddc 325 break;
phgbartels 0:2386012c6594 326 }
phgbartels 0:2386012c6594 327
phgbartels 0:2386012c6594 328 case EMG_KALIBRATIE:
phgbartels 0:2386012c6594 329 {
phgbartels 0:2386012c6594 330 emg_kalibratie();
lauradeheus 6:a7379a681adf 331 if (current_herhalingen >=1000)
phgbartels 0:2386012c6594 332 {
phgbartels 0:2386012c6594 333 state = METEN_RICHTING;
phgbartels 0:2386012c6594 334 current_herhalingen = 0;
phgbartels 0:2386012c6594 335 previous_herhalingen = 0;
phgbartels 0:2386012c6594 336 }
phgbartels 1:b08ac32d1ddc 337 break;
phgbartels 0:2386012c6594 338 }
phgbartels 0:2386012c6594 339
phgbartels 0:2386012c6594 340 case METEN_RICHTING:
phgbartels 0:2386012c6594 341 {
phgbartels 0:2386012c6594 342 meten_richting();
phgbartels 5:4842219cb77c 343 if (current_herhalingen == 100)
phgbartels 0:2386012c6594 344 {
phgbartels 0:2386012c6594 345 state = METEN_HOOGTE;
phgbartels 0:2386012c6594 346 current_herhalingen = 0;
phgbartels 0:2386012c6594 347 previous_herhalingen = 0;
phgbartels 0:2386012c6594 348 }
phgbartels 1:b08ac32d1ddc 349 break;
phgbartels 0:2386012c6594 350 }
phgbartels 0:2386012c6594 351
phgbartels 0:2386012c6594 352 case METEN_HOOGTE:
phgbartels 0:2386012c6594 353 {
phgbartels 0:2386012c6594 354 meten_hoogte();
phgbartels 5:4842219cb77c 355 if (current_herhalingen == 100)
phgbartels 0:2386012c6594 356 {
phgbartels 0:2386012c6594 357 state = INSTELLEN_RICHTING;
phgbartels 0:2386012c6594 358 current_herhalingen = 0;
phgbartels 0:2386012c6594 359 previous_herhalingen = 0;
phgbartels 0:2386012c6594 360 }
phgbartels 1:b08ac32d1ddc 361 break;
phgbartels 0:2386012c6594 362 }
phgbartels 0:2386012c6594 363
phgbartels 0:2386012c6594 364 case INSTELLEN_RICHTING:
phgbartels 0:2386012c6594 365 {
phgbartels 0:2386012c6594 366 instellen_richting();
phgbartels 5:4842219cb77c 367 if (current_herhalingen == 100)
phgbartels 0:2386012c6594 368 {
phgbartels 0:2386012c6594 369 state = SLAAN;
phgbartels 0:2386012c6594 370 current_herhalingen = 0;
phgbartels 0:2386012c6594 371 previous_herhalingen = 0;
phgbartels 0:2386012c6594 372 }
phgbartels 2:de7b6c1d67c4 373 break;
phgbartels 1:b08ac32d1ddc 374
phgbartels 0:2386012c6594 375 }
phgbartels 0:2386012c6594 376
phgbartels 0:2386012c6594 377 case SLAAN:
phgbartels 0:2386012c6594 378 {
phgbartels 5:4842219cb77c 379 GotoPosition(1.5 ,8);
phgbartels 5:4842219cb77c 380 if (current_herhalingen == 100)
phgbartels 0:2386012c6594 381 {
phgbartels 0:2386012c6594 382 state = RETURN2RUST;
phgbartels 0:2386012c6594 383 current_herhalingen = 0;
phgbartels 0:2386012c6594 384 previous_herhalingen = 0;
phgbartels 2:de7b6c1d67c4 385 prev_setpoint =0;
phgbartels 2:de7b6c1d67c4 386 setpoint =0;
phgbartels 2:de7b6c1d67c4 387
phgbartels 0:2386012c6594 388 }
phgbartels 2:de7b6c1d67c4 389 break;
phgbartels 0:2386012c6594 390 }
phgbartels 0:2386012c6594 391
phgbartels 0:2386012c6594 392 case RETURN2RUST:
phgbartels 0:2386012c6594 393 {
phgbartels 5:4842219cb77c 394 GotoPosition(0,2);
phgbartels 5:4842219cb77c 395 if (current_herhalingen == 100)
phgbartels 0:2386012c6594 396 {
phgbartels 0:2386012c6594 397 state = RUST;
phgbartels 0:2386012c6594 398 current_herhalingen = 0;
phgbartels 0:2386012c6594 399 previous_herhalingen = 0;
phgbartels 0:2386012c6594 400 }
phgbartels 5:4842219cb77c 401
phgbartels 1:b08ac32d1ddc 402 break;
phgbartels 0:2386012c6594 403 }
phgbartels 0:2386012c6594 404
phgbartels 0:2386012c6594 405 default: {
phgbartels 0:2386012c6594 406 state = RUST;
phgbartels 0:2386012c6594 407 }
phgbartels 0:2386012c6594 408
phgbartels 0:2386012c6594 409 }//switch(state)
phgbartels 0:2386012c6594 410 }//void statemachinefunction
phgbartels 0:2386012c6594 411
phgbartels 0:2386012c6594 412
phgbartels 0:2386012c6594 413 void screenupdate(){
phgbartels 0:2386012c6594 414 if(state==RUST){
phgbartels 0:2386012c6594 415 lcd.cls();
phgbartels 0:2386012c6594 416 lcd.locate(0,0);
phgbartels 5:4842219cb77c 417 lcd.printf("V.I.C.T.O.R.Y."); //regel 1 LCD scherm
phgbartels 0:2386012c6594 418 lcd.locate(0,1);
phgbartels 0:2386012c6594 419 lcd.printf(" GROEP 7 ");
phgbartels 0:2386012c6594 420 }
lauradeheus 6:a7379a681adf 421 else if(state==EMG_KALIBRATIE){
lauradeheus 6:a7379a681adf 422 lcd.cls();
lauradeheus 6:a7379a681adf 423 lcd.locate(0,0);
lauradeheus 6:a7379a681adf 424 lcd.printf("Max. aanspannen");
lauradeheus 6:a7379a681adf 425 if(current_herhalingen<=200){
lauradeheus 6:a7379a681adf 426 lcd.locate(0,1);
lauradeheus 6:a7379a681adf 427 lcd.printf("nog 5 sec.");
lauradeheus 6:a7379a681adf 428 }
lauradeheus 6:a7379a681adf 429 else if(current_herhalingen<=400){
lauradeheus 6:a7379a681adf 430 lcd.locate(0,1);
lauradeheus 6:a7379a681adf 431 lcd.printf("nog 4 sec.");
lauradeheus 6:a7379a681adf 432 }
lauradeheus 6:a7379a681adf 433 else if(current_herhalingen<=600){
lauradeheus 6:a7379a681adf 434 lcd.locate(0,1);
lauradeheus 6:a7379a681adf 435 lcd.printf("nog 3 sec.");
lauradeheus 6:a7379a681adf 436 }
lauradeheus 6:a7379a681adf 437 else if(current_herhalingen<=800){
lauradeheus 6:a7379a681adf 438 lcd.locate(0,1);
lauradeheus 6:a7379a681adf 439 lcd.printf("nog 2 sec.");
lauradeheus 6:a7379a681adf 440 }
lauradeheus 6:a7379a681adf 441 else if(current_herhalingen<=1000){
lauradeheus 6:a7379a681adf 442 lcd.locate(0,1);
lauradeheus 6:a7379a681adf 443 lcd.printf("nog 1 sec.");
lauradeheus 6:a7379a681adf 444 }
lauradeheus 6:a7379a681adf 445 }
phgbartels 0:2386012c6594 446 else{
phgbartels 0:2386012c6594 447 lcd.cls();
phgbartels 0:2386012c6594 448 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 449 }
phgbartels 0:2386012c6594 450 }
phgbartels 0:2386012c6594 451
lauradeheus 6:a7379a681adf 452 int main(){
phgbartels 4:377ddd65e4a6 453 pc.baud(115200);
lauradeheus 6:a7379a681adf 454 arm_biquad_cascade_df1_init_f32(&lowpass_1,1 , lowpass_1_const, lowpass_1_states);
lauradeheus 6:a7379a681adf 455 arm_biquad_cascade_df1_init_f32(&highpass,1 , highpass_const, highpass_states);
lauradeheus 6:a7379a681adf 456 arm_biquad_cascade_df1_init_f32(&notch,1 , notch_const, notch_states);
lauradeheus 6:a7379a681adf 457 arm_biquad_cascade_df1_init_f32(&lowpass_2,1 , lowpass_2_const, lowpass_2_states);
phgbartels 4:377ddd65e4a6 458 statemachine.attach(&statemachinefunction, TSAMP); // the address of the function to be attached (flip) and the interval (2 seconds)
phgbartels 0:2386012c6594 459 screen.attach(&screenupdate, 0.2);
phgbartels 4:377ddd65e4a6 460 while(1);
phgbartels 0:2386012c6594 461 }