So someting like this?
Code:
class PDLoop {
PIDController m_controller;
private PIDSource source = new PIDSource() {
public double pidGet() {
return m_encoder.getDistance();
}
private PIDOutput output = new PIDOutput() {
public void pidWrite(double output) {
driveTrain.mecanumDrive_Polar(output,0,0);
}
public PDLoop() {
m_controller=new PIDController(1.0,0,0.01,source,output);
}
public void driveForDistance(double distance)
{
m_controller.setSetpoint(distance);
m_controller.enable();
}
}
Does your class look something like this?
If so, to go an extra distance you could rewrite the last method as
Code:
public void driveForDistance(double distance)
{
m_controller.setSetpoint(source.pidGet() + distance);
m_controller.enable();
}
This way you can call again, and the setpoint will get reset to its current encoder distance plus the distance requested and it will begin moving again.
Allowing strafing to distance would require some extra logic or another class all together. The above class could probably accomodate strafing with minimal changes.
Code:
class PDLoop {
PIDController m_controller;
double m_direction=0;
private PIDSource source = new PIDSource() {
public double pidGet() {
//probably need encoder from left front wheel for strafe right signs
//to work.
return m_encoder.getDistance();
}
private PIDOutput output = new PIDOutput() {
public void pidWrite(double output) {
driveTrain.mecanumDrive_Polar(output,m_direction,0);
}
public PDLoop() {
m_controller=new PIDController(1.0,0,0.01,source,output);
}
private void setDistanceAndEnable(double distance)
{
m_controller.setSetpoint(source.pidGet()+distance);
m_controller.enable();
}
public void driveForDistance(double distance)
{
//negative distance is same as m_direction=180
m_direction=0;
setDistanceAndEnable(distance);
}
public void strafeRightForDistance(double distance) {
//pass in negative distance to strafe left
m_direction=90;
setDistanceAndEnable(distance);
}
}
I'm not at all certain though if the encoder reads horizontal distance the same as forward distance or if it reads either correctly. Still on our to-do list to figure out how encoders behave distance wise with mecanum.
I'm not really sure the above can work-- it's just an approach I'd try given the situation you seem to be in. I think the left front goes forward for either driving forward or strafing right so using that encoder for distance makes sense to me for a strafeRight command.