Thomas Burgers / Mbed 2 deprecated ZZ-TheChenneRobot

Dependencies:   Encoder HIDScope MODSERIAL QEI biquadFilter mbed

Committer:
ThomasBNL
Date:
Mon Oct 12 21:13:56 2015 +0000
Revision:
31:113f630f7e7d
Parent:
30:176ca1193a0a
Child:
32:10e6160fdbaa
corrected a mistake: changed || to && (or to and)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ThomasBNL 0:40052f5ca77b 1 #include "mbed.h"
ThomasBNL 21:c75210216204 2 #include "HIDScope.h"
ThomasBNL 0:40052f5ca77b 3 #include "QEI.h"
ThomasBNL 0:40052f5ca77b 4 #include "MODSERIAL.h"
ThomasBNL 8:50d6e2323d3b 5 #include "biquadFilter.h"
ThomasBNL 0:40052f5ca77b 6 #include "encoder.h"
ThomasBNL 0:40052f5ca77b 7
ThomasBNL 24:8ca471562212 8 MODSERIAL pc(USBTX,USBRX);
ThomasBNL 28:482ac040fb0d 9
ThomasBNL 28:482ac040fb0d 10 // (DEBUGGING AND TESTING Tools) (0 when pressed and 1 when not pressed) //
ThomasBNL 8:50d6e2323d3b 11 DigitalIn buttonL1(PTC6); // Button 1 (laag op board) for testing at the lower board
ThomasBNL 8:50d6e2323d3b 12 DigitalIn buttonL2(PTA4); // Button 2 (laag op board) for testing at the lower board
ThomasBNL 8:50d6e2323d3b 13 DigitalIn buttonH1(D2); // Button 3 (hoog op board) for testing at the top board
ThomasBNL 8:50d6e2323d3b 14 DigitalIn buttonH2(D6); // Button 4 (hoog op board) for testing at the top board
ThomasBNL 0:40052f5ca77b 15
ThomasBNL 28:482ac040fb0d 16 DigitalOut debug_led_red(LED_RED); // Debug LED
ThomasBNL 28:482ac040fb0d 17 DigitalOut debug_led_green(LED_GREEN); // Debug LED
ThomasBNL 28:482ac040fb0d 18 DigitalOut debug_led_blue(LED_BLUE); // Debug LED
ThomasBNL 8:50d6e2323d3b 19
ThomasBNL 28:482ac040fb0d 20 HIDScope scope(2); // HIDSCOPE declared
ThomasBNL 28:482ac040fb0d 21 Ticker Hidscope_measure;
ThomasBNL 28:482ac040fb0d 22
ThomasBNL 28:482ac040fb0d 23 AnalogIn potmeter1(A0); // POTMETER -> DOEN ALSOF EMG SIGNAAL
ThomasBNL 28:482ac040fb0d 24 AnalogIn potmeter2(A1); // POTMETER -> DOEN ALSOF EMG SIGNAAL
ThomasBNL 8:50d6e2323d3b 25
ThomasBNL 31:113f630f7e7d 26 double max_L_EMG = 100;
ThomasBNL 28:482ac040fb0d 27 // Defining constants //
ThomasBNL 28:482ac040fb0d 28 volatile bool send_flag;
ThomasBNL 28:482ac040fb0d 29 const double cw=0; // zero is clockwise (front view)
ThomasBNL 28:482ac040fb0d 30 const double ccw=1; // one is counterclockwise (front view)
ThomasBNL 28:482ac040fb0d 31 const double off=1; //Led off
ThomasBNL 28:482ac040fb0d 32 const double on=0; //Led on
ThomasBNL 28:482ac040fb0d 33 const double sample_time=0.005; // tijd voor een sample (200Hz)
ThomasBNL 28:482ac040fb0d 34
ThomasBNL 28:482ac040fb0d 35
ThomasBNL 28:482ac040fb0d 36 // Functions used (described after main) //
ThomasBNL 28:482ac040fb0d 37 void send(void);
ThomasBNL 28:482ac040fb0d 38 void measure_Hidscope(void);
ThomasBNL 28:482ac040fb0d 39 void deactivate_PID_Controller_strike(void);
ThomasBNL 28:482ac040fb0d 40 void activate_PID_Controller_strike(void);
ThomasBNL 28:482ac040fb0d 41
ThomasBNL 28:482ac040fb0d 42
ThomasBNL 28:482ac040fb0d 43 // MAIN function //
ThomasBNL 28:482ac040fb0d 44
ThomasBNL 0:40052f5ca77b 45 int main() {
ThomasBNL 28:482ac040fb0d 46 QEI motor_turn(D12,D13,NC,32); // Encoder for motor Turn
ThomasBNL 28:482ac040fb0d 47 PwmOut pwm_motor_strike(D5); // Pwm for motor Turn
ThomasBNL 28:482ac040fb0d 48 DigitalOut motordirection_strike(D4); // Direction of the motor Turn
ThomasBNL 27:b7caf668a682 49
ThomasBNL 28:482ac040fb0d 50 double reference_turn=0; // Set constant to store reference value of the Turn motor
ThomasBNL 28:482ac040fb0d 51 double position_turn; // Set constant to store current position of the Turn motor
ThomasBNL 28:482ac040fb0d 52 double position_strike;
ThomasBNL 30:176ca1193a0a 53 float EMG_R;
ThomasBNL 30:176ca1193a0a 54 float EMG_L;
ThomasBNL 28:482ac040fb0d 55 double n;
ThomasBNL 28:482ac040fb0d 56 debug_led_red=off;
ThomasBNL 28:482ac040fb0d 57 debug_led_blue=off;
ThomasBNL 28:482ac040fb0d 58 debug_led_green=off;
ThomasBNL 31:113f630f7e7d 59 double max_L_EMG;
ThomasBNL 31:113f630f7e7d 60 double min_L_EMG;
ThomasBNL 31:113f630f7e7d 61 double max_R_EMG;
ThomasBNL 31:113f630f7e7d 62 double min_R_EMG;
ThomasBNL 31:113f630f7e7d 63 double Hit;
ThomasBNL 28:482ac040fb0d 64
ThomasBNL 28:482ac040fb0d 65 // START OF CODE //
ThomasBNL 30:176ca1193a0a 66 pc.printf(" Start of code \n\r");
ThomasBNL 0:40052f5ca77b 67
ThomasBNL 29:263c680068db 68 pc.baud(115200); // Set the baudrate
ThomasBNL 0:40052f5ca77b 69
ThomasBNL 28:482ac040fb0d 70 // Calibratie //
ThomasBNL 31:113f630f7e7d 71 max_L_EMG = 100; // Calibreren (max average over 5 seconde?) gemeten integraal EMG over tijd / (tijdsample stappen)=100
ThomasBNL 31:113f630f7e7d 72 min_L_EMG = 0;
ThomasBNL 31:113f630f7e7d 73 max_R_EMG = 100; // Calibreren
ThomasBNL 31:113f630f7e7d 74 min_R_EMG = 0;
ThomasBNL 31:113f630f7e7d 75 Hit=60; // position when bottle is hit
ThomasBNL 28:482ac040fb0d 76
ThomasBNL 31:113f630f7e7d 77
ThomasBNL 31:113f630f7e7d 78 const float Threshold_Bicep_Left_1=((max_L_EMG-min_L_EMG)*0.2)+min_L_EMG;; //(waarde waarop het gemeten EMG signaal 20% van max het maximale is); // LEFT
ThomasBNL 31:113f630f7e7d 79 const float Threshold_Bicep_Left_2=((max_L_EMG-min_L_EMG)*0.6)+min_L_EMG; //(waarde waarop het gemeten EMG signaal 60% van max het maximale is);
ThomasBNL 31:113f630f7e7d 80 const float Threshold_Bicep_Right_1=((max_R_EMG-min_R_EMG)*0.2)+min_R_EMG; //(waarde waarop het gemeten EMG signaal 20% van max het maximale is); // RIGHT
ThomasBNL 31:113f630f7e7d 81 const float Threshold_Bicep_Right_2=((max_R_EMG-min_R_EMG)*0.6)+min_R_EMG; //(waarde waarop het gemeten EMG signaal 60% van max het maximale is);
ThomasBNL 31:113f630f7e7d 82 const float change_one_bottle=45; //(45 graden change)
ThomasBNL 30:176ca1193a0a 83
ThomasBNL 31:113f630f7e7d 84 pc.printf("left 1: %f left 2: %f right 1: %f right 2: %f \n\r", Threshold_Bicep_Left_1, Threshold_Bicep_Left_2, Threshold_Bicep_Right_1, Threshold_Bicep_Right_2);
ThomasBNL 0:40052f5ca77b 85
ThomasBNL 30:176ca1193a0a 86 // Tickers //
ThomasBNL 29:263c680068db 87 Hidscope_measure.attach(send, sample_time); // GAAT NOG NIET GOED WAARSCHIJNLIJK ALS EEN WHILE LOOP WORDT UTIGEVOERD
ThomasBNL 14:599896acf576 88
ThomasBNL 28:482ac040fb0d 89 pc.printf("wait \n\r");
ThomasBNL 28:482ac040fb0d 90 wait (3); // Wait before starting system
ThomasBNL 28:482ac040fb0d 91 pc.printf("start control \n\r");
ThomasBNL 0:40052f5ca77b 92
ThomasBNL 30:176ca1193a0a 93 //INFINITE LOOP //
ThomasBNL 5:8fb74a22fe3c 94 while(1)
ThomasBNL 28:482ac040fb0d 95 { // Start while loop (ACTION loop)
ThomasBNL 28:482ac040fb0d 96 // INPUT Filtered EMG SIGNAAL
ThomasBNL 27:b7caf668a682 97
ThomasBNL 28:482ac040fb0d 98 Nieuwe_actie: // start here again when action has finished
ThomasBNL 28:482ac040fb0d 99 debug_led_red=off;
ThomasBNL 28:482ac040fb0d 100 debug_led_blue=off;
ThomasBNL 28:482ac040fb0d 101 debug_led_green=on; // LED Turns green if ready for a new action
ThomasBNL 30:176ca1193a0a 102 wait(1);
ThomasBNL 30:176ca1193a0a 103 EMG_R = (potmeter1.read()); //EMG Right bicep (tussen nul en 100%)
ThomasBNL 30:176ca1193a0a 104 EMG_L = (potmeter2.read()); // EMG Left bicep (tussen nul en 100%)
ThomasBNL 30:176ca1193a0a 105 pc.printf("EMG_L = %f EMG_R = %f \n\r", EMG_L, EMG_R);
ThomasBNL 30:176ca1193a0a 106
ThomasBNL 0:40052f5ca77b 107
ThomasBNL 28:482ac040fb0d 108 // SLAG // (No LED -> 0.55s -> red on (turns on every time a new pwm is given) -> finish)
ThomasBNL 31:113f630f7e7d 109 if (EMG_L > Threshold_Bicep_Left_1 && EMG_R > Threshold_Bicep_Right_1)
ThomasBNL 27:b7caf668a682 110 {
ThomasBNL 28:482ac040fb0d 111
ThomasBNL 28:482ac040fb0d 112 debug_led_green=off;
ThomasBNL 28:482ac040fb0d 113 n=0;
ThomasBNL 28:482ac040fb0d 114 pc.printf("slag \n\r");
ThomasBNL 28:482ac040fb0d 115 wait(0.5);
ThomasBNL 28:482ac040fb0d 116
ThomasBNL 31:113f630f7e7d 117 while(EMG_L > Threshold_Bicep_Left_1 && EMG_R > Threshold_Bicep_Right_1) // Threshold statement still true after 0.5 seconds?
ThomasBNL 27:b7caf668a682 118 {
ThomasBNL 28:482ac040fb0d 119 if (n==0) //wordt maar 1 keer uitgevoerd
ThomasBNL 28:482ac040fb0d 120 {
ThomasBNL 28:482ac040fb0d 121 deactivate_PID_Controller_strike(); // function that sets P, I and D gain values to zero
ThomasBNL 28:482ac040fb0d 122 n=1;
ThomasBNL 28:482ac040fb0d 123 }
ThomasBNL 28:482ac040fb0d 124 debug_led_red=off;
ThomasBNL 30:176ca1193a0a 125 wait(0.10); // wait 20 samples
ThomasBNL 28:482ac040fb0d 126 debug_led_red=on;
ThomasBNL 29:263c680068db 127 pwm_motor_strike=((EMG_L-min_L_EMG)+(EMG_R-min_R_EMG))/((max_L_EMG-min_L_EMG)+(max_R_EMG-min_R_EMG))*0.7 + 0.3; // min speed 0.3 en max 1
ThomasBNL 30:176ca1193a0a 128 wait(0.10); // wait 20 samples more (pwm changes per 0.1 seconds)
ThomasBNL 28:482ac040fb0d 129 motordirection_strike=cw; // towards bottle
ThomasBNL 28:482ac040fb0d 130
ThomasBNL 28:482ac040fb0d 131 if((position_strike-Hit)<2) // bottle is hit when position-hit <2 defrees
ThomasBNL 28:482ac040fb0d 132 {
ThomasBNL 28:482ac040fb0d 133 activate_PID_Controller_strike(); // function sets back P, I and D gain values
ThomasBNL 28:482ac040fb0d 134 pc.printf("einde \n\r");
ThomasBNL 28:482ac040fb0d 135 goto Nieuwe_actie; // Finished: Get Ready for new Action control
ThomasBNL 27:b7caf668a682 136 }
ThomasBNL 27:b7caf668a682 137 }
ThomasBNL 28:482ac040fb0d 138 }
ThomasBNL 27:b7caf668a682 139
ThomasBNL 28:482ac040fb0d 140 activate_PID_Controller_strike(); // function sets back P, I and D gain values
ThomasBNL 28:482ac040fb0d 141
ThomasBNL 28:482ac040fb0d 142 debug_led_red=off; // not inside an action loop (green light)
ThomasBNL 28:482ac040fb0d 143 debug_led_blue=off;
ThomasBNL 28:482ac040fb0d 144 debug_led_green=on; // want het kan zijn dat bovenste script half wordt uitgevoerd en dan moet het slag mechanisme wel weer terug
ThomasBNL 3:11a7da46e093 145
ThomasBNL 28:482ac040fb0d 146 // DRAAI LINKS // // (blue->blue off (very short/visible?)-> red<->blue<-> red -> klaar)
ThomasBNL 31:113f630f7e7d 147 if (EMG_L > Threshold_Bicep_Left_2 && EMG_R < Threshold_Bicep_Right_1)
ThomasBNL 27:b7caf668a682 148 {
ThomasBNL 28:482ac040fb0d 149 debug_led_green=off; // Executing action
ThomasBNL 27:b7caf668a682 150 n=0;
ThomasBNL 28:482ac040fb0d 151 pc.printf("links \n\r");
ThomasBNL 31:113f630f7e7d 152 while(EMG_L > Threshold_Bicep_Left_1 && EMG_R < Threshold_Bicep_Right_1)
ThomasBNL 27:b7caf668a682 153 {
ThomasBNL 28:482ac040fb0d 154 debug_led_blue=on;
ThomasBNL 28:482ac040fb0d 155 if (n==0) //wordt maar 1 keer uitgevoerd
ThomasBNL 27:b7caf668a682 156 {
ThomasBNL 28:482ac040fb0d 157 debug_led_blue=off;
ThomasBNL 28:482ac040fb0d 158 reference_turn=reference_turn+change_one_bottle;
ThomasBNL 27:b7caf668a682 159 n=1;
ThomasBNL 28:482ac040fb0d 160 }
ThomasBNL 27:b7caf668a682 161
ThomasBNL 29:263c680068db 162 if (fabs(position_turn-reference_turn)<2) // als error en kleiner dan twee graden
ThomasBNL 27:b7caf668a682 163 {
ThomasBNL 28:482ac040fb0d 164 debug_led_blue=off;
ThomasBNL 28:482ac040fb0d 165 debug_led_red=on;
ThomasBNL 27:b7caf668a682 166 wait(0.5);
ThomasBNL 29:263c680068db 167 if (fabs(position_turn-reference_turn)<2) // Is de error na 0.5 seconde nog steeds kleiner dan twee graden?
ThomasBNL 27:b7caf668a682 168 {
ThomasBNL 28:482ac040fb0d 169 goto Nieuwe_actie; // kunt weer iets nieuws doen indien vorige actie is uitgevoerd
ThomasBNL 27:b7caf668a682 170 }
ThomasBNL 27:b7caf668a682 171 }
ThomasBNL 27:b7caf668a682 172 }
ThomasBNL 3:11a7da46e093 173 }
ThomasBNL 8:50d6e2323d3b 174
ThomasBNL 28:482ac040fb0d 175 debug_led_red=off; // not inside an action loop
ThomasBNL 28:482ac040fb0d 176 debug_led_blue=off;
ThomasBNL 28:482ac040fb0d 177 debug_led_green=on; // not executing an action
ThomasBNL 28:482ac040fb0d 178
ThomasBNL 28:482ac040fb0d 179
ThomasBNL 28:482ac040fb0d 180 // DRAAI RECHTS // (blue->blue uit (heel kort/zichtbaar?)-> red<->blue<-> red -> klaar)
ThomasBNL 31:113f630f7e7d 181 if (EMG_R > Threshold_Bicep_Right_2 && EMG_L < Threshold_Bicep_Right_1)
ThomasBNL 28:482ac040fb0d 182 {
ThomasBNL 28:482ac040fb0d 183 debug_led_green=off; // Executing action
ThomasBNL 28:482ac040fb0d 184 pc.printf("rechts \n\r");
ThomasBNL 27:b7caf668a682 185 n=0;
ThomasBNL 31:113f630f7e7d 186 while(EMG_R > Threshold_Bicep_Right_1 && EMG_L < Threshold_Bicep_Left_1)
ThomasBNL 27:b7caf668a682 187 {
ThomasBNL 28:482ac040fb0d 188 debug_led_blue=on;
ThomasBNL 27:b7caf668a682 189 if (n==0)
ThomasBNL 27:b7caf668a682 190 {
ThomasBNL 28:482ac040fb0d 191 debug_led_blue=off;
ThomasBNL 28:482ac040fb0d 192 reference_turn=reference_turn-change_one_bottle;
ThomasBNL 27:b7caf668a682 193 n=1;
ThomasBNL 28:482ac040fb0d 194 } //(45 graden naar rechts voor volgende fles) //wordt maar 1 keer uitgevoerd
ThomasBNL 27:b7caf668a682 195
ThomasBNL 29:263c680068db 196 if (abs(position_turn-reference_turn)<2) // als error en kleiner dan twee graden
ThomasBNL 27:b7caf668a682 197 {
ThomasBNL 28:482ac040fb0d 198 debug_led_blue=off;
ThomasBNL 28:482ac040fb0d 199 debug_led_red=on;
ThomasBNL 27:b7caf668a682 200 wait(0.5);
ThomasBNL 29:263c680068db 201 if (abs(position_turn-reference_turn)<2) // Is de error na 0.5 seconde nog steeds kleiner dan twee graden?
ThomasBNL 27:b7caf668a682 202 {
ThomasBNL 28:482ac040fb0d 203 goto Nieuwe_actie; // kunt weer iets nieuws doen indien vorige actie is uitgevoerd
ThomasBNL 27:b7caf668a682 204 }
ThomasBNL 27:b7caf668a682 205 }
ThomasBNL 27:b7caf668a682 206 }
ThomasBNL 27:b7caf668a682 207 }
ThomasBNL 28:482ac040fb0d 208
ThomasBNL 28:482ac040fb0d 209 debug_led_red=off; // not inside an action loop
ThomasBNL 28:482ac040fb0d 210 debug_led_blue=off;
ThomasBNL 28:482ac040fb0d 211 debug_led_green=on; // not executing an action
ThomasBNL 28:482ac040fb0d 212
ThomasBNL 28:482ac040fb0d 213
ThomasBNL 28:482ac040fb0d 214 if(send_flag)
ThomasBNL 28:482ac040fb0d 215 {
ThomasBNL 28:482ac040fb0d 216 send_flag = false;
ThomasBNL 28:482ac040fb0d 217 scope.set(0,EMG_R); // HIDSCOPE channel 0 : EMG_R
ThomasBNL 28:482ac040fb0d 218 scope.set(1,EMG_L); // HIDSCOPE channel 1 : EMG_L
ThomasBNL 28:482ac040fb0d 219 scope.set(2,reference_turn); // HIDSCOPE channel 2 : Reference_Turn
ThomasBNL 28:482ac040fb0d 220 scope.send(); // Send channel info to HIDSCOPE
ThomasBNL 28:482ac040fb0d 221 }
ThomasBNL 28:482ac040fb0d 222 } // end while loop
ThomasBNL 28:482ac040fb0d 223 }
ThomasBNL 27:b7caf668a682 224
ThomasBNL 0:40052f5ca77b 225
ThomasBNL 0:40052f5ca77b 226 // Keep in range function
ThomasBNL 0:40052f5ca77b 227 void keep_in_range(double * in, double min, double max)
ThomasBNL 0:40052f5ca77b 228 {
ThomasBNL 0:40052f5ca77b 229 *in > min ? *in < max? : *in = max: *in = min;
ThomasBNL 0:40052f5ca77b 230 }
ThomasBNL 0:40052f5ca77b 231
ThomasBNL 0:40052f5ca77b 232 // Looptimerflag function
ThomasBNL 28:482ac040fb0d 233 void send(void)
ThomasBNL 0:40052f5ca77b 234 {
ThomasBNL 28:482ac040fb0d 235 send_flag = true;
ThomasBNL 1:dc683e88b44e 236 }
ThomasBNL 1:dc683e88b44e 237
ThomasBNL 28:482ac040fb0d 238 // Deactivate PID Controller strike
ThomasBNL 28:482ac040fb0d 239 void deactivate_PID_Controller_strike(void)
ThomasBNL 28:482ac040fb0d 240 {
ThomasBNL 28:482ac040fb0d 241 double P_gain_strike=0;
ThomasBNL 28:482ac040fb0d 242 double I_gain_strike=0;
ThomasBNL 28:482ac040fb0d 243 double D_gain_strike=0;
ThomasBNL 28:482ac040fb0d 244 }
ThomasBNL 28:482ac040fb0d 245
ThomasBNL 28:482ac040fb0d 246 // Activate PID Controller strike
ThomasBNL 28:482ac040fb0d 247 void activate_PID_Controller_strike(void)
ThomasBNL 28:482ac040fb0d 248 {
ThomasBNL 28:482ac040fb0d 249 double P_gain_strike=10;
ThomasBNL 28:482ac040fb0d 250 double I_gain_strike=0.1;
ThomasBNL 28:482ac040fb0d 251 double D_gain_strike=50;
ThomasBNL 28:482ac040fb0d 252 }