I am trying to make a robot move and turn to a ball target. Whenever the ball is to the left of the robot, the right wheels will move faster than then the left wheel so the robots turns left. And vice versa for the right side. For some reason while it is in this stage, of either turning left or right, it will move forward and turn in the correct direction but stop prematurely. I get the distance to the target using PhotonLib and use encoders to track the robot’s progression toward the target. What could be going wrong? Is my math possibly wrong. And if so, how can I fix it.
Sidenote: leftModularEncoder returns negative values when the left wheels rotate foward and in order to drive the robot forward you must write theTank.drive(-1, 1);
Math:
currentTurningDistanceFromTarget = (degreeToTarget*DEGREESTOMETERS) - (-leftModularEncoder.getDistance() - rightModularEncoder.getDistance());
Concerned Code
//If ball is right of the robot
if( (degreeToTarget*DEGREESTOMETERS) - 0.04 > 0)
{
hasTurned = true;
//Rotate information
currentTurningDistanceFromTarget = (degreeToTarget*DEGREESTOMETERS) - (-leftModularEncoder.getDistance() - rightModularEncoder.getDistance());
motorPowerToTurn = MathUtil.clamp(rotationPIDController.calculate(currentTurningDistanceFromTarget, 0), -1.0, 1.0);
//Foward information: May be faulty when it comes to math
currentDistanceAwayFromTarget = (distanceToTarget*DISTANCETOTARGETFACTOR) - rightModularEncoder.getDistance();
motorPower = MathUtil.clamp(rotatingdistancePIDController.calculate(currentDistanceAwayFromTarget, 0), -1.0, 1.0);
//Moving to Target
theTank.drive(MathUtil.clamp(-motorPowerToTurn-motorPower, -1.0, 1.0), motorPower);
System.out.println("To Left excess | Left: "+ MathUtil.clamp(-motorPowerToTurn-motorPower, -1.0, 1.0) + " Right: " + motorPower + " Current distance to target: " + distanceToTarget*DISTANCETOTARGETFACTOR);
//System.out.println("Moving left Current Distance To Target: " + currentDistanceAwayFromTarget + " Current Angle Away from Target: " + currentTurningDistanceFromTarget);
}
//If ball is left of the camera
else if(((-degreeToTarget*DEGREESTOMETERS) - 0.04 > 0))
{
hasTurned = true;
//Rotate information
currentTurningDistanceFromTarget = (-degreeToTarget*DEGREESTOMETERS) - (rightModularEncoder.getDistance() + leftModularEncoder.getDistance());
motorPowerToTurn = MathUtil.clamp(rotationPIDController.calculate(currentTurningDistanceFromTarget, 0), -1.0, 1.0);
//Foward information
currentDistanceAwayFromTarget = (distanceToTarget*DISTANCETOTARGETFACTOR) - rightModularEncoder.getDistance();
motorPower = MathUtil.clamp(rotatingdistancePIDController.calculate(currentDistanceAwayFromTarget, 0), -1.0, 1.0);
//Moving to Target
theTank.drive(-motorPower, MathUtil.clamp(motorPowerToTurn+motorPower, -1.0, 1.0));
System.out.println("To Right excess | Left: " + -motorPower + " Right: " + MathUtil.clamp(motorPowerToTurn+motorPower, -1.0, 1.0) + " Current distance to target: " + distanceToTarget*DISTANCETOTARGETFACTOR);
//System.out.println("Moving right Current Distance To Target: " + currentDistanceAwayFromTarget + " Current Angle Away from Target: " + currentTurningDistanceFromTarget);
}
else
{
if(hasTurned){
leftModularEncoder.reset();
rightModularEncoder.reset();
hasTurned = false;
}
//Foward information
currentDistanceAwayFromTarget = (distanceToTarget*DISTANCETOTARGETFACTOR) - rightModularEncoder.getDistance();
motorPower = MathUtil.clamp(distancePIDController.calculate(currentDistanceAwayFromTarget, 0), -1.0, 1.0);
//Moving to target
theTank.drive(-motorPower, motorPower);
System.out.println("Straight with motor power " + motorPower + " Distance from target: " + distanceToTarget*DISTANCETOTARGETFACTOR + " Encoder Distance Covered: " + -leftModularEncoder.getDistance());
//System.out.println("Moving Straight Current Distance To Target: " + currentDistanceAwayFromTarget + " Current Angle Away from Target: " + currentTurningDistanceFromTarget);
}
try {
info = ballTracker.getTargetGoal();
} catch (NullPointerException e) {
System.out.println("Lost sight of a target");
theTank.drive(0, 0);
}
distanceToTarget = info.get("Distance");
degreeToTarget = info.get("Yaw");
}
Robot.java (17.6 KB)