MOtor control Pololu
Dependencies: Encoder HIDScope mbed-dsp mbed
Diff: main.cpp
- Revision:
- 7:37b06688b449
- Parent:
- 6:0832c6c6bcba
- Child:
- 8:15c6cb82c725
--- a/main.cpp Mon Sep 29 21:39:29 2014 +0000 +++ b/main.cpp Tue Sep 30 20:30:35 2014 +0000 @@ -1,13 +1,11 @@ #include "mbed.h" #include "encoder.h" -//#include "MODSERIAL.h" #include "HIDScope.h" - #define TSAMP 0.01 -#define K_P (10 *TSAMP) +#define K_P (2 *TSAMP) #define K_I (0.00 *TSAMP) -#define K_D (5 *TSAMP) +#define K_D (0 *TSAMP) #define POT_AVG 50 @@ -16,7 +14,7 @@ AnalogIn potmeter(PTC2); volatile bool looptimerflag; float potsamples[POT_AVG]; -HIDScope scope(4); +HIDScope scope(5); void setlooptimerflag(void) @@ -24,6 +22,7 @@ looptimerflag = true; } +/* void potAverager(void) { static uint16_t sample_index = 0; @@ -46,20 +45,20 @@ } return sum / (POT_AVG*1.); } +*/ int main() { //start Encoder Encoder motor1(PTD0,PTC9); //Ticker to average potmeter values - Ticker potaverage; - //MODSERIAL pc(USBTX,USBRX); - PwmOut pwm_motor(PTA12); + // Ticker potaverage; + /*PwmOut to motor driver*/ + PwmOut pwm_motor(PTA5); //10kHz PWM frequency pwm_motor.period_us(100); - DigitalOut motordir(PTD3); - potaverage.attach(potAverager,0.002); - //pc.baud(921600); + DigitalOut motordir(PTD1); +// potaverage.attach(potAverager,0.002); Ticker looptimer; looptimer.attach(setlooptimerflag,TSAMP); while(1) { @@ -67,19 +66,19 @@ float new_pwm; while(!looptimerflag); looptimerflag = false; - setpoint = (getpotAverage())*2000; + setpoint = (potmeter.read()-.5)*2000; //new_pwm = (setpoint - motor1.getPosition())*.001; new_pwm = pid(setpoint, motor1.getPosition()); coerce(&new_pwm, -1,1); + scope.set(4,motor1.getPosition()); 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 = 0; + else motordir = 1; - else - motordir = 0; pwm_motor.write(abs(new_pwm)); } }