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: HIDScope MODSERIAL QEI biquadFilter mbed
Fork of Robot_Software by
help_functions/PID_controller.h@0:ef81b9f14f58, 2018-10-19 (annotated)
- Committer:
- bjonkheer
- Date:
- Fri Oct 19 07:50:39 2018 +0000
- Revision:
- 0:ef81b9f14f58
- Child:
- 1:ce487c9929dd
Set-up of the complete software for the robot
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bjonkheer | 0:ef81b9f14f58 | 1 | #include "mbed.h" |
bjonkheer | 0:ef81b9f14f58 | 2 | |
bjonkheer | 0:ef81b9f14f58 | 3 | double PID_controller(double error) |
bjonkheer | 0:ef81b9f14f58 | 4 | { |
bjonkheer | 0:ef81b9f14f58 | 5 | Kp = potmeter2.read()*3; |
bjonkheer | 0:ef81b9f14f58 | 6 | |
bjonkheer | 0:ef81b9f14f58 | 7 | double u_k = Kp * error; |
bjonkheer | 0:ef81b9f14f58 | 8 | |
bjonkheer | 0:ef81b9f14f58 | 9 | static double error_integral = 0; |
bjonkheer | 0:ef81b9f14f58 | 10 | static double error_prev = error; // initialization with this value only done once! |
bjonkheer | 0:ef81b9f14f58 | 11 | static BiQuad LowPassFilter(0.0640, 0.1279, 0.0640, -1.1683, 0.4241); |
bjonkheer | 0:ef81b9f14f58 | 12 | |
bjonkheer | 0:ef81b9f14f58 | 13 | error_integral = error_integral + error * 1/samplingfreq; |
bjonkheer | 0:ef81b9f14f58 | 14 | double u_i = Ki * error_integral; |
bjonkheer | 0:ef81b9f14f58 | 15 | |
bjonkheer | 0:ef81b9f14f58 | 16 | double error_derivative = (error - error_prev)*samplingfreq; |
bjonkheer | 0:ef81b9f14f58 | 17 | double filtered_error_derivative = LowPassFilter.step(error_derivative); |
bjonkheer | 0:ef81b9f14f58 | 18 | double u_d = Kd * filtered_error_derivative; |
bjonkheer | 0:ef81b9f14f58 | 19 | error_prev = error; |
bjonkheer | 0:ef81b9f14f58 | 20 | return u_k+u_i+u_d; |
bjonkheer | 0:ef81b9f14f58 | 21 | } |