kappa
Dependencies: Encoder HIDScope MODSERIAL- mbed-dsp mbed
Fork of PROJECT_incl_regelaar2 by
PROJECT_main.cpp@6:14051758db6f, 2014-11-03 (annotated)
- Committer:
- Hooglugt
- Date:
- Mon Nov 03 13:05:31 2014 +0000
- Revision:
- 6:14051758db6f
- Parent:
- 5:e5ca53305b87
- Child:
- 7:ca1ade91bd14
PID regelaar van gemaakt en 4 PID's (12 K-waarden)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Hooglugt | 0:99cbc87af37c | 1 | #include "mbed.h" |
Hooglugt | 0:99cbc87af37c | 2 | #include "MODSERIAL.h" |
Hooglugt | 0:99cbc87af37c | 3 | #include "HIDScope.h" |
Hooglugt | 0:99cbc87af37c | 4 | #include "arm_math.h" |
Hooglugt | 0:99cbc87af37c | 5 | #include "encoder.h" |
Hooglugt | 0:99cbc87af37c | 6 | |
Hooglugt | 0:99cbc87af37c | 7 | #define TSAMP 0.001 // sample freq encoder motor |
Hooglugt | 0:99cbc87af37c | 8 | #define TIMEB4NEXTCHOICE 1 // sec keuzelampje blijft aan |
Hooglugt | 1:d44a866de64f | 9 | #define TIMEBETWEENBLINK 100 // sec voor volgende blink |
Hooglugt | 0:99cbc87af37c | 10 | #define TSAMP_EMG 0.002 //sample frequency emg |
Hooglugt | 0:99cbc87af37c | 11 | #define KALIBRATIONTIME 1000 // 10 sec voor bepalen van maximale biceps/triceps waarde |
Hooglugt | 1:d44a866de64f | 12 | #define FACTOR 0.6 //factor*max_waarde = threshold emg |
Hooglugt | 0:99cbc87af37c | 13 | //Define objects |
Hooglugt | 0:99cbc87af37c | 14 | AnalogIn emg0(PTB1); //Analog input biceps |
Hooglugt | 0:99cbc87af37c | 15 | AnalogIn emg1(PTB2); //Analog input triceps |
Hooglugt | 0:99cbc87af37c | 16 | |
Hooglugt | 0:99cbc87af37c | 17 | Ticker log_timer; //sample emg |
Hooglugt | 1:d44a866de64f | 18 | Ticker blink; //ledjes aan/uit |
Hooglugt | 1:d44a866de64f | 19 | Ticker blink2; //extra tikker zodat kalbi en kaltri tegelijkertijd aankunnen |
Hooglugt | 0:99cbc87af37c | 20 | Ticker looptimer; //motor regelaar |
Hooglugt | 0:99cbc87af37c | 21 | |
Hooglugt | 0:99cbc87af37c | 22 | MODSERIAL pc(USBTX,USBRX); |
Hooglugt | 0:99cbc87af37c | 23 | |
Hooglugt | 0:99cbc87af37c | 24 | arm_biquad_casd_df1_inst_f32 bihighpass; |
Hooglugt | 0:99cbc87af37c | 25 | float bihighpass_const[] = {0.8751821104711265, -1.750364220942253, 0.8751821104711265, 1.7347238224240125 , -0.7660046194604936}; //highpass, Fc: 15 Hz, Fsample: 500Hz, Q = 0.7071 |
Hooglugt | 0:99cbc87af37c | 26 | float bihighpass_states[4]; |
Hooglugt | 0:99cbc87af37c | 27 | |
Hooglugt | 0:99cbc87af37c | 28 | arm_biquad_casd_df1_inst_f32 binotch; |
Hooglugt | 0:99cbc87af37c | 29 | float binotch_const[] = {0.9714498065192796, -1.5718388053127037, 0.9714498065192796, 1.5718388053127037 , -0.9428996130385592}; //notch, Fc: 50 Hz, Fsample: 500Hz, Q = 10 |
Hooglugt | 0:99cbc87af37c | 30 | float binotch_states[4]; |
Hooglugt | 0:99cbc87af37c | 31 | |
Hooglugt | 0:99cbc87af37c | 32 | arm_biquad_casd_df1_inst_f32 trihighpass; |
Hooglugt | 0:99cbc87af37c | 33 | float trihighpass_const[] = {0.8751821104711265, -1.750364220942253, 0.8751821104711265, 1.7347238224240125 , -0.7660046194604936}; //highpass, Fc: 15 Hz, Fsample: 500Hz, Q = 0.7071 |
Hooglugt | 0:99cbc87af37c | 34 | float trihighpass_states[4]; |
Hooglugt | 0:99cbc87af37c | 35 | |
Hooglugt | 0:99cbc87af37c | 36 | arm_biquad_casd_df1_inst_f32 trinotch; |
Hooglugt | 0:99cbc87af37c | 37 | float trinotch_const[] = {0.9714498065192796, -1.5718388053127037, 0.9714498065192796, 1.5718388053127037 , -0.9428996130385592}; //notch, Fc: 50 Hz, Fsample: 500Hz, Q = 10 |
Hooglugt | 0:99cbc87af37c | 38 | float trinotch_states[4]; |
Hooglugt | 0:99cbc87af37c | 39 | |
Hooglugt | 0:99cbc87af37c | 40 | float bi_result = 0; |
Hooglugt | 0:99cbc87af37c | 41 | float tri_result = 0; |
Hooglugt | 0:99cbc87af37c | 42 | |
Hooglugt | 0:99cbc87af37c | 43 | float bi_max = 0; |
Hooglugt | 0:99cbc87af37c | 44 | float tri_max = 0; |
Hooglugt | 0:99cbc87af37c | 45 | |
Hooglugt | 0:99cbc87af37c | 46 | // variables for biceps MAF |
Hooglugt | 0:99cbc87af37c | 47 | float y0 = 0; |
Hooglugt | 0:99cbc87af37c | 48 | float y1 = 0; |
Hooglugt | 0:99cbc87af37c | 49 | float y2 = 0; |
Hooglugt | 0:99cbc87af37c | 50 | float y3 = 0; |
Hooglugt | 0:99cbc87af37c | 51 | float y4 = 0; |
Hooglugt | 0:99cbc87af37c | 52 | float y5 = 0; |
Hooglugt | 0:99cbc87af37c | 53 | float y6 = 0; |
Hooglugt | 0:99cbc87af37c | 54 | float y7 = 0; |
Hooglugt | 0:99cbc87af37c | 55 | float y8 = 0; |
Hooglugt | 0:99cbc87af37c | 56 | float y9 = 0; |
Hooglugt | 0:99cbc87af37c | 57 | |
Hooglugt | 0:99cbc87af37c | 58 | // variables for triceps MAF |
Hooglugt | 0:99cbc87af37c | 59 | float x0 = 0; |
Hooglugt | 0:99cbc87af37c | 60 | float x1 = 0; |
Hooglugt | 0:99cbc87af37c | 61 | float x2 = 0; |
Hooglugt | 0:99cbc87af37c | 62 | float x3 = 0; |
Hooglugt | 0:99cbc87af37c | 63 | float x4 = 0; |
Hooglugt | 0:99cbc87af37c | 64 | float x5 = 0; |
Hooglugt | 0:99cbc87af37c | 65 | float x6 = 0; |
Hooglugt | 0:99cbc87af37c | 66 | float x7 = 0; |
Hooglugt | 0:99cbc87af37c | 67 | float x8 = 0; |
Hooglugt | 0:99cbc87af37c | 68 | float x9 = 0; |
Hooglugt | 0:99cbc87af37c | 69 | |
Hooglugt | 0:99cbc87af37c | 70 | //LED interface |
Hooglugt | 0:99cbc87af37c | 71 | DigitalOut dir1(PTA1); |
Hooglugt | 0:99cbc87af37c | 72 | DigitalOut dir2(PTA2); |
Hooglugt | 0:99cbc87af37c | 73 | DigitalOut dir3(PTD4); |
Hooglugt | 0:99cbc87af37c | 74 | DigitalOut for1(PTA12); |
Hooglugt | 0:99cbc87af37c | 75 | DigitalOut for2(PTA13); |
Hooglugt | 0:99cbc87af37c | 76 | DigitalOut for3(PTD1); |
Hooglugt | 0:99cbc87af37c | 77 | |
Hooglugt | 0:99cbc87af37c | 78 | uint8_t direction = 0; |
Hooglugt | 0:99cbc87af37c | 79 | uint8_t force = 0; |
Hooglugt | 0:99cbc87af37c | 80 | |
Hooglugt | 0:99cbc87af37c | 81 | //motorcontrol objects |
Hooglugt | 0:99cbc87af37c | 82 | |
Hooglugt | 0:99cbc87af37c | 83 | //motor 1, voltage pins op M2 |
Hooglugt | 0:99cbc87af37c | 84 | Encoder motor1(PTD3, PTD5); |
Hooglugt | 0:99cbc87af37c | 85 | DigitalOut motor1dir(PTC9); |
Hooglugt | 0:99cbc87af37c | 86 | PwmOut pwm_motor1(PTC8); |
Hooglugt | 0:99cbc87af37c | 87 | |
Hooglugt | 0:99cbc87af37c | 88 | //motor 2, voltage pins op M1 |
Hooglugt | 0:99cbc87af37c | 89 | Encoder motor2(PTD2,PTD0); |
Hooglugt | 0:99cbc87af37c | 90 | DigitalOut motor2dir(PTA4); |
Hooglugt | 0:99cbc87af37c | 91 | PwmOut pwm_motor2(PTA5); |
Hooglugt | 0:99cbc87af37c | 92 | |
Hooglugt | 0:99cbc87af37c | 93 | float integral = 0; |
Hooglugt | 4:697d5a806cc4 | 94 | float batjeset = 0; //een if statement wordt "true" wanneer batje voor een bepaalde tijd de juiste hoek heeft behouden |
Hooglugt | 4:697d5a806cc4 | 95 | float balhit = 0; //balhit wordt 1 wanneer arm een bepaalde hoek heeft afgelegd |
Hooglugt | 0:99cbc87af37c | 96 | float controlerror = 0; |
Hooglugt | 6:14051758db6f | 97 | float previouserror = 0; |
Hooglugt | 0:99cbc87af37c | 98 | float pwm = 0; |
Hooglugt | 0:99cbc87af37c | 99 | |
Hooglugt | 0:99cbc87af37c | 100 | float omrekenfactor1 = 0.0028035714; // 6.28/(32*70) |
Hooglugt | 1:d44a866de64f | 101 | float omrekenfactor2 = 0.0015213178; // 6.28/(24*172); |
Hooglugt | 0:99cbc87af37c | 102 | |
Hooglugt | 1:d44a866de64f | 103 | float setpoint1 = 0; //te behalen speed van motor1 (37D) |
Hooglugt | 1:d44a866de64f | 104 | float setpoint2 = 0; //te behalen hoek van motor2 (25D) |
Hooglugt | 0:99cbc87af37c | 105 | |
Hooglugt | 0:99cbc87af37c | 106 | float Kp1 = 1.10; //DEZE KP1 EN KP1 ZIJN NOG NIET DEFINITIEF |
Hooglugt | 0:99cbc87af37c | 107 | float Ki1 = 0.20; //Kp en Ki van motor1, voor de slag |
Hooglugt | 6:14051758db6f | 108 | float Kd1 = 0.0; |
Hooglugt | 6:14051758db6f | 109 | |
Hooglugt | 6:14051758db6f | 110 | float Kp3 = 0.09; //Kp en Ki van motor1, voor de return |
Hooglugt | 6:14051758db6f | 111 | float Ki3 = 0.05; |
Hooglugt | 6:14051758db6f | 112 | float Kd3 = 0.0; |
Hooglugt | 0:99cbc87af37c | 113 | |
Hooglugt | 0:99cbc87af37c | 114 | float Kp2 = 0.30; //Kp en Ki van motor2, voor in het positie brengen en voor de return |
Hooglugt | 0:99cbc87af37c | 115 | float Ki2 = 0.20; |
Hooglugt | 6:14051758db6f | 116 | float Kd2 = 0.0; |
Hooglugt | 0:99cbc87af37c | 117 | |
Hooglugt | 6:14051758db6f | 118 | float Kp4 = 0.30; //Kp en Ki van motor2, voor de return |
Hooglugt | 6:14051758db6f | 119 | float Ki4 = 0.20; |
Hooglugt | 6:14051758db6f | 120 | float Kd4 = 0.0; |
Hooglugt | 0:99cbc87af37c | 121 | |
Hooglugt | 0:99cbc87af37c | 122 | volatile bool looptimerflag; //voor motorcontrol TSAMP |
Hooglugt | 0:99cbc87af37c | 123 | |
Hooglugt | 0:99cbc87af37c | 124 | //functies |
Hooglugt | 0:99cbc87af37c | 125 | |
Hooglugt | 0:99cbc87af37c | 126 | void setlooptimerflag(void) |
Hooglugt | 0:99cbc87af37c | 127 | { |
Hooglugt | 0:99cbc87af37c | 128 | looptimerflag = true; |
Hooglugt | 0:99cbc87af37c | 129 | |
Hooglugt | 0:99cbc87af37c | 130 | } |
Hooglugt | 0:99cbc87af37c | 131 | |
Hooglugt | 0:99cbc87af37c | 132 | void keep_in_range(float * in, float min, float max) |
Hooglugt | 0:99cbc87af37c | 133 | { |
Hooglugt | 0:99cbc87af37c | 134 | *in > min ? *in < max? : *in = max: *in = max; |
Hooglugt | 0:99cbc87af37c | 135 | } |
Hooglugt | 0:99cbc87af37c | 136 | |
Hooglugt | 0:99cbc87af37c | 137 | void looper() |
Hooglugt | 0:99cbc87af37c | 138 | { |
Hooglugt | 0:99cbc87af37c | 139 | //put raw emg value of biceps and triceps in emg_biceps and emg_triceps, respectively |
Hooglugt | 0:99cbc87af37c | 140 | float emg_biceps; //Float voor EMG-waarde biceps |
Hooglugt | 0:99cbc87af37c | 141 | float emg_triceps; //Float voor EMG-waarde triceps |
Hooglugt | 0:99cbc87af37c | 142 | |
Hooglugt | 0:99cbc87af37c | 143 | emg_biceps = emg0.read(); // read float value (0..1 = 0..3.3V) biceps |
Hooglugt | 0:99cbc87af37c | 144 | emg_triceps = emg1.read(); // read float value (0..1 = 0..3.3V) triceps |
Hooglugt | 0:99cbc87af37c | 145 | |
Hooglugt | 0:99cbc87af37c | 146 | //process emg biceps |
Hooglugt | 0:99cbc87af37c | 147 | arm_biquad_cascade_df1_f32(&bihighpass, &emg_biceps, &emg_biceps, 1 ); |
Hooglugt | 0:99cbc87af37c | 148 | arm_biquad_cascade_df1_f32(&binotch, &emg_biceps, &emg_biceps, 1 ); |
Hooglugt | 0:99cbc87af37c | 149 | y0 = fabs(emg_biceps); |
Hooglugt | 0:99cbc87af37c | 150 | bi_result = (y0*0.1 +y1*0.1 + y2*0.1 + y3*0.1 + y4*0.1 + y5*0.1 + y6*0.1 + y7*0.1 + y8*0.1 + y9*0.1); |
Hooglugt | 0:99cbc87af37c | 151 | y9=y8; |
Hooglugt | 0:99cbc87af37c | 152 | y8=y7; |
Hooglugt | 0:99cbc87af37c | 153 | y7=y6; |
Hooglugt | 0:99cbc87af37c | 154 | y6=y5; |
Hooglugt | 0:99cbc87af37c | 155 | y5=y4; |
Hooglugt | 0:99cbc87af37c | 156 | y4=y3; |
Hooglugt | 0:99cbc87af37c | 157 | y3=y2; |
Hooglugt | 0:99cbc87af37c | 158 | y2=y1; |
Hooglugt | 0:99cbc87af37c | 159 | y1=y0; |
Hooglugt | 0:99cbc87af37c | 160 | |
Hooglugt | 0:99cbc87af37c | 161 | //process emg triceps |
Hooglugt | 0:99cbc87af37c | 162 | arm_biquad_cascade_df1_f32(&trihighpass, &emg_triceps, &emg_triceps, 1 ); |
Hooglugt | 0:99cbc87af37c | 163 | arm_biquad_cascade_df1_f32(&trinotch, &emg_triceps, &emg_triceps, 1 ); |
Hooglugt | 0:99cbc87af37c | 164 | x0 = fabs(emg_triceps); |
Hooglugt | 0:99cbc87af37c | 165 | tri_result = (x0*0.1 +x1*0.1 + x2*0.1 + x3*0.1 + x4*0.1 + x5*0.1 + x6*0.1 + x7*0.1 + x8*0.1 + x9*0.1); |
Hooglugt | 0:99cbc87af37c | 166 | x9=x8; |
Hooglugt | 0:99cbc87af37c | 167 | x8=x7; |
Hooglugt | 0:99cbc87af37c | 168 | x7=x6; |
Hooglugt | 0:99cbc87af37c | 169 | x6=x5; |
Hooglugt | 0:99cbc87af37c | 170 | x5=x4; |
Hooglugt | 0:99cbc87af37c | 171 | x4=x3; |
Hooglugt | 0:99cbc87af37c | 172 | x3=x2; |
Hooglugt | 0:99cbc87af37c | 173 | x2=x1; |
Hooglugt | 0:99cbc87af37c | 174 | x1=x0; |
Hooglugt | 0:99cbc87af37c | 175 | } |
Hooglugt | 0:99cbc87af37c | 176 | |
Hooglugt | 0:99cbc87af37c | 177 | void kalbi() //blinking three lights, first row - 2nd row unlit |
Hooglugt | 0:99cbc87af37c | 178 | { |
Hooglugt | 0:99cbc87af37c | 179 | if(dir1==0) { |
Hooglugt | 0:99cbc87af37c | 180 | dir1 = dir2 = dir3 = 1; |
Hooglugt | 0:99cbc87af37c | 181 | } else { |
Hooglugt | 0:99cbc87af37c | 182 | dir1 = dir2 = dir3 = 0; |
Hooglugt | 0:99cbc87af37c | 183 | } |
Hooglugt | 0:99cbc87af37c | 184 | } |
Hooglugt | 0:99cbc87af37c | 185 | |
Hooglugt | 0:99cbc87af37c | 186 | void kaltri() //blinking three lights, 2nd row - first row lit |
Hooglugt | 0:99cbc87af37c | 187 | { |
Hooglugt | 0:99cbc87af37c | 188 | if(for1==0) { |
Hooglugt | 0:99cbc87af37c | 189 | for1 = for2 = for3 = 1; |
Hooglugt | 0:99cbc87af37c | 190 | } else { |
Hooglugt | 0:99cbc87af37c | 191 | for1 = for2 = for3 = 0; |
Hooglugt | 0:99cbc87af37c | 192 | } |
Hooglugt | 0:99cbc87af37c | 193 | } |
Hooglugt | 0:99cbc87af37c | 194 | |
Hooglugt | 0:99cbc87af37c | 195 | void okay() //blinking the two lights you have chosen (misschien is hier een betere manier van coderen voor :P) |
Hooglugt | 0:99cbc87af37c | 196 | { |
Hooglugt | 0:99cbc87af37c | 197 | if(direction == 1 && force == 1) { // links zwak |
Hooglugt | 0:99cbc87af37c | 198 | if(for1 == 0 && dir1 == 0) { |
Hooglugt | 0:99cbc87af37c | 199 | for1 = dir1 = 1; |
Hooglugt | 0:99cbc87af37c | 200 | } else { |
Hooglugt | 0:99cbc87af37c | 201 | for1 = dir1 = 0; |
Hooglugt | 0:99cbc87af37c | 202 | } |
Hooglugt | 0:99cbc87af37c | 203 | } |
Hooglugt | 0:99cbc87af37c | 204 | if(direction == 1 && force == 2) { // links normaal |
Hooglugt | 0:99cbc87af37c | 205 | if(for2 == 0 && dir1 == 0) { |
Hooglugt | 0:99cbc87af37c | 206 | for2 = dir1 = 1; |
Hooglugt | 0:99cbc87af37c | 207 | } else { |
Hooglugt | 0:99cbc87af37c | 208 | for2 = dir1 = 0; |
Hooglugt | 0:99cbc87af37c | 209 | } |
Hooglugt | 0:99cbc87af37c | 210 | } |
Hooglugt | 0:99cbc87af37c | 211 | if(direction == 1 && force == 3) { // links sterk |
Hooglugt | 0:99cbc87af37c | 212 | if(for3 == 0 && dir1 == 0) { |
Hooglugt | 0:99cbc87af37c | 213 | for3 = dir1 = 1; |
Hooglugt | 0:99cbc87af37c | 214 | } else { |
Hooglugt | 0:99cbc87af37c | 215 | for3 = dir1 = 0; |
Hooglugt | 0:99cbc87af37c | 216 | } |
Hooglugt | 0:99cbc87af37c | 217 | } |
Hooglugt | 0:99cbc87af37c | 218 | if(direction == 2 && force == 1) { // mid zwak |
Hooglugt | 0:99cbc87af37c | 219 | if(for1 == 0 && dir2 == 0) { |
Hooglugt | 0:99cbc87af37c | 220 | for1 = dir2 = 1; |
Hooglugt | 0:99cbc87af37c | 221 | } else { |
Hooglugt | 0:99cbc87af37c | 222 | for1 = dir2 = 0; |
Hooglugt | 0:99cbc87af37c | 223 | } |
Hooglugt | 0:99cbc87af37c | 224 | } |
Hooglugt | 0:99cbc87af37c | 225 | if(direction == 2 && force == 2) { // mid normaal |
Hooglugt | 0:99cbc87af37c | 226 | if(for2 == 0 && dir2 == 0) { |
Hooglugt | 0:99cbc87af37c | 227 | for2 = dir2 = 1; |
Hooglugt | 0:99cbc87af37c | 228 | } else { |
Hooglugt | 0:99cbc87af37c | 229 | for2 = dir2 = 0; |
Hooglugt | 0:99cbc87af37c | 230 | } |
Hooglugt | 0:99cbc87af37c | 231 | } |
Hooglugt | 0:99cbc87af37c | 232 | if(direction == 2 && force == 3) { // mid sterk |
Hooglugt | 0:99cbc87af37c | 233 | if(for3 == 0 && dir2 == 0) { |
Hooglugt | 0:99cbc87af37c | 234 | for3 = dir2 = 1; |
Hooglugt | 0:99cbc87af37c | 235 | } else { |
Hooglugt | 0:99cbc87af37c | 236 | for3 = dir2 = 0; |
Hooglugt | 0:99cbc87af37c | 237 | } |
Hooglugt | 0:99cbc87af37c | 238 | } |
Hooglugt | 0:99cbc87af37c | 239 | if(direction == 3 && force == 1) { // rechts zwak |
Hooglugt | 0:99cbc87af37c | 240 | if(for1 == 0 && dir3 == 0) { |
Hooglugt | 0:99cbc87af37c | 241 | for1 = dir3 = 1; |
Hooglugt | 0:99cbc87af37c | 242 | } else { |
Hooglugt | 0:99cbc87af37c | 243 | for1 = dir3 = 0; |
Hooglugt | 0:99cbc87af37c | 244 | } |
Hooglugt | 0:99cbc87af37c | 245 | } |
Hooglugt | 0:99cbc87af37c | 246 | if(direction == 3 && force == 2) { // rechts normaal |
Hooglugt | 0:99cbc87af37c | 247 | if(for2 == 0 && dir3 == 0) { |
Hooglugt | 0:99cbc87af37c | 248 | for2 = dir3 = 1; |
Hooglugt | 0:99cbc87af37c | 249 | } else { |
Hooglugt | 0:99cbc87af37c | 250 | for2 = dir3 = 0; |
Hooglugt | 0:99cbc87af37c | 251 | } |
Hooglugt | 0:99cbc87af37c | 252 | } |
Hooglugt | 0:99cbc87af37c | 253 | if(direction == 3 && force == 3) { // rechts sterk |
Hooglugt | 0:99cbc87af37c | 254 | if(for3 == 0 && dir3 == 0) { |
Hooglugt | 0:99cbc87af37c | 255 | for3 = dir3 = 1; |
Hooglugt | 0:99cbc87af37c | 256 | } else { |
Hooglugt | 0:99cbc87af37c | 257 | for3 = dir3 = 0; |
Hooglugt | 0:99cbc87af37c | 258 | } |
Hooglugt | 0:99cbc87af37c | 259 | } |
Hooglugt | 0:99cbc87af37c | 260 | } |
Hooglugt | 0:99cbc87af37c | 261 | |
Hooglugt | 0:99cbc87af37c | 262 | int main() |
Hooglugt | 0:99cbc87af37c | 263 | { |
Hooglugt | 0:99cbc87af37c | 264 | pc.baud(115200); //baudrate instellen |
Hooglugt | 0:99cbc87af37c | 265 | log_timer.attach(looper, TSAMP_EMG); //EMG, Fsample 500 Hz |
Hooglugt | 0:99cbc87af37c | 266 | looptimer.attach(setlooptimerflag,TSAMP); |
Hooglugt | 0:99cbc87af37c | 267 | pwm_motor1.period_us(100); //10kHz PWM frequency |
Hooglugt | 0:99cbc87af37c | 268 | pwm_motor2.period_us(100); //10kHz PWM frequency |
Hooglugt | 0:99cbc87af37c | 269 | |
Hooglugt | 0:99cbc87af37c | 270 | //set up filters |
Hooglugt | 0:99cbc87af37c | 271 | arm_biquad_cascade_df1_init_f32(&binotch, 1, binotch_const, binotch_states); |
Hooglugt | 0:99cbc87af37c | 272 | arm_biquad_cascade_df1_init_f32(&bihighpass, 1, bihighpass_const, bihighpass_states); |
Hooglugt | 0:99cbc87af37c | 273 | |
Hooglugt | 0:99cbc87af37c | 274 | arm_biquad_cascade_df1_init_f32(&trinotch, 1, trinotch_const, trinotch_states); |
Hooglugt | 0:99cbc87af37c | 275 | arm_biquad_cascade_df1_init_f32(&trihighpass, 1, trihighpass_const, trihighpass_states); |
Hooglugt | 0:99cbc87af37c | 276 | |
Hooglugt | 0:99cbc87af37c | 277 | //kalibratie |
Hooglugt | 0:99cbc87af37c | 278 | |
Hooglugt | 0:99cbc87af37c | 279 | //motorarm naar nul-positie |
Hooglugt | 0:99cbc87af37c | 280 | blink.attach(kalbi, 0.2); |
Hooglugt | 1:d44a866de64f | 281 | blink2.attach(kaltri, 0.2); |
Hooglugt | 0:99cbc87af37c | 282 | |
Hooglugt | 0:99cbc87af37c | 283 | //calibration motor 2 |
Hooglugt | 0:99cbc87af37c | 284 | pwm_motor2.write(0.6); //lage PWM |
Hooglugt | 5:e5ca53305b87 | 285 | motor2dir = 0; //rechtsom |
Hooglugt | 0:99cbc87af37c | 286 | wait(1); // anders wordt de while(1) meteen onderbroken |
Hooglugt | 4:697d5a806cc4 | 287 | while(1) { |
Hooglugt | 5:e5ca53305b87 | 288 | if(motor2.getSpeed()*omrekenfactor2 > -0.70 && motor2.getSpeed()*omrekenfactor2 < 0.70) { // motor2.getSpeed()*omrekenfactor2 > -0.70), 0.70 is nog aan te passen |
Hooglugt | 0:99cbc87af37c | 289 | pwm_motor2.write(0); |
Hooglugt | 4:697d5a806cc4 | 290 | motor2.setPosition(0); |
Hooglugt | 4:697d5a806cc4 | 291 | goto motor1cal; |
Hooglugt | 0:99cbc87af37c | 292 | } |
Hooglugt | 0:99cbc87af37c | 293 | wait(0.01); |
Hooglugt | 0:99cbc87af37c | 294 | } |
Hooglugt | 4:697d5a806cc4 | 295 | motor1cal: |
Hooglugt | 0:99cbc87af37c | 296 | //calibration motor 1 |
Hooglugt | 0:99cbc87af37c | 297 | pwm_motor1.write(0.55); //lage PWM |
Hooglugt | 5:e5ca53305b87 | 298 | motor1dir = 1; //linksom |
Hooglugt | 0:99cbc87af37c | 299 | wait(1); // anders wordt de while(1) meteen onderbroken |
Hooglugt | 4:697d5a806cc4 | 300 | while(1) { |
Hooglugt | 5:e5ca53305b87 | 301 | if(motor1.getSpeed()*omrekenfactor1 > -0.20 && motor1.getSpeed()*omrekenfactor1 < 0.20) { // motor1.getSpeed()*omrekenfactor1 < 0.20, 0.20 is nog aan te passen |
Hooglugt | 0:99cbc87af37c | 302 | pwm_motor1.write(0); |
Hooglugt | 4:697d5a806cc4 | 303 | motor1.setPosition(0); |
Hooglugt | 4:697d5a806cc4 | 304 | goto emgcal; |
Hooglugt | 0:99cbc87af37c | 305 | } |
Hooglugt | 0:99cbc87af37c | 306 | wait(0.01); |
Hooglugt | 0:99cbc87af37c | 307 | } |
Hooglugt | 4:697d5a806cc4 | 308 | emgcal: |
Hooglugt | 0:99cbc87af37c | 309 | blink.detach(); |
Hooglugt | 1:d44a866de64f | 310 | blink2.detach(); |
Hooglugt | 0:99cbc87af37c | 311 | dir1 = dir2 = dir3 = 1; |
Hooglugt | 4:697d5a806cc4 | 312 | for1 = for2 = for3 = 1; |
Hooglugt | 4:697d5a806cc4 | 313 | pc.printf("kalmoarm "); |
Hooglugt | 0:99cbc87af37c | 314 | wait (1); |
Hooglugt | 0:99cbc87af37c | 315 | for1 = for2 = for3 = 0; |
Hooglugt | 0:99cbc87af37c | 316 | |
Hooglugt | 0:99cbc87af37c | 317 | //biceps kalibratie |
Hooglugt | 0:99cbc87af37c | 318 | blink.attach(kalbi, 0.2); |
Hooglugt | 0:99cbc87af37c | 319 | for (int kaltime = 0; kaltime<KALIBRATIONTIME; kaltime++) { |
Hooglugt | 0:99cbc87af37c | 320 | if (bi_max < bi_result) { |
Hooglugt | 0:99cbc87af37c | 321 | bi_max = bi_result; |
Hooglugt | 0:99cbc87af37c | 322 | } |
Hooglugt | 0:99cbc87af37c | 323 | wait (0.01); |
Hooglugt | 1:d44a866de64f | 324 | } |
Hooglugt | 1:d44a866de64f | 325 | blink.detach(); |
Hooglugt | 1:d44a866de64f | 326 | dir1 = dir2 = dir3 = 1; |
Hooglugt | 4:697d5a806cc4 | 327 | pc.printf("kalbi "); |
Hooglugt | 1:d44a866de64f | 328 | wait (1); |
Hooglugt | 0:99cbc87af37c | 329 | |
Hooglugt | 1:d44a866de64f | 330 | //triceps kalibratie |
Hooglugt | 1:d44a866de64f | 331 | blink.attach(kaltri, 0.2); |
Hooglugt | 1:d44a866de64f | 332 | for (int kaltime = 0; kaltime<KALIBRATIONTIME; kaltime++) { |
Hooglugt | 1:d44a866de64f | 333 | if (tri_max < tri_result) { |
Hooglugt | 1:d44a866de64f | 334 | tri_max = tri_result; |
Hooglugt | 0:99cbc87af37c | 335 | } |
Hooglugt | 1:d44a866de64f | 336 | wait (0.01); |
Hooglugt | 0:99cbc87af37c | 337 | } |
Hooglugt | 1:d44a866de64f | 338 | blink.detach(); |
Hooglugt | 1:d44a866de64f | 339 | for1 = for2 = for3 = 1; |
Hooglugt | 4:697d5a806cc4 | 340 | pc.printf("kaltri "); |
Hooglugt | 1:d44a866de64f | 341 | wait (1); |
Hooglugt | 1:d44a866de64f | 342 | for1 = for2 = for3 = 0; |
Hooglugt | 0:99cbc87af37c | 343 | |
Hooglugt | 4:697d5a806cc4 | 344 | directionchoice: |
Hooglugt | 1:d44a866de64f | 345 | log_timer.attach(looper, TSAMP_EMG); |
Hooglugt | 0:99cbc87af37c | 346 | |
Hooglugt | 4:697d5a806cc4 | 347 | while(1) { //Loop keuze DIRECTION |
Hooglugt | 0:99cbc87af37c | 348 | for(int i=1; i<4; i++) { |
Hooglugt | 0:99cbc87af37c | 349 | if(i==1) { //red |
Hooglugt | 0:99cbc87af37c | 350 | dir1=1; |
Hooglugt | 0:99cbc87af37c | 351 | dir2=0; |
Hooglugt | 0:99cbc87af37c | 352 | dir3=0; |
Hooglugt | 0:99cbc87af37c | 353 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
Hooglugt | 1:d44a866de64f | 354 | if(bi_result>FACTOR*bi_max) { |
Hooglugt | 0:99cbc87af37c | 355 | direction = 1; |
Hooglugt | 4:697d5a806cc4 | 356 | pc.printf("links "); |
Hooglugt | 0:99cbc87af37c | 357 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie |
Hooglugt | 0:99cbc87af37c | 358 | goto forcechoice; // goes to second while(1) for the deciding the force |
Hooglugt | 0:99cbc87af37c | 359 | } else { |
Hooglugt | 0:99cbc87af37c | 360 | wait(0.01); |
Hooglugt | 0:99cbc87af37c | 361 | } |
Hooglugt | 0:99cbc87af37c | 362 | } |
Hooglugt | 0:99cbc87af37c | 363 | } |
Hooglugt | 0:99cbc87af37c | 364 | if(i==2) { //green |
Hooglugt | 0:99cbc87af37c | 365 | dir1 =0; |
Hooglugt | 0:99cbc87af37c | 366 | dir2 =1; |
Hooglugt | 0:99cbc87af37c | 367 | dir3 =0; |
Hooglugt | 0:99cbc87af37c | 368 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
Hooglugt | 1:d44a866de64f | 369 | if(bi_result>FACTOR*bi_max) { |
Hooglugt | 0:99cbc87af37c | 370 | direction = 2; |
Hooglugt | 4:697d5a806cc4 | 371 | pc.printf("mid "); |
Hooglugt | 0:99cbc87af37c | 372 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie |
Hooglugt | 0:99cbc87af37c | 373 | goto forcechoice; |
Hooglugt | 0:99cbc87af37c | 374 | } else { |
Hooglugt | 0:99cbc87af37c | 375 | wait(0.01); |
Hooglugt | 0:99cbc87af37c | 376 | } |
Hooglugt | 0:99cbc87af37c | 377 | } |
Hooglugt | 0:99cbc87af37c | 378 | } |
Hooglugt | 0:99cbc87af37c | 379 | if(i==3) { //blue |
Hooglugt | 0:99cbc87af37c | 380 | dir1 =0; |
Hooglugt | 0:99cbc87af37c | 381 | dir2 =0; |
Hooglugt | 0:99cbc87af37c | 382 | dir3 =1; |
Hooglugt | 0:99cbc87af37c | 383 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
Hooglugt | 1:d44a866de64f | 384 | if(bi_result>FACTOR*bi_max) { |
Hooglugt | 0:99cbc87af37c | 385 | direction = 3; |
Hooglugt | 4:697d5a806cc4 | 386 | pc.printf("rechts "); |
Hooglugt | 0:99cbc87af37c | 387 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie |
Hooglugt | 0:99cbc87af37c | 388 | goto forcechoice; |
Hooglugt | 0:99cbc87af37c | 389 | } else { |
Hooglugt | 0:99cbc87af37c | 390 | wait(0.01); |
Hooglugt | 0:99cbc87af37c | 391 | } |
Hooglugt | 0:99cbc87af37c | 392 | } |
Hooglugt | 0:99cbc87af37c | 393 | } |
Hooglugt | 0:99cbc87af37c | 394 | } |
Hooglugt | 0:99cbc87af37c | 395 | } |
Hooglugt | 4:697d5a806cc4 | 396 | forcechoice: |
Hooglugt | 4:697d5a806cc4 | 397 | while(1) { //Loop keuze FORCE |
Hooglugt | 0:99cbc87af37c | 398 | for(int j=1; j<4; j++) { |
Hooglugt | 0:99cbc87af37c | 399 | if(j==1) { //red |
Hooglugt | 0:99cbc87af37c | 400 | for1=1; |
Hooglugt | 0:99cbc87af37c | 401 | for2=0; |
Hooglugt | 0:99cbc87af37c | 402 | for3=0; |
Hooglugt | 0:99cbc87af37c | 403 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
Hooglugt | 1:d44a866de64f | 404 | if(tri_result>FACTOR*tri_max) { |
Hooglugt | 0:99cbc87af37c | 405 | for1 = for2 = for3 = 0; |
Hooglugt | 4:697d5a806cc4 | 406 | pc.printf("reset "); |
Hooglugt | 0:99cbc87af37c | 407 | goto directionchoice; |
Hooglugt | 0:99cbc87af37c | 408 | } else { |
Hooglugt | 1:d44a866de64f | 409 | if(bi_result>FACTOR*bi_max) { |
Hooglugt | 0:99cbc87af37c | 410 | force = 1; |
Hooglugt | 4:697d5a806cc4 | 411 | pc.printf("zwak "); |
Hooglugt | 0:99cbc87af37c | 412 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie |
Hooglugt | 0:99cbc87af37c | 413 | goto choicesmade; |
Hooglugt | 0:99cbc87af37c | 414 | } else { |
Hooglugt | 0:99cbc87af37c | 415 | wait(0.01); |
Hooglugt | 0:99cbc87af37c | 416 | } |
Hooglugt | 0:99cbc87af37c | 417 | } |
Hooglugt | 0:99cbc87af37c | 418 | } |
Hooglugt | 0:99cbc87af37c | 419 | } |
Hooglugt | 0:99cbc87af37c | 420 | if(j==2) { //green |
Hooglugt | 0:99cbc87af37c | 421 | for1=0; |
Hooglugt | 0:99cbc87af37c | 422 | for2=1; |
Hooglugt | 0:99cbc87af37c | 423 | for3=0; |
Hooglugt | 0:99cbc87af37c | 424 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
Hooglugt | 1:d44a866de64f | 425 | if(tri_result>FACTOR*tri_max) { |
Hooglugt | 0:99cbc87af37c | 426 | for1 = for2 = for3 = 0; |
Hooglugt | 4:697d5a806cc4 | 427 | pc.printf("reset "); |
Hooglugt | 0:99cbc87af37c | 428 | goto directionchoice; |
Hooglugt | 0:99cbc87af37c | 429 | } else { |
Hooglugt | 1:d44a866de64f | 430 | if(bi_result>FACTOR*bi_max) { |
Hooglugt | 0:99cbc87af37c | 431 | force = 2; |
Hooglugt | 4:697d5a806cc4 | 432 | pc.printf("normaal "); |
Hooglugt | 0:99cbc87af37c | 433 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie |
Hooglugt | 0:99cbc87af37c | 434 | goto choicesmade; |
Hooglugt | 0:99cbc87af37c | 435 | } else { |
Hooglugt | 0:99cbc87af37c | 436 | wait(0.01); |
Hooglugt | 0:99cbc87af37c | 437 | } |
Hooglugt | 0:99cbc87af37c | 438 | } |
Hooglugt | 0:99cbc87af37c | 439 | } |
Hooglugt | 0:99cbc87af37c | 440 | } |
Hooglugt | 0:99cbc87af37c | 441 | if(j==3) { //blue |
Hooglugt | 0:99cbc87af37c | 442 | for1=0; |
Hooglugt | 0:99cbc87af37c | 443 | for2=0; |
Hooglugt | 0:99cbc87af37c | 444 | for3=1; |
Hooglugt | 0:99cbc87af37c | 445 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
Hooglugt | 1:d44a866de64f | 446 | if(tri_result>FACTOR*tri_max) { |
Hooglugt | 0:99cbc87af37c | 447 | for1 = for2 = for3 = 0; |
Hooglugt | 4:697d5a806cc4 | 448 | pc.printf("reset "); |
Hooglugt | 0:99cbc87af37c | 449 | goto directionchoice; |
Hooglugt | 0:99cbc87af37c | 450 | } else { |
Hooglugt | 1:d44a866de64f | 451 | if(bi_result>FACTOR*bi_max) { |
Hooglugt | 0:99cbc87af37c | 452 | force = 3; |
Hooglugt | 4:697d5a806cc4 | 453 | pc.printf("sterk "); |
Hooglugt | 0:99cbc87af37c | 454 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie |
Hooglugt | 0:99cbc87af37c | 455 | goto choicesmade; |
Hooglugt | 0:99cbc87af37c | 456 | } else { |
Hooglugt | 0:99cbc87af37c | 457 | wait(0.01); |
Hooglugt | 0:99cbc87af37c | 458 | } |
Hooglugt | 0:99cbc87af37c | 459 | } |
Hooglugt | 0:99cbc87af37c | 460 | } |
Hooglugt | 0:99cbc87af37c | 461 | } |
Hooglugt | 0:99cbc87af37c | 462 | } |
Hooglugt | 0:99cbc87af37c | 463 | } |
Hooglugt | 0:99cbc87af37c | 464 | |
Hooglugt | 0:99cbc87af37c | 465 | choicesmade: |
Hooglugt | 0:99cbc87af37c | 466 | blink.attach(okay, 0.2); |
Hooglugt | 4:697d5a806cc4 | 467 | while(1) { |
Hooglugt | 4:697d5a806cc4 | 468 | if(tri_result>FACTOR*tri_max) { |
Hooglugt | 0:99cbc87af37c | 469 | blink.detach(); |
Hooglugt | 4:697d5a806cc4 | 470 | pc.printf("reset "); |
Hooglugt | 1:d44a866de64f | 471 | switch (direction) { |
Hooglugt | 1:d44a866de64f | 472 | case 1: |
Hooglugt | 1:d44a866de64f | 473 | dir1 = 1; |
Hooglugt | 1:d44a866de64f | 474 | for1 = 1; |
Hooglugt | 1:d44a866de64f | 475 | for2 = for3 = 0; |
Hooglugt | 1:d44a866de64f | 476 | break; |
Hooglugt | 1:d44a866de64f | 477 | case 2: |
Hooglugt | 1:d44a866de64f | 478 | dir2 = 1; |
Hooglugt | 1:d44a866de64f | 479 | for1 = 1; |
Hooglugt | 1:d44a866de64f | 480 | for2 = for3 = 0; |
Hooglugt | 1:d44a866de64f | 481 | break; |
Hooglugt | 1:d44a866de64f | 482 | case 3: |
Hooglugt | 1:d44a866de64f | 483 | dir3 = 1; |
Hooglugt | 1:d44a866de64f | 484 | for1 = 1; |
Hooglugt | 1:d44a866de64f | 485 | for2 = for3 = 0; |
Hooglugt | 1:d44a866de64f | 486 | break; |
Hooglugt | 1:d44a866de64f | 487 | } |
Hooglugt | 4:697d5a806cc4 | 488 | |
Hooglugt | 4:697d5a806cc4 | 489 | wait(1); // 1 sec wait, anders reset je meteen ook de biceps keuze |
Hooglugt | 4:697d5a806cc4 | 490 | goto forcechoice; |
Hooglugt | 0:99cbc87af37c | 491 | } else { |
Hooglugt | 1:d44a866de64f | 492 | if(bi_result>FACTOR*bi_max && (dir1==1||dir2==1||dir3==1)) { |
Hooglugt | 4:697d5a806cc4 | 493 | blink.detach(); |
Hooglugt | 4:697d5a806cc4 | 494 | log_timer.detach(); |
Hooglugt | 4:697d5a806cc4 | 495 | goto motorcontrol; |
Hooglugt | 0:99cbc87af37c | 496 | } else { |
Hooglugt | 0:99cbc87af37c | 497 | wait(0.01); // not sure of de wait noodzakelijk is (nu toegevoegd zodat het niet teveel strain levert op bordje) |
Hooglugt | 0:99cbc87af37c | 498 | } |
Hooglugt | 0:99cbc87af37c | 499 | } |
Hooglugt | 0:99cbc87af37c | 500 | } |
Hooglugt | 4:697d5a806cc4 | 501 | |
Hooglugt | 4:697d5a806cc4 | 502 | motorcontrol: |
Hooglugt | 4:697d5a806cc4 | 503 | |
Hooglugt | 0:99cbc87af37c | 504 | /* Vanaf hier komt de aansturing van de motor */ |
Hooglugt | 0:99cbc87af37c | 505 | |
Hooglugt | 1:d44a866de64f | 506 | switch (direction) { |
Hooglugt | 1:d44a866de64f | 507 | case 1: |
Hooglugt | 5:e5ca53305b87 | 508 | setpoint2 = -1*0.436332313+0.197222205; //25 graden + 11,3 graden, slag naar linkerdoel |
Hooglugt | 1:d44a866de64f | 509 | break; |
Hooglugt | 1:d44a866de64f | 510 | case 2: |
Hooglugt | 5:e5ca53305b87 | 511 | setpoint2 = -1*0.436332313; //25 graden vanaf nul-punt (precies midden) |
Hooglugt | 1:d44a866de64f | 512 | break; |
Hooglugt | 1:d44a866de64f | 513 | case 3: |
Hooglugt | 5:e5ca53305b87 | 514 | setpoint2 = -1*0.436332313-0.197222205; // 25 graden - 11,3 graden, slag naar rechterdoel |
Hooglugt | 1:d44a866de64f | 515 | break; |
Hooglugt | 0:99cbc87af37c | 516 | } |
Hooglugt | 1:d44a866de64f | 517 | |
Hooglugt | 1:d44a866de64f | 518 | switch (force) { |
Hooglugt | 1:d44a866de64f | 519 | case 1: |
Hooglugt | 5:e5ca53305b87 | 520 | setpoint1 = 6.8; |
Hooglugt | 1:d44a866de64f | 521 | break; |
Hooglugt | 1:d44a866de64f | 522 | case 2: |
Hooglugt | 5:e5ca53305b87 | 523 | setpoint1 = 7.4; |
Hooglugt | 1:d44a866de64f | 524 | break; |
Hooglugt | 1:d44a866de64f | 525 | case 3: |
Hooglugt | 5:e5ca53305b87 | 526 | setpoint1 = 8.0; |
Hooglugt | 1:d44a866de64f | 527 | break; |
Hooglugt | 0:99cbc87af37c | 528 | } |
Hooglugt | 0:99cbc87af37c | 529 | |
Hooglugt | 4:697d5a806cc4 | 530 | while(1) { // loop voor het goed plaatsen van motor2 (batje hoek) |
Hooglugt | 0:99cbc87af37c | 531 | while(!looptimerflag); |
Hooglugt | 0:99cbc87af37c | 532 | looptimerflag = false; //clear flag |
Hooglugt | 0:99cbc87af37c | 533 | |
Hooglugt | 0:99cbc87af37c | 534 | //regelaar motor2, bepaalt positie |
Hooglugt | 0:99cbc87af37c | 535 | controlerror = setpoint2 - motor2.getPosition()*omrekenfactor2; |
Hooglugt | 0:99cbc87af37c | 536 | integral = integral + controlerror*TSAMP; |
Hooglugt | 6:14051758db6f | 537 | derivative = (controlerror - previouserror)/TSAMP; |
Hooglugt | 6:14051758db6f | 538 | pwm = Kp2*controlerror + Ki2*integral + Kd2*derivative; |
Hooglugt | 6:14051758db6f | 539 | previouserror = controlerror; |
Hooglugt | 0:99cbc87af37c | 540 | |
Hooglugt | 0:99cbc87af37c | 541 | keep_in_range(&pwm, -1,1); |
Hooglugt | 0:99cbc87af37c | 542 | pwm_motor2.write(abs(pwm)); |
Hooglugt | 0:99cbc87af37c | 543 | if(pwm > 0) { |
Hooglugt | 0:99cbc87af37c | 544 | motor2dir = 1; |
Hooglugt | 0:99cbc87af37c | 545 | } else { |
Hooglugt | 0:99cbc87af37c | 546 | motor2dir = 0; |
Hooglugt | 0:99cbc87af37c | 547 | } |
Hooglugt | 0:99cbc87af37c | 548 | |
Hooglugt | 4:697d5a806cc4 | 549 | //controleert of batje positie heeft bepaald |
Hooglugt | 4:697d5a806cc4 | 550 | if(batjeset < 200) { // dit is nog te bepalen, op dit moment als binnen marge van 1% voor 2 seconde, dan naar volgende motorcontrol |
Hooglugt | 4:697d5a806cc4 | 551 | if (motor2.getPosition()*omrekenfactor2 > setpoint2*1.01 || motor2.getPosition()*omrekenfactor2 < setpoint2*0.99) { |
Hooglugt | 4:697d5a806cc4 | 552 | batjeset = 0; |
Hooglugt | 4:697d5a806cc4 | 553 | } else { |
Hooglugt | 4:697d5a806cc4 | 554 | batjeset++; |
Hooglugt | 4:697d5a806cc4 | 555 | } |
Hooglugt | 0:99cbc87af37c | 556 | } else { |
Hooglugt | 4:697d5a806cc4 | 557 | pwm_motor2.write(0); |
Hooglugt | 6:14051758db6f | 558 | batjeset = integral = derivative = previouserror = 0; |
Hooglugt | 4:697d5a806cc4 | 559 | wait(1); |
Hooglugt | 4:697d5a806cc4 | 560 | goto motor1control; |
Hooglugt | 0:99cbc87af37c | 561 | } |
Hooglugt | 0:99cbc87af37c | 562 | } |
Hooglugt | 4:697d5a806cc4 | 563 | |
Hooglugt | 4:697d5a806cc4 | 564 | motor1control: |
Hooglugt | 4:697d5a806cc4 | 565 | while(1) { // loop voor het slaan mbv motor1 (batje snelheid) |
Hooglugt | 0:99cbc87af37c | 566 | while(!looptimerflag); |
Hooglugt | 0:99cbc87af37c | 567 | looptimerflag = false; //clear flag |
Hooglugt | 0:99cbc87af37c | 568 | |
Hooglugt | 4:697d5a806cc4 | 569 | if (balhit == 0) { //regelaar motor1, bepaalt snelheid |
Hooglugt | 4:697d5a806cc4 | 570 | controlerror = setpoint1 - motor1.getSpeed()*omrekenfactor1; |
Hooglugt | 4:697d5a806cc4 | 571 | integral = integral + controlerror*TSAMP; |
Hooglugt | 6:14051758db6f | 572 | derivative = (controlerror - previouserror)/TSAMP; |
Hooglugt | 6:14051758db6f | 573 | pwm = Kp1*controlerror + Ki1*integral + Kd1*derivative; |
Hooglugt | 6:14051758db6f | 574 | previouserror = controlerror; |
Hooglugt | 4:697d5a806cc4 | 575 | } else { //regelaar motor1, bepaalt positie |
Hooglugt | 4:697d5a806cc4 | 576 | pwm_motor1.write(0); |
Hooglugt | 6:14051758db6f | 577 | balhit = integral = derivative = previouserror = 0; |
Hooglugt | 4:697d5a806cc4 | 578 | wait(1); // wait voordat arm weer naar beginpositie terugkeert |
Hooglugt | 4:697d5a806cc4 | 579 | goto resetpositionmotor1; |
Hooglugt | 4:697d5a806cc4 | 580 | } |
Hooglugt | 4:697d5a806cc4 | 581 | |
Hooglugt | 0:99cbc87af37c | 582 | keep_in_range(&pwm, -1,1); |
Hooglugt | 0:99cbc87af37c | 583 | pwm_motor1.write(abs(pwm)); |
Hooglugt | 0:99cbc87af37c | 584 | |
Hooglugt | 0:99cbc87af37c | 585 | if(pwm > 0) { |
Hooglugt | 0:99cbc87af37c | 586 | motor1dir = 1; |
Hooglugt | 0:99cbc87af37c | 587 | } else { |
Hooglugt | 0:99cbc87af37c | 588 | motor1dir = 0; |
Hooglugt | 0:99cbc87af37c | 589 | } |
Hooglugt | 0:99cbc87af37c | 590 | |
Hooglugt | 0:99cbc87af37c | 591 | //controleert of batje balletje heeft bereikt |
Hooglugt | 0:99cbc87af37c | 592 | //if (motor1.getSpeed()*omrekenfactor1 >= 7.5 && motor1.getPosition()*omrekenfactor1 > 1.03 && motor1.getPosition()*omrekenfactor1 < 1.07) { vrij specifieke if-statement ter controle |
Hooglugt | 0:99cbc87af37c | 593 | if (motor1.getPosition()*omrekenfactor1 > 1.10) { |
Hooglugt | 0:99cbc87af37c | 594 | balhit = 1; |
Hooglugt | 0:99cbc87af37c | 595 | } |
Hooglugt | 0:99cbc87af37c | 596 | } |
Hooglugt | 4:697d5a806cc4 | 597 | |
Hooglugt | 4:697d5a806cc4 | 598 | resetpositionmotor1: |
Hooglugt | 4:697d5a806cc4 | 599 | while(1) { // slagarm wordt weer in oorspronkelijke positie geplaatst |
Hooglugt | 0:99cbc87af37c | 600 | while(!looptimerflag); |
Hooglugt | 0:99cbc87af37c | 601 | looptimerflag = false; //clear flag |
Hooglugt | 0:99cbc87af37c | 602 | |
Hooglugt | 0:99cbc87af37c | 603 | //regelaar motor1, bepaalt positie |
Hooglugt | 0:99cbc87af37c | 604 | controlerror = -1*motor1.getPosition()*omrekenfactor1; |
Hooglugt | 0:99cbc87af37c | 605 | integral = integral + controlerror*TSAMP; |
Hooglugt | 6:14051758db6f | 606 | derivative = (controlerror - previouserror)/TSAMP; |
Hooglugt | 6:14051758db6f | 607 | pwm = Kp3*controlerror + Ki3*integral + Kd3*derivative; |
Hooglugt | 6:14051758db6f | 608 | previouserror = controlerror; |
Hooglugt | 0:99cbc87af37c | 609 | |
Hooglugt | 0:99cbc87af37c | 610 | keep_in_range(&pwm, -1,1); |
Hooglugt | 1:d44a866de64f | 611 | if(pwm > 0) { |
Hooglugt | 0:99cbc87af37c | 612 | motor1dir = 1; |
Hooglugt | 0:99cbc87af37c | 613 | } else { |
Hooglugt | 0:99cbc87af37c | 614 | motor1dir = 0; //1 = rechtsom, 0 = linksom |
Hooglugt | 0:99cbc87af37c | 615 | } |
Hooglugt | 0:99cbc87af37c | 616 | |
Hooglugt | 0:99cbc87af37c | 617 | pwm_motor1.write(abs(pwm)); |
Hooglugt | 0:99cbc87af37c | 618 | |
Hooglugt | 0:99cbc87af37c | 619 | //controleert of arm terug in positie is |
Hooglugt | 4:697d5a806cc4 | 620 | if(batjeset < 200) { |
Hooglugt | 4:697d5a806cc4 | 621 | if (motor1.getPosition()*omrekenfactor1 > 0.03 || motor1.getPosition()*omrekenfactor1 < -0.03) { |
Hooglugt | 4:697d5a806cc4 | 622 | batjeset = 0; |
Hooglugt | 4:697d5a806cc4 | 623 | } else { |
Hooglugt | 4:697d5a806cc4 | 624 | batjeset++; |
Hooglugt | 4:697d5a806cc4 | 625 | } |
Hooglugt | 0:99cbc87af37c | 626 | } else { |
Hooglugt | 4:697d5a806cc4 | 627 | pwm_motor1.write(0); |
Hooglugt | 6:14051758db6f | 628 | batjeset = integral = derivative = previouserror = 0; |
Hooglugt | 4:697d5a806cc4 | 629 | wait(1); |
Hooglugt | 4:697d5a806cc4 | 630 | goto resetpositionmotor2; |
Hooglugt | 0:99cbc87af37c | 631 | } |
Hooglugt | 0:99cbc87af37c | 632 | } |
Hooglugt | 4:697d5a806cc4 | 633 | |
Hooglugt | 4:697d5a806cc4 | 634 | resetpositionmotor2: |
Hooglugt | 4:697d5a806cc4 | 635 | while(1) { // loop voor het goed plaatsen van motor2 (batje hoek) |
Hooglugt | 0:99cbc87af37c | 636 | while(!looptimerflag); |
Hooglugt | 0:99cbc87af37c | 637 | looptimerflag = false; //clear flag |
Hooglugt | 0:99cbc87af37c | 638 | |
Hooglugt | 0:99cbc87af37c | 639 | //regelaar motor2, bepaalt positie |
Hooglugt | 0:99cbc87af37c | 640 | controlerror = -1*motor2.getPosition()*omrekenfactor2; |
Hooglugt | 0:99cbc87af37c | 641 | integral = integral + controlerror*TSAMP; |
Hooglugt | 6:14051758db6f | 642 | derivative = (controlerror - previouserror)/TSAMP; |
Hooglugt | 6:14051758db6f | 643 | pwm = Kp4*controlerror + Ki4*integral + Kd4*derivative; |
Hooglugt | 6:14051758db6f | 644 | previouserror = controlerror; |
Hooglugt | 0:99cbc87af37c | 645 | |
Hooglugt | 0:99cbc87af37c | 646 | keep_in_range(&pwm, -1,1); |
Hooglugt | 0:99cbc87af37c | 647 | |
Hooglugt | 0:99cbc87af37c | 648 | if(pwm > 0) { |
Hooglugt | 0:99cbc87af37c | 649 | motor2dir = 1; |
Hooglugt | 0:99cbc87af37c | 650 | } else { |
Hooglugt | 0:99cbc87af37c | 651 | motor2dir = 0; |
Hooglugt | 0:99cbc87af37c | 652 | } |
Hooglugt | 0:99cbc87af37c | 653 | |
Hooglugt | 0:99cbc87af37c | 654 | pwm_motor2.write(abs(pwm)); |
Hooglugt | 0:99cbc87af37c | 655 | |
Hooglugt | 0:99cbc87af37c | 656 | //controleert of batje positie heeft bepaald |
Hooglugt | 4:697d5a806cc4 | 657 | if(batjeset < 200) { // dit is nog te bepalen, op dit moment als binnen marge van 1% voor 2 seconde, dan naar volgende motorcontrol |
Hooglugt | 4:697d5a806cc4 | 658 | if (motor2.getPosition()*omrekenfactor2 > 0.03 || motor2.getPosition()*omrekenfactor2 < -0.03) { |
Hooglugt | 4:697d5a806cc4 | 659 | batjeset = 0; |
Hooglugt | 4:697d5a806cc4 | 660 | } else { |
Hooglugt | 4:697d5a806cc4 | 661 | batjeset++; |
Hooglugt | 4:697d5a806cc4 | 662 | } |
Hooglugt | 0:99cbc87af37c | 663 | } else { |
Hooglugt | 4:697d5a806cc4 | 664 | pwm_motor2.write(0); |
Hooglugt | 6:14051758db6f | 665 | batjeset = integral = derivative = previouserror = 0; |
Hooglugt | 4:697d5a806cc4 | 666 | wait(1); |
Hooglugt | 4:697d5a806cc4 | 667 | direction = force = 0; |
Hooglugt | 4:697d5a806cc4 | 668 | goto directionchoice; |
Hooglugt | 4:697d5a806cc4 | 669 | } |
Hooglugt | 0:99cbc87af37c | 670 | } |
Hooglugt | 0:99cbc87af37c | 671 | } // end main |