In my previous matches I’ve figured that our robot starts to curve slightly towards the left after a considerable distance. Is there any way I could fix this issue in the code?

How would that concept apply to 4 wheel drives though?

did you read the rodotc article? which way is your root turning?

The robotc article only adds the error to the slower wheel and subtracts the error from the faster wheel, but it wouldn’t work with 4 wheels because each wheel would be turning at a different speed.

If you didn’t know, both wheels on either side of a four-wheel drive spin at the same speed. So the RobotC article still applies.

or you could just change the speed of one of the wheels slightly…

ok thanks for the clarification!

You would compute the error between the leading wheel every other wheel individually and find the correction required for each wheel.

pseudocode:

while(not at target) {

errorWheel2 = wheel1 - wheel2;

errorWheel3 = wheel1 - wheel3;

errorWheel4 = wheel1 - wheel4;

//do the computation for the integral and derivative here for each wheel

speedWheel1 = speed;

speedWheel2 = speed + kP * errorWheel2 + kI * integralWheel2 + kD * derivativeWheel2;

speedWheel3 = speed + kP*errorWheel3 + kI * integralWheel3 + kD * derivativeWheel3;
speedWheel4 = speed + kP * errorWheel4 + kI*integralWheel4 + kD * derivativeWheel4;

lastErrorWheel2 = errorWheel2;

lastErrorWheel3 = errorWheel4;

lastErrorWheel4 = errorWheel4;

}

How would you include this to driving to a target?

Also, why are the integral and derivative components included when you are just correcting the bot to a straight line?

If you wanted to drive to an absolute target you would need to keep track of your robot’s position using motion tracking. There are plenty of great resources for that on this website.

In order to have the best corrective abilities, you should use a PID loop (proportion, integral, and derivative). Using only the proportion to correct is known as proportion only control (P loop). This may work for a simple application such as driving straight but in general if you want to be as consistent as possible you should stick with a PID loop. The exact specifics are complicated but a PID is able to leverage the instantaneous change between adjaent loop which allows for another degree of correction.

Sample code for PID in vex: A PID controller in RobotC

So if I were to include the driving straight “algorithm” will I have to use separate errors, integrals, and derivatives to calculate the main power (master motor)?

You need to have an error, integral, derivative and last derivative for each of the wheels being tracked (12 values in total).

In a 4 wheel drive there is a single motor going at a set speed and the other wheels will be adjusted to match that motor. This means there is no “main power” but 3 seprate velocites that have to be calculated for each wheel other than the leading wheel.

This is the formula for PID expression. The ∫ symbol means the integral and de/dt is the derivative. You need to calculate this expression and then add it to the target speed of each wheel.

ex:

speedWheel3 = speed + kP *errorWheel3 + kI * integralWheel3 + kD * derivativeWheel3;*

note: Make sure that the speed is below sufficiently below 100% since the motors may not be able to correct if all the wheels are going full speed.

If you are using pros I believe there is already a library called okapilib with PID control built in if you are having trouble wrapping your head around how to do it from scratch. I don’t have much experience with pros, but i’m sure that someone else on the forum could help you get that set up.

So will I have to calculate the velocity errors and distance errors (error relative to target point) for each wheel?

The error in this particular case is the difference between the encoder readings (distance traveled) between the leading and following wheels. I don’t exactly understand what you mean by error relative to target point since this entire process is just to travel a certain distance and maintain a constant heading. (move in a straight line)

If the robot is trying to reach a certain point, there would be a PID loop adjusting the power of the wheels according to the distance to the target (error). However, to make sure the robot is driving in the correct direction, the robot would have to drive in a straight line to prevent going off track.

So, to solve these problems, would it require 2 PID loops - slowing down/speeding up according to robot’s distance to target point & driving in a straight line?

Take a look at these threads…

And

@JD6 and @Micah_7157X based their pid code off a tutorial @Connor made. It has turn pid in the drive pid which if set to zero should make it a driv straight pid.

If your intention is to drive straight your error is the encoderValue of the leading wheel minus the encoder value of the following wheel. The distance traveled is the exit condition for the PID. You will drive straight using PID until the wheel has traveled the required number of rotations.