PID
PID.cpp@0:3d03a93d9671, 2018-05-16 (annotated)
- Committer:
- ckalintra
- Date:
- Wed May 16 10:27:33 2018 +0000
- Revision:
- 0:3d03a93d9671
PID
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ckalintra | 0:3d03a93d9671 | 1 | #include "PID.h" |
ckalintra | 0:3d03a93d9671 | 2 | #define Kp 0.1 |
ckalintra | 0:3d03a93d9671 | 3 | #define Ki 0.00001 |
ckalintra | 0:3d03a93d9671 | 4 | |
ckalintra | 0:3d03a93d9671 | 5 | void PID::control(float x, float *output, float time, float integral) |
ckalintra | 0:3d03a93d9671 | 6 | { |
ckalintra | 0:3d03a93d9671 | 7 | float error = - x; |
ckalintra | 0:3d03a93d9671 | 8 | float Pout = Kp * error;//multiply the error with kp to get P output |
ckalintra | 0:3d03a93d9671 | 9 | |
ckalintra | 0:3d03a93d9671 | 10 | integral += error * time;//integral adds the previous error + the error now * time the error presist |
ckalintra | 0:3d03a93d9671 | 11 | |
ckalintra | 0:3d03a93d9671 | 12 | float Iout = Ki * integral;//multiply by Ki |
ckalintra | 0:3d03a93d9671 | 13 | output[0] = Pout + Iout; |
ckalintra | 0:3d03a93d9671 | 14 | if( output[0] > 1 )//limit the output |
ckalintra | 0:3d03a93d9671 | 15 | {output[0] = 1;} |
ckalintra | 0:3d03a93d9671 | 16 | else if( output[0] < -1 ) |
ckalintra | 0:3d03a93d9671 | 17 | {output[0] = -1;} |
ckalintra | 0:3d03a93d9671 | 18 | } |