I’m trying to use Quadrature Encoders on the TalonSRX to make the robot drive straight. I’ve looked at a couple of different bits of example code, and all of them have a value called kP
. I’m not sure what this constant is, but changing it has dramatic effects on my driveStraight
function, and I haven’t been able to get the robot to drive even reasonably straight yet. Here’s my driveStraight
function:
final double kP = 0.03; //I've messed with a bunch of values for kP, but can't figure out what will make it work.
public void driveStraight() {
int error = backLeft.selectedSensorPosition - backRight.selectedSensorPosition;
resetEncoderCounts(); //Sets the sensor positions both to 0
double turnPower = kP * error;
driveAngle(MAX_SPEED * 0.75, turnPower); //If you need to see this function, let me know. It's just what most codebases call tankDrive.
}
This code makes the robot turn back and forth erratically. What am I doing wrong? This seems like such a simple problem, but I’ve not been able to figure it out for the life of me!
KP is the proportional gain constant of a PID Controller where the error of whatever is being controlled is used to correct the actual to the set point. There are many resources available on the web for PID.
Getting the correct value is called tuning. It is a trial and error process until you get a stable system. You mention a tank drive but it looks like an arcade drive equation.
Have you considered using a gyro to correct instead of encoders?
1 Like
The reason that your robot is turning back and forth so erratically is more than likely due to the fact that your kP value is to high I would recommend turning it down until it no longer just turns erratically. The kP value is part of a PID “controller” basically what your code is doing is taking the error between your left and right encoders on your drivetrain and it is multiplying it by the kP value in order to get a motor speed if your kP value is to high then your robot will turn past its target and then will stop and try and go back and miss its target again and it will just keeps doing that until it reaches an error of zero or until the robot spins out of control. CTRE does provide an example of all of this math being done on the talon, I prefer this way over all the rest but if you don’t feel like complicating things then you can stick with your way of doing things.(Example)
1 Like
Oh whoops, yeah. Meant arcade drive there.
I’ve thought about that, but the Gyroscope drifts so quickly (like 2° a second) I feel like I would be better off just setting both motors to the same speed than using the gyro. Do you know any resources that can distinguish between drift and actual turns by the robot?
Thanks, this helps a lot actually. I’ll try this example out and do some reading on PID controllers and specifically the SRX that I should have done a long time ago.
A good gyro won’t drift significantly during Autonomous. The Kauai labs navx mxp is one we use. The TalonSRX integrates more closely with their Pigeon gyro.
1 Like
Okay. Our Gyro is pretty old, I’ll get a new one ordered asap. Thanks for the help. This is the first year we’ve used any sort of sensors on our robot, so I’m quite uneducated when it comes to the sensor code and math.
I personally recommend the pigeon.
1 Like