MOtor control Pololu
Dependencies: Encoder HIDScope mbed-dsp mbed
Diff: main.cpp
- Revision:
- 8:15c6cb82c725
- Parent:
- 7:37b06688b449
- Child:
- 9:e09d81850a05
--- a/main.cpp Tue Sep 30 20:30:35 2014 +0000 +++ b/main.cpp Wed Oct 01 09:01:15 2014 +0000 @@ -3,18 +3,19 @@ #include "HIDScope.h" #define TSAMP 0.01 -#define K_P (2 *TSAMP) -#define K_I (0.00 *TSAMP) +#define K_P (3 *TSAMP) +#define K_I (0.02 *TSAMP) #define K_D (0 *TSAMP) +#define I_LIMIT 1. #define POT_AVG 50 -void coerce(float * in, float min, float max); +void clamp(float * in, float min, float max); float pid(float setpoint, float measurement); AnalogIn potmeter(PTC2); volatile bool looptimerflag; float potsamples[POT_AVG]; -HIDScope scope(5); +HIDScope scope(6); void setlooptimerflag(void) @@ -22,58 +23,34 @@ looptimerflag = true; } -/* -void potAverager(void) -{ - static uint16_t sample_index = 0; - float voltage = potmeter.read()-.5; - potsamples[sample_index] = voltage; - - sample_index++; - if(sample_index >= POT_AVG) - sample_index = 0; -} - -float getpotAverage(void) -{ - uint16_t valuecounter; - float sum = 0; - for(valuecounter = 0 ; valuecounter < POT_AVG ; valuecounter++) - { - sum += potsamples[valuecounter]; - } - return sum / (POT_AVG*1.); -} -*/ int main() { //start Encoder Encoder motor1(PTD0,PTC9); - //Ticker to average potmeter values - // Ticker potaverage; /*PwmOut to motor driver*/ PwmOut pwm_motor(PTA5); //10kHz PWM frequency pwm_motor.period_us(100); DigitalOut motordir(PTD1); -// potaverage.attach(potAverager,0.002); Ticker looptimer; looptimer.attach(setlooptimerflag,TSAMP); while(1) { float setpoint; float new_pwm; + /*wait until timer has elapsed*/ while(!looptimerflag); - looptimerflag = false; - setpoint = (potmeter.read()-.5)*2000; - //new_pwm = (setpoint - motor1.getPosition())*.001; + looptimerflag = false; //clear flag + /*potmeter value: 0-1*/ + setpoint = (potmeter.read()-.5)*500; + /*new_pwm = (setpoint - motor1.getPosition())*.001; -> P action*/ new_pwm = pid(setpoint, motor1.getPosition()); - coerce(&new_pwm, -1,1); - scope.set(4,motor1.getPosition()); + clamp(&new_pwm, -1,1); scope.set(0, setpoint); - scope.set(1,new_pwm); - // ch 2 and 3 set in pid controller */ + scope.set(4, new_pwm); + scope.set(5, motor1.getPosition()); + // ch 1, 2 and 3 set in pid controller */ scope.send(); if(new_pwm > 0) motordir = 0; @@ -84,10 +61,10 @@ } -//coerces value 'in' to min or max when exceeding those values +//clamps value 'in' to min or max when exceeding those values //if you'd like to understand the statement below take a google for //'ternary operators'. -void coerce(float * in, float min, float max) +void clamp(float * in, float min, float max) { *in > min ? *in < max? : *in = max: *in = min; } @@ -104,10 +81,11 @@ out_p = error*K_P; out_i += error*K_I; out_d = (error-prev_error)*K_D; - coerce(&out_i,-0.5,0.5); + clamp(&out_i,-I_LIMIT,I_LIMIT); prev_error = error; - scope.set(2,error); - scope.set(3,out_p); + scope.set(1,out_p); + scope.set(2,out_i); + scope.set(3,out_d); return out_p + out_i + out_d; }