Programma om motor 1 aan te sturen
Dependencies: Encoder MODSERIAL mbed-dsp mbed
Fork of motor1aansturing by
main.cpp@16:bf4c5affd3e9, 2014-10-10 (annotated)
- Committer:
- Jolein
- Date:
- Fri Oct 10 07:56:24 2014 +0000
- Revision:
- 16:bf4c5affd3e9
- Parent:
- 15:3a6074f3ceaf
- Child:
- 17:0116e2f17d0d
added if's for setting toestand
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 | 15:3a6074f3ceaf | 3 | #include "MODSERIAL.h" |
Jolein | 15:3a6074f3ceaf | 4 | |
Jolein | 0:eb00992c1597 | 5 | #define TSAMP 0.01 |
Jolein | 0:eb00992c1597 | 6 | #define K_P (0.01) |
Jolein | 0:eb00992c1597 | 7 | #define K_I (0 *TSAMP) |
Jolein | 2:ef0fa691e77e | 8 | #define K_D (0 /TSAMP) |
Jolein | 0:eb00992c1597 | 9 | #define I_LIMIT 1. |
Jolein | 0:eb00992c1597 | 10 | #define POT_AVG 50 |
Jolein | 16:bf4c5affd3e9 | 11 | #define WACHTEN 1 |
Jolein | 15:3a6074f3ceaf | 12 | #define SLAAN 2 |
Jolein | 15:3a6074f3ceaf | 13 | #define TERUGKEREN 3 |
Jolein | 0:eb00992c1597 | 14 | |
Jolein | 15:3a6074f3ceaf | 15 | MODSERIAL pc(USBTX,USBRX); |
Jolein | 0:eb00992c1597 | 16 | void clamp(float * in, float min, float max); |
Jolein | 0:eb00992c1597 | 17 | float pid(float setpoint, float measurement); |
Jolein | 2:ef0fa691e77e | 18 | //-------------------------------------------------------------------------------------------Input potentiometer------------------------------- |
Jolein | 0:eb00992c1597 | 19 | AnalogIn potmeter(PTC2); |
Jolein | 0:eb00992c1597 | 20 | volatile bool looptimerflag; |
Jolein | 0:eb00992c1597 | 21 | float potsamples[POT_AVG]; |
Jolein | 16:bf4c5affd3e9 | 22 | int EMG(float biceps, float triceps) // misschien niet float omdat input functies zijn |
Jolein | 0:eb00992c1597 | 23 | |
Jolein | 0:eb00992c1597 | 24 | void setlooptimerflag(void) |
Jolein | 0:eb00992c1597 | 25 | { |
Jolein | 0:eb00992c1597 | 26 | looptimerflag = true; |
Jolein | 0:eb00992c1597 | 27 | } |
Jolein | 0:eb00992c1597 | 28 | |
Jolein | 0:eb00992c1597 | 29 | int main() |
Jolein | 0:eb00992c1597 | 30 | { |
Jolein | 16:bf4c5affd3e9 | 31 | //----------------------------------------------------------------dit is de initiële toestand zodat hij begint met hoek 0, moet misschien nog een wait achter |
Jolein | 16:bf4c5affd3e9 | 32 | //-----------------------------------------------------en de snelheid van toestand 1,2,3 moeten bepaald worden in het script. |
Jolein | 15:3a6074f3ceaf | 33 | float angle,v3=0.2,v2=0.1,v1=0.05,anglemax=1 |
Jolein | 0:eb00992c1597 | 34 | Encoder motor1(PTD0,PTC9); |
Jolein | 0:eb00992c1597 | 35 | PwmOut pwm_motor(PTA5); |
Jolein | 0:eb00992c1597 | 36 | pwm_motor.period_us(100); |
Jolein | 0:eb00992c1597 | 37 | DigitalOut motordir(PTD1); |
Jolein | 16:bf4c5affd3e9 | 38 | |
Jolein | 16:bf4c5affd3e9 | 39 | int toestand = TERUGKEREN; |
Jolein | 16:bf4c5affd3e9 | 40 | |
Jolein | 0:eb00992c1597 | 41 | Ticker looptimer; |
Jolein | 0:eb00992c1597 | 42 | looptimer.attach(setlooptimerflag,TSAMP); |
Jolein | 0:eb00992c1597 | 43 | while(1) { |
Jolein | 0:eb00992c1597 | 44 | float setpoint; |
Jolein | 0:eb00992c1597 | 45 | float new_pwm; |
Jolein | 0:eb00992c1597 | 46 | while(!looptimerflag); |
Jolein | 2:ef0fa691e77e | 47 | looptimerflag = false; |
Jolein | 16:bf4c5affd3e9 | 48 | |
Jolein | 16:bf4c5affd3e9 | 49 | if (angle<anglemin) |
Jolein | 16:bf4c5affd3e9 | 50 | { |
Jolein | 16:bf4c5affd3e9 | 51 | toestand = WACHTEN; |
Jolein | 16:bf4c5affd3e9 | 52 | } |
Jolein | 16:bf4c5affd3e9 | 53 | |
Jolein | 16:bf4c5affd3e9 | 54 | if (toestand = WACHTEN && EMG(readbiceps, readtriceps) != 0) |
Jolein | 16:bf4c5affd3e9 | 55 | { |
Jolein | 16:bf4c5affd3e9 | 56 | toestand = SLAAN |
Jolein | 16:bf4c5affd3e9 | 57 | } |
Jolein | 16:bf4c5affd3e9 | 58 | // De toestand moet vanaf wacht naar slaan gaan bij aanspanning van spier(1) Hier zit waarschijnlijk een delay in |
Jolein | 16:bf4c5affd3e9 | 59 | // vanwege het middelen over de tijd van het signaal. van MK en Tanja stukje script met functie met return (0/1/2 of 3). |
Jolein | 16:bf4c5affd3e9 | 60 | // angle = ???? Encoder |
Jolein | 15:3a6074f3ceaf | 61 | if(toestand==SLAAN) |
Jolein | 4:2a3b8c562f4a | 62 | { |
Jolein | 16:bf4c5affd3e9 | 63 | // potmeter.read() wordt waarschijnlijk output EMG functie (0,1,2,3) |
Jolein | 15:3a6074f3ceaf | 64 | if ( 0.75 < potmeter.read() && angle<anglemax) //angle<anglemax moet ergens anders staan (iets met toestand) |
Jolein | 14:6ec84bfda0f3 | 65 | { |
Jolein | 14:6ec84bfda0f3 | 66 | setpoint = v3; //experimenteel bepaald, snelheid om bovenste goal te raken |
Jolein | 14:6ec84bfda0f3 | 67 | } |
Jolein | 15:3a6074f3ceaf | 68 | else if ( 0.50 < potmeter.read()) |
Jolein | 14:6ec84bfda0f3 | 69 | { |
Jolein | 14:6ec84bfda0f3 | 70 | setpoint = v2; //experimenteel bepaald, snelheid om middelste goal te raken |
Jolein | 14:6ec84bfda0f3 | 71 | } |
Jolein | 15:3a6074f3ceaf | 72 | else if (0.25 < potmeter.read()) |
Jolein | 14:6ec84bfda0f3 | 73 | { |
Jolein | 14:6ec84bfda0f3 | 74 | setpoint = v1; //experimenteel bepaald, snelheid om bovenste goal te raken |
Jolein | 14:6ec84bfda0f3 | 75 | } |
Jolein | 14:6ec84bfda0f3 | 76 | else |
Jolein | 14:6ec84bfda0f3 | 77 | { |
Jolein | 15:3a6074f3ceaf | 78 | setpoint = 0; // toestand = TERGUGKEREN? |
Jolein | 14:6ec84bfda0f3 | 79 | } |
Jolein | 4:2a3b8c562f4a | 80 | } |
Jolein | 15:3a6074f3ceaf | 81 | //----------------------------------------------------------------------new_pwm = getal---------------------------------------------------- |
Jolein | 0:eb00992c1597 | 82 | new_pwm = pid(setpoint, motor1.getPosition()); |
Jolein | 0:eb00992c1597 | 83 | clamp(&new_pwm, -1,1); |
Jolein | 4:2a3b8c562f4a | 84 | //-------------------------------------------------------------------------------------output motor richting------------------------------------- |
Jolein | 0:eb00992c1597 | 85 | if(new_pwm > 0) |
Jolein | 0:eb00992c1597 | 86 | motordir = 0; |
Jolein | 0:eb00992c1597 | 87 | else |
Jolein | 0:eb00992c1597 | 88 | motordir = 1; |
Jolein | 0:eb00992c1597 | 89 | pwm_motor.write(abs(new_pwm)); |
Jolein | 15:3a6074f3ceaf | 90 | if(angle>anglemax && toestand=SLAAN) |
Jolein | 14:6ec84bfda0f3 | 91 | { |
Jolein | 15:3a6074f3ceaf | 92 | toestand = TERUGKEREN; |
Jolein | 15:3a6074f3ceaf | 93 | pc.printf ("Toestand = terugkeren!"); |
Jolein | 14:6ec84bfda0f3 | 94 | } |
Jolein | 0:eb00992c1597 | 95 | } |
Jolein | 14:6ec84bfda0f3 | 96 | |
Jolein | 0:eb00992c1597 | 97 | } |
Jolein | 0:eb00992c1597 | 98 | |
Jolein | 0:eb00992c1597 | 99 | void clamp(float * in, float min, float max) |
Jolein | 0:eb00992c1597 | 100 | { |
Jolein | 0:eb00992c1597 | 101 | *in > min ? *in < max? : *in = max: *in = min; |
Jolein | 0:eb00992c1597 | 102 | } |
Jolein | 0:eb00992c1597 | 103 | |
Jolein | 0:eb00992c1597 | 104 | |
Jolein | 0:eb00992c1597 | 105 | float pid(float setpoint, float measurement) |
Jolein | 0:eb00992c1597 | 106 | { |
Jolein | 0:eb00992c1597 | 107 | float error; |
Jolein | 0:eb00992c1597 | 108 | static float prev_error = 0; |
Jolein | 0:eb00992c1597 | 109 | float out_p = 0; |
Jolein | 0:eb00992c1597 | 110 | static float out_i = 0; |
Jolein | 0:eb00992c1597 | 111 | float out_d = 0; |
Jolein | 0:eb00992c1597 | 112 | error = setpoint-measurement; |
Jolein | 0:eb00992c1597 | 113 | out_p = error*K_P; |
Jolein | 0:eb00992c1597 | 114 | out_i += error*K_I; |
Jolein | 0:eb00992c1597 | 115 | out_d = (error-prev_error)*K_D; |
Jolein | 0:eb00992c1597 | 116 | clamp(&out_i,-I_LIMIT,I_LIMIT); |
Jolein | 0:eb00992c1597 | 117 | prev_error = error; |
Jolein | 0:eb00992c1597 | 118 | return out_p + out_i + out_d; |
Jolein | 0:eb00992c1597 | 119 | } |