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: Encoder MODSERIAL mbed-dsp mbed
Fork of motor1aansturing by
main.cpp@14:6ec84bfda0f3, 2014-10-08 (annotated)
- Committer:
- Jolein
- Date:
- Wed Oct 08 12:56:20 2014 +0000
- Revision:
- 14:6ec84bfda0f3
- Parent:
- 13:76a40e4c8982
- Child:
- 15:3a6074f3ceaf
wat kleine verbeteringen;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jolein | 0:eb00992c1597 | 1 | #include "mbed.h" |
Jolein | 0:eb00992c1597 | 2 | #include "encoder.h" |
Jolein | 0:eb00992c1597 | 3 | #define TSAMP 0.01 |
Jolein | 0:eb00992c1597 | 4 | #define K_P (0.01) |
Jolein | 0:eb00992c1597 | 5 | #define K_I (0 *TSAMP) |
Jolein | 2:ef0fa691e77e | 6 | #define K_D (0 /TSAMP) |
Jolein | 0:eb00992c1597 | 7 | #define I_LIMIT 1. |
Jolein | 0:eb00992c1597 | 8 | #define POT_AVG 50 |
Jolein | 0:eb00992c1597 | 9 | |
Jolein | 0:eb00992c1597 | 10 | void clamp(float * in, float min, float max); |
Jolein | 0:eb00992c1597 | 11 | float pid(float setpoint, float measurement); |
Jolein | 2:ef0fa691e77e | 12 | //-------------------------------------------------------------------------------------------Input potentiometer------------------------------- |
Jolein | 0:eb00992c1597 | 13 | AnalogIn potmeter(PTC2); |
Jolein | 0:eb00992c1597 | 14 | volatile bool looptimerflag; |
Jolein | 0:eb00992c1597 | 15 | float potsamples[POT_AVG]; |
Jolein | 0:eb00992c1597 | 16 | |
Jolein | 0:eb00992c1597 | 17 | |
Jolein | 0:eb00992c1597 | 18 | void setlooptimerflag(void) |
Jolein | 0:eb00992c1597 | 19 | { |
Jolein | 0:eb00992c1597 | 20 | looptimerflag = true; |
Jolein | 0:eb00992c1597 | 21 | } |
Jolein | 0:eb00992c1597 | 22 | |
Jolein | 0:eb00992c1597 | 23 | int main() |
Jolein | 0:eb00992c1597 | 24 | { |
Jolein | 14:6ec84bfda0f3 | 25 | int toestand; |
Jolein | 14:6ec84bfda0f3 | 26 | |
Jolein | 0:eb00992c1597 | 27 | Encoder motor1(PTD0,PTC9); |
Jolein | 0:eb00992c1597 | 28 | PwmOut pwm_motor(PTA5); |
Jolein | 0:eb00992c1597 | 29 | pwm_motor.period_us(100); |
Jolein | 0:eb00992c1597 | 30 | DigitalOut motordir(PTD1); |
Jolein | 0:eb00992c1597 | 31 | Ticker looptimer; |
Jolein | 0:eb00992c1597 | 32 | looptimer.attach(setlooptimerflag,TSAMP); |
Jolein | 0:eb00992c1597 | 33 | while(1) { |
Jolein | 0:eb00992c1597 | 34 | float setpoint; |
Jolein | 0:eb00992c1597 | 35 | float new_pwm; |
Jolein | 0:eb00992c1597 | 36 | while(!looptimerflag); |
Jolein | 2:ef0fa691e77e | 37 | looptimerflag = false; |
Jolein | 12:92a64fe08de8 | 38 | //---------------------------------------------------------leest potentiometer (?), probably won't do what I want it to do ------------------ |
Jolein | 12:92a64fe08de8 | 39 | //---------------------------------------------------------int 6? --------------------------------------------------------------------------- |
Jolein | 14:6ec84bfda0f3 | 40 | if(toestand==2) |
Jolein | 4:2a3b8c562f4a | 41 | { |
Jolein | 14:6ec84bfda0f3 | 42 | if ( 4.5 < potmeter.read() && potmeter.read()< 6.0 && angle<maxangle) |
Jolein | 14:6ec84bfda0f3 | 43 | { |
Jolein | 14:6ec84bfda0f3 | 44 | setpoint = v3; //experimenteel bepaald, snelheid om bovenste goal te raken |
Jolein | 14:6ec84bfda0f3 | 45 | } |
Jolein | 14:6ec84bfda0f3 | 46 | else if ( 3 < potmeter.read()) |
Jolein | 14:6ec84bfda0f3 | 47 | { |
Jolein | 14:6ec84bfda0f3 | 48 | setpoint = v2; //experimenteel bepaald, snelheid om middelste goal te raken |
Jolein | 14:6ec84bfda0f3 | 49 | } |
Jolein | 14:6ec84bfda0f3 | 50 | else if (1.5 < potmeter.read()) |
Jolein | 14:6ec84bfda0f3 | 51 | { |
Jolein | 14:6ec84bfda0f3 | 52 | setpoint = v1; //experimenteel bepaald, snelheid om bovenste goal te raken |
Jolein | 14:6ec84bfda0f3 | 53 | } |
Jolein | 14:6ec84bfda0f3 | 54 | else |
Jolein | 14:6ec84bfda0f3 | 55 | { |
Jolein | 14:6ec84bfda0f3 | 56 | setpoint = 0; |
Jolein | 14:6ec84bfda0f3 | 57 | } |
Jolein | 4:2a3b8c562f4a | 58 | } |
Jolein | 4:2a3b8c562f4a | 59 | // setpoint = (potmeter.read()-.5)*500; |
Jolein | 4:2a3b8c562f4a | 60 | //----------------------------------------------------------------------new_pwm = getal?.....---------------------------------------------------- |
Jolein | 0:eb00992c1597 | 61 | new_pwm = pid(setpoint, motor1.getPosition()); |
Jolein | 0:eb00992c1597 | 62 | clamp(&new_pwm, -1,1); |
Jolein | 4:2a3b8c562f4a | 63 | //-------------------------------------------------------------------------------------output motor richting------------------------------------- |
Jolein | 0:eb00992c1597 | 64 | if(new_pwm > 0) |
Jolein | 0:eb00992c1597 | 65 | motordir = 0; |
Jolein | 0:eb00992c1597 | 66 | else |
Jolein | 0:eb00992c1597 | 67 | motordir = 1; |
Jolein | 4:2a3b8c562f4a | 68 | //-------------------------------------------------------------------------------output motorsnelhied (wij willen 4 distinctive stappen)--------- |
Jolein | 0:eb00992c1597 | 69 | pwm_motor.write(abs(new_pwm)); |
Jolein | 14:6ec84bfda0f3 | 70 | if(angle>anglemax) |
Jolein | 14:6ec84bfda0f3 | 71 | { |
Jolein | 14:6ec84bfda0f3 | 72 | toestand = terugkeren; |
Jolein | 14:6ec84bfda0f3 | 73 | } |
Jolein | 0:eb00992c1597 | 74 | } |
Jolein | 14:6ec84bfda0f3 | 75 | |
Jolein | 0:eb00992c1597 | 76 | } |
Jolein | 0:eb00992c1597 | 77 | |
Jolein | 0:eb00992c1597 | 78 | void clamp(float * in, float min, float max) |
Jolein | 0:eb00992c1597 | 79 | { |
Jolein | 0:eb00992c1597 | 80 | *in > min ? *in < max? : *in = max: *in = min; |
Jolein | 0:eb00992c1597 | 81 | } |
Jolein | 0:eb00992c1597 | 82 | |
Jolein | 0:eb00992c1597 | 83 | |
Jolein | 0:eb00992c1597 | 84 | float pid(float setpoint, float measurement) |
Jolein | 0:eb00992c1597 | 85 | { |
Jolein | 12:92a64fe08de8 | 86 | //What I want: if getposition gives value beneath min v=0!, If above max: return to 0 position(from the way it came) |
Jolein | 0:eb00992c1597 | 87 | float error; |
Jolein | 0:eb00992c1597 | 88 | static float prev_error = 0; |
Jolein | 0:eb00992c1597 | 89 | float out_p = 0; |
Jolein | 0:eb00992c1597 | 90 | static float out_i = 0; |
Jolein | 0:eb00992c1597 | 91 | float out_d = 0; |
Jolein | 0:eb00992c1597 | 92 | error = setpoint-measurement; |
Jolein | 0:eb00992c1597 | 93 | out_p = error*K_P; |
Jolein | 0:eb00992c1597 | 94 | out_i += error*K_I; |
Jolein | 0:eb00992c1597 | 95 | out_d = (error-prev_error)*K_D; |
Jolein | 0:eb00992c1597 | 96 | clamp(&out_i,-I_LIMIT,I_LIMIT); |
Jolein | 0:eb00992c1597 | 97 | prev_error = error; |
Jolein | 0:eb00992c1597 | 98 | return out_p + out_i + out_d; |
Jolein | 0:eb00992c1597 | 99 | } |