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 MotorLab3Code
main.cpp@3:4d7ff98b0e96, 2019-03-07 (annotated)
- Committer:
- rjs718
- Date:
- Thu Mar 07 00:16:47 2019 +0000
- Revision:
- 3:4d7ff98b0e96
- Parent:
- 2:060a86df835f
- Child:
- 4:07e5212862ab
sedfgvhbjn;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| rjs718 | 0:cb571e42343c | 1 | #include "mbed.h" |
| rjs718 | 3:4d7ff98b0e96 | 2 | #include "Terminal.h" |
| rjs718 | 3:4d7ff98b0e96 | 3 | #include <cmath> |
| rjs718 | 3:4d7ff98b0e96 | 4 | #include <algorithm> |
| rjs718 | 3:4d7ff98b0e96 | 5 | #define TI 0.0001 //1kHz Sample time |
| rjs718 | 3:4d7ff98b0e96 | 6 | #define KP 1.0f //Proportional gain |
| rjs718 | 3:4d7ff98b0e96 | 7 | #define KI 1.0f //Integral gain |
| rjs718 | 3:4d7ff98b0e96 | 8 | #define LOWER_BOUNDARY 0.0f //Lower boundary |
| rjs718 | 3:4d7ff98b0e96 | 9 | #define UPPER_BOUNDARY 1.0f //upper boundary |
| rjs718 | 3:4d7ff98b0e96 | 10 | #define LOWERDC_BOUNDARY 0.4f1 //Lower boundary |
| rjs718 | 3:4d7ff98b0e96 | 11 | #define UPPERDC_BOUNDARY 1.0f //upper boundary |
| rjs718 | 2:060a86df835f | 12 | |
| rjs718 | 3:4d7ff98b0e96 | 13 | volatile float INTEGRAL_ERROR = 0.0; |
| rjs718 | 3:4d7ff98b0e96 | 14 | |
| rjs718 | 3:4d7ff98b0e96 | 15 | float errorArea; |
| rjs718 | 3:4d7ff98b0e96 | 16 | |
| rjs718 | 3:4d7ff98b0e96 | 17 | Terminal term(USBTX, USBRX); |
| rjs718 | 0:cb571e42343c | 18 | |
| rjs718 | 0:cb571e42343c | 19 | AnalogIn AI_in(PTB0); |
| rjs718 | 2:060a86df835f | 20 | PwmOut PWM_out(PTE21); |
| rjs718 | 3:4d7ff98b0e96 | 21 | AnalogIn Tach_in(PTB2); //0-3.3 feedback |
| rjs718 | 3:4d7ff98b0e96 | 22 | Ticker Time_Wizard; |
| rjs718 | 3:4d7ff98b0e96 | 23 | DigitalOut Adjustingled |
| rjs718 | 0:cb571e42343c | 24 | |
| rjs718 | 3:4d7ff98b0e96 | 25 | void Adjusting(){ |
| rjs718 | 3:4d7ff98b0e96 | 26 | float feedback = Tach_in.read(); |
| rjs718 | 3:4d7ff98b0e96 | 27 | float vref = AI_in.read(); |
| rjs718 | 3:4d7ff98b0e96 | 28 | float err = vref - feedback; |
| rjs718 | 3:4d7ff98b0e96 | 29 | errorArea += err * TI; |
| rjs718 | 3:4d7ff98b0e96 | 30 | errorArea = min(max(LOWER_BOUNDARY, errorArea),UPPER_BOUNDARY); |
| rjs718 | 3:4d7ff98b0e96 | 31 | float control_out = KP*error + KI*errorArea; |
| rjs718 | 3:4d7ff98b0e96 | 32 | control_out = min(max(LOWERDC_BOUNDARY, control_out),UPPERDC_BOUNDARY); |
| rjs718 | 3:4d7ff98b0e96 | 33 | PWM_out.write(control_out); |
| rjs718 | 3:4d7ff98b0e96 | 34 | |
| rjs718 | 3:4d7ff98b0e96 | 35 | } |
| rjs718 | 0:cb571e42343c | 36 | |
| rjs718 | 0:cb571e42343c | 37 | int main() { |
| rjs718 | 2:060a86df835f | 38 | PWM_out.period(.005); |
| rjs718 | 3:4d7ff98b0e96 | 39 | Time_Wizard.attach(&Adjusting, TI); |
| rjs718 | 0:cb571e42343c | 40 | while(1) { |
| rjs718 | 3:4d7ff98b0e96 | 41 | |
| rjs718 | 2:060a86df835f | 42 | term.cls(); |
| rjs718 | 2:060a86df835f | 43 | term.printf("\nVrefV:\r\n %f", 3.3*AI_in.read(), AI_in.read()); |
| rjs718 | 2:060a86df835f | 44 | term.printf("\nVrefADC/Duty: \r\n %f" ,AI_in.read() ); |
| rjs718 | 3:4d7ff98b0e96 | 45 | term.printf("\nVrefADC/Duty: \r\n %f" ,Tach_in.read() ); |
| rjs718 | 2:060a86df835f | 46 | |
| Loganlp | 1:5b2bc7ec7b14 | 47 | wait(.05); |
| rjs718 | 0:cb571e42343c | 48 | } |
| rjs718 | 0:cb571e42343c | 49 | } |