Jesus Fausto
/
Basic_PID
Basic example for PID library
main.cpp@0:8454aaf72e4c, 2018-08-14 (annotated)
- Committer:
- jvfausto
- Date:
- Tue Aug 14 23:25:00 2018 +0000
- Revision:
- 0:8454aaf72e4c
PID basic example for pid library;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jvfausto | 0:8454aaf72e4c | 1 | #include "mbed.h" |
jvfausto | 0:8454aaf72e4c | 2 | #include "PID.h" |
jvfausto | 0:8454aaf72e4c | 3 | |
jvfausto | 0:8454aaf72e4c | 4 | Serial pc(USBTX,USBRX); |
jvfausto | 0:8454aaf72e4c | 5 | |
jvfausto | 0:8454aaf72e4c | 6 | //Define Variables we'll be connecting to |
jvfausto | 0:8454aaf72e4c | 7 | double Input; |
jvfausto | 0:8454aaf72e4c | 8 | double Output; |
jvfausto | 0:8454aaf72e4c | 9 | double Setpoint; |
jvfausto | 0:8454aaf72e4c | 10 | |
jvfausto | 0:8454aaf72e4c | 11 | PwmOut pinOut(D0); |
jvfausto | 0:8454aaf72e4c | 12 | DigitalIn a(D1); |
jvfausto | 0:8454aaf72e4c | 13 | //Specify the links and initial tuning parameters |
jvfausto | 0:8454aaf72e4c | 14 | double Kp=1, Ki=1, Kd=1; |
jvfausto | 0:8454aaf72e4c | 15 | PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, P_ON_M, DIRECT); |
jvfausto | 0:8454aaf72e4c | 16 | |
jvfausto | 0:8454aaf72e4c | 17 | int main() |
jvfausto | 0:8454aaf72e4c | 18 | { |
jvfausto | 0:8454aaf72e4c | 19 | //initialize the variables we're linked to |
jvfausto | 0:8454aaf72e4c | 20 | pinOut.period(.001f); |
jvfausto | 0:8454aaf72e4c | 21 | |
jvfausto | 0:8454aaf72e4c | 22 | Input = a.read(); |
jvfausto | 0:8454aaf72e4c | 23 | Setpoint = 90; |
jvfausto | 0:8454aaf72e4c | 24 | |
jvfausto | 0:8454aaf72e4c | 25 | myPID.SetMode(AUTOMATIC); |
jvfausto | 0:8454aaf72e4c | 26 | |
jvfausto | 0:8454aaf72e4c | 27 | while(1) |
jvfausto | 0:8454aaf72e4c | 28 | { |
jvfausto | 0:8454aaf72e4c | 29 | Input = a.read(); |
jvfausto | 0:8454aaf72e4c | 30 | myPID.Compute(); |
jvfausto | 0:8454aaf72e4c | 31 | pinOut = Output; |
jvfausto | 0:8454aaf72e4c | 32 | wait(.1); |
jvfausto | 0:8454aaf72e4c | 33 | } |
jvfausto | 0:8454aaf72e4c | 34 | } |
jvfausto | 0:8454aaf72e4c | 35 |