MOtor control Pololu
Dependencies: Encoder HIDScope mbed-dsp mbed
Diff: main.cpp
- Revision:
- 6:0832c6c6bcba
- Parent:
- 5:06381e54b94a
- Child:
- 7:37b06688b449
diff -r 06381e54b94a -r 0832c6c6bcba main.cpp --- a/main.cpp Mon Sep 29 21:20:29 2014 +0000 +++ b/main.cpp Mon Sep 29 21:39:29 2014 +0000 @@ -1,20 +1,23 @@ #include "mbed.h" #include "encoder.h" -#include "MODSERIAL.h" +//#include "MODSERIAL.h" +#include "HIDScope.h" + #define TSAMP 0.01 #define K_P (10 *TSAMP) - #define K_I (0.00 *TSAMP) - #define K_D (5 *TSAMP) #define POT_AVG 50 + void coerce(float * in, float min, float max); float pid(float setpoint, float measurement); AnalogIn potmeter(PTC2); volatile bool looptimerflag; float potsamples[POT_AVG]; +HIDScope scope(4); + void setlooptimerflag(void) { @@ -46,16 +49,19 @@ int main() { + //start Encoder Encoder motor1(PTD0,PTC9); + //Ticker to average potmeter values Ticker potaverage; - MODSERIAL pc(USBTX,USBRX); + //MODSERIAL pc(USBTX,USBRX); PwmOut pwm_motor(PTA12); + //10kHz PWM frequency + pwm_motor.period_us(100); DigitalOut motordir(PTD3); potaverage.attach(potAverager,0.002); - pc.baud(921600); + //pc.baud(921600); Ticker looptimer; looptimer.attach(setlooptimerflag,TSAMP); - pc.printf("bla"); while(1) { float setpoint; float new_pwm; @@ -65,7 +71,11 @@ //new_pwm = (setpoint - motor1.getPosition())*.001; new_pwm = pid(setpoint, motor1.getPosition()); coerce(&new_pwm, -1,1); - pc.printf("%f %f %f 0\n", setpoint/2000.+0.5, motor1.getPosition()/2000.+.5,new_pwm/2.+0.5); + scope.set(0, setpoint); + scope.set(1,new_pwm); + // ch 2 and 3 set in pid controller */ + scope.send(); + //pc.printf("%f %f %f 0\n", setpoint/2000.+0.5, motor1.getPosition()/2000.+.5,new_pwm/2.+0.5); if(new_pwm > 0) motordir = 1; else @@ -80,7 +90,7 @@ //'ternary operators'. void coerce(float * in, float min, float max) { -*in > min ? *in < max? : *in = max: *in = min; + *in > min ? *in < max? : *in = max: *in = min; } @@ -97,6 +107,8 @@ out_d = (error-prev_error)*K_D; coerce(&out_i,-0.5,0.5); prev_error = error; + scope.set(2,error); + scope.set(3,out_p); return out_p + out_i + out_d; }