MOtor control Pololu
Dependencies: Encoder HIDScope mbed-dsp mbed
Diff: main.cpp
- Revision:
- 12:19ea553567a3
- Parent:
- 11:afe2140e469e
- Child:
- 13:0d4bc34e410f
diff -r afe2140e469e -r 19ea553567a3 main.cpp --- a/main.cpp Wed Oct 01 11:50:54 2014 +0000 +++ b/main.cpp Tue Oct 07 08:19:17 2014 +0000 @@ -5,14 +5,18 @@ #define TSAMP 0.01 #define K_P (0.1) #define K_I (0.03 *TSAMP) -#define K_D (0 /TSAMP) +#define K_D (0.001 /TSAMP) #define I_LIMIT 1. +#define M1_PWM PTC8 +#define M1_DIR PTC9 +#define M2_PWM PTA5 +#define M2_DIR PTA4 + //#define POT_AVG 50 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(6); @@ -23,21 +27,20 @@ looptimerflag = true; } - - int main() { - //start Encoder - Encoder motor1(PTD0,PTC9); + AnalogIn potmeter(PTC2); + //start Encoder-> first pin should be PTDx or PTAx, second pin doesn't matter + Encoder motor1(PTD0,PTA13); /*PwmOut to motor driver*/ - PwmOut pwm_motor(PTA5); + PwmOut pwm_motor(M2_PWM); //10kHz PWM frequency - pwm_motor.period_us(100); - DigitalOut motordir(PTD1); + pwm_motor.period_us(75); + DigitalOut motordir(M2_DIR); Ticker looptimer; looptimer.attach(setlooptimerflag,TSAMP); while(1) { - float setpoint; + int16_t setpoint; float new_pwm; /*wait until timer has elapsed*/ while(!looptimerflag); @@ -52,7 +55,7 @@ scope.set(5, motor1.getPosition()); // ch 1, 2 and 3 set in pid controller */ scope.send(); - if(new_pwm > 0) + if(new_pwm < 0) motordir = 0; else motordir = 1;