Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Encoder HIDScope MODSERIAL QEI biquadFilter mbed
main.cpp@13:bcf8ec7120ab, 2015-10-07 (annotated)
- Committer:
- ThomasBNL
- Date:
- Wed Oct 07 13:51:17 2015 +0000
- Revision:
- 13:bcf8ec7120ab
- Parent:
- 12:26759959c960
- Child:
- 14:599896acf576
added pwm_motor_turn_D
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ThomasBNL | 0:40052f5ca77b | 1 | #include "mbed.h" |
ThomasBNL | 0:40052f5ca77b | 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 | 7:ddd7fb357786 | 8 | MODSERIAL pc(USBTX,USBRX); |
ThomasBNL | 8:50d6e2323d3b | 9 | |
ThomasBNL | 8:50d6e2323d3b | 10 | // (DEBUGGING AND TESTING BUTTONS) (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 | 0:40052f5ca77b | 16 | volatile bool looptimerflag; |
ThomasBNL | 8:50d6e2323d3b | 17 | const double cw=0; // zero is clockwise (front view) |
ThomasBNL | 8:50d6e2323d3b | 18 | const double ccw=1; // one is counterclockwise (front view) |
ThomasBNL | 8:50d6e2323d3b | 19 | |
ThomasBNL | 8:50d6e2323d3b | 20 | const double Gain_P_turn=0.0067; |
ThomasBNL | 8:50d6e2323d3b | 21 | // stel setpoint tussen (0 en 360) en position tussen (0 en 360) |
ThomasBNL | 8:50d6e2323d3b | 22 | // max verschil: 360 -> dan pwm_to_motor 1 tot aan een verschil van 15 graden-> bij 15 moet pwm_to_motor ong 0.1 zijn |
ThomasBNL | 8:50d6e2323d3b | 23 | // dus 0.1=15*gain gain=0.0067 |
ThomasBNL | 13:bcf8ec7120ab | 24 | |
ThomasBNL | 13:bcf8ec7120ab | 25 | double Gain_I_turn=0.025; //(1/2000) //0.00000134 |
ThomasBNL | 13:bcf8ec7120ab | 26 | // pwm_motor_I=(integrate_error_turn + sample_time*error)*gain; pwm = (4*0.01 + 4)* Gain => 0.1 pwm gewenst (na 1 seconde een verschil van 4 graden) |
ThomasBNL | 13:bcf8ec7120ab | 27 | // 0.1 / (4.01) = Gain = 0.025 |
ThomasBNL | 13:bcf8ec7120ab | 28 | |
ThomasBNL | 13:bcf8ec7120ab | 29 | double Gain_D_turn=1; |
ThomasBNL | 13:bcf8ec7120ab | 30 | // error_derivative_turn=(error - previous_error_turn)/sample_time |
ThomasBNL | 13:bcf8ec7120ab | 31 | // |
ThomasBNL | 8:50d6e2323d3b | 32 | |
ThomasBNL | 8:50d6e2323d3b | 33 | double conversion_counts_to_degrees=0.085877862594198; |
ThomasBNL | 8:50d6e2323d3b | 34 | // gear ratio motor = 131 |
ThomasBNL | 8:50d6e2323d3b | 35 | // ticks per magnet rotation = 32 (X2 Encoder) |
ThomasBNL | 8:50d6e2323d3b | 36 | // One revolution = 360 degrees |
ThomasBNL | 8:50d6e2323d3b | 37 | // degrees_per_encoder_tick = 360/(gear_ratio*ticks_per_magnet_rotation)=360/131*32=0.085877862594198 |
ThomasBNL | 8:50d6e2323d3b | 38 | |
ThomasBNL | 8:50d6e2323d3b | 39 | const double sample_time=0.01; // tijd voor een sample (100Hz) |
ThomasBNL | 8:50d6e2323d3b | 40 | |
ThomasBNL | 8:50d6e2323d3b | 41 | // PID motor constants |
ThomasBNL | 8:50d6e2323d3b | 42 | double integrate_error_turn=0; // integration error turn motor |
ThomasBNL | 8:50d6e2323d3b | 43 | double previous_error_turn=0; // previous error turn motor |
ThomasBNL | 8:50d6e2323d3b | 44 | |
ThomasBNL | 1:dc683e88b44e | 45 | |
ThomasBNL | 7:ddd7fb357786 | 46 | // Functions used (described after main) |
ThomasBNL | 7:ddd7fb357786 | 47 | void keep_in_range(double * in, double min, double max); |
ThomasBNL | 7:ddd7fb357786 | 48 | void setlooptimerflag(void); |
ThomasBNL | 8:50d6e2323d3b | 49 | double get_reference(double input); |
ThomasBNL | 8:50d6e2323d3b | 50 | |
ThomasBNL | 0:40052f5ca77b | 51 | |
ThomasBNL | 7:ddd7fb357786 | 52 | // MAIN function |
ThomasBNL | 0:40052f5ca77b | 53 | int main() { |
ThomasBNL | 8:50d6e2323d3b | 54 | AnalogIn potmeter(A0); // Potmeter that can read a reference value (DEBUG TOOL) |
ThomasBNL | 8:50d6e2323d3b | 55 | QEI motor_turn(D12,D13,NC,32); // Encoder for motor Turn |
ThomasBNL | 8:50d6e2323d3b | 56 | PwmOut pwm_motor_turn(D5); // Pwm for motor Turn |
ThomasBNL | 8:50d6e2323d3b | 57 | DigitalOut motordirection_turn(D4); // Direction of the motor Turn |
ThomasBNL | 8:50d6e2323d3b | 58 | double reference_turn; // Set constant to store reference value of the Turn motor |
ThomasBNL | 8:50d6e2323d3b | 59 | double position_turn; // Set constant to store current position of the Turn motor |
ThomasBNL | 8:50d6e2323d3b | 60 | double error; |
ThomasBNL | 10:09ba965045a7 | 61 | double pwm_to_motor_turn; |
ThomasBNL | 10:09ba965045a7 | 62 | double pwm_motor_turn_P; |
ThomasBNL | 10:09ba965045a7 | 63 | double pwm_motor_turn_I; |
ThomasBNL | 10:09ba965045a7 | 64 | double pwm_motor_turn_D; |
ThomasBNL | 0:40052f5ca77b | 65 | |
ThomasBNL | 7:ddd7fb357786 | 66 | //START OF CODE |
ThomasBNL | 7:ddd7fb357786 | 67 | pc.printf("Start of code \n\r"); |
ThomasBNL | 0:40052f5ca77b | 68 | |
ThomasBNL | 8:50d6e2323d3b | 69 | pc.baud(9600); // Set the baudrate |
ThomasBNL | 0:40052f5ca77b | 70 | |
ThomasBNL | 7:ddd7fb357786 | 71 | // Tickers |
ThomasBNL | 8:50d6e2323d3b | 72 | Ticker looptimer; // Ticker called looptimer to set a looptimerflag |
ThomasBNL | 8:50d6e2323d3b | 73 | looptimer.attach(setlooptimerflag,sample_time); // calls the looptimer flag every 0.01s |
ThomasBNL | 0:40052f5ca77b | 74 | |
ThomasBNL | 7:ddd7fb357786 | 75 | pc.printf("Start infinite loop \n\r"); |
ThomasBNL | 8:50d6e2323d3b | 76 | wait (3); // Rest before starting system |
ThomasBNL | 0:40052f5ca77b | 77 | |
ThomasBNL | 0:40052f5ca77b | 78 | //INFINITE LOOP |
ThomasBNL | 5:8fb74a22fe3c | 79 | while(1) |
ThomasBNL | 8:50d6e2323d3b | 80 | { // Start while loop |
ThomasBNL | 8:50d6e2323d3b | 81 | // DEBUGGING BUTTON: interrupt button Disbalances the current motor position |
ThomasBNL | 8:50d6e2323d3b | 82 | if (buttonL2.read() < 0.5){ //if button pressed |
ThomasBNL | 8:50d6e2323d3b | 83 | motordirection_turn = cw; |
ThomasBNL | 8:50d6e2323d3b | 84 | pwm_motor_turn = 0.5f; // motorspeed |
ThomasBNL | 8:50d6e2323d3b | 85 | pc.printf("positie = %d \r\n", motor_turn.getPulses()); } |
ThomasBNL | 8:50d6e2323d3b | 86 | |
ThomasBNL | 8:50d6e2323d3b | 87 | // Wait until looptimer flag is true then execute PID controller. |
ThomasBNL | 5:8fb74a22fe3c | 88 | else |
ThomasBNL | 8:50d6e2323d3b | 89 | { |
ThomasBNL | 0:40052f5ca77b | 90 | while(looptimerflag != true); |
ThomasBNL | 0:40052f5ca77b | 91 | |
ThomasBNL | 0:40052f5ca77b | 92 | looptimerflag = false; |
ThomasBNL | 0:40052f5ca77b | 93 | |
ThomasBNL | 8:50d6e2323d3b | 94 | //reference = (potmeter.read()-0.5)*2000; // Potmeter bepaald reference (uitgeschakeld) |
ThomasBNL | 8:50d6e2323d3b | 95 | reference_turn = 15; |
ThomasBNL | 0:40052f5ca77b | 96 | |
ThomasBNL | 8:50d6e2323d3b | 97 | // Keep motor position between -4200 and 4200 counts |
ThomasBNL | 8:50d6e2323d3b | 98 | 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 | 3:11a7da46e093 | 99 | { |
ThomasBNL | 8:50d6e2323d3b | 100 | motor_turn.reset(); |
ThomasBNL | 3:11a7da46e093 | 101 | pc.printf("RESET \n\r"); |
ThomasBNL | 3:11a7da46e093 | 102 | } |
ThomasBNL | 3:11a7da46e093 | 103 | |
ThomasBNL | 8:50d6e2323d3b | 104 | // Convert position to degrees |
ThomasBNL | 8:50d6e2323d3b | 105 | position_turn = conversion_counts_to_degrees * motor_turn.getPulses(); |
ThomasBNL | 0:40052f5ca77b | 106 | |
ThomasBNL | 8:50d6e2323d3b | 107 | pc.printf("calibrated setpoint: %f, calibrated position motor %i, position %f \n\r", reference_turn, motor_turn.getPulses(), position_turn); |
ThomasBNL | 3:11a7da46e093 | 108 | |
ThomasBNL | 0:40052f5ca77b | 109 | |
ThomasBNL | 8:50d6e2323d3b | 110 | // P-CONTROLLER |
ThomasBNL | 8:50d6e2323d3b | 111 | // Calculate error then multiply it with the gain, and store in pwm_to_motor |
ThomasBNL | 8:50d6e2323d3b | 112 | |
ThomasBNL | 10:09ba965045a7 | 113 | error=(reference_turn - position_turn); // Current error (input controller) |
ThomasBNL | 8:50d6e2323d3b | 114 | |
ThomasBNL | 10:09ba965045a7 | 115 | integrate_error_turn=integrate_error_turn + sample_time*error; // integral error output |
ThomasBNL | 8:50d6e2323d3b | 116 | // // overwrite previous integrate error by adding the current error multiplied by the sample time. |
ThomasBNL | 8:50d6e2323d3b | 117 | // |
ThomasBNL | 11:ecd83b303252 | 118 | double error_derivative_turn=(error - previous_error_turn)/sample_time; // derivative error output |
ThomasBNL | 8:50d6e2323d3b | 119 | |
ThomasBNL | 8:50d6e2323d3b | 120 | // FILTER error_derivative_turn (lowpassfilter) |
ThomasBNL | 11:ecd83b303252 | 121 | |
ThomasBNL | 12:26759959c960 | 122 | const double mT_f_a1=-1.965293372622690e+00; |
ThomasBNL | 12:26759959c960 | 123 | const double mT_f_a2=9.658854605688177e-01; |
ThomasBNL | 12:26759959c960 | 124 | const double mT_f_b0=1.480219865318266e-04; |
ThomasBNL | 12:26759959c960 | 125 | const double mT_f_b1=2.960439730636533e-04; |
ThomasBNL | 12:26759959c960 | 126 | const double mT_f_b2=1.480219865318266e-04; // Motor Turn filter constants |
ThomasBNL | 11:ecd83b303252 | 127 | |
ThomasBNL | 12:26759959c960 | 128 | biquadFilter Lowpassfilter(mT_f_a1,mT_f_a2,mT_f_b0,mT_f_b1,mT_f_b2); |
ThomasBNL | 11:ecd83b303252 | 129 | |
ThomasBNL | 12:26759959c960 | 130 | error_derivative_turn=Lowpassfilter.step(error_derivative_turn); |
ThomasBNL | 3:11a7da46e093 | 131 | |
ThomasBNL | 11:ecd83b303252 | 132 | previous_error_turn=error; // current error is saved to memory constant to be used in |
ThomasBNL | 8:50d6e2323d3b | 133 | // the next loop for calculating the derivative error |
ThomasBNL | 8:50d6e2323d3b | 134 | |
ThomasBNL | 10:09ba965045a7 | 135 | pwm_to_motor_turn = error*Gain_P_turn; // output P controller to pwm |
ThomasBNL | 8:50d6e2323d3b | 136 | |
ThomasBNL | 10:09ba965045a7 | 137 | pwm_motor_turn_P = error*Gain_P_turn; // output P controller to pwm |
ThomasBNL | 10:09ba965045a7 | 138 | pwm_motor_turn_I = integrate_error_turn*Gain_I_turn; // output I controller to pwm |
ThomasBNL | 11:ecd83b303252 | 139 | pwm_motor_turn_D = error_derivative_turn*Gain_D_turn; // output D controller to pwm |
ThomasBNL | 10:09ba965045a7 | 140 | |
ThomasBNL | 13:bcf8ec7120ab | 141 | pwm_to_motor_turn = pwm_motor_turn_P + pwm_motor_turn_I + pwm_motor_turn_D; |
ThomasBNL | 10:09ba965045a7 | 142 | |
ThomasBNL | 8:50d6e2323d3b | 143 | // |
ThomasBNL | 10:09ba965045a7 | 144 | // double pwm_to_motor_turn = pwm_motor_turn_P + pwm_motor_turn_I + pwm_motor_turn_D; // Total output PID controller to pwm |
ThomasBNL | 8:50d6e2323d3b | 145 | // |
ThomasBNL | 0:40052f5ca77b | 146 | |
ThomasBNL | 8:50d6e2323d3b | 147 | // Keep Pwm between -1 and 1 |
ThomasBNL | 10:09ba965045a7 | 148 | keep_in_range(&pwm_to_motor_turn, -1,1); // Pass to motor controller but keep it in range! |
ThomasBNL | 10:09ba965045a7 | 149 | pc.printf("pwm %f \n\r", pwm_to_motor_turn); |
ThomasBNL | 0:40052f5ca77b | 150 | |
ThomasBNL | 8:50d6e2323d3b | 151 | // Check error and decide direction to turn |
ThomasBNL | 10:09ba965045a7 | 152 | if(pwm_to_motor_turn > 0) |
ThomasBNL | 3:11a7da46e093 | 153 | { |
ThomasBNL | 8:50d6e2323d3b | 154 | motordirection_turn=ccw; |
ThomasBNL | 3:11a7da46e093 | 155 | pc.printf("if loop pwm_to_motor > 0 \n\r"); |
ThomasBNL | 3:11a7da46e093 | 156 | } |
ThomasBNL | 0:40052f5ca77b | 157 | else |
ThomasBNL | 3:11a7da46e093 | 158 | { |
ThomasBNL | 8:50d6e2323d3b | 159 | motordirection_turn=cw; |
ThomasBNL | 3:11a7da46e093 | 160 | pc.printf("else loop pwm_to_motor < 0 \n\r"); |
ThomasBNL | 3:11a7da46e093 | 161 | } |
ThomasBNL | 8:50d6e2323d3b | 162 | |
ThomasBNL | 8:50d6e2323d3b | 163 | // Put pwm_motor to the motor |
ThomasBNL | 10:09ba965045a7 | 164 | pwm_motor_turn=(abs(pwm_to_motor_turn)); |
ThomasBNL | 0:40052f5ca77b | 165 | } |
ThomasBNL | 0:40052f5ca77b | 166 | } |
ThomasBNL | 5:8fb74a22fe3c | 167 | } |
ThomasBNL | 0:40052f5ca77b | 168 | |
ThomasBNL | 0:40052f5ca77b | 169 | // Keep in range function |
ThomasBNL | 0:40052f5ca77b | 170 | void keep_in_range(double * in, double min, double max) |
ThomasBNL | 0:40052f5ca77b | 171 | { |
ThomasBNL | 0:40052f5ca77b | 172 | *in > min ? *in < max? : *in = max: *in = min; |
ThomasBNL | 0:40052f5ca77b | 173 | } |
ThomasBNL | 0:40052f5ca77b | 174 | |
ThomasBNL | 0:40052f5ca77b | 175 | // Looptimerflag function |
ThomasBNL | 0:40052f5ca77b | 176 | void setlooptimerflag(void) |
ThomasBNL | 0:40052f5ca77b | 177 | { |
ThomasBNL | 0:40052f5ca77b | 178 | looptimerflag = true; |
ThomasBNL | 1:dc683e88b44e | 179 | } |
ThomasBNL | 1:dc683e88b44e | 180 | |
ThomasBNL | 8:50d6e2323d3b | 181 | // Get setpoint -> potmeter (MOMENTEEL UITGESCHAKELD) |
ThomasBNL | 8:50d6e2323d3b | 182 | double get_reference(double input) |
ThomasBNL | 1:dc683e88b44e | 183 | { |
ThomasBNL | 1:dc683e88b44e | 184 | const float offset = 0.5; |
ThomasBNL | 1:dc683e88b44e | 185 | const float gain = 4.0; |
ThomasBNL | 1:dc683e88b44e | 186 | return (input-offset)*gain; |
ThomasBNL | 5:8fb74a22fe3c | 187 | } |