Script voor aansturen motoren

Dependencies:   Encoder HIDScope MODSERIAL mbed-dsp mbed

Committer:
jessekaiser
Date:
Tue Oct 21 13:14:02 2014 +0000
Revision:
0:d5d3b731340c
Child:
1:97d6b160f708
Kleine motor draait :)

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 0:d5d3b731340c 13 #define M2_PWM PTA5
jessekaiser 0:d5d3b731340c 14 #define M2_DIR PTA4
jessekaiser 0:d5d3b731340c 15
jessekaiser 0:d5d3b731340c 16
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 0:d5d3b731340c 20 HIDScope scope(6);
jessekaiser 0:d5d3b731340c 21
jessekaiser 0:d5d3b731340c 22 void setlooptimerflag(void)
jessekaiser 0:d5d3b731340c 23 {
jessekaiser 0:d5d3b731340c 24 looptimerflag = true;
jessekaiser 0:d5d3b731340c 25 }
jessekaiser 0:d5d3b731340c 26
jessekaiser 0:d5d3b731340c 27 int main()
jessekaiser 0:d5d3b731340c 28 {
jessekaiser 0:d5d3b731340c 29 //start Encoder-> first pin should be PTDx or PTAx, second pin doesn't matter
jessekaiser 0:d5d3b731340c 30 Encoder motor1(PTD3,PTD5); //wit, geel
jessekaiser 0:d5d3b731340c 31 /*PwmOut to motor driver*/
jessekaiser 0:d5d3b731340c 32 PwmOut pwm_motor(M1_PWM); // PTC8, blauw
jessekaiser 0:d5d3b731340c 33 //10kHz PWM frequency
jessekaiser 0:d5d3b731340c 34 pwm_motor.period_us(75);
jessekaiser 0:d5d3b731340c 35 DigitalOut motordir(M1_DIR); //PTC9, groen
jessekaiser 0:d5d3b731340c 36 Ticker looptimer;
jessekaiser 0:d5d3b731340c 37 looptimer.attach(setlooptimerflag,TSAMP);
jessekaiser 0:d5d3b731340c 38 while(1) {
jessekaiser 0:d5d3b731340c 39 int16_t setpoint;
jessekaiser 0:d5d3b731340c 40 float new_pwm;
jessekaiser 0:d5d3b731340c 41 /*wait until timer has elapsed*/
jessekaiser 0:d5d3b731340c 42 while(!looptimerflag);
jessekaiser 0:d5d3b731340c 43 looptimerflag = false; //clear flag
jessekaiser 0:d5d3b731340c 44 /*potmeter value: 0-1*/
jessekaiser 0:d5d3b731340c 45 setpoint = (potmeter.read()-.5)*500;
jessekaiser 0:d5d3b731340c 46 /*new_pwm = (setpoint - motor1.getPosition())*.001; -> P action*/
jessekaiser 0:d5d3b731340c 47 new_pwm = pid(setpoint, motor1.getPosition());
jessekaiser 0:d5d3b731340c 48 clamp(&new_pwm, -1,1);
jessekaiser 0:d5d3b731340c 49 scope.set(0, setpoint);
jessekaiser 0:d5d3b731340c 50 scope.set(4, new_pwm);
jessekaiser 0:d5d3b731340c 51 scope.set(5, motor1.getPosition());
jessekaiser 0:d5d3b731340c 52 // ch 1, 2 and 3 set in pid controller */
jessekaiser 0:d5d3b731340c 53 scope.send();
jessekaiser 0:d5d3b731340c 54 if(new_pwm < 0)
jessekaiser 0:d5d3b731340c 55 motordir = 0;
jessekaiser 0:d5d3b731340c 56 else
jessekaiser 0:d5d3b731340c 57 motordir = 1;
jessekaiser 0:d5d3b731340c 58 pwm_motor.write(abs(new_pwm));
jessekaiser 0:d5d3b731340c 59 }
jessekaiser 0:d5d3b731340c 60 }
jessekaiser 0:d5d3b731340c 61
jessekaiser 0:d5d3b731340c 62
jessekaiser 0:d5d3b731340c 63 //clamps value 'in' to min or max when exceeding those values
jessekaiser 0:d5d3b731340c 64 //if you'd like to understand the statement below take a google for
jessekaiser 0:d5d3b731340c 65 //'ternary operators'.
jessekaiser 0:d5d3b731340c 66 void clamp(float * in, float min, float max)
jessekaiser 0:d5d3b731340c 67 {
jessekaiser 0:d5d3b731340c 68 *in > min ? *in < max? : *in = max: *in = min;
jessekaiser 0:d5d3b731340c 69 }
jessekaiser 0:d5d3b731340c 70
jessekaiser 0:d5d3b731340c 71
jessekaiser 0:d5d3b731340c 72 float pid(float setpoint, float measurement)
jessekaiser 0:d5d3b731340c 73 {
jessekaiser 0:d5d3b731340c 74 float error;
jessekaiser 0:d5d3b731340c 75 static float prev_error = 0;
jessekaiser 0:d5d3b731340c 76 float out_p = 0;
jessekaiser 0:d5d3b731340c 77 static float out_i = 0;
jessekaiser 0:d5d3b731340c 78 float out_d = 0;
jessekaiser 0:d5d3b731340c 79 error = setpoint-measurement;
jessekaiser 0:d5d3b731340c 80 out_p = error*K_P;
jessekaiser 0:d5d3b731340c 81 out_i += error*K_I;
jessekaiser 0:d5d3b731340c 82 out_d = (error-prev_error)*K_D;
jessekaiser 0:d5d3b731340c 83 clamp(&out_i,-I_LIMIT,I_LIMIT);
jessekaiser 0:d5d3b731340c 84 prev_error = error;
jessekaiser 0:d5d3b731340c 85 scope.set(1,out_p);
jessekaiser 0:d5d3b731340c 86 scope.set(2,out_i);
jessekaiser 0:d5d3b731340c 87 scope.set(3,out_d);
jessekaiser 0:d5d3b731340c 88 return out_p + out_i + out_d;
jessekaiser 0:d5d3b731340c 89 }