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@3:f557fc495719, 2018-03-29 (annotated)
- Committer:
- KDrainEE
- Date:
- Thu Mar 29 18:12:44 2018 +0000
- Revision:
- 3:f557fc495719
- Parent:
- 2:1fd3a4aff5d3
Seems to work fairly well again
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 | 1:c6d269c69853 | 4 | #define SCALAR 0.6f |
KDrainEE | 1:c6d269c69853 | 5 | #define TACH PTB1 |
KDrainEE | 1:c6d269c69853 | 6 | #define SPEED_CONTROL PTD5 |
KDrainEE | 1:c6d269c69853 | 7 | #define BRAKE PTA13 |
KDrainEE | 0:8c9ee304ed9f | 8 | |
KDrainEE | 1:c6d269c69853 | 9 | #define TI 0.001f |
KDrainEE | 3:f557fc495719 | 10 | #define MINM 0.0f |
KDrainEE | 3:f557fc495719 | 11 | #define MAXM 1.0f |
KDrainEE | 3:f557fc495719 | 12 | #define KPM 0.1414f |
KDrainEE | 1:c6d269c69853 | 13 | #define KI 19.7408f |
KDrainEE | 0:8c9ee304ed9f | 14 | |
KDrainEE | 1:c6d269c69853 | 15 | Ticker sample; |
KDrainEE | 1:c6d269c69853 | 16 | AnalogIn speed(TACH); |
KDrainEE | 1:c6d269c69853 | 17 | PwmOut gateDrive(SPEED_CONTROL); |
KDrainEE | 3:f557fc495719 | 18 | //DigitalOut brake(BRAKE); |
KDrainEE | 3:f557fc495719 | 19 | DigitalOut myLed(LED_GREEN); |
KDrainEE | 1:c6d269c69853 | 20 | |
KDrainEE | 3:f557fc495719 | 21 | float Setpoint = 0.0; |
KDrainEE | 3:f557fc495719 | 22 | float errSum = 0.0; |
KDrainEE | 1:c6d269c69853 | 23 | |
KDrainEE | 3:f557fc495719 | 24 | //Serial pc(USBTX, USBRX); |
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 | 3:f557fc495719 | 30 | if (x == 'u') |
KDrainEE | 0:8c9ee304ed9f | 31 | { |
KDrainEE | 3:f557fc495719 | 32 | Setpoint = Setpoint + 0.05; |
KDrainEE | 0:8c9ee304ed9f | 33 | } |
KDrainEE | 3:f557fc495719 | 34 | else if(x == 'h') |
KDrainEE | 0:8c9ee304ed9f | 35 | { |
KDrainEE | 3:f557fc495719 | 36 | Setpoint = Setpoint - 0.05; |
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 | 3:f557fc495719 | 49 | if(iTerm > MAXM) iTerm = MAXM; |
KDrainEE | 3:f557fc495719 | 50 | if(iTerm < MINM) iTerm = MINM; |
KDrainEE | 3:f557fc495719 | 51 | float output = KPM*error + iTerm; |
KDrainEE | 3:f557fc495719 | 52 | if(output > MAXM) output = MAXM; |
KDrainEE | 3:f557fc495719 | 53 | if(output < MINM) output = MINM; |
KDrainEE | 3:f557fc495719 | 54 | if(output < MINM) |
KDrainEE | 1:c6d269c69853 | 55 | { |
KDrainEE | 3:f557fc495719 | 56 | gateDrive.write(MINM); |
KDrainEE | 3:f557fc495719 | 57 | //brake.write(1); |
KDrainEE | 1:c6d269c69853 | 58 | } |
KDrainEE | 1:c6d269c69853 | 59 | else |
KDrainEE | 1:c6d269c69853 | 60 | { |
KDrainEE | 3:f557fc495719 | 61 | //brake.write(0); |
KDrainEE | 1:c6d269c69853 | 62 | gateDrive.write(output); |
KDrainEE | 3:f557fc495719 | 63 | } |
KDrainEE | 3:f557fc495719 | 64 | //data[1] = output; |
KDrainEE | 0:8c9ee304ed9f | 65 | } |
KDrainEE | 0:8c9ee304ed9f | 66 | |
KDrainEE | 0:8c9ee304ed9f | 67 | int main() |
KDrainEE | 1:c6d269c69853 | 68 | { |
KDrainEE | 1:c6d269c69853 | 69 | sample.attach(&compute_PI, TI); |
KDrainEE | 3:f557fc495719 | 70 | |
KDrainEE | 3:f557fc495719 | 71 | myLed = 0; |
KDrainEE | 0:8c9ee304ed9f | 72 | bt.baud(115200); |
KDrainEE | 3:f557fc495719 | 73 | bt.printf("Press 'u' to speed up and 'h' to slow down%\r\n"); |
KDrainEE | 3:f557fc495719 | 74 | |
KDrainEE | 0:8c9ee304ed9f | 75 | bt.attach(&serCb); |
KDrainEE | 1:c6d269c69853 | 76 | while(true) |
KDrainEE | 3:f557fc495719 | 77 | {} |
KDrainEE | 0:8c9ee304ed9f | 78 | } |