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.
main.cpp@1:f2a8a330983b, 2016-03-16 (annotated)
- Committer:
- openg
- Date:
- Wed Mar 16 01:51:38 2016 +0000
- Revision:
- 1:f2a8a330983b
- Parent:
- 0:f641e7f8973f
- Child:
- 2:486214873b2c
with PID library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
openg | 0:f641e7f8973f | 1 | #include "mbed.h" |
openg | 0:f641e7f8973f | 2 | #include "PID.h" |
openg | 0:f641e7f8973f | 3 | |
openg | 0:f641e7f8973f | 4 | float Kp = 30.0; |
openg | 0:f641e7f8973f | 5 | float Ki = 0.0; |
openg | 0:f641e7f8973f | 6 | float Kd = 0.0; |
openg | 0:f641e7f8973f | 7 | float interval = 0.1; //in seconds |
openg | 1:f2a8a330983b | 8 | float outputPWM = 0.0; |
openg | 0:f641e7f8973f | 9 | |
openg | 0:f641e7f8973f | 10 | PID controller(Kp, Ki, Kd, interval); |
openg | 0:f641e7f8973f | 11 | float currentVelocity; |
openg | 1:f2a8a330983b | 12 | PwmOut motorControl(PTD5); |
openg | 0:f641e7f8973f | 13 | |
openg | 0:f641e7f8973f | 14 | Ticker executeController; |
openg | 0:f641e7f8973f | 15 | |
openg | 0:f641e7f8973f | 16 | void control() { |
openg | 0:f641e7f8973f | 17 | controller.setProcessValue(currentVelocity); |
openg | 0:f641e7f8973f | 18 | outputPWM = controller.compute(); |
openg | 1:f2a8a330983b | 19 | motorControl.pulsewidth_us(int(1000*outputPWM)); |
openg | 0:f641e7f8973f | 20 | } |
openg | 0:f641e7f8973f | 21 | |
openg | 0:f641e7f8973f | 22 | int main() { |
openg | 0:f641e7f8973f | 23 | controller.setInputLimits(0.0, 6.0); //max input speeds in m/s |
openg | 0:f641e7f8973f | 24 | controller.setOutputLimits(0.0, 1.0); //PWM outputs from 0% to 100% |
openg | 0:f641e7f8973f | 25 | controller.setSetPoint(3.0); //reference velocity is 3.0 m/s |
openg | 0:f641e7f8973f | 26 | controller.setBias(0.0); //no bias for controller to output |
openg | 0:f641e7f8973f | 27 | controller.setMode(1); //AUTO mode |
openg | 1:f2a8a330983b | 28 | |
openg | 1:f2a8a330983b | 29 | motorControl.period_us(1000); |
openg | 1:f2a8a330983b | 30 | |
openg | 0:f641e7f8973f | 31 | executeController.attach(&control, interval); |
openg | 0:f641e7f8973f | 32 | |
openg | 0:f641e7f8973f | 33 | } |