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
Fork of Driving by
main.cpp@1:c6d269c69853, 2018-03-29 (annotated)
- Committer:
- KDrainEE
- Date:
- Thu Mar 29 02:58:47 2018 +0000
- Revision:
- 1:c6d269c69853
- Parent:
- 0:8c9ee304ed9f
- Child:
- 2:1fd3a4aff5d3
hopefully optimized the code a bit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| KDrainEE | 0:8c9ee304ed9f | 1 | #include "mbed.h" |
| KDrainEE | 0:8c9ee304ed9f | 2 | #include <iostream> |
| KDrainEE | 0:8c9ee304ed9f | 3 | |
| KDrainEE | 0:8c9ee304ed9f | 4 | using namespace std; |
| KDrainEE | 0:8c9ee304ed9f | 5 | |
| KDrainEE | 1:c6d269c69853 | 6 | #define SCALAR 0.6f |
| KDrainEE | 1:c6d269c69853 | 7 | #define TACH PTB1 |
| KDrainEE | 1:c6d269c69853 | 8 | #define SPEED_CONTROL PTD5 |
| KDrainEE | 1:c6d269c69853 | 9 | #define BRAKE PTA13 |
| KDrainEE | 0:8c9ee304ed9f | 10 | |
| KDrainEE | 1:c6d269c69853 | 11 | #define TI 0.001f |
| KDrainEE | 1:c6d269c69853 | 12 | #define MINm 0.0f |
| KDrainEE | 1:c6d269c69853 | 13 | #define MAXm 1.0f |
| KDrainEE | 1:c6d269c69853 | 14 | #define KPm 0.1414f |
| KDrainEE | 1:c6d269c69853 | 15 | #define KI 19.7408f |
| KDrainEE | 0:8c9ee304ed9f | 16 | |
| KDrainEE | 1:c6d269c69853 | 17 | Ticker sample; |
| KDrainEE | 1:c6d269c69853 | 18 | AnalogIn speed(TACH); |
| KDrainEE | 1:c6d269c69853 | 19 | PwmOut gateDrive(SPEED_CONTROL); |
| KDrainEE | 1:c6d269c69853 | 20 | DigitalOut brake(BRAKE); |
| KDrainEE | 1:c6d269c69853 | 21 | |
| KDrainEE | 1:c6d269c69853 | 22 | float Setpoint; |
| KDrainEE | 1:c6d269c69853 | 23 | float errSum; |
| KDrainEE | 1:c6d269c69853 | 24 | |
| KDrainEE | 0:8c9ee304ed9f | 25 | Serial bt(PTE0, PTE1); //COM12 |
| KDrainEE | 0:8c9ee304ed9f | 26 | |
| KDrainEE | 0:8c9ee304ed9f | 27 | void serCb() |
| KDrainEE | 0:8c9ee304ed9f | 28 | { |
| KDrainEE | 0:8c9ee304ed9f | 29 | char x = bt.getc(); |
| KDrainEE | 0:8c9ee304ed9f | 30 | if (x == 'a') |
| KDrainEE | 0:8c9ee304ed9f | 31 | { |
| KDrainEE | 0:8c9ee304ed9f | 32 | Setpoint = 0.25; |
| KDrainEE | 0:8c9ee304ed9f | 33 | } |
| KDrainEE | 0:8c9ee304ed9f | 34 | else if(x == 's') |
| KDrainEE | 0:8c9ee304ed9f | 35 | { |
| KDrainEE | 0:8c9ee304ed9f | 36 | Setpoint = 0.6; |
| KDrainEE | 0:8c9ee304ed9f | 37 | } |
| KDrainEE | 0:8c9ee304ed9f | 38 | else |
| KDrainEE | 0:8c9ee304ed9f | 39 | { |
| KDrainEE | 0:8c9ee304ed9f | 40 | bt.putc(x); |
| KDrainEE | 0:8c9ee304ed9f | 41 | } |
| KDrainEE | 0:8c9ee304ed9f | 42 | } |
| KDrainEE | 0:8c9ee304ed9f | 43 | |
| KDrainEE | 1:c6d269c69853 | 44 | void compute_PI() |
| KDrainEE | 0:8c9ee304ed9f | 45 | { |
| KDrainEE | 1:c6d269c69853 | 46 | float error = Setpoint-speed.read(); |
| KDrainEE | 1:c6d269c69853 | 47 | errSum +=(error * TI); |
| KDrainEE | 1:c6d269c69853 | 48 | float iTerm = KI*errSum; |
| KDrainEE | 1:c6d269c69853 | 49 | if(iTerm > MAXm) iTerm = MAXm; |
| KDrainEE | 1:c6d269c69853 | 50 | if(iTerm < MINm) iTerm = MINm; |
| KDrainEE | 1:c6d269c69853 | 51 | float output = KPm*error + iTerm; |
| KDrainEE | 1:c6d269c69853 | 52 | if(output > MAXm) output = MAXm; |
| KDrainEE | 1:c6d269c69853 | 53 | if(output < MINm) output = MINm; |
| KDrainEE | 1:c6d269c69853 | 54 | if(output < MINm) |
| KDrainEE | 1:c6d269c69853 | 55 | { |
| KDrainEE | 1:c6d269c69853 | 56 | gateDrive.write(MINm); |
| KDrainEE | 1:c6d269c69853 | 57 | brake.write(1); |
| KDrainEE | 1:c6d269c69853 | 58 | } |
| KDrainEE | 1:c6d269c69853 | 59 | else |
| KDrainEE | 1:c6d269c69853 | 60 | { |
| KDrainEE | 1:c6d269c69853 | 61 | brake.write(0); |
| KDrainEE | 1:c6d269c69853 | 62 | gateDrive.write(output); |
| KDrainEE | 1:c6d269c69853 | 63 | } |
| KDrainEE | 0:8c9ee304ed9f | 64 | } |
| KDrainEE | 0:8c9ee304ed9f | 65 | |
| KDrainEE | 0:8c9ee304ed9f | 66 | int main() |
| KDrainEE | 1:c6d269c69853 | 67 | { |
| KDrainEE | 1:c6d269c69853 | 68 | Setpoint = 0.0; |
| KDrainEE | 1:c6d269c69853 | 69 | errSum = 0.0; |
| KDrainEE | 1:c6d269c69853 | 70 | sample.attach(&compute_PI, TI); |
| KDrainEE | 0:8c9ee304ed9f | 71 | |
| KDrainEE | 0:8c9ee304ed9f | 72 | bt.baud(115200); |
| KDrainEE | 0:8c9ee304ed9f | 73 | bt.printf("Press 'a' to go 25 and 's' to go 60%\r\n"); |
| KDrainEE | 0:8c9ee304ed9f | 74 | bt.attach(&serCb); |
| KDrainEE | 1:c6d269c69853 | 75 | |
| KDrainEE | 1:c6d269c69853 | 76 | while(true) |
| KDrainEE | 0:8c9ee304ed9f | 77 | { |
| KDrainEE | 1:c6d269c69853 | 78 | |
| KDrainEE | 1:c6d269c69853 | 79 | } |
| KDrainEE | 0:8c9ee304ed9f | 80 | } |
