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: mbed
main.cpp@2:a17f7da1ca7c, 2019-03-26 (annotated)
- Committer:
- Jtroper
- Date:
- Tue Mar 26 16:01:53 2019 +0000
- Revision:
- 2:a17f7da1ca7c
- Parent:
- 1:b86f099a314a
- Child:
- 3:b17ae88bfa54
Before Combination
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Jtroper | 0:0e723924ae7c | 1 | #include "mbed.h" |
| Jtroper | 1:b86f099a314a | 2 | #include <iostream> |
| Jtroper | 2:a17f7da1ca7c | 3 | #define TI .001f //1kHz sample time |
| Jtroper | 2:a17f7da1ca7c | 4 | #define KP 5.1491f //Proportional Gain |
| Jtroper | 2:a17f7da1ca7c | 5 | #define KI 119.3089f //Integral Gain |
| Jtroper | 2:a17f7da1ca7c | 6 | #define FPWM .00004f //PWM Frequency |
| Jtroper | 2:a17f7da1ca7c | 7 | |
| Jtroper | 2:a17f7da1ca7c | 8 | //Pins |
| Jtroper | 1:b86f099a314a | 9 | DigitalOut myRled(LED1); |
| Jtroper | 1:b86f099a314a | 10 | DigitalOut myBled(LED2); |
| Jtroper | 2:a17f7da1ca7c | 11 | AnalogIn _Reference(PTB0); |
| Jtroper | 2:a17f7da1ca7c | 12 | AnalogIn _Tacho(PTB2); |
| Jtroper | 2:a17f7da1ca7c | 13 | PwmOut _MotorControl(PTB1); |
| Jtroper | 2:a17f7da1ca7c | 14 | |
| Jtroper | 2:a17f7da1ca7c | 15 | Ticker Update; |
| Jtroper | 2:a17f7da1ca7c | 16 | |
| Jtroper | 2:a17f7da1ca7c | 17 | //Integral Value |
| Jtroper | 2:a17f7da1ca7c | 18 | float _errorArea; |
| Jtroper | 2:a17f7da1ca7c | 19 | float _error; |
| Jtroper | 2:a17f7da1ca7c | 20 | float _controllerOutput; |
| Jtroper | 2:a17f7da1ca7c | 21 | float _ref; |
| Jtroper | 2:a17f7da1ca7c | 22 | |
| Jtroper | 2:a17f7da1ca7c | 23 | //Methods |
| Jtroper | 2:a17f7da1ca7c | 24 | void Init(); |
| Jtroper | 2:a17f7da1ca7c | 25 | void Loop(); |
| Jtroper | 2:a17f7da1ca7c | 26 | void UpdateMethod(); |
| Jtroper | 2:a17f7da1ca7c | 27 | void UpdateControllerOutput(); |
| Jtroper | 2:a17f7da1ca7c | 28 | void SetMotor(float); |
| Jtroper | 0:0e723924ae7c | 29 | |
| Jtroper | 0:0e723924ae7c | 30 | int main() { |
| Jtroper | 2:a17f7da1ca7c | 31 | Init(); |
| Jtroper | 2:a17f7da1ca7c | 32 | while(1) {Loop();} |
| Jtroper | 2:a17f7da1ca7c | 33 | } |
| Jtroper | 2:a17f7da1ca7c | 34 | |
| Jtroper | 2:a17f7da1ca7c | 35 | void Init() //Initializer |
| Jtroper | 2:a17f7da1ca7c | 36 | { |
| Jtroper | 2:a17f7da1ca7c | 37 | _MotorControl.period(FPWM); |
| Jtroper | 2:a17f7da1ca7c | 38 | SetMotor(0.0f); //Turns off the motor |
| Jtroper | 2:a17f7da1ca7c | 39 | _error = 0.0f; |
| Jtroper | 2:a17f7da1ca7c | 40 | _errorArea = 0.0f; |
| Jtroper | 2:a17f7da1ca7c | 41 | _ref = 0.0f; |
| Jtroper | 2:a17f7da1ca7c | 42 | Update.attach(&UpdateMethod, TI); |
| Jtroper | 2:a17f7da1ca7c | 43 | //SetMotor(_ref); |
| Jtroper | 2:a17f7da1ca7c | 44 | } |
| Jtroper | 2:a17f7da1ca7c | 45 | |
| Jtroper | 2:a17f7da1ca7c | 46 | void Loop() //Repeated Loop |
| Jtroper | 2:a17f7da1ca7c | 47 | { |
| Jtroper | 2:a17f7da1ca7c | 48 | |
| Jtroper | 2:a17f7da1ca7c | 49 | wait(5); |
| Jtroper | 2:a17f7da1ca7c | 50 | cout<< _ref << endl; |
| Jtroper | 2:a17f7da1ca7c | 51 | |
| Jtroper | 0:0e723924ae7c | 52 | } |
| Jtroper | 2:a17f7da1ca7c | 53 | |
| Jtroper | 2:a17f7da1ca7c | 54 | void UpdateMethod() // |
| Jtroper | 2:a17f7da1ca7c | 55 | { |
| Jtroper | 2:a17f7da1ca7c | 56 | UpdateControllerOutput(); |
| Jtroper | 2:a17f7da1ca7c | 57 | } |
| Jtroper | 2:a17f7da1ca7c | 58 | |
| Jtroper | 2:a17f7da1ca7c | 59 | void UpdateControllerOutput() |
| Jtroper | 2:a17f7da1ca7c | 60 | { |
| Jtroper | 2:a17f7da1ca7c | 61 | _ref = _Reference.read(); |
| Jtroper | 2:a17f7da1ca7c | 62 | _error = _ref - _Tacho.read(); |
| Jtroper | 2:a17f7da1ca7c | 63 | _errorArea += TI*_error; |
| Jtroper | 2:a17f7da1ca7c | 64 | _controllerOutput = KP*_error+ KI*_errorArea; |
| Jtroper | 2:a17f7da1ca7c | 65 | SetMotor(_controllerOutput); |
| Jtroper | 2:a17f7da1ca7c | 66 | } |
| Jtroper | 2:a17f7da1ca7c | 67 | |
| Jtroper | 2:a17f7da1ca7c | 68 | void SetMotor(float speed) |
| Jtroper | 2:a17f7da1ca7c | 69 | { |
| Jtroper | 2:a17f7da1ca7c | 70 | float invD = 0.0f; |
| Jtroper | 2:a17f7da1ca7c | 71 | if(speed<0.0f) |
| Jtroper | 2:a17f7da1ca7c | 72 | invD =0.0f; |
| Jtroper | 2:a17f7da1ca7c | 73 | else if(speed> .75f) |
| Jtroper | 2:a17f7da1ca7c | 74 | invD = .75f; |
| Jtroper | 2:a17f7da1ca7c | 75 | else |
| Jtroper | 2:a17f7da1ca7c | 76 | invD = speed; |
| Jtroper | 2:a17f7da1ca7c | 77 | |
| Jtroper | 2:a17f7da1ca7c | 78 | _MotorControl.write(/*1.0f-*/invD); |
| Jtroper | 2:a17f7da1ca7c | 79 | } |