Thomas Burgers / Mbed 2 deprecated ZZ-TheChenneRobot

Dependencies:   Encoder HIDScope MODSERIAL QEI biquadFilter mbed

Committer:
ThomasBNL
Date:
Tue Oct 20 20:01:10 2015 +0000
Revision:
67:65750d716788
Parent:
66:04a203e43510
Child:
68:b262b349c902
put calibration motor inside a function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ThomasBNL 46:9279c7a725bf 1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 2 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 3 ///// //// ////// ////// /////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 4 ///// //// ////// ////// /////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 5 //////////// //////////// ////// ////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 6 //////////// //////////// ////// ////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 7 //////////// //////////// ////// /////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 8 //////////// //////////// ////// /////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 9 //////////// //////////// ////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 10 //////////// //////////// ////// ////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 11 //////////// //////////// ////// ////// /////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 12 //////////// //////////// ////// ////// /////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 13 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 14 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 15 /////// ////// ////// ////// ////// /////// ////// /////// ////// /////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 16 /////// ////// ////// ////// ////// ///// ////// ///// ////// /////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 17 /////// ///////////////// ////// ////// //////////// //// ////// //// ////// ///////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 18 /////// ///////////////// ////// ////// //////////// /// ////// /// ////// ///////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 19 /////// ///////////////// ////// ////// // ////// // ////// /////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 20 /////// ///////////////// ////// ////// // ////// // ////// /////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 21 /////// ///////////////// ////// //////////// /// ////// /// ////// ///////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 22 /////// ///////////////// ////// ////// //////////// //// ////// //// ////// ///////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 23 /////// ////// ////// ////// ////// ///// ////// ///// ////// /////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 24 /////// ////// ////// ////// ////// ////// ////// ////// ////// /////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 25 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 26 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 27 /////// /////////////// ///////////// /////// ////// /////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 28 /////// ///////////// //////////// ///// ////// ////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 29 /////// ////// /////////// /////////// //// ////// ///// ///////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 30 /////// ////// ////////// // ////////// /// ////// ///// ///////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 31 /////// ///////// //// ///////// // ////// ///// ///////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 32 /////// //////// ////// //////// // ////// ///// ///////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 33 /////// ////// /////// /////// /// ////// ///// ///////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 34 /////// ////// ////// ////// //// ////// ///// ///////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 35 /////// ////// //////////// ///// ///// ////// ////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 36 /////// ////// ////////////// //// ////// ////// /////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 37 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 38 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 46:9279c7a725bf 39 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////|
ThomasBNL 47:c61873a0b646 40 // Groep 12: The Chenne Band //
ThomasBNL 47:c61873a0b646 41 // ___________________________
ThomasBNL 47:c61873a0b646 42 // // \\
ThomasBNL 47:c61873a0b646 43 // || [Table of content] ||
ThomasBNL 47:c61873a0b646 44 // \\___________________________//
ThomasBNL 47:c61873a0b646 45 //
ThomasBNL 47:c61873a0b646 46 // Introduction
ThomasBNL 47:c61873a0b646 47
ThomasBNL 47:c61873a0b646 48 // Libraries.................................................
ThomasBNL 47:c61873a0b646 49 // Flow and debugging tools .................................
ThomasBNL 47:c61873a0b646 50 // LEDS.................................................
ThomasBNL 47:c61873a0b646 51 // Buttons..............................................
ThomasBNL 47:c61873a0b646 52 // Potmeter.............................................
ThomasBNL 47:c61873a0b646 53 // Flow.................................................
ThomasBNL 47:c61873a0b646 54 // HIDscope.............................................
ThomasBNL 47:c61873a0b646 55 // EMG.......................................................
ThomasBNL 47:c61873a0b646 56 // EMG variables........................................
ThomasBNL 47:c61873a0b646 57 // EMG filter...........................................
ThomasBNL 47:c61873a0b646 58 // motors.....................................................
ThomasBNL 47:c61873a0b646 59 // motor turn...........................................
ThomasBNL 47:c61873a0b646 60 // motor strike.........................................
ThomasBNL 47:c61873a0b646 61 // d-action filter......................................
ThomasBNL 47:c61873a0b646 62 // system constants...........................................
ThomasBNL 47:c61873a0b646 63 // functions used.............................................
ThomasBNL 47:c61873a0b646 64 // motor function.......................................
ThomasBNL 47:c61873a0b646 65 // EMG functions........................................
ThomasBNL 47:c61873a0b646 66 // action controller....................................
ThomasBNL 47:c61873a0b646 67
ThomasBNL 47:c61873a0b646 68 // Main
ThomasBNL 47:c61873a0b646 69
ThomasBNL 47:c61873a0b646 70 // Start of code..............................................
ThomasBNL 48:950b1f34161b 71 // calibrate..................................................
ThomasBNL 48:950b1f34161b 72 // starting position motor.............................. (RED LED)
ThomasBNL 48:950b1f34161b 73 // EMG calibration...................................... (BLUE LED)
ThomasBNL 54:9eb449571f4f 74 // Attach Ticker.............................................. (BLUE LED)
ThomasBNL 47:c61873a0b646 75 // Start infinite loop........................................
ThomasBNL 54:9eb449571f4f 76 // Debug buttons........................................ (GREEN LED)
ThomasBNL 54:9eb449571f4f 77 // Wait for go signal................................... (GREEN LED)
ThomasBNL 54:9eb449571f4f 78 // Limit position value and convert to degrees..........
ThomasBNL 47:c61873a0b646 79 // Get current EMG values...............................
ThomasBNL 47:c61873a0b646 80 // Action controller....................................
ThomasBNL 47:c61873a0b646 81 // Strike.........................................
ThomasBNL 47:c61873a0b646 82 // Turn left......................................
ThomasBNL 47:c61873a0b646 83 // Turn right.....................................
ThomasBNL 47:c61873a0b646 84 // PID controller.......................................
ThomasBNL 47:c61873a0b646 85 // PID error -> pwm motor...............................
ThomasBNL 47:c61873a0b646 86 // pwm -> plant.........................................
ThomasBNL 47:c61873a0b646 87 // HIDscope.............................................
ThomasBNL 47:c61873a0b646 88 // Deactivate if reached position.......................
ThomasBNL 47:c61873a0b646 89
ThomasBNL 47:c61873a0b646 90 // Functions described
ThomasBNL 47:c61873a0b646 91
ThomasBNL 47:c61873a0b646 92 // Motor Functions............................................
ThomasBNL 47:c61873a0b646 93 // EMG functions..............................................
ThomasBNL 47:c61873a0b646 94 // Action controller functions................................
ThomasBNL 47:c61873a0b646 95
ThomasBNL 47:c61873a0b646 96 //
ThomasBNL 47:c61873a0b646 97 //
ThomasBNL 47:c61873a0b646 98 //
ThomasBNL 47:c61873a0b646 99 //
ThomasBNL 47:c61873a0b646 100 //
ThomasBNL 47:c61873a0b646 101
ThomasBNL 47:c61873a0b646 102
ThomasBNL 46:9279c7a725bf 103 //============================================================================================================================
ThomasBNL 43:fb69ef657f30 104 // ___________________________
ThomasBNL 43:fb69ef657f30 105 // // \\
ThomasBNL 43:fb69ef657f30 106 // || [Libraries] ||
ThomasBNL 43:fb69ef657f30 107 // \\___________________________//
ThomasBNL 43:fb69ef657f30 108 //
ThomasBNL 43:fb69ef657f30 109
ThomasBNL 0:40052f5ca77b 110 #include "mbed.h"
ThomasBNL 21:c75210216204 111 #include "HIDScope.h"
ThomasBNL 0:40052f5ca77b 112 #include "QEI.h"
ThomasBNL 0:40052f5ca77b 113 #include "MODSERIAL.h"
ThomasBNL 8:50d6e2323d3b 114 #include "biquadFilter.h"
ThomasBNL 0:40052f5ca77b 115 #include "encoder.h"
ThomasBNL 0:40052f5ca77b 116
ThomasBNL 46:9279c7a725bf 117 //============================================================================================================================
ThomasBNL 34:c672f5c0763f 118 // ___________________________
ThomasBNL 34:c672f5c0763f 119 // // \\
ThomasBNL 46:9279c7a725bf 120 // || [FLOW AND DEBUGGING TOOLS] ||
ThomasBNL 34:c672f5c0763f 121 // \\___________________________//
ThomasBNL 66:04a203e43510 122
ThomasBNL 67:65750d716788 123 //HIDScope scope(1); // DEBUG : HIDSCOPE has the ability to display signals over time and can be used to monitor signals
ThomasBNL 46:9279c7a725bf 124
ThomasBNL 67:65750d716788 125 MODSERIAL pc(USBTX,USBRX); // MODSERIAL : makes it possible to send messages to the computer (eg. inside Putty)
ThomasBNL 34:c672f5c0763f 126
ThomasBNL 67:65750d716788 127 DigitalOut debug_led_red(LED_RED) , debug_led_green(LED_GREEN) , debug_led_blue(LED_BLUE); // DEBUG : Red, Blue and Green LED
ThomasBNL 66:04a203e43510 128 DigitalIn buttonL1(PTC6) , buttonL2(PTA4) , buttonH1(D2) , buttonH2(D1); // DEBUG/CALIBRATION: 4 buttons for calibration and debugging
ThomasBNL 67:65750d716788 129 Ticker looptimer; // FLOW : Ticker called looptimer to set a looptimerflag that puts the volatile bool control_go to true every sample
ThomasBNL 46:9279c7a725bf 130
ThomasBNL 67:65750d716788 131 volatile bool control_go = false, sample = false, sample_error, sample_error_strike; // EMG : booleans for controlling sample time of moving average and emg signal
ThomasBNL 67:65750d716788 132 volatile bool looptimerflag; // CONTROLLER : boolean that controls the sample time of the whole controller
ThomasBNL 66:04a203e43510 133
ThomasBNL 67:65750d716788 134 double e30, e29, e28, e27, e26, e25, e24, e23, e22, e21, e20, // ACTION : in the action mechanism these variables calculate a current moving average error
ThomasBNL 67:65750d716788 135 e19, e18, e17, e16, e15, e14, e13, e12, e11, e10, e9,
ThomasBNL 67:65750d716788 136 e8, e7, e6, e5, e4, e3, e2, e1, er, error_count, error_turn_average, error_strike_average;
ThomasBNL 46:9279c7a725bf 137
ThomasBNL 67:65750d716788 138 AnalogIn input1(A0), input2(A1); // EMG : Two AnalogIn EMG inputs, input1 (Left bicep), input2 (Right bicep)
ThomasBNL 44:5dd0a3d24662 139
ThomasBNL 67:65750d716788 140 double Sample_EMG_L_1, Sample_EMG_L_2, Sample_EMG_L_3, Sample_EMG_L_4, Sample_EMG_L_5, Sample_EMG_L_6,// EMG : Left/Right bicep moving average memory variables (moving average is calculated over ten values)
ThomasBNL 66:04a203e43510 141 Sample_EMG_L_7, Sample_EMG_L_8, Sample_EMG_L_9, Sample_EMG_L_10, moving_average_left;
ThomasBNL 66:04a203e43510 142 double Sample_EMG_R_1, Sample_EMG_R_2, Sample_EMG_R_3, Sample_EMG_R_4, Sample_EMG_R_5, Sample_EMG_R_6,
ThomasBNL 66:04a203e43510 143 Sample_EMG_R_7, Sample_EMG_R_8, Sample_EMG_R_9, Sample_EMG_R_10, moving_average_right;
ThomasBNL 46:9279c7a725bf 144
ThomasBNL 66:04a203e43510 145 double minimum_L, maximum_L, EMG_L_min, EMG_L_max; // EMG CALIBRATION: variables that are used during the EMG calibration
ThomasBNL 66:04a203e43510 146 double minimum_R, maximum_R, EMG_R_min, EMG_R_max;
ThomasBNL 66:04a203e43510 147
ThomasBNL 67:65750d716788 148 double EMG_left_Bicep, EMG_Left_Bicep_filtered,
ThomasBNL 67:65750d716788 149 EMG_Left_Bicep_filtered_notch_1, EMG_Right_Bicep_filtered_notch_1;
ThomasBNL 67:65750d716788 150 double EMG_Right_Bicep, EMG_Left_Bicep_filtered_notch_2,
ThomasBNL 67:65750d716788 151 EMG_Right_Bicep_filtered_notch_2, EMG_Right_Bicep_filtered;
ThomasBNL 46:9279c7a725bf 152
ThomasBNL 67:65750d716788 153 double Threshold_Bicep_Left_1, Threshold_Bicep_Left_2, // EMG ACTION: variables to witch the threshold values calculated after the calibration get asigned to
ThomasBNL 67:65750d716788 154 Threshold_Bicep_Right_1, Threshold_Bicep_Right_2;
ThomasBNL 67:65750d716788 155
ThomasBNL 67:65750d716788 156 double n=0; double c=0; double k=0; double p=0; // FLOW : these flow variables are assigned to certain values through out the script values in order to regulate the flow of the script
ThomasBNL 54:9eb449571f4f 157
ThomasBNL 66:04a203e43510 158 // FILTERS EMG
ThomasBNL 67:65750d716788 159 const double low_b0 = 0.05892937945281792 , low_b1 = 0.11785875890563584 , low_b2 = 0.05892937945281792, // Notch 1 LOW : VIA online biquad calculator Lowpass 520-48-0.7071-6
ThomasBNL 66:04a203e43510 160 low_a1 = -1.205716572226748 , low_a2 = 0.44143409003801976 ;
ThomasBNL 66:04a203e43510 161 const double high_b0 = 0.6389437261127494 , high_b1 = -1.2778874522254988 , high_b2 = 0.6389437261127494, // Notch 2 HIGH: VIA online biquad calculator Highpass 520-52-0.7071-6
ThomasBNL 66:04a203e43510 162 high_a1 = -1.1429772843080923 , high_a2 = 0.41279762014290533 ;
ThomasBNL 67:65750d716788 163 const double high_b0_HP = 0.84855234544818812 , high_b1_HP = -1.6970469089637623 , high_b2_HP = 0.8485234544818812, // HIGHPASS : NOG OPZOEKEN!! : >25Hz? sample rate 512Hz
ThomasBNL 66:04a203e43510 164 high_a1_HP = -1.6564788473046559 , high_a2_HP = 0.7376149706228688 ;
ThomasBNL 67:65750d716788 165 const double low_b0_LP = 0.0013067079602315681, low_b1_LP = 0.0026134159204631363, low_b2_LP = 0.0013067079602315681, // LOWPASS : NOG OPZOEKEN!! <5-10 Hz? sample rate 512Hz
ThomasBNL 66:04a203e43510 166 low_a1_LP = -1.9238184798980429 , low_a2_LP = 0.9290453117389691 ;
ThomasBNL 46:9279c7a725bf 167
ThomasBNL 66:04a203e43510 168 //Left bicep Filters
ThomasBNL 67:65750d716788 169 biquadFilter highpassfilter_1(high_a1_HP, high_a2_HP, high_b0_HP, high_b1_HP, high_b2_HP); // EMG : moeten nog waardes voor gemaakt worden? (>25Hz doorlaten)
ThomasBNL 67:65750d716788 170 biquadFilter notchL1(high_a1, high_a2, high_b0, high_b1, high_b2); // EMG : moeten nog waardes voor gemaakt worden (>52Hz doorlaten)
ThomasBNL 67:65750d716788 171 biquadFilter notchL2(low_a1, low_a2, low_b0, low_b1, low_b2); // EMG : moeten nog waardes voor gemaakt worden (<48Hz doorlaten)
ThomasBNL 66:04a203e43510 172 biquadFilter lowpassfilter_1(low_a1_LP, low_a2_LP, low_b0_LP, low_b1_LP, low_b2_LP);
ThomasBNL 46:9279c7a725bf 173
ThomasBNL 66:04a203e43510 174 // Right bicep Filters
ThomasBNL 67:65750d716788 175 biquadFilter highpassfilter_2(high_a1_HP, high_a2_HP, high_b0_HP, high_b1_HP, high_b2_HP); // EMG : moeten nog waardes voor gemaakt worden?
ThomasBNL 67:65750d716788 176 biquadFilter notchR1(high_a1, high_a2, high_b0, high_b1, high_b2); // EMG : moeten nog waardes voor gemaakt worden
ThomasBNL 67:65750d716788 177 biquadFilter notchR2(low_a1, low_a2, low_b0, low_b1, low_b2); // EMG : moeten nog waardes voor gemaakt worden
ThomasBNL 67:65750d716788 178 biquadFilter lowpassfilter_2(low_a1_LP, low_a2_LP, low_b0_LP, low_b1_LP, low_b2_LP); // EMG : moeten nog waardes voor gemaakt worden
ThomasBNL 46:9279c7a725bf 179
ThomasBNL 46:9279c7a725bf 180
ThomasBNL 66:04a203e43510 181 // MOTORS
ThomasBNL 67:65750d716788 182 QEI motor_turn(D12,D13,NC,32); QEI motor_strike(D9,D10,NC,32); // TURN - STRIKE : Encoder for motor
ThomasBNL 67:65750d716788 183 PwmOut pwm_motor_turn(D5); PwmOut pwm_motor_strike(D6); // TURN - STRIKE : Pwm for motor
ThomasBNL 67:65750d716788 184 DigitalOut motordirection_turn(D4); DigitalOut motordirection_strike(D7); // TURN - STRIKE : Direction of the motor
ThomasBNL 66:04a203e43510 185
ThomasBNL 67:65750d716788 186 double integrate_error_turn=0, previous_error_turn=0; double integrate_error_strike=0, previous_error_strike=0; // TURN - STRIKE : previous error and integration error motor
ThomasBNL 66:04a203e43510 187
ThomasBNL 66:04a203e43510 188 double position_turn, error_turn, reference_turn; double position_strike, error_strike, reference_strike;
ThomasBNL 67:65750d716788 189 double P_gain_turn; double I_gain_turn; double D_gain_turn; double P_gain_strike; double I_gain_strike; double D_gain_strike; // TURN - STRIKE : these gains get multiplied with the error inside the PID controller // TURN: these gains get multiplied with the error inside the PID controller
ThomasBNL 67:65750d716788 190 double pwm_motor_turn_P, pwm_motor_turn_I, pwm_motor_turn_D; double pwm_motor_strike_P, pwm_motor_strike_I, pwm_motor_strike_D; // TURN - STRIKE : variables that store the P, I and D action part of the error
ThomasBNL 67:65750d716788 191 double pwm_to_motor_turn; double pwm_to_motor_strike; // TURN - STRIKE : variable that is constructed by summing the values of the P, I and D action
ThomasBNL 46:9279c7a725bf 192
ThomasBNL 46:9279c7a725bf 193
ThomasBNL 66:04a203e43510 194 // FILTER: D-action
ThomasBNL 66:04a203e43510 195 const double a0 = 0.000332685098529822, a1 = 0.000665370197059644, a2 = 0.000332685098529822, b1 = -1.9625271814290315, b2 = 0.9638579218231508;
ThomasBNL 66:04a203e43510 196 // const double a0 = 0.000021080713160785432, a1 = 0.000042161426321570865, a2 = 0.000021080713160785432, b1 = -1.990754082898736, b2 = 0.9908384057513788; (0.75Hz)
ThomasBNL 66:04a203e43510 197 // const double a0 = 0.003543360146633312, a1 = 0.007086720293266624, a2 = 0.003543360146633312, b1 = -1.8704759567901301, b2 = 0.8846493973766635; (10Hz)
ThomasBNL 66:04a203e43510 198 // const double a0 = 0.0009129521023626334, a1 = 0.0018259042047252668, a2 = 0.0009129521023626334, b1 = -1.9368516414202819, b2 = 0.9405034498297324; (5Hz)
ThomasBNL 64:97e2db3eb0eb 199 biquadFilter Lowpassfilter_derivative(b1,b2,a0,a1,a2); // BiquadFilter used for the filtering of the Derivative action of the PID-action
ThomasBNL 46:9279c7a725bf 200
ThomasBNL 66:04a203e43510 201 const double cw=0; // MOTOR: turn direction zero is clockwise (front view)
ThomasBNL 66:04a203e43510 202 const double ccw=1; // MOTOR: turn direction one is counterclockwise (front view)
ThomasBNL 43:fb69ef657f30 203 const double off=1; // Led off
ThomasBNL 43:fb69ef657f30 204 const double on=0; // Led on
ThomasBNL 43:fb69ef657f30 205 const int Fs = 512; // sampling frequency (512 Hz)
ThomasBNL 43:fb69ef657f30 206 const double sample_time = 0.001953125; // duration of one sample
ThomasBNL 44:5dd0a3d24662 207 double conversion_counts_to_degrees=0.085877862594198; // Calculation conversion counts to degrees
ThomasBNL 44:5dd0a3d24662 208 // gear ratio motor = 131
ThomasBNL 44:5dd0a3d24662 209 // ticks per magnet rotation = 32 (X2 Encoder)
ThomasBNL 44:5dd0a3d24662 210 // One revolution = 360 degrees
ThomasBNL 44:5dd0a3d24662 211 // degrees_per_encoder_tick = 360/(gear_ratio*ticks_per_magnet_rotation)=360/131*32=0.085877862594198
ThomasBNL 46:9279c7a725bf 212 const double change_one_bottle=45;
ThomasBNL 66:04a203e43510 213 const double Hit=60; // position when bottle is hit
ThomasBNL 55:f4ab878ae910 214
ThomasBNL 66:04a203e43510 215
ThomasBNL 46:9279c7a725bf 216 //============================================================================================================================
ThomasBNL 34:c672f5c0763f 217 // ___________________________
ThomasBNL 34:c672f5c0763f 218 // // \\
ThomasBNL 34:c672f5c0763f 219 // || [FUNCTIONS USED] ||
ThomasBNL 34:c672f5c0763f 220 // \\___________________________//
ThomasBNL 67:65750d716788 221 void execute_plant_turn (); // TURN : Check error -> execute PID controller -> write pwm and direction to motor
ThomasBNL 66:04a203e43510 222 void execute_plant_strike ();
ThomasBNL 54:9eb449571f4f 223 double PID_control (double reference, double position, double &integrate_error,
ThomasBNL 54:9eb449571f4f 224 double sample_time, double &previous_error,
ThomasBNL 54:9eb449571f4f 225 double P_gain, double I_gain, double D_gain);
ThomasBNL 66:04a203e43510 226
ThomasBNL 46:9279c7a725bf 227 void keep_in_range (double * in, double min, double max); // Put in a value and makes sure that the value stays between assigned boundary's
ThomasBNL 46:9279c7a725bf 228 void setlooptimerflag (void); // Sets looptimerflag volatile bool to true
ThomasBNL 8:50d6e2323d3b 229
ThomasBNL 46:9279c7a725bf 230 void deactivate_PID_Controller (double& P_gain, double& I_gain, double& D_gain); // STRIKE/TURN: Deactivate PID Controller
ThomasBNL 46:9279c7a725bf 231
ThomasBNL 46:9279c7a725bf 232 void activate_PID_Controller_strike (double& P_gain, double& I_gain, double& D_gain); // STRIKE: Activate PID Controller
ThomasBNL 67:65750d716788 233 void activate_PID_Controller_turn (double& P_gain, double& I_gain, double& D_gain); // TURN : Activate PID Controller
ThomasBNL 46:9279c7a725bf 234
ThomasBNL 67:65750d716788 235 void Change_Turn_Position_Left (double& reference, double change_one_bottle); // TURN : Change Reference position one bottle to the left
ThomasBNL 67:65750d716788 236 void Change_Turn_Position_Right (double& reference, double change_one_bottle); // TURN : Change Reference position one bottle to the right
ThomasBNL 39:104a038f7b92 237
ThomasBNL 67:65750d716788 238 void countdown_from_5(); // PUTTY : 5 second countdown inside
ThomasBNL 67:65750d716788 239 void calibrate_motor(); // MOTOR : Calibrate starting position motor
ThomasBNL 67:65750d716788 240 void calibration(); // EMG : Calibrate the EMG signal (calculate min and max signal and determine threshold values)
ThomasBNL 67:65750d716788 241 void Filter(); // EMG : Filter the incoming EMG signals
ThomasBNL 67:65750d716788 242 void sample_filter(); // EMG : Calculate moving average (10 samples, one sample per 25 samples) using sample_filter => moving average over +-0.5 seconds
ThomasBNL 67:65750d716788 243 void take_sample(); // EMG : Take a sample once every 25 samples that's used to calculate the moving average
ThomasBNL 67:65750d716788 244 void ControlGo(); // EMG : function that gets a ticker attached and sets a certain loop to true every sample
ThomasBNL 66:04a203e43510 245
ThomasBNL 66:04a203e43510 246 void red();void blue();void green();void white();void yellow();void cyan();void purple(); void black(); // DEBUG: Different color LEDS
ThomasBNL 39:104a038f7b92 247
ThomasBNL 67:65750d716788 248 void calibrate_potmeter(); // DEBUG/TEST: Calibration thresholds with potmeter
ThomasBNL 46:9279c7a725bf 249 //============================================================================================================================
ThomasBNL 46:9279c7a725bf 250 ///////////////////////////////
ThomasBNL 46:9279c7a725bf 251 // //
ThomasBNL 46:9279c7a725bf 252 ///////////////////////////////////////////// [MAIN FUNCTION] /////////////////////////////////////////////////////
ThomasBNL 46:9279c7a725bf 253 // //
ThomasBNL 46:9279c7a725bf 254 ///////////////////////////////
ThomasBNL 46:9279c7a725bf 255 //============================================================================================================================
ThomasBNL 0:40052f5ca77b 256 int main() {
ThomasBNL 43:fb69ef657f30 257
ThomasBNL 67:65750d716788 258 black(); // No LED active
ThomasBNL 43:fb69ef657f30 259 pc.printf("Start of code \n\r");
ThomasBNL 43:fb69ef657f30 260
ThomasBNL 67:65750d716788 261 pc.baud(115200); // PC contactspeed : Set the baudrate
ThomasBNL 67:65750d716788 262
ThomasBNL 67:65750d716788 263 red();
ThomasBNL 67:65750d716788 264 calibrate_motor(); // MOTOR CALIBRATE : The motor starting position (RED LED)
ThomasBNL 67:65750d716788 265
ThomasBNL 67:65750d716788 266 //blue();
ThomasBNL 67:65750d716788 267 //calibrate(); // EMG CALIBRATE : The motor starting position (BLUE LED)
ThomasBNL 0:40052f5ca77b 268
ThomasBNL 67:65750d716788 269 calibrate_potmeter(); // DEBUG/TEST : Calibration thresholds with potmeter
ThomasBNL 67:65750d716788 270
ThomasBNL 67:65750d716788 271 looptimer.attach(setlooptimerflag,(float)1/Fs); // CONTROLLER FLOW : Calls the looptimer flag every sample
ThomasBNL 40:bbe7922723df 272
ThomasBNL 67:65750d716788 273 black();
ThomasBNL 67:65750d716788 274 wait (3); // REST before starting position
ThomasBNL 46:9279c7a725bf 275
ThomasBNL 67:65750d716788 276 green(); // START CONTROLLOOP (GREEN LED)
ThomasBNL 67:65750d716788 277
ThomasBNL 67:65750d716788 278
ThomasBNL 5:8fb74a22fe3c 279 while(1)
ThomasBNL 64:97e2db3eb0eb 280 { // Start while loop
ThomasBNL 64:97e2db3eb0eb 281 while(looptimerflag != true);
ThomasBNL 64:97e2db3eb0eb 282 looptimerflag = false;
ThomasBNL 64:97e2db3eb0eb 283 green();
ThomasBNL 64:97e2db3eb0eb 284 execute_plant_turn();
ThomasBNL 64:97e2db3eb0eb 285
ThomasBNL 67:65750d716788 286 Nieuwe_actie:
ThomasBNL 46:9279c7a725bf 287
ThomasBNL 46:9279c7a725bf 288 // ___________________________
ThomasBNL 46:9279c7a725bf 289 // // \\
ThomasBNL 46:9279c7a725bf 290 // || [GET CURRENT EMG VALUES] ||
ThomasBNL 46:9279c7a725bf 291 // \\___________________________//
ThomasBNL 64:97e2db3eb0eb 292
ThomasBNL 64:97e2db3eb0eb 293 ////// //sample_filter(); // TIJDELIJK UIT
ThomasBNL 61:5b644df6f6ab 294
ThomasBNL 64:97e2db3eb0eb 295 moving_average_left = (input1.read())*100; // EMG Right bicep (tussen nul en 100%) // DEBUG TEST TOOL: substitute EMG input for potmeter inputs
ThomasBNL 64:97e2db3eb0eb 296 moving_average_right = (input2.read())*100; // EMG Left bicep (tussen nul en 100%) // DEBUG TEST TOOL: substitute EMG input for potmeter inputs
ThomasBNL 53:05194bab0bfd 297 pc.printf("mov_r = %f, mov_l = %f \n\r", moving_average_right, moving_average_left);
ThomasBNL 60:084fd3352882 298 /// ___________________________
ThomasBNL 60:084fd3352882 299 // // \\
ThomasBNL 64:97e2db3eb0eb 300 // || [Action 1: Strike] ||
ThomasBNL 60:084fd3352882 301 // \\___________________________//
ThomasBNL 64:97e2db3eb0eb 302 // // blue \\
ThomasBNL 60:084fd3352882 303
ThomasBNL 65:2da8cf778181 304 moving_average_left = 40; // TIJDELIJK PID TEST
ThomasBNL 65:2da8cf778181 305 moving_average_right = 40; // TIJDELIJK PID TEST
ThomasBNL 60:084fd3352882 306 if (moving_average_right > Threshold_Bicep_Right_1 && moving_average_left > Threshold_Bicep_Left_1)
ThomasBNL 60:084fd3352882 307 {
ThomasBNL 60:084fd3352882 308 blue();
ThomasBNL 64:97e2db3eb0eb 309 n=0; k=0; p=0;
ThomasBNL 60:084fd3352882 310 pc.printf("Slag \n\r");
ThomasBNL 61:5b644df6f6ab 311
ThomasBNL 61:5b644df6f6ab 312 wait(0.5); // TIJDELIJK
ThomasBNL 61:5b644df6f6ab 313
ThomasBNL 64:97e2db3eb0eb 314 if(moving_average_left > Threshold_Bicep_Left_1 && moving_average_right > Threshold_Bicep_Right_1)
ThomasBNL 64:97e2db3eb0eb 315 {
ThomasBNL 64:97e2db3eb0eb 316 while(1)
ThomasBNL 60:084fd3352882 317 {
ThomasBNL 64:97e2db3eb0eb 318 if (n==0)
ThomasBNL 61:5b644df6f6ab 319 {
ThomasBNL 64:97e2db3eb0eb 320 reference_strike=90;
ThomasBNL 61:5b644df6f6ab 321 n=1;
ThomasBNL 61:5b644df6f6ab 322 error_count=0;
ThomasBNL 64:97e2db3eb0eb 323 }
ThomasBNL 63:d86a46c8aa0c 324
ThomasBNL 60:084fd3352882 325 if (looptimerflag == true)
ThomasBNL 60:084fd3352882 326 {
ThomasBNL 60:084fd3352882 327 looptimerflag=false;
ThomasBNL 64:97e2db3eb0eb 328 activate_PID_Controller_strike(P_gain_strike, I_gain_strike, D_gain_strike);
ThomasBNL 63:d86a46c8aa0c 329 execute_plant_strike();
ThomasBNL 60:084fd3352882 330 }
ThomasBNL 61:5b644df6f6ab 331
ThomasBNL 64:97e2db3eb0eb 332 if (fabs(position_strike)>90)
ThomasBNL 61:5b644df6f6ab 333 {
ThomasBNL 61:5b644df6f6ab 334 while(1)
ThomasBNL 64:97e2db3eb0eb 335 { yellow();
ThomasBNL 61:5b644df6f6ab 336 if(k==0)
ThomasBNL 61:5b644df6f6ab 337 {
ThomasBNL 64:97e2db3eb0eb 338 p=1;
ThomasBNL 64:97e2db3eb0eb 339 reference_strike=0; error_count=0; k=1;
ThomasBNL 64:97e2db3eb0eb 340 pc.printf("return \n\r");
ThomasBNL 61:5b644df6f6ab 341 }
ThomasBNL 64:97e2db3eb0eb 342
ThomasBNL 64:97e2db3eb0eb 343 //pc.printf("ref_t = %f, e_cnt= %f e_av=%f \n\r k=%f, er_cnt= %f", reference_strike, error_strike, error_strike_average, k, error_count);
ThomasBNL 61:5b644df6f6ab 344
ThomasBNL 61:5b644df6f6ab 345 if (looptimerflag == true)
ThomasBNL 61:5b644df6f6ab 346 {
ThomasBNL 61:5b644df6f6ab 347 looptimerflag=false;
ThomasBNL 64:97e2db3eb0eb 348 activate_PID_Controller_strike(P_gain_strike, I_gain_strike, D_gain_strike);
ThomasBNL 64:97e2db3eb0eb 349 //D_gain_strike=(input1.read())*0.000001;
ThomasBNL 64:97e2db3eb0eb 350 //printf("D_s = %f \n\r",D_gain_strike);
ThomasBNL 64:97e2db3eb0eb 351 execute_plant_strike();
ThomasBNL 61:5b644df6f6ab 352 }
ThomasBNL 61:5b644df6f6ab 353
ThomasBNL 64:97e2db3eb0eb 354 printf(" %f \n\r",error_strike_average);
ThomasBNL 64:97e2db3eb0eb 355
ThomasBNL 65:2da8cf778181 356 if (fabs(error_strike_average) < 0.5 && error_count>100)
ThomasBNL 64:97e2db3eb0eb 357 {
ThomasBNL 64:97e2db3eb0eb 358 yellow();
ThomasBNL 64:97e2db3eb0eb 359 pc.printf("new action \n\r");
ThomasBNL 64:97e2db3eb0eb 360 deactivate_PID_Controller(P_gain_strike, I_gain_strike, D_gain_strike);
ThomasBNL 64:97e2db3eb0eb 361 execute_plant_strike();
ThomasBNL 64:97e2db3eb0eb 362 goto Nieuwe_actie;
ThomasBNL 64:97e2db3eb0eb 363 }
ThomasBNL 64:97e2db3eb0eb 364 }
ThomasBNL 61:5b644df6f6ab 365 }
ThomasBNL 61:5b644df6f6ab 366 }
ThomasBNL 60:084fd3352882 367 }
ThomasBNL 60:084fd3352882 368 }
ThomasBNL 58:141787606c4a 369 // ___________________________
ThomasBNL 58:141787606c4a 370 // // \\
ThomasBNL 64:97e2db3eb0eb 371 // || [Action 2: TURN LEFT] ||
ThomasBNL 58:141787606c4a 372 // \\___________________________//
ThomasBNL 64:97e2db3eb0eb 373 // // yellow \\
ThomasBNL 58:141787606c4a 374
ThomasBNL 59:38a302b9f7f9 375 if (moving_average_left > Threshold_Bicep_Left_2 && moving_average_right < Threshold_Bicep_Right_1)
ThomasBNL 53:05194bab0bfd 376 {
ThomasBNL 64:97e2db3eb0eb 377 yellow();
ThomasBNL 53:05194bab0bfd 378 n=0;
ThomasBNL 64:97e2db3eb0eb 379 pc.printf("LEFT \n\r");
ThomasBNL 64:97e2db3eb0eb 380
ThomasBNL 59:38a302b9f7f9 381 wait(2); // TIJDELIJK
ThomasBNL 64:97e2db3eb0eb 382
ThomasBNL 53:05194bab0bfd 383 while(moving_average_left > Threshold_Bicep_Left_1 && moving_average_right < Threshold_Bicep_Right_1)
ThomasBNL 53:05194bab0bfd 384 {
ThomasBNL 64:97e2db3eb0eb 385 if (n==0)
ThomasBNL 55:f4ab878ae910 386 {
ThomasBNL 59:38a302b9f7f9 387 Change_Turn_Position_Left(reference_turn, change_one_bottle);
ThomasBNL 64:97e2db3eb0eb 388 n=1; error_count=0;
ThomasBNL 64:97e2db3eb0eb 389 }
ThomasBNL 64:97e2db3eb0eb 390
ThomasBNL 55:f4ab878ae910 391 pc.printf("ref_t = %f, e_cnt= %f e_av=%f \n\r", reference_turn, error_count, error_turn_average);
ThomasBNL 54:9eb449571f4f 392
ThomasBNL 54:9eb449571f4f 393 if (looptimerflag == true)
ThomasBNL 54:9eb449571f4f 394 {
ThomasBNL 55:f4ab878ae910 395 looptimerflag=false;
ThomasBNL 55:f4ab878ae910 396 activate_PID_Controller_turn(P_gain_turn, I_gain_turn, D_gain_turn);
ThomasBNL 54:9eb449571f4f 397 execute_plant_turn();
ThomasBNL 54:9eb449571f4f 398 }
ThomasBNL 54:9eb449571f4f 399
ThomasBNL 64:97e2db3eb0eb 400 if (fabs(error_turn_average) < 1 && error_count>250)
ThomasBNL 53:05194bab0bfd 401 {
ThomasBNL 55:f4ab878ae910 402 pc.printf("new action \n\r");
ThomasBNL 55:f4ab878ae910 403 deactivate_PID_Controller(P_gain_turn, I_gain_turn, D_gain_turn);
ThomasBNL 55:f4ab878ae910 404 execute_plant_turn();
ThomasBNL 64:97e2db3eb0eb 405 goto Nieuwe_actie;
ThomasBNL 53:05194bab0bfd 406 }
ThomasBNL 53:05194bab0bfd 407 }
ThomasBNL 53:05194bab0bfd 408 }
ThomasBNL 59:38a302b9f7f9 409 /// ___________________________
ThomasBNL 59:38a302b9f7f9 410 // // \\
ThomasBNL 64:97e2db3eb0eb 411 // || [Action 3: TURN RIGHT] ||
ThomasBNL 59:38a302b9f7f9 412 // \\___________________________//
ThomasBNL 64:97e2db3eb0eb 413 // //Purple\\
ThomasBNL 46:9279c7a725bf 414
ThomasBNL 59:38a302b9f7f9 415 if (moving_average_right > Threshold_Bicep_Right_2 && moving_average_left < Threshold_Bicep_Left_1)
ThomasBNL 59:38a302b9f7f9 416 {
ThomasBNL 64:97e2db3eb0eb 417 purple();
ThomasBNL 59:38a302b9f7f9 418 n=0;
ThomasBNL 59:38a302b9f7f9 419 pc.printf("Right \n\r");
ThomasBNL 64:97e2db3eb0eb 420
ThomasBNL 59:38a302b9f7f9 421 wait(2); // TIJDELIJK
ThomasBNL 64:97e2db3eb0eb 422
ThomasBNL 59:38a302b9f7f9 423 while(moving_average_right > Threshold_Bicep_Right_1 && moving_average_left < Threshold_Bicep_Left_1)
ThomasBNL 59:38a302b9f7f9 424 {
ThomasBNL 64:97e2db3eb0eb 425 if (n==0)
ThomasBNL 59:38a302b9f7f9 426 {
ThomasBNL 59:38a302b9f7f9 427 Change_Turn_Position_Right(reference_turn, change_one_bottle);
ThomasBNL 64:97e2db3eb0eb 428 n=1; error_count=0;
ThomasBNL 64:97e2db3eb0eb 429 }
ThomasBNL 64:97e2db3eb0eb 430
ThomasBNL 59:38a302b9f7f9 431 pc.printf("ref_t = %f, e_cnt= %f e_av=%f \n\r", reference_turn, error_count, error_turn_average);
ThomasBNL 59:38a302b9f7f9 432
ThomasBNL 59:38a302b9f7f9 433 if (looptimerflag == true)
ThomasBNL 59:38a302b9f7f9 434 {
ThomasBNL 59:38a302b9f7f9 435 looptimerflag=false;
ThomasBNL 59:38a302b9f7f9 436 activate_PID_Controller_turn(P_gain_turn, I_gain_turn, D_gain_turn);
ThomasBNL 59:38a302b9f7f9 437 execute_plant_turn();
ThomasBNL 59:38a302b9f7f9 438 }
ThomasBNL 59:38a302b9f7f9 439
ThomasBNL 64:97e2db3eb0eb 440 if (fabs(error_turn_average) < 1 && error_count>250)
ThomasBNL 59:38a302b9f7f9 441 {
ThomasBNL 59:38a302b9f7f9 442 pc.printf("new action \n\r");
ThomasBNL 59:38a302b9f7f9 443 deactivate_PID_Controller(P_gain_turn, I_gain_turn, D_gain_turn);
ThomasBNL 59:38a302b9f7f9 444 execute_plant_turn();
ThomasBNL 64:97e2db3eb0eb 445 goto Nieuwe_actie;
ThomasBNL 59:38a302b9f7f9 446 }
ThomasBNL 59:38a302b9f7f9 447 }
ThomasBNL 59:38a302b9f7f9 448 }
ThomasBNL 46:9279c7a725bf 449
ThomasBNL 34:c672f5c0763f 450 // ___________________________
ThomasBNL 34:c672f5c0763f 451 // // \\
ThomasBNL 34:c672f5c0763f 452 // || [HIDSCOPE] ||
ThomasBNL 34:c672f5c0763f 453 // \\___________________________//
ThomasBNL 34:c672f5c0763f 454 // // Check signals inside HIDSCOPE \\
ThomasBNL 34:c672f5c0763f 455
ThomasBNL 64:97e2db3eb0eb 456 //scope.set(0,error_turn); // HIDSCOPE channel 0 : Current Error
ThomasBNL 48:950b1f34161b 457 //scope.set(1,position_turn); // HIDSCOPE channel 1 : Position_turn
ThomasBNL 48:950b1f34161b 458 //scope.set(2,pwm_to_motor_turn); // HIDSCOPE channel 2 : Pwm_to_motor_turn
ThomasBNL 48:950b1f34161b 459 //scope.send(); // Send channel info to HIDSCOPE
ThomasBNL 40:bbe7922723df 460
ThomasBNL 0:40052f5ca77b 461 }
ThomasBNL 5:8fb74a22fe3c 462 }
ThomasBNL 0:40052f5ca77b 463
ThomasBNL 46:9279c7a725bf 464 //============================================================================================================================
ThomasBNL 46:9279c7a725bf 465 ///////////////////////////////
ThomasBNL 46:9279c7a725bf 466 // //
ThomasBNL 46:9279c7a725bf 467 ///////////////////////////////////////////// [FUNCTIONS DESCRIBED] /////////////////////////////////////////////////////
ThomasBNL 46:9279c7a725bf 468 // //
ThomasBNL 46:9279c7a725bf 469 ///////////////////////////////
ThomasBNL 46:9279c7a725bf 470 //============================================================================================================================
ThomasBNL 67:65750d716788 471
ThomasBNL 67:65750d716788 472 // ___________________________
ThomasBNL 67:65750d716788 473 // // \\
ThomasBNL 67:65750d716788 474 // || [CALIBRATE] ||
ThomasBNL 67:65750d716788 475 // \\___________________________//
ThomasBNL 67:65750d716788 476 // Calibrate starting postion (RED LED)
ThomasBNL 67:65750d716788 477 // ___________________________
ThomasBNL 67:65750d716788 478 // : [Starting position motor] :
ThomasBNL 67:65750d716788 479 // :___________________________:
ThomasBNL 67:65750d716788 480 //
ThomasBNL 67:65750d716788 481
ThomasBNL 67:65750d716788 482 void calibrate_motor()
ThomasBNL 67:65750d716788 483 {
ThomasBNL 67:65750d716788 484 pc.printf("Button calibration \n\r");
ThomasBNL 67:65750d716788 485 while(1)
ThomasBNL 67:65750d716788 486 {
ThomasBNL 67:65750d716788 487 red();// RED LED // TURN: LOW buttons
ThomasBNL 67:65750d716788 488
ThomasBNL 67:65750d716788 489 if (buttonL2.read() < 0.5){ // TURN: turn the motor clockwise with pwm of 0.2
ThomasBNL 67:65750d716788 490 motordirection_turn = cw;
ThomasBNL 67:65750d716788 491 pwm_motor_turn = 0.02;
ThomasBNL 67:65750d716788 492 pc.printf("cw calibration \n\r");}
ThomasBNL 67:65750d716788 493
ThomasBNL 67:65750d716788 494 if (buttonL1.read() < 0.5){ // TURN: turn the motor counterclockwise with pwm of 0.2
ThomasBNL 67:65750d716788 495 motordirection_turn = ccw;
ThomasBNL 67:65750d716788 496 pwm_motor_turn = 0.02;
ThomasBNL 67:65750d716788 497 pc.printf("ccw calibration \n\r");}
ThomasBNL 67:65750d716788 498
ThomasBNL 67:65750d716788 499 pwm_motor_turn = 0;
ThomasBNL 67:65750d716788 500 // STRIKE: HIGH buttons
ThomasBNL 67:65750d716788 501
ThomasBNL 67:65750d716788 502 if (buttonH2.read() < 0.5){ // STRIKE: turn the motor clockwise with pwm of 0.2
ThomasBNL 67:65750d716788 503 motordirection_strike = cw;
ThomasBNL 67:65750d716788 504 pwm_motor_strike = 0.02;
ThomasBNL 67:65750d716788 505 pc.printf("cw calibration \n\r");}
ThomasBNL 67:65750d716788 506 //
ThomasBNL 67:65750d716788 507 if (buttonH1.read() < 0.5){ // STRIKE: turn the motor clockwise with pwm of 0.2
ThomasBNL 67:65750d716788 508 motordirection_strike = ccw;
ThomasBNL 67:65750d716788 509 pwm_motor_strike = 0.02;
ThomasBNL 67:65750d716788 510 pc.printf("ccw calibration \n\r");}
ThomasBNL 67:65750d716788 511
ThomasBNL 67:65750d716788 512 pwm_motor_strike = 0;
ThomasBNL 67:65750d716788 513 //
ThomasBNL 67:65750d716788 514 if ((buttonL2.read() < 0.5) && (buttonL1.read() < 0.5)) // Save current TURN and STRIKE positions as starting position
ThomasBNL 67:65750d716788 515 {
ThomasBNL 67:65750d716788 516 motor_turn.reset(); // TURN: Starting Position
ThomasBNL 67:65750d716788 517 reference_turn=0; // TURN: Set reference position to zero
ThomasBNL 67:65750d716788 518 motor_strike.reset(); // STRIKE: Starting Position
ThomasBNL 67:65750d716788 519 goto calibration_starting_position_complete; // Calibration complete
ThomasBNL 67:65750d716788 520 }
ThomasBNL 67:65750d716788 521 }
ThomasBNL 67:65750d716788 522 calibration_starting_position_complete:
ThomasBNL 67:65750d716788 523 }
ThomasBNL 67:65750d716788 524
ThomasBNL 46:9279c7a725bf 525 // ___________________________
ThomasBNL 46:9279c7a725bf 526 // // \\
ThomasBNL 46:9279c7a725bf 527 // || [MOTOR FUCNTIONS] ||
ThomasBNL 46:9279c7a725bf 528 // \\___________________________//
ThomasBNL 49:a8a68abf814f 529 //
ThomasBNL 54:9eb449571f4f 530 // PLANT CONTROL TURN
ThomasBNL 54:9eb449571f4f 531 void execute_plant_turn()
ThomasBNL 55:f4ab878ae910 532 {
ThomasBNL 55:f4ab878ae910 533 if ((motor_turn.getPulses()>4200) || (motor_turn.getPulses()<-4200)) // If value is outside -4200 and 4200 (number of counts equal to one revolution) reset to zero
ThomasBNL 55:f4ab878ae910 534 {
ThomasBNL 55:f4ab878ae910 535 motor_turn.reset();
ThomasBNL 55:f4ab878ae910 536 pc.printf("RESET \n\r");
ThomasBNL 55:f4ab878ae910 537 }
ThomasBNL 55:f4ab878ae910 538
ThomasBNL 55:f4ab878ae910 539 position_turn = conversion_counts_to_degrees * motor_turn.getPulses();
ThomasBNL 55:f4ab878ae910 540
ThomasBNL 54:9eb449571f4f 541 double pwm_to_motor_turn=PID_control(reference_turn, position_turn, integrate_error_turn, sample_time, previous_error_turn, P_gain_turn, I_gain_turn, D_gain_turn);
ThomasBNL 54:9eb449571f4f 542 keep_in_range(&pwm_to_motor_turn, -1,1); // Pass to motor controller but keep it in range!
ThomasBNL 55:f4ab878ae910 543 //pc.printf("pwm %f \n\r", pwm_to_motor_turn);
ThomasBNL 54:9eb449571f4f 544
ThomasBNL 54:9eb449571f4f 545 // Check error and decide direction to turn
ThomasBNL 54:9eb449571f4f 546 if(pwm_to_motor_turn > 0)
ThomasBNL 54:9eb449571f4f 547 {
ThomasBNL 54:9eb449571f4f 548 motordirection_turn=ccw;
ThomasBNL 55:f4ab878ae910 549 //pc.printf("if loop pwm > 0 \n\r");
ThomasBNL 54:9eb449571f4f 550 }
ThomasBNL 54:9eb449571f4f 551 else
ThomasBNL 54:9eb449571f4f 552 {
ThomasBNL 54:9eb449571f4f 553 motordirection_turn=cw;
ThomasBNL 55:f4ab878ae910 554 //pc.printf("else loop pwm < 0 \n\r");
ThomasBNL 54:9eb449571f4f 555 }
ThomasBNL 54:9eb449571f4f 556 pwm_motor_turn=(abs(pwm_to_motor_turn));
ThomasBNL 55:f4ab878ae910 557
ThomasBNL 55:f4ab878ae910 558 take_sample();
ThomasBNL 64:97e2db3eb0eb 559
ThomasBNL 64:97e2db3eb0eb 560 // sample_filter();
ThomasBNL 55:f4ab878ae910 561
ThomasBNL 55:f4ab878ae910 562 if(sample_error) // sample_error -- e10;e9;e8;e7;e6;e5:e4;e3;e2;e1 -- error_turn_average --- er
ThomasBNL 55:f4ab878ae910 563 {
ThomasBNL 55:f4ab878ae910 564 sample_error=false;
ThomasBNL 55:f4ab878ae910 565 e1 = (position_turn - reference_turn);
ThomasBNL 57:8f3603cc2e71 566 e30=e29; e29=e28 ;e28=e27; e27=e26; e26=e25; e25=e24; e24=e23; e23=e22; e22=e21; e21=e20;
ThomasBNL 57:8f3603cc2e71 567 e20=e19; e19=e18 ;e18=e17; e17=e16; e16=e15; e15=e14; e14=e13; e13=e12; e12=e11; e11=e10;
ThomasBNL 55:f4ab878ae910 568 e10=e9 ;e9=e8; e8=e7; e7=e6; e6=e5; e5=e4; e4=e3; e3=e2; e2=e1;
ThomasBNL 55:f4ab878ae910 569 }
ThomasBNL 57:8f3603cc2e71 570 error_turn_average=(e1+e2+e3+e4+e5+e6+e7+e8+e9+e10+e11+e12+e13+e14+e15+e16+e17+e18+e19+e20+e21+e22+e23+e24+e25+e26+e27+e28+e29+e30)/30;
ThomasBNL 55:f4ab878ae910 571 er++;
ThomasBNL 55:f4ab878ae910 572 error_count++;
ThomasBNL 54:9eb449571f4f 573 }
ThomasBNL 63:d86a46c8aa0c 574
ThomasBNL 63:d86a46c8aa0c 575 // PLANT CONTROL STRIKE
ThomasBNL 63:d86a46c8aa0c 576 void execute_plant_strike()
ThomasBNL 63:d86a46c8aa0c 577 {
ThomasBNL 64:97e2db3eb0eb 578 if ((motor_strike.getPulses()>4200) || (motor_strike.getPulses()<-4200)) // If value is outside -4200 and 4200 (number of counts equal to one revolution) reset to zero
ThomasBNL 63:d86a46c8aa0c 579 {
ThomasBNL 64:97e2db3eb0eb 580 motor_strike.reset();
ThomasBNL 63:d86a46c8aa0c 581 pc.printf("RESET \n\r");
ThomasBNL 63:d86a46c8aa0c 582 }
ThomasBNL 63:d86a46c8aa0c 583
ThomasBNL 64:97e2db3eb0eb 584 position_strike = conversion_counts_to_degrees * motor_strike.getPulses();
ThomasBNL 63:d86a46c8aa0c 585
ThomasBNL 65:2da8cf778181 586
ThomasBNL 64:97e2db3eb0eb 587 double pwm_to_motor_strike=PID_control(reference_strike, position_strike, integrate_error_strike, sample_time, previous_error_strike, P_gain_strike, I_gain_strike, D_gain_strike);
ThomasBNL 64:97e2db3eb0eb 588 keep_in_range(&pwm_to_motor_strike, -1,1); // Pass to motor controller but keep it in range!
ThomasBNL 63:d86a46c8aa0c 589 //pc.printf("pwm %f \n\r", pwm_to_motor_turn);
ThomasBNL 63:d86a46c8aa0c 590
ThomasBNL 63:d86a46c8aa0c 591 // Check error and decide direction to turn
ThomasBNL 64:97e2db3eb0eb 592 if(pwm_to_motor_strike > 0)
ThomasBNL 63:d86a46c8aa0c 593 {
ThomasBNL 64:97e2db3eb0eb 594 motordirection_strike=cw;
ThomasBNL 63:d86a46c8aa0c 595 //pc.printf("if loop pwm > 0 \n\r");
ThomasBNL 63:d86a46c8aa0c 596 }
ThomasBNL 63:d86a46c8aa0c 597 else
ThomasBNL 63:d86a46c8aa0c 598 {
ThomasBNL 64:97e2db3eb0eb 599 motordirection_strike=ccw;
ThomasBNL 63:d86a46c8aa0c 600 //pc.printf("else loop pwm < 0 \n\r");
ThomasBNL 63:d86a46c8aa0c 601 }
ThomasBNL 64:97e2db3eb0eb 602
ThomasBNL 64:97e2db3eb0eb 603 if(p==1)
ThomasBNL 64:97e2db3eb0eb 604 {
ThomasBNL 64:97e2db3eb0eb 605 pwm_motor_strike=(abs(pwm_to_motor_strike));
ThomasBNL 64:97e2db3eb0eb 606 }
ThomasBNL 64:97e2db3eb0eb 607
ThomasBNL 64:97e2db3eb0eb 608
ThomasBNL 63:d86a46c8aa0c 609 // TEMPORARY USAGE WHILE POTMETER ACTIVE
ThomasBNL 63:d86a46c8aa0c 610 EMG_L_max = 100; // Calibreren (max average over 5 seconde?) gemeten integraal EMG over tijd / (tijdsample stappen)=100
ThomasBNL 63:d86a46c8aa0c 611 EMG_L_min = 0;
ThomasBNL 63:d86a46c8aa0c 612 EMG_R_max = 100; // Calibreren
ThomasBNL 63:d86a46c8aa0c 613 EMG_R_min = 0;
ThomasBNL 63:d86a46c8aa0c 614
ThomasBNL 63:d86a46c8aa0c 615 const double Threshold_Bicep_Left_1 =((EMG_L_max-EMG_L_min)*0.2)+EMG_L_min;; //(waarde waarop het gemeten EMG signaal 20% van max het maximale is); // LEFT
ThomasBNL 63:d86a46c8aa0c 616 const double Threshold_Bicep_Right_1 =((EMG_R_max-EMG_R_min)*0.2)+EMG_R_min; //(waarde waarop het gemeten EMG signaal 20% van max het maximale is); // RIGHT
ThomasBNL 63:d86a46c8aa0c 617
ThomasBNL 65:2da8cf778181 618 moving_average_left = 40; // TIJDELIJK PID TEST
ThomasBNL 65:2da8cf778181 619 moving_average_right = 40; // TIJDELIJK PID TEST
ThomasBNL 63:d86a46c8aa0c 620 double signal_above_threshold=(moving_average_right-Threshold_Bicep_Right_1)+(moving_average_left-Threshold_Bicep_Left_1);
ThomasBNL 63:d86a46c8aa0c 621 double max_signal=(EMG_R_max-Threshold_Bicep_Right_1)+(EMG_L_max-Threshold_Bicep_Left_1);
ThomasBNL 63:d86a46c8aa0c 622 double pwm_strike=signal_above_threshold/max_signal;
ThomasBNL 63:d86a46c8aa0c 623
ThomasBNL 64:97e2db3eb0eb 624 //pc.printf("mov_r = %f, mov_l = %f, pwm_strike = %f position = %f \n\r", moving_average_right, moving_average_left, pwm_strike, position_strike);
ThomasBNL 64:97e2db3eb0eb 625 if(p==0)
ThomasBNL 64:97e2db3eb0eb 626 {
ThomasBNL 64:97e2db3eb0eb 627 pwm_motor_strike=pwm_strike;
ThomasBNL 64:97e2db3eb0eb 628 }
ThomasBNL 63:d86a46c8aa0c 629
ThomasBNL 63:d86a46c8aa0c 630 take_sample();
ThomasBNL 64:97e2db3eb0eb 631
ThomasBNL 64:97e2db3eb0eb 632 // sample_filter();
ThomasBNL 64:97e2db3eb0eb 633
ThomasBNL 64:97e2db3eb0eb 634 if(sample_error_strike)
ThomasBNL 64:97e2db3eb0eb 635 {
ThomasBNL 64:97e2db3eb0eb 636 sample_error_strike=false;
ThomasBNL 64:97e2db3eb0eb 637 e1 = fabs(position_strike - reference_strike);
ThomasBNL 63:d86a46c8aa0c 638 e30=e29; e29=e28 ;e28=e27; e27=e26; e26=e25; e25=e24; e24=e23; e23=e22; e22=e21; e21=e20;
ThomasBNL 63:d86a46c8aa0c 639 e20=e19; e19=e18 ;e18=e17; e17=e16; e16=e15; e15=e14; e14=e13; e13=e12; e12=e11; e11=e10;
ThomasBNL 63:d86a46c8aa0c 640 e10=e9 ;e9=e8; e8=e7; e7=e6; e6=e5; e5=e4; e4=e3; e3=e2; e2=e1;
ThomasBNL 64:97e2db3eb0eb 641 }
ThomasBNL 64:97e2db3eb0eb 642
ThomasBNL 64:97e2db3eb0eb 643 error_strike_average=(e1+e2+e3+e4+e5+e6+e7+e8+e9+e10+e11+e12+e13+e14+e15+e16+e17+e18+e19+e20+e21+e22+e23+e24+e25+e26+e27+e28+e29+e30)/30;
ThomasBNL 63:d86a46c8aa0c 644 er++;
ThomasBNL 63:d86a46c8aa0c 645 error_count++;
ThomasBNL 63:d86a46c8aa0c 646 }
ThomasBNL 54:9eb449571f4f 647
ThomasBNL 49:a8a68abf814f 648 // PID CONTROLLER
ThomasBNL 49:a8a68abf814f 649 double PID_control(double reference, double position, double &integrate_error, double sample_time, double &previous_error, double P_gain, double I_gain, double D_gain)
ThomasBNL 49:a8a68abf814f 650 {
ThomasBNL 50:6060f45d343a 651 double error=(reference - position); // TURN: Current error (input controller)
ThomasBNL 49:a8a68abf814f 652 integrate_error=integrate_error_turn + sample_time*error_turn; // TURN: Integral error output
ThomasBNL 49:a8a68abf814f 653 // overwrite previous integrate error by adding the current error
ThomasBNL 49:a8a68abf814f 654 // multiplied by the sample time.
ThomasBNL 52:b530adf72f79 655
ThomasBNL 55:f4ab878ae910 656 //pc.printf("error = %f", error);
ThomasBNL 55:f4ab878ae910 657 //pc.printf("P= %f", P_gain);
ThomasBNL 55:f4ab878ae910 658 //pc.printf("I = %f", I_gain);
ThomasBNL 55:f4ab878ae910 659 //pc.printf("D = %f", D_gain);
ThomasBNL 50:6060f45d343a 660 double error_derivative=(error - previous_error)/sample_time; // TURN: Derivative error output
ThomasBNL 50:6060f45d343a 661 error_derivative=Lowpassfilter_derivative.step(error_derivative); // TURN: Filter
ThomasBNL 49:a8a68abf814f 662
ThomasBNL 49:a8a68abf814f 663 previous_error_turn=error_turn; // current error is saved to memory constant to be used in
ThomasBNL 49:a8a68abf814f 664 // the next loop for calculating the derivative error
ThomasBNL 49:a8a68abf814f 665
ThomasBNL 50:6060f45d343a 666 double pwm_motor_P = error*P_gain; // Output P controller to pwm
ThomasBNL 50:6060f45d343a 667 double pwm_motor_I = integrate_error*I_gain; // Output I controller to pwm
ThomasBNL 50:6060f45d343a 668 double pwm_motor_D = error_derivative*D_gain; // Output D controller to pwm
ThomasBNL 49:a8a68abf814f 669
ThomasBNL 50:6060f45d343a 670 double pwm_to_motor = pwm_motor_P + pwm_motor_I + pwm_motor_D;
ThomasBNL 49:a8a68abf814f 671
ThomasBNL 49:a8a68abf814f 672 return pwm_to_motor;
ThomasBNL 49:a8a68abf814f 673 }
ThomasBNL 34:c672f5c0763f 674
ThomasBNL 0:40052f5ca77b 675 // Keep in range function
ThomasBNL 0:40052f5ca77b 676 void keep_in_range(double * in, double min, double max)
ThomasBNL 0:40052f5ca77b 677 {
ThomasBNL 0:40052f5ca77b 678 *in > min ? *in < max? : *in = max: *in = min;
ThomasBNL 0:40052f5ca77b 679 }
ThomasBNL 0:40052f5ca77b 680
ThomasBNL 0:40052f5ca77b 681 // Looptimerflag function
ThomasBNL 0:40052f5ca77b 682 void setlooptimerflag(void)
ThomasBNL 0:40052f5ca77b 683 {
ThomasBNL 0:40052f5ca77b 684 looptimerflag = true;
ThomasBNL 1:dc683e88b44e 685 }
ThomasBNL 1:dc683e88b44e 686
ThomasBNL 39:104a038f7b92 687 // Change reference
ThomasBNL 58:141787606c4a 688 void Change_Turn_Position_Right (double& reference, double change_one_bottle)
ThomasBNL 39:104a038f7b92 689 {
ThomasBNL 46:9279c7a725bf 690 if(reference==90) // added reference if at boundary bottle and try to go to the side where no bottles are; than immediatly turn 5 bottles to the left
ThomasBNL 45:359df0594588 691 {
ThomasBNL 45:359df0594588 692 reference=-90;
ThomasBNL 45:359df0594588 693 }
ThomasBNL 45:359df0594588 694 else
ThomasBNL 45:359df0594588 695 {
ThomasBNL 45:359df0594588 696 reference = reference + change_one_bottle;
ThomasBNL 46:9279c7a725bf 697 keep_in_range(&reference, -90, 90); // reference position stays between -90 and 90 degrees (IF bottles at -90, -45, 0, 45, 90 degrees)
ThomasBNL 45:359df0594588 698 }
ThomasBNL 39:104a038f7b92 699 }
ThomasBNL 39:104a038f7b92 700
ThomasBNL 58:141787606c4a 701 void Change_Turn_Position_Left (double& reference, double change_one_bottle)
ThomasBNL 39:104a038f7b92 702 {
ThomasBNL 45:359df0594588 703 if(reference==-90)
ThomasBNL 45:359df0594588 704 {
ThomasBNL 45:359df0594588 705 reference=90;
ThomasBNL 45:359df0594588 706 }
ThomasBNL 45:359df0594588 707 else
ThomasBNL 45:359df0594588 708 {
ThomasBNL 45:359df0594588 709 reference = reference - change_one_bottle;
ThomasBNL 45:359df0594588 710 keep_in_range(&reference, -90, 90);
ThomasBNL 45:359df0594588 711 }
ThomasBNL 39:104a038f7b92 712 }
ThomasBNL 39:104a038f7b92 713
ThomasBNL 42:0a7898cb3e94 714 // Deactivate or Activate PID_Controller
ThomasBNL 42:0a7898cb3e94 715 void deactivate_PID_Controller(double& P_gain, double& I_gain, double& D_gain)
ThomasBNL 42:0a7898cb3e94 716 {
ThomasBNL 42:0a7898cb3e94 717 P_gain=0;
ThomasBNL 42:0a7898cb3e94 718 I_gain=0;
ThomasBNL 42:0a7898cb3e94 719 D_gain=0;
ThomasBNL 54:9eb449571f4f 720 pwm_motor_turn=0;
ThomasBNL 64:97e2db3eb0eb 721 pwm_motor_strike=0;
ThomasBNL 42:0a7898cb3e94 722 }
ThomasBNL 42:0a7898cb3e94 723
ThomasBNL 42:0a7898cb3e94 724 void activate_PID_Controller_turn(double& P_gain, double& I_gain, double& D_gain)
ThomasBNL 42:0a7898cb3e94 725 {
ThomasBNL 57:8f3603cc2e71 726 P_gain_turn=0.02; // hier waardes P,I,D veranderen (waardes bovenaan doen (tijdelijk) niks meer
ThomasBNL 57:8f3603cc2e71 727 I_gain_turn=0.1;
ThomasBNL 52:b530adf72f79 728 D_gain_turn=0;
ThomasBNL 42:0a7898cb3e94 729 }
ThomasBNL 42:0a7898cb3e94 730
ThomasBNL 42:0a7898cb3e94 731 void activate_PID_Controller_strike(double& P_gain, double& I_gain, double& D_gain)
ThomasBNL 42:0a7898cb3e94 732 {
ThomasBNL 65:2da8cf778181 733 double Ku = (input1.read())*1; // EMG Right bicep (tussen nul en 100%) // DEBUG TEST TOOL: substitute EMG input for potmeter inputs
ThomasBNL 65:2da8cf778181 734 double Pu = (input2.read())*1; // EMG Left bicep (tussen nul en 100%)
ThomasBNL 65:2da8cf778181 735
ThomasBNL 66:04a203e43510 736 P_gain_strike=0.8*Ku; // Ku=0.2 (ultimate gain Ziegler-Nichols method)
ThomasBNL 66:04a203e43510 737 // Pu=0.25 (ultimate period) (4Hz)
ThomasBNL 65:2da8cf778181 738
ThomasBNL 65:2da8cf778181 739 pc.printf("Ku=%f Pu=%f \n\r", Ku, Pu);
ThomasBNL 65:2da8cf778181 740 //0.09090909;
ThomasBNL 65:2da8cf778181 741 //PI tyreus luyben : 0.0625, 0.55;
ThomasBNL 65:2da8cf778181 742 //PID tyreus luyben : 0.09090909, 0.55, 0.0396825;
ThomasBNL 65:2da8cf778181 743 // Ku=0.2 (ultimate gain Ziegler-Nichols method)
ThomasBNL 65:2da8cf778181 744 // Pu=0.25 (ultimate period) (4Hz)
ThomasBNL 65:2da8cf778181 745 // hier waardes P,I,D veranderen (waardes bovenaan doen (tijdelijk) niks meer // 0.00045 // 0.03
ThomasBNL 65:2da8cf778181 746 I_gain_strike=0; //0.55;
ThomasBNL 66:04a203e43510 747 D_gain_strike=Pu/8; //0.0396825;
ThomasBNL 46:9279c7a725bf 748 }
ThomasBNL 46:9279c7a725bf 749
ThomasBNL 46:9279c7a725bf 750 //============================================================================================================================
ThomasBNL 46:9279c7a725bf 751 // ___________________________
ThomasBNL 46:9279c7a725bf 752 // // \\
ThomasBNL 46:9279c7a725bf 753 // || [EMG FUCNTIONS] ||
ThomasBNL 46:9279c7a725bf 754 // \\___________________________//
ThomasBNL 46:9279c7a725bf 755 //
ThomasBNL 46:9279c7a725bf 756
ThomasBNL 46:9279c7a725bf 757 // [CALIBRATION] // (blue LED)
ThomasBNL 46:9279c7a725bf 758 void calibration()
ThomasBNL 46:9279c7a725bf 759 {
ThomasBNL 46:9279c7a725bf 760
ThomasBNL 46:9279c7a725bf 761
ThomasBNL 46:9279c7a725bf 762 // [MINIMUM VALUE BICEPS CALIBRATION] //
ThomasBNL 46:9279c7a725bf 763
ThomasBNL 46:9279c7a725bf 764
ThomasBNL 46:9279c7a725bf 765 debug_led_blue=on;
ThomasBNL 46:9279c7a725bf 766 pc.printf("Start minimum calibration in 5 seconds \n\r");
ThomasBNL 46:9279c7a725bf 767 pc.printf("Keep your biceps as relaxed as possible \n\r");
ThomasBNL 46:9279c7a725bf 768
ThomasBNL 46:9279c7a725bf 769 countdown_from_5();
ThomasBNL 46:9279c7a725bf 770 c=0;
ThomasBNL 46:9279c7a725bf 771
ThomasBNL 46:9279c7a725bf 772 while(c<2560) // 512Hz -> 2560 is equal to five seconds
ThomasBNL 46:9279c7a725bf 773 {
ThomasBNL 46:9279c7a725bf 774 Filter(); // Filter EMG signal
ThomasBNL 46:9279c7a725bf 775 minimum_L=EMG_Left_Bicep_filtered+minimum_L; // Take previous sample EMG_Left_Bicep_filtered and add the new value
ThomasBNL 46:9279c7a725bf 776 minimum_R=EMG_Right_Bicep_filtered+minimum_R;
ThomasBNL 46:9279c7a725bf 777 // scope.set(0,EMG_left_Bicep);
ThomasBNL 46:9279c7a725bf 778 // scope.set(1,EMG_Left_Bicep_filtered);
ThomasBNL 46:9279c7a725bf 779 // scope.set(2,minimum_L);
ThomasBNL 46:9279c7a725bf 780 // scope.send();
ThomasBNL 46:9279c7a725bf 781 c++; // Every sample c is increased by one until the statement c<2560 is false
ThomasBNL 46:9279c7a725bf 782 wait(0.001953125); // wait one sample
ThomasBNL 46:9279c7a725bf 783 }
ThomasBNL 46:9279c7a725bf 784
ThomasBNL 46:9279c7a725bf 785 pc.printf("Finished minimum calibration \n\r");
ThomasBNL 46:9279c7a725bf 786
ThomasBNL 46:9279c7a725bf 787 EMG_L_min=minimum_L/2560; // Divide the summation by the number of measurements (2560 measurements) to get a mean value over 5 seconds
ThomasBNL 46:9279c7a725bf 788 EMG_R_min=minimum_R/2560;
ThomasBNL 46:9279c7a725bf 789
ThomasBNL 46:9279c7a725bf 790 pc.printf("EMG_L_min = %f \n\r EMG_R_min = %f \n\r", EMG_L_min, EMG_R_min);
ThomasBNL 46:9279c7a725bf 791
ThomasBNL 46:9279c7a725bf 792 wait (3); //cooldown
ThomasBNL 46:9279c7a725bf 793
ThomasBNL 46:9279c7a725bf 794
ThomasBNL 46:9279c7a725bf 795 // [MAXIMUM VALUE BICEPS CALIBRATION] //
ThomasBNL 46:9279c7a725bf 796
ThomasBNL 46:9279c7a725bf 797
ThomasBNL 46:9279c7a725bf 798 pc.printf("start maximum calibration in 5 seconds (start contraction at 3) \n\r");
ThomasBNL 46:9279c7a725bf 799
ThomasBNL 46:9279c7a725bf 800 countdown_from_5();
ThomasBNL 46:9279c7a725bf 801 c=0;
ThomasBNL 46:9279c7a725bf 802
ThomasBNL 46:9279c7a725bf 803 while(c<2560) // 512Hz -> 2560 is equal to five seconds
ThomasBNL 46:9279c7a725bf 804 {
ThomasBNL 46:9279c7a725bf 805 Filter(); // Filter EMG signal
ThomasBNL 46:9279c7a725bf 806 maximum_L=EMG_Left_Bicep_filtered+maximum_L; // Take previous sample EMG_Left_Bicep_filtered and add the new value
ThomasBNL 46:9279c7a725bf 807 maximum_R=EMG_Right_Bicep_filtered+maximum_R;
ThomasBNL 46:9279c7a725bf 808 // scope.set(0,EMG_left_Bicep);
ThomasBNL 46:9279c7a725bf 809 // scope.set(1,EMG_Left_Bicep_filtered);
ThomasBNL 46:9279c7a725bf 810 // scope.set(2,maximum_R);
ThomasBNL 46:9279c7a725bf 811 // scope.send();
ThomasBNL 46:9279c7a725bf 812 c++; // Every sample c is increased by one until the statement c<2560 is false
ThomasBNL 50:6060f45d343a 813 wait(0.001953125);
ThomasBNL 46:9279c7a725bf 814 }
ThomasBNL 46:9279c7a725bf 815
ThomasBNL 46:9279c7a725bf 816 pc.printf("Finished minimum calibration \n\r");
ThomasBNL 46:9279c7a725bf 817
ThomasBNL 46:9279c7a725bf 818 EMG_L_max=maximum_L/2560; // Divide the summation by the number of measurements (2560 measurements) to get a mean value over 5 seconds
ThomasBNL 46:9279c7a725bf 819 EMG_R_max=maximum_R/2560;
ThomasBNL 46:9279c7a725bf 820
ThomasBNL 46:9279c7a725bf 821 pc.printf("EMG_L_max = %f \n\r EMG_R_max = %f \n\r", EMG_L_max, EMG_R_max);
ThomasBNL 46:9279c7a725bf 822
ThomasBNL 46:9279c7a725bf 823 wait (3); //cooldown
ThomasBNL 46:9279c7a725bf 824
ThomasBNL 46:9279c7a725bf 825 debug_led_blue=off;
ThomasBNL 46:9279c7a725bf 826
ThomasBNL 46:9279c7a725bf 827
ThomasBNL 46:9279c7a725bf 828 // [MAXIMUM VALUE BICEPS CALIBRATION] //
ThomasBNL 46:9279c7a725bf 829 // Calculate threshold percentages //
ThomasBNL 46:9279c7a725bf 830
ThomasBNL 46:9279c7a725bf 831 const float Threshold_Bicep_Left_1=((EMG_L_max-EMG_L_min)*0.2)+EMG_L_min;; //(waarde waarop het gemeten EMG signaal 20% van max het maximale is); // LEFT
ThomasBNL 46:9279c7a725bf 832 const float Threshold_Bicep_Left_2=((EMG_L_max-EMG_L_min)*0.6)+EMG_L_min; //(waarde waarop het gemeten EMG signaal 60% van max het maximale is);
ThomasBNL 46:9279c7a725bf 833 const float Threshold_Bicep_Right_1=((EMG_R_max-EMG_R_min)*0.2)+EMG_R_min; //(waarde waarop het gemeten EMG signaal 20% van max het maximale is); // RIGHT
ThomasBNL 46:9279c7a725bf 834 const float Threshold_Bicep_Right_2=((EMG_R_max-EMG_R_min)*0.6)+EMG_R_min; //(waarde waarop het gemeten EMG signaal 60% van max het maximale is);
ThomasBNL 46:9279c7a725bf 835
ThomasBNL 46:9279c7a725bf 836 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 46:9279c7a725bf 837
ThomasBNL 46:9279c7a725bf 838 }
ThomasBNL 46:9279c7a725bf 839
ThomasBNL 46:9279c7a725bf 840 // [COUNTDOWN FUNCTION] //
ThomasBNL 46:9279c7a725bf 841
ThomasBNL 46:9279c7a725bf 842
ThomasBNL 46:9279c7a725bf 843 void countdown_from_5() // Countdown from 5 till 0 inside Putty (interface)
ThomasBNL 46:9279c7a725bf 844 {
ThomasBNL 46:9279c7a725bf 845 wait(1); pc.printf("5 \n\r"); wait(1); pc.printf("4 \n\r"); wait(1); pc.printf("3 \n\r"); wait(1); pc.printf("2 Ready \n\r");
ThomasBNL 46:9279c7a725bf 846 wait(1); pc.printf("1 Set \n\r"); wait(1); pc.printf("Go \n\r");
ThomasBNL 46:9279c7a725bf 847 }
ThomasBNL 46:9279c7a725bf 848
ThomasBNL 46:9279c7a725bf 849
ThomasBNL 46:9279c7a725bf 850 // [FLOW CONTROL FUNCTIONS] //
ThomasBNL 46:9279c7a725bf 851
ThomasBNL 46:9279c7a725bf 852
ThomasBNL 46:9279c7a725bf 853 void ControlGo() //Control flag
ThomasBNL 46:9279c7a725bf 854 {
ThomasBNL 46:9279c7a725bf 855 control_go = true;
ThomasBNL 46:9279c7a725bf 856 }
ThomasBNL 46:9279c7a725bf 857
ThomasBNL 46:9279c7a725bf 858 void take_sample() // Take a sample every 25th sample
ThomasBNL 46:9279c7a725bf 859 {
ThomasBNL 46:9279c7a725bf 860 if(n==25)
ThomasBNL 46:9279c7a725bf 861 {
ThomasBNL 64:97e2db3eb0eb 862 sample = true; n=0;
ThomasBNL 46:9279c7a725bf 863 }
ThomasBNL 55:f4ab878ae910 864
ThomasBNL 57:8f3603cc2e71 865 if(er==5)
ThomasBNL 55:f4ab878ae910 866 {
ThomasBNL 64:97e2db3eb0eb 867 sample_error = true; er=0;
ThomasBNL 55:f4ab878ae910 868 }
ThomasBNL 64:97e2db3eb0eb 869
ThomasBNL 64:97e2db3eb0eb 870 sample_error_strike = true;
ThomasBNL 46:9279c7a725bf 871 }
ThomasBNL 46:9279c7a725bf 872
ThomasBNL 46:9279c7a725bf 873 // [FILTER FUNCTIONS] //
ThomasBNL 46:9279c7a725bf 874 // [EMG] //
ThomasBNL 46:9279c7a725bf 875
ThomasBNL 46:9279c7a725bf 876 void Filter() // Unfiltered EMG (input) -> highpass filter -> rectify -> lowpass filter -> Filtered EMG (output)
ThomasBNL 46:9279c7a725bf 877 {
ThomasBNL 46:9279c7a725bf 878 EMG_left_Bicep = input1; EMG_Right_Bicep = input2;
ThomasBNL 46:9279c7a725bf 879
ThomasBNL 46:9279c7a725bf 880 EMG_Left_Bicep_filtered = highpassfilter_1.step(EMG_left_Bicep); EMG_Right_Bicep_filtered = highpassfilter_2.step(EMG_Right_Bicep);
ThomasBNL 46:9279c7a725bf 881 EMG_Left_Bicep_filtered = fabs(EMG_Left_Bicep_filtered); EMG_Right_Bicep_filtered = fabs(EMG_Right_Bicep_filtered);
ThomasBNL 67:65750d716788 882
ThomasBNL 67:65750d716788 883 EMG_Left_Bicep_filtered_notch_1 = notchL1.step(EMG_Left_Bicep_filtered); EMG_Right_Bicep_filtered_notch_1 = notchR1.step(EMG_Right_Bicep_filtered);
ThomasBNL 67:65750d716788 884 EMG_Left_Bicep_filtered_notch_2 = notchL2.step(EMG_Left_Bicep_filtered_notch_1); EMG_Right_Bicep_filtered_notch_2 = notchR2.step(EMG_Right_Bicep_filtered_notch_1);
ThomasBNL 67:65750d716788 885
ThomasBNL 67:65750d716788 886 EMG_Left_Bicep_filtered = lowpassfilter_1.step(EMG_Left_Bicep_filtered_notch_2); EMG_Right_Bicep_filtered = lowpassfilter_2.step(EMG_Right_Bicep_filtered_notch_2);
ThomasBNL 46:9279c7a725bf 887 }
ThomasBNL 46:9279c7a725bf 888
ThomasBNL 46:9279c7a725bf 889 // [FILTER FUNCTIONS] //
ThomasBNL 46:9279c7a725bf 890 // [Include Moving Average] //
ThomasBNL 46:9279c7a725bf 891
ThomasBNL 46:9279c7a725bf 892 void sample_filter()
ThomasBNL 46:9279c7a725bf 893 {
ThomasBNL 46:9279c7a725bf 894 Filter();
ThomasBNL 46:9279c7a725bf 895 take_sample();
ThomasBNL 46:9279c7a725bf 896 if(sample)
ThomasBNL 46:9279c7a725bf 897 {
ThomasBNL 46:9279c7a725bf 898 sample=false;
ThomasBNL 46:9279c7a725bf 899 Sample_EMG_L_1 = EMG_Left_Bicep_filtered; Sample_EMG_R_1 = EMG_Right_Bicep_filtered;
ThomasBNL 46:9279c7a725bf 900
ThomasBNL 46:9279c7a725bf 901 Sample_EMG_L_10= Sample_EMG_L_9; Sample_EMG_R_10= Sample_EMG_R_9;
ThomasBNL 46:9279c7a725bf 902 Sample_EMG_L_9 = Sample_EMG_L_8; Sample_EMG_R_9 = Sample_EMG_R_8;
ThomasBNL 46:9279c7a725bf 903 Sample_EMG_L_8 = Sample_EMG_L_7; Sample_EMG_R_8 = Sample_EMG_R_7;
ThomasBNL 46:9279c7a725bf 904 Sample_EMG_L_7 = Sample_EMG_L_6; Sample_EMG_R_7 = Sample_EMG_R_6;
ThomasBNL 46:9279c7a725bf 905 Sample_EMG_L_6 = Sample_EMG_L_5; Sample_EMG_R_6 = Sample_EMG_R_5;
ThomasBNL 46:9279c7a725bf 906 Sample_EMG_L_5 = Sample_EMG_L_4; Sample_EMG_R_5 = Sample_EMG_R_4;
ThomasBNL 46:9279c7a725bf 907 Sample_EMG_L_4 = Sample_EMG_L_3; Sample_EMG_R_4 = Sample_EMG_R_3;
ThomasBNL 46:9279c7a725bf 908 Sample_EMG_L_3 = Sample_EMG_L_2; Sample_EMG_R_3 = Sample_EMG_R_2;
ThomasBNL 46:9279c7a725bf 909 Sample_EMG_L_2 = Sample_EMG_L_1; Sample_EMG_R_2 = Sample_EMG_R_1;
ThomasBNL 46:9279c7a725bf 910 }
ThomasBNL 46:9279c7a725bf 911 moving_average_left=Sample_EMG_L_1*0.1+Sample_EMG_L_2*0.1+Sample_EMG_L_3*0.1+Sample_EMG_L_4*0.1+Sample_EMG_L_5*0.1+Sample_EMG_L_6*0.1+Sample_EMG_L_7*0.1+Sample_EMG_L_8*0.1+Sample_EMG_L_9*0.1+Sample_EMG_L_10*0.1;
ThomasBNL 46:9279c7a725bf 912 moving_average_right=Sample_EMG_R_1*0.1+Sample_EMG_R_2*0.1+Sample_EMG_R_3*0.1+Sample_EMG_R_4*0.1+Sample_EMG_R_5*0.1+Sample_EMG_R_6*0.1+Sample_EMG_R_7*0.1+Sample_EMG_R_8*0.1+Sample_EMG_R_9*0.1+Sample_EMG_R_10*0.1;
ThomasBNL 46:9279c7a725bf 913 n++;
ThomasBNL 46:9279c7a725bf 914 }
ThomasBNL 46:9279c7a725bf 915
ThomasBNL 47:c61873a0b646 916 //============================================================================================================================
ThomasBNL 47:c61873a0b646 917 // ___________________________
ThomasBNL 47:c61873a0b646 918 // // \\
ThomasBNL 47:c61873a0b646 919 // || [Action Controller] ||
ThomasBNL 47:c61873a0b646 920 // \\___________________________//
ThomasBNL 55:f4ab878ae910 921 //
ThomasBNL 55:f4ab878ae910 922 void red() { debug_led_red=on; debug_led_blue=off; debug_led_green=off; }
ThomasBNL 55:f4ab878ae910 923 void blue() { debug_led_red=off; debug_led_blue=on; debug_led_green=off; }
ThomasBNL 55:f4ab878ae910 924 void green() { debug_led_red=off; debug_led_blue=off; debug_led_green=on; }
ThomasBNL 55:f4ab878ae910 925 void white() { debug_led_red=on; debug_led_blue=on; debug_led_green=on; }
ThomasBNL 55:f4ab878ae910 926 void yellow() { debug_led_red=on; debug_led_blue=off; debug_led_green=on; }
ThomasBNL 55:f4ab878ae910 927 void cyan() { debug_led_red=off; debug_led_blue=on; debug_led_green=on; }
ThomasBNL 55:f4ab878ae910 928 void purple() { debug_led_red=on; debug_led_blue=on; debug_led_green=off; }
ThomasBNL 67:65750d716788 929 void black() { debug_led_red=off; debug_led_blue=off; debug_led_green=off; }
ThomasBNL 67:65750d716788 930
ThomasBNL 67:65750d716788 931
ThomasBNL 67:65750d716788 932
ThomasBNL 67:65750d716788 933 void calibrate_potmeter() // DEBUG/TEST: Calibration thresholds with potmeter
ThomasBNL 67:65750d716788 934 {
ThomasBNL 67:65750d716788 935 // TEMPORARY USAGE WHILE POTMETER ACTIVE
ThomasBNL 67:65750d716788 936 EMG_L_max = 100;
ThomasBNL 67:65750d716788 937 EMG_L_min = 0;
ThomasBNL 67:65750d716788 938 EMG_R_max = 100;
ThomasBNL 67:65750d716788 939 EMG_R_min = 0;
ThomasBNL 67:65750d716788 940 Threshold_Bicep_Left_1 =((EMG_L_max-EMG_L_min)*0.2)+EMG_L_min;; //(waarde waarop het gemeten EMG signaal 20% van max het maximale is); // LEFT
ThomasBNL 67:65750d716788 941 Threshold_Bicep_Left_2 =((EMG_L_max-EMG_L_min)*0.6)+EMG_L_min; //(waarde waarop het gemeten EMG signaal 60% van max het maximale is);
ThomasBNL 67:65750d716788 942 Threshold_Bicep_Right_1 =((EMG_R_max-EMG_R_min)*0.2)+EMG_R_min; //(waarde waarop het gemeten EMG signaal 20% van max het maximale is); // RIGHT
ThomasBNL 67:65750d716788 943 Threshold_Bicep_Right_2 =((EMG_R_max-EMG_R_min)*0.6)+EMG_R_min; //(waarde waarop het gemeten EMG signaal 60% van max het maximale is);
ThomasBNL 67:65750d716788 944 }