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@9:697134d3564e, 2015-10-07 (annotated)
- Committer:
- ThomasBNL
- Date:
- Wed Oct 07 12:40:08 2015 +0000
- Revision:
- 9:697134d3564e
- Parent:
- 8:50d6e2323d3b
- Child:
- 10:09ba965045a7
vanaf hier werkt de p-controller, lijkt wel alsof het langer duurt dan eerst voor evenwicht
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 | 8:50d6e2323d3b | 24 | |
ThomasBNL | 8:50d6e2323d3b | 25 | double conversion_counts_to_degrees=0.085877862594198; |
ThomasBNL | 8:50d6e2323d3b | 26 | // gear ratio motor = 131 |
ThomasBNL | 8:50d6e2323d3b | 27 | // ticks per magnet rotation = 32 (X2 Encoder) |
ThomasBNL | 8:50d6e2323d3b | 28 | // One revolution = 360 degrees |
ThomasBNL | 8:50d6e2323d3b | 29 | // degrees_per_encoder_tick = 360/(gear_ratio*ticks_per_magnet_rotation)=360/131*32=0.085877862594198 |
ThomasBNL | 8:50d6e2323d3b | 30 | |
ThomasBNL | 8:50d6e2323d3b | 31 | const double sample_time=0.01; // tijd voor een sample (100Hz) |
ThomasBNL | 8:50d6e2323d3b | 32 | |
ThomasBNL | 8:50d6e2323d3b | 33 | // PID motor constants |
ThomasBNL | 8:50d6e2323d3b | 34 | double integrate_error_turn=0; // integration error turn motor |
ThomasBNL | 8:50d6e2323d3b | 35 | double previous_error_turn=0; // previous error turn motor |
ThomasBNL | 8:50d6e2323d3b | 36 | |
ThomasBNL | 1:dc683e88b44e | 37 | |
ThomasBNL | 7:ddd7fb357786 | 38 | // Functions used (described after main) |
ThomasBNL | 7:ddd7fb357786 | 39 | void keep_in_range(double * in, double min, double max); |
ThomasBNL | 7:ddd7fb357786 | 40 | void setlooptimerflag(void); |
ThomasBNL | 8:50d6e2323d3b | 41 | double get_reference(double input); |
ThomasBNL | 8:50d6e2323d3b | 42 | |
ThomasBNL | 0:40052f5ca77b | 43 | |
ThomasBNL | 7:ddd7fb357786 | 44 | // MAIN function |
ThomasBNL | 0:40052f5ca77b | 45 | int main() { |
ThomasBNL | 8:50d6e2323d3b | 46 | AnalogIn potmeter(A0); // Potmeter that can read a reference value (DEBUG TOOL) |
ThomasBNL | 8:50d6e2323d3b | 47 | QEI motor_turn(D12,D13,NC,32); // Encoder for motor Turn |
ThomasBNL | 8:50d6e2323d3b | 48 | PwmOut pwm_motor_turn(D5); // Pwm for motor Turn |
ThomasBNL | 8:50d6e2323d3b | 49 | DigitalOut motordirection_turn(D4); // Direction of the motor Turn |
ThomasBNL | 8:50d6e2323d3b | 50 | double reference_turn; // Set constant to store reference value of the Turn motor |
ThomasBNL | 8:50d6e2323d3b | 51 | double position_turn; // Set constant to store current position of the Turn motor |
ThomasBNL | 8:50d6e2323d3b | 52 | double error; |
ThomasBNL | 0:40052f5ca77b | 53 | |
ThomasBNL | 7:ddd7fb357786 | 54 | //START OF CODE |
ThomasBNL | 7:ddd7fb357786 | 55 | pc.printf("Start of code \n\r"); |
ThomasBNL | 0:40052f5ca77b | 56 | |
ThomasBNL | 8:50d6e2323d3b | 57 | pc.baud(9600); // Set the baudrate |
ThomasBNL | 0:40052f5ca77b | 58 | |
ThomasBNL | 7:ddd7fb357786 | 59 | // Tickers |
ThomasBNL | 8:50d6e2323d3b | 60 | Ticker looptimer; // Ticker called looptimer to set a looptimerflag |
ThomasBNL | 8:50d6e2323d3b | 61 | looptimer.attach(setlooptimerflag,sample_time); // calls the looptimer flag every 0.01s |
ThomasBNL | 0:40052f5ca77b | 62 | |
ThomasBNL | 7:ddd7fb357786 | 63 | pc.printf("Start infinite loop \n\r"); |
ThomasBNL | 8:50d6e2323d3b | 64 | wait (3); // Rest before starting system |
ThomasBNL | 0:40052f5ca77b | 65 | |
ThomasBNL | 0:40052f5ca77b | 66 | //INFINITE LOOP |
ThomasBNL | 5:8fb74a22fe3c | 67 | while(1) |
ThomasBNL | 8:50d6e2323d3b | 68 | { // Start while loop |
ThomasBNL | 8:50d6e2323d3b | 69 | // DEBUGGING BUTTON: interrupt button Disbalances the current motor position |
ThomasBNL | 8:50d6e2323d3b | 70 | if (buttonL2.read() < 0.5){ //if button pressed |
ThomasBNL | 8:50d6e2323d3b | 71 | motordirection_turn = cw; |
ThomasBNL | 8:50d6e2323d3b | 72 | pwm_motor_turn = 0.5f; // motorspeed |
ThomasBNL | 8:50d6e2323d3b | 73 | pc.printf("positie = %d \r\n", motor_turn.getPulses()); } |
ThomasBNL | 8:50d6e2323d3b | 74 | |
ThomasBNL | 8:50d6e2323d3b | 75 | // Wait until looptimer flag is true then execute PID controller. |
ThomasBNL | 5:8fb74a22fe3c | 76 | else |
ThomasBNL | 8:50d6e2323d3b | 77 | { |
ThomasBNL | 0:40052f5ca77b | 78 | while(looptimerflag != true); |
ThomasBNL | 0:40052f5ca77b | 79 | |
ThomasBNL | 0:40052f5ca77b | 80 | looptimerflag = false; |
ThomasBNL | 0:40052f5ca77b | 81 | |
ThomasBNL | 8:50d6e2323d3b | 82 | //reference = (potmeter.read()-0.5)*2000; // Potmeter bepaald reference (uitgeschakeld) |
ThomasBNL | 8:50d6e2323d3b | 83 | reference_turn = 15; |
ThomasBNL | 0:40052f5ca77b | 84 | |
ThomasBNL | 8:50d6e2323d3b | 85 | // Keep motor position between -4200 and 4200 counts |
ThomasBNL | 8:50d6e2323d3b | 86 | 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 | 87 | { |
ThomasBNL | 8:50d6e2323d3b | 88 | motor_turn.reset(); |
ThomasBNL | 3:11a7da46e093 | 89 | pc.printf("RESET \n\r"); |
ThomasBNL | 3:11a7da46e093 | 90 | } |
ThomasBNL | 3:11a7da46e093 | 91 | |
ThomasBNL | 8:50d6e2323d3b | 92 | // Convert position to degrees |
ThomasBNL | 8:50d6e2323d3b | 93 | position_turn = conversion_counts_to_degrees * motor_turn.getPulses(); |
ThomasBNL | 0:40052f5ca77b | 94 | |
ThomasBNL | 8:50d6e2323d3b | 95 | pc.printf("calibrated setpoint: %f, calibrated position motor %i, position %f \n\r", reference_turn, motor_turn.getPulses(), position_turn); |
ThomasBNL | 3:11a7da46e093 | 96 | |
ThomasBNL | 0:40052f5ca77b | 97 | |
ThomasBNL | 8:50d6e2323d3b | 98 | // P-CONTROLLER |
ThomasBNL | 8:50d6e2323d3b | 99 | // Calculate error then multiply it with the gain, and store in pwm_to_motor |
ThomasBNL | 8:50d6e2323d3b | 100 | |
ThomasBNL | 8:50d6e2323d3b | 101 | error=(reference_turn - position_turn); // Current error (input controller) |
ThomasBNL | 8:50d6e2323d3b | 102 | |
ThomasBNL | 8:50d6e2323d3b | 103 | // integrate_error_turn=integrate_error_turn + sample_time*error; // integral error output |
ThomasBNL | 8:50d6e2323d3b | 104 | // // overwrite previous integrate error by adding the current error multiplied by the sample time. |
ThomasBNL | 8:50d6e2323d3b | 105 | // |
ThomasBNL | 8:50d6e2323d3b | 106 | //double error_derivative_turn=(error - previous_error_turn)/sample_time; // derivative error output |
ThomasBNL | 8:50d6e2323d3b | 107 | |
ThomasBNL | 8:50d6e2323d3b | 108 | // FILTER error_derivative_turn (lowpassfilter) |
ThomasBNL | 8:50d6e2323d3b | 109 | // biquadFilter Lowpassfilter(mT_f_a1,mT_f_a2,mT_f_b0,mT_f_b1,mT_f_b2); |
ThomasBNL | 8:50d6e2323d3b | 110 | // const double mT_f_a1=-1.965293372622690e+00; |
ThomasBNL | 8:50d6e2323d3b | 111 | //const double mT_f_a2=9.658854605688177e-01; |
ThomasBNL | 8:50d6e2323d3b | 112 | //const double mT_f_b0=1.480219865318266e-04; |
ThomasBNL | 8:50d6e2323d3b | 113 | //const double mT_f_b1=2.960439730636533e-04; |
ThomasBNL | 8:50d6e2323d3b | 114 | //const double mT_f_b2=1.480219865318266e-04; // Motor Turn filter constants |
ThomasBNL | 8:50d6e2323d3b | 115 | // Lowpassfilter.step(e_der) |
ThomasBNL | 3:11a7da46e093 | 116 | |
ThomasBNL | 9:697134d3564e | 117 | //previous_error_turn=error; // current error is saved to memory constant to be used in |
ThomasBNL | 8:50d6e2323d3b | 118 | // the next loop for calculating the derivative error |
ThomasBNL | 8:50d6e2323d3b | 119 | |
ThomasBNL | 9:697134d3564e | 120 | // double Gain_I_turn=1; |
ThomasBNL | 9:697134d3564e | 121 | // double Gain_D_turn=1; |
ThomasBNL | 8:50d6e2323d3b | 122 | |
ThomasBNL | 8:50d6e2323d3b | 123 | double pwm_motor_turn = error*Gain_P_turn; // output P controller to pwm |
ThomasBNL | 8:50d6e2323d3b | 124 | |
ThomasBNL | 8:50d6e2323d3b | 125 | // double pwm_motor_turn_P = error*Gain_P_turn; // output P controller to pwm |
ThomasBNL | 8:50d6e2323d3b | 126 | // double pwm_motor_turn_I = integrate_error_turn*Gain_I_turn; // output I controller to pwm |
ThomasBNL | 8:50d6e2323d3b | 127 | // double pwm_motor_turn_D = error_derivative_turn*Gain_D_turn; // output D controller to pwm |
ThomasBNL | 8:50d6e2323d3b | 128 | // |
ThomasBNL | 8:50d6e2323d3b | 129 | // double pwm_motor_turn = pwm_motor_turn_P + pwm_motor_turn_I + pwm_motor_turn_D; // Total output PID controller to pwm |
ThomasBNL | 8:50d6e2323d3b | 130 | // |
ThomasBNL | 0:40052f5ca77b | 131 | |
ThomasBNL | 8:50d6e2323d3b | 132 | // Keep Pwm between -1 and 1 |
ThomasBNL | 8:50d6e2323d3b | 133 | keep_in_range(&pwm_motor_turn, -1,1); // Pass to motor controller but keep it in range! |
ThomasBNL | 8:50d6e2323d3b | 134 | pc.printf("pwm %f \n\r", pwm_motor_turn); |
ThomasBNL | 0:40052f5ca77b | 135 | |
ThomasBNL | 8:50d6e2323d3b | 136 | // Check error and decide direction to turn |
ThomasBNL | 8:50d6e2323d3b | 137 | if(pwm_motor_turn > 0) |
ThomasBNL | 3:11a7da46e093 | 138 | { |
ThomasBNL | 8:50d6e2323d3b | 139 | motordirection_turn=ccw; |
ThomasBNL | 3:11a7da46e093 | 140 | pc.printf("if loop pwm_to_motor > 0 \n\r"); |
ThomasBNL | 3:11a7da46e093 | 141 | } |
ThomasBNL | 0:40052f5ca77b | 142 | else |
ThomasBNL | 3:11a7da46e093 | 143 | { |
ThomasBNL | 8:50d6e2323d3b | 144 | motordirection_turn=cw; |
ThomasBNL | 3:11a7da46e093 | 145 | pc.printf("else loop pwm_to_motor < 0 \n\r"); |
ThomasBNL | 3:11a7da46e093 | 146 | } |
ThomasBNL | 8:50d6e2323d3b | 147 | |
ThomasBNL | 8:50d6e2323d3b | 148 | // Put pwm_motor to the motor |
ThomasBNL | 8:50d6e2323d3b | 149 | pwm_motor_turn=(abs(pwm_motor_turn)); |
ThomasBNL | 0:40052f5ca77b | 150 | } |
ThomasBNL | 0:40052f5ca77b | 151 | } |
ThomasBNL | 5:8fb74a22fe3c | 152 | } |
ThomasBNL | 0:40052f5ca77b | 153 | |
ThomasBNL | 0:40052f5ca77b | 154 | // Keep in range function |
ThomasBNL | 0:40052f5ca77b | 155 | void keep_in_range(double * in, double min, double max) |
ThomasBNL | 0:40052f5ca77b | 156 | { |
ThomasBNL | 0:40052f5ca77b | 157 | *in > min ? *in < max? : *in = max: *in = min; |
ThomasBNL | 0:40052f5ca77b | 158 | } |
ThomasBNL | 0:40052f5ca77b | 159 | |
ThomasBNL | 0:40052f5ca77b | 160 | // Looptimerflag function |
ThomasBNL | 0:40052f5ca77b | 161 | void setlooptimerflag(void) |
ThomasBNL | 0:40052f5ca77b | 162 | { |
ThomasBNL | 0:40052f5ca77b | 163 | looptimerflag = true; |
ThomasBNL | 1:dc683e88b44e | 164 | } |
ThomasBNL | 1:dc683e88b44e | 165 | |
ThomasBNL | 8:50d6e2323d3b | 166 | // Get setpoint -> potmeter (MOMENTEEL UITGESCHAKELD) |
ThomasBNL | 8:50d6e2323d3b | 167 | double get_reference(double input) |
ThomasBNL | 1:dc683e88b44e | 168 | { |
ThomasBNL | 1:dc683e88b44e | 169 | const float offset = 0.5; |
ThomasBNL | 1:dc683e88b44e | 170 | const float gain = 4.0; |
ThomasBNL | 1:dc683e88b44e | 171 | return (input-offset)*gain; |
ThomasBNL | 5:8fb74a22fe3c | 172 | } |