Aukie Hooglugt / Mbed 2 deprecated PROJECT_incl_regelaar3

Dependencies:   Encoder HIDScope MODSERIAL- mbed-dsp mbed

Fork of PROJECT_incl_regelaar2 by Aukie Hooglugt

Committer:
Hooglugt
Date:
Mon Nov 03 18:52:57 2014 +0000
Revision:
8:75980dc35763
Parent:
7:ca1ade91bd14
Child:
9:0bc7f83b761e
switch state, 2 regelaars in 1 while (arvid)

Who changed what in which revision?

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