Thomas Burgers / Mbed 2 deprecated ZZ-TheChenneRobot

Dependencies:   Encoder HIDScope MODSERIAL QEI biquadFilter mbed

Committer:
ThomasBNL
Date:
Sat Oct 10 22:00:24 2015 +0000
Revision:
29:263c680068db
Parent:
28:482ac040fb0d
Child:
30:176ca1193a0a
changed EMG threshold and removed some unused variables

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