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@0:30871514c229, 2018-04-12 (annotated)
- Committer:
- KDrainEE
- Date:
- Thu Apr 12 21:07:30 2018 +0000
- Revision:
- 0:30871514c229
- Child:
- 1:9149cfedd4d5
She fast. But if the speed sensor is malfunctioning she a rocket.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
KDrainEE | 0:30871514c229 | 1 | //Works at speed 2.0, KPS 2.0e-2, KD 1.0e-4 |
KDrainEE | 0:30871514c229 | 2 | |
KDrainEE | 0:30871514c229 | 3 | //Has tolerance check, Steering doesn't |
KDrainEE | 0:30871514c229 | 4 | //Values from Simulation |
KDrainEE | 0:30871514c229 | 5 | //#define KPS 0.0896852742245504f |
KDrainEE | 0:30871514c229 | 6 | //#define KD 0.072870569295611f |
KDrainEE | 0:30871514c229 | 7 | //#define KPS 0.03f |
KDrainEE | 0:30871514c229 | 8 | |
KDrainEE | 0:30871514c229 | 9 | #include "mbed.h" |
KDrainEE | 0:30871514c229 | 10 | #include <iostream> |
KDrainEE | 0:30871514c229 | 11 | |
KDrainEE | 0:30871514c229 | 12 | #define TI 0.001f |
KDrainEE | 0:30871514c229 | 13 | |
KDrainEE | 0:30871514c229 | 14 | #define SCALAR 0.6f |
KDrainEE | 0:30871514c229 | 15 | #define MINM 0.0f |
KDrainEE | 0:30871514c229 | 16 | #define MAXM 1.0f |
KDrainEE | 0:30871514c229 | 17 | #define KPM 0.1414f |
KDrainEE | 0:30871514c229 | 18 | #define KI 19.7408f |
KDrainEE | 0:30871514c229 | 19 | |
KDrainEE | 0:30871514c229 | 20 | #define KPS 2.0E-2f //Original 2.0e-2 |
KDrainEE | 0:30871514c229 | 21 | #define KD 1.0e-4f |
KDrainEE | 0:30871514c229 | 22 | #define SET 0.0f |
KDrainEE | 0:30871514c229 | 23 | #define MINS 0.05f |
KDrainEE | 0:30871514c229 | 24 | #define MAXS 0.1f |
KDrainEE | 0:30871514c229 | 25 | #define BIAS 0.075f |
KDrainEE | 0:30871514c229 | 26 | #define TOL 0.02f |
KDrainEE | 0:30871514c229 | 27 | #define STEER_FREQ 0.02f //50 Hz |
KDrainEE | 0:30871514c229 | 28 | #define STEERUPDATEPERIOD 0.05 |
KDrainEE | 0:30871514c229 | 29 | |
KDrainEE | 0:30871514c229 | 30 | AnalogIn _right(PTB1); //Right sensor |
KDrainEE | 0:30871514c229 | 31 | AnalogIn _left(PTB3); //Left sensor |
KDrainEE | 0:30871514c229 | 32 | AnalogIn speed(PTB2); //tachometer |
KDrainEE | 0:30871514c229 | 33 | |
KDrainEE | 0:30871514c229 | 34 | PwmOut servoSig(PTA13); //PWM output to control servo |
KDrainEE | 0:30871514c229 | 35 | PwmOut gateDrive(PTA4); |
KDrainEE | 0:30871514c229 | 36 | |
KDrainEE | 0:30871514c229 | 37 | Serial bt(PTE0, PTE1); //COM12 |
KDrainEE | 0:30871514c229 | 38 | DigitalOut myled(LED_BLUE); |
KDrainEE | 0:30871514c229 | 39 | |
KDrainEE | 0:30871514c229 | 40 | Ticker control; |
KDrainEE | 0:30871514c229 | 41 | Timer ctrlTimer; |
KDrainEE | 0:30871514c229 | 42 | float data[6]; |
KDrainEE | 0:30871514c229 | 43 | |
KDrainEE | 0:30871514c229 | 44 | float Setpoint = 0.0f; |
KDrainEE | 0:30871514c229 | 45 | float errSum = 0.0; |
KDrainEE | 0:30871514c229 | 46 | |
KDrainEE | 0:30871514c229 | 47 | float fbPrev = 0.0f; |
KDrainEE | 0:30871514c229 | 48 | |
KDrainEE | 0:30871514c229 | 49 | void serCb() |
KDrainEE | 0:30871514c229 | 50 | { |
KDrainEE | 0:30871514c229 | 51 | char x = bt.getc(); |
KDrainEE | 0:30871514c229 | 52 | if (x == 'u') |
KDrainEE | 0:30871514c229 | 53 | { |
KDrainEE | 0:30871514c229 | 54 | Setpoint = Setpoint + 0.05; |
KDrainEE | 0:30871514c229 | 55 | } |
KDrainEE | 0:30871514c229 | 56 | else if(x == 'h') |
KDrainEE | 0:30871514c229 | 57 | { |
KDrainEE | 0:30871514c229 | 58 | Setpoint = Setpoint - 0.05; |
KDrainEE | 0:30871514c229 | 59 | } |
KDrainEE | 0:30871514c229 | 60 | else |
KDrainEE | 0:30871514c229 | 61 | { |
KDrainEE | 0:30871514c229 | 62 | bt.putc(x); |
KDrainEE | 0:30871514c229 | 63 | } |
KDrainEE | 0:30871514c229 | 64 | if(Setpoint > MAXM) Setpoint = MAXM; |
KDrainEE | 0:30871514c229 | 65 | if(Setpoint < MINM) Setpoint = MINM; |
KDrainEE | 0:30871514c229 | 66 | bt.printf("%f\r\n", Setpoint); |
KDrainEE | 0:30871514c229 | 67 | } |
KDrainEE | 0:30871514c229 | 68 | |
KDrainEE | 0:30871514c229 | 69 | void stop() |
KDrainEE | 0:30871514c229 | 70 | { |
KDrainEE | 0:30871514c229 | 71 | Setpoint = 0.0f; |
KDrainEE | 0:30871514c229 | 72 | } |
KDrainEE | 0:30871514c229 | 73 | |
KDrainEE | 0:30871514c229 | 74 | void steer() |
KDrainEE | 0:30871514c229 | 75 | { |
KDrainEE | 0:30871514c229 | 76 | float L = _left.read(); |
KDrainEE | 0:30871514c229 | 77 | float R = _right.read(); |
KDrainEE | 0:30871514c229 | 78 | if(L == 0.0f && R == 0.0f) |
KDrainEE | 0:30871514c229 | 79 | { |
KDrainEE | 0:30871514c229 | 80 | stop(); |
KDrainEE | 0:30871514c229 | 81 | } |
KDrainEE | 0:30871514c229 | 82 | float fb = L - R; |
KDrainEE | 0:30871514c229 | 83 | float e = SET - fb; |
KDrainEE | 0:30871514c229 | 84 | float Controlleroutput = KPS * e - (KD * (fb - fbPrev)/TI)+ BIAS;//subtract derivative of error?? |
KDrainEE | 0:30871514c229 | 85 | if (Controlleroutput > MAXS) Controlleroutput = MAXS; |
KDrainEE | 0:30871514c229 | 86 | else if (Controlleroutput < MINS) Controlleroutput = MINS; |
KDrainEE | 0:30871514c229 | 87 | if (abs(Controlleroutput - servoSig.read()) > TOL || ctrlTimer.read() >= STEERUPDATEPERIOD) |
KDrainEE | 0:30871514c229 | 88 | { |
KDrainEE | 0:30871514c229 | 89 | ctrlTimer.reset(); |
KDrainEE | 0:30871514c229 | 90 | servoSig.write(Controlleroutput); |
KDrainEE | 0:30871514c229 | 91 | } |
KDrainEE | 0:30871514c229 | 92 | fbPrev = fb; |
KDrainEE | 0:30871514c229 | 93 | data[0] = _right.read(); |
KDrainEE | 0:30871514c229 | 94 | data[1] = _left.read(); |
KDrainEE | 0:30871514c229 | 95 | data[2] = Controlleroutput; |
KDrainEE | 0:30871514c229 | 96 | data[3] = e; |
KDrainEE | 0:30871514c229 | 97 | |
KDrainEE | 0:30871514c229 | 98 | } |
KDrainEE | 0:30871514c229 | 99 | void drive() |
KDrainEE | 0:30871514c229 | 100 | { |
KDrainEE | 0:30871514c229 | 101 | float error = Setpoint-speed.read(); |
KDrainEE | 0:30871514c229 | 102 | errSum +=(error * TI); |
KDrainEE | 0:30871514c229 | 103 | float iTerm = KI*errSum; |
KDrainEE | 0:30871514c229 | 104 | if(iTerm > MAXM) iTerm = MAXM; |
KDrainEE | 0:30871514c229 | 105 | if(iTerm < MINM) iTerm = MINM; |
KDrainEE | 0:30871514c229 | 106 | float output = KPM*error + iTerm; |
KDrainEE | 0:30871514c229 | 107 | if(output > MAXM) output = MAXM; |
KDrainEE | 0:30871514c229 | 108 | if(output < MINM) output = MINM; |
KDrainEE | 0:30871514c229 | 109 | if(output < MINM) |
KDrainEE | 0:30871514c229 | 110 | { |
KDrainEE | 0:30871514c229 | 111 | gateDrive.write(MINM); |
KDrainEE | 0:30871514c229 | 112 | //brake.write(1); |
KDrainEE | 0:30871514c229 | 113 | } |
KDrainEE | 0:30871514c229 | 114 | else |
KDrainEE | 0:30871514c229 | 115 | { |
KDrainEE | 0:30871514c229 | 116 | //brake.write(0); |
KDrainEE | 0:30871514c229 | 117 | gateDrive.write(output); |
KDrainEE | 0:30871514c229 | 118 | } |
KDrainEE | 0:30871514c229 | 119 | data[4] = gateDrive.read(); |
KDrainEE | 0:30871514c229 | 120 | data[5] = speed.read(); |
KDrainEE | 0:30871514c229 | 121 | } |
KDrainEE | 0:30871514c229 | 122 | |
KDrainEE | 0:30871514c229 | 123 | void cb() |
KDrainEE | 0:30871514c229 | 124 | { |
KDrainEE | 0:30871514c229 | 125 | steer(); |
KDrainEE | 0:30871514c229 | 126 | drive(); |
KDrainEE | 0:30871514c229 | 127 | } |
KDrainEE | 0:30871514c229 | 128 | |
KDrainEE | 0:30871514c229 | 129 | int main() |
KDrainEE | 0:30871514c229 | 130 | { |
KDrainEE | 0:30871514c229 | 131 | //startup checks |
KDrainEE | 0:30871514c229 | 132 | bt.attach(&serCb); |
KDrainEE | 0:30871514c229 | 133 | servoSig.period(STEER_FREQ); |
KDrainEE | 0:30871514c229 | 134 | gateDrive.period(.00005f); |
KDrainEE | 0:30871514c229 | 135 | gateDrive.write(Setpoint); |
KDrainEE | 0:30871514c229 | 136 | |
KDrainEE | 0:30871514c229 | 137 | ctrlTimer.start(); |
KDrainEE | 0:30871514c229 | 138 | control.attach(&cb, TI); |
KDrainEE | 0:30871514c229 | 139 | |
KDrainEE | 0:30871514c229 | 140 | bt.baud(115200); |
KDrainEE | 0:30871514c229 | 141 | bt.printf("Right Left SteerControl SteerError GateDrive Speed\r\n"); |
KDrainEE | 0:30871514c229 | 142 | while(1) { |
KDrainEE | 0:30871514c229 | 143 | myled = !myled; |
KDrainEE | 0:30871514c229 | 144 | bt.printf("%f ",data[0]); |
KDrainEE | 0:30871514c229 | 145 | bt.printf("%f ", data[1]); |
KDrainEE | 0:30871514c229 | 146 | bt.printf("%f ", data[2]); |
KDrainEE | 0:30871514c229 | 147 | bt.printf("%f ", data[3]); |
KDrainEE | 0:30871514c229 | 148 | bt.printf("%f ", data[4]); |
KDrainEE | 0:30871514c229 | 149 | bt.printf("%f\r\n", data[5]); |
KDrainEE | 0:30871514c229 | 150 | } |
KDrainEE | 0:30871514c229 | 151 | } |