Alright, so I'm using this encoder library:
http://www.pjrc.com/teensy/td_libs_Encoder.html
This PID library:
and this h-bridge:
I need some help because I'm not sure if I am doing this correctly. When I'd like to move the motor, I increment the setpoint:
setpoint += rec_msg.data[0];
Then I do this over and over in a loop:
/* Get the current number of pulses from the quadrature encoder */
input = Enc.read();
/* Compute the new output */
myPID.Compute();
/* slow decay, drive the motor in the needed direction */
if (input <= setpoint) // if the current position is less than the setpoint, drive the motor forward
{
digitalWrite(MOT_PIN1, 1);
analogWrite(MOT_PIN2, output);
}
else
{ // the current position is past the desired setpoint, we need to reverse the motor
digitalWrite(MOT_PIN2, 1);
analogWrite(MOT_PIN1, output);
}
I am confused as to how I should be reversing the motor if it overshoots. Does the PID controller output negative values?