Script voor aansturen motoren

Dependencies:   Encoder HIDScope MODSERIAL mbed-dsp mbed

Committer:
jessekaiser
Date:
Tue Oct 21 13:48:51 2014 +0000
Revision:
2:11076f69e0a7
Parent:
1:97d6b160f708
Child:
3:dcc4cebba0d7
Beide motoren draaien.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jessekaiser 0:d5d3b731340c 1 #include "mbed.h"
jessekaiser 0:d5d3b731340c 2 #include "encoder.h"
jessekaiser 0:d5d3b731340c 3 #include "HIDScope.h"
jessekaiser 0:d5d3b731340c 4
jessekaiser 0:d5d3b731340c 5 #define TSAMP 0.01
jessekaiser 0:d5d3b731340c 6 #define K_P (0.1)
jessekaiser 0:d5d3b731340c 7 #define K_I (0.03 *TSAMP)
jessekaiser 0:d5d3b731340c 8 #define K_D (0.001 /TSAMP)
jessekaiser 0:d5d3b731340c 9 #define I_LIMIT 1.
jessekaiser 0:d5d3b731340c 10
jessekaiser 0:d5d3b731340c 11 #define M1_PWM PTC8 //blauw
jessekaiser 0:d5d3b731340c 12 #define M1_DIR PTC9 //groen
jessekaiser 2:11076f69e0a7 13 #define M2_PWM PTA5 //blauw
jessekaiser 2:11076f69e0a7 14 #define M2_DIR PTA4 //groen
jessekaiser 0:d5d3b731340c 15
jessekaiser 2:11076f69e0a7 16 //#define POT_AVG 50
jessekaiser 0:d5d3b731340c 17 void clamp(float * in, float min, float max);
jessekaiser 0:d5d3b731340c 18 float pid(float setpoint, float measurement);
jessekaiser 0:d5d3b731340c 19 volatile bool looptimerflag;
jessekaiser 2:11076f69e0a7 20 //float potsamples[POT_AVG];
jessekaiser 0:d5d3b731340c 21 HIDScope scope(6);
jessekaiser 0:d5d3b731340c 22
jessekaiser 0:d5d3b731340c 23 void setlooptimerflag(void)
jessekaiser 0:d5d3b731340c 24 {
jessekaiser 0:d5d3b731340c 25 looptimerflag = true;
jessekaiser 0:d5d3b731340c 26 }
jessekaiser 0:d5d3b731340c 27
jessekaiser 0:d5d3b731340c 28 int main()
jessekaiser 1:97d6b160f708 29 { //Let op dat de jumpers goed staan als het motortje niet wilt draaien. De E1 jumper moet onder de nummer 7 pin. De locatie van de M1 pin
jessekaiser 1:97d6b160f708 30 //bepaalt of de motor CW of CCW draait.
jessekaiser 0:d5d3b731340c 31 //start Encoder-> first pin should be PTDx or PTAx, second pin doesn't matter
jessekaiser 2:11076f69e0a7 32 AnalogIn potmeter(PTC2);
jessekaiser 0:d5d3b731340c 33 Encoder motor1(PTD3,PTD5); //wit, geel
jessekaiser 2:11076f69e0a7 34 PwmOut pwm_motor1(M1_PWM); // PTC8, blauw, /*PwmOut to motor driver*/
jessekaiser 2:11076f69e0a7 35 pwm_motor1.period_us(75); //10kHz PWM frequency
jessekaiser 2:11076f69e0a7 36 DigitalOut motordir1(M1_DIR); //PTC9, groen
jessekaiser 2:11076f69e0a7 37
jessekaiser 2:11076f69e0a7 38
jessekaiser 2:11076f69e0a7 39 Encoder motor2(PTD2, PTD0); //wit, geel
jessekaiser 2:11076f69e0a7 40 PwmOut pwm_motor2(M2_PWM); //PTA5, blauw
jessekaiser 2:11076f69e0a7 41 pwm_motor2.period_us(75);
jessekaiser 2:11076f69e0a7 42 DigitalOut motordir2(M2_DIR); //PTA4, groen
jessekaiser 2:11076f69e0a7 43
jessekaiser 0:d5d3b731340c 44 Ticker looptimer;
jessekaiser 0:d5d3b731340c 45 looptimer.attach(setlooptimerflag,TSAMP);
jessekaiser 2:11076f69e0a7 46
jessekaiser 2:11076f69e0a7 47
jessekaiser 0:d5d3b731340c 48 while(1) {
jessekaiser 0:d5d3b731340c 49 int16_t setpoint;
jessekaiser 0:d5d3b731340c 50 float new_pwm;
jessekaiser 0:d5d3b731340c 51 /*wait until timer has elapsed*/
jessekaiser 0:d5d3b731340c 52 while(!looptimerflag);
jessekaiser 0:d5d3b731340c 53 looptimerflag = false; //clear flag
jessekaiser 0:d5d3b731340c 54 /*potmeter value: 0-1*/
jessekaiser 0:d5d3b731340c 55 setpoint = (potmeter.read()-.5)*500;
jessekaiser 0:d5d3b731340c 56 /*new_pwm = (setpoint - motor1.getPosition())*.001; -> P action*/
jessekaiser 0:d5d3b731340c 57 new_pwm = pid(setpoint, motor1.getPosition());
jessekaiser 0:d5d3b731340c 58 clamp(&new_pwm, -1,1);
jessekaiser 0:d5d3b731340c 59 scope.set(0, setpoint);
jessekaiser 0:d5d3b731340c 60 scope.set(4, new_pwm);
jessekaiser 0:d5d3b731340c 61 scope.set(5, motor1.getPosition());
jessekaiser 0:d5d3b731340c 62 // ch 1, 2 and 3 set in pid controller */
jessekaiser 0:d5d3b731340c 63 scope.send();
jessekaiser 0:d5d3b731340c 64 if(new_pwm < 0)
jessekaiser 2:11076f69e0a7 65 motordir1 = 0;
jessekaiser 0:d5d3b731340c 66 else
jessekaiser 2:11076f69e0a7 67 motordir1 = 1;
jessekaiser 2:11076f69e0a7 68 pwm_motor1.write(abs(new_pwm));
jessekaiser 2:11076f69e0a7 69
jessekaiser 2:11076f69e0a7 70 if(new_pwm < 0)
jessekaiser 2:11076f69e0a7 71 motordir2 = 0;
jessekaiser 2:11076f69e0a7 72 else
jessekaiser 2:11076f69e0a7 73 motordir2 = 1;
jessekaiser 2:11076f69e0a7 74 pwm_motor2.write(abs(new_pwm));
jessekaiser 0:d5d3b731340c 75 }
jessekaiser 0:d5d3b731340c 76 }
jessekaiser 0:d5d3b731340c 77
jessekaiser 0:d5d3b731340c 78
jessekaiser 0:d5d3b731340c 79 //clamps value 'in' to min or max when exceeding those values
jessekaiser 0:d5d3b731340c 80 //if you'd like to understand the statement below take a google for
jessekaiser 0:d5d3b731340c 81 //'ternary operators'.
jessekaiser 0:d5d3b731340c 82 void clamp(float * in, float min, float max)
jessekaiser 0:d5d3b731340c 83 {
jessekaiser 0:d5d3b731340c 84 *in > min ? *in < max? : *in = max: *in = min;
jessekaiser 0:d5d3b731340c 85 }
jessekaiser 0:d5d3b731340c 86
jessekaiser 0:d5d3b731340c 87
jessekaiser 0:d5d3b731340c 88 float pid(float setpoint, float measurement)
jessekaiser 0:d5d3b731340c 89 {
jessekaiser 0:d5d3b731340c 90 float error;
jessekaiser 0:d5d3b731340c 91 static float prev_error = 0;
jessekaiser 0:d5d3b731340c 92 float out_p = 0;
jessekaiser 0:d5d3b731340c 93 static float out_i = 0;
jessekaiser 0:d5d3b731340c 94 float out_d = 0;
jessekaiser 0:d5d3b731340c 95 error = setpoint-measurement;
jessekaiser 0:d5d3b731340c 96 out_p = error*K_P;
jessekaiser 0:d5d3b731340c 97 out_i += error*K_I;
jessekaiser 0:d5d3b731340c 98 out_d = (error-prev_error)*K_D;
jessekaiser 0:d5d3b731340c 99 clamp(&out_i,-I_LIMIT,I_LIMIT);
jessekaiser 0:d5d3b731340c 100 prev_error = error;
jessekaiser 0:d5d3b731340c 101 scope.set(1,out_p);
jessekaiser 0:d5d3b731340c 102 scope.set(2,out_i);
jessekaiser 0:d5d3b731340c 103 scope.set(3,out_d);
jessekaiser 0:d5d3b731340c 104 return out_p + out_i + out_d;
jessekaiser 0:d5d3b731340c 105 }