How do I make an efficient PROS code which concerns PID and gyroscopes?
Is the gyroscope meant to keep the robot going straight? I mean, that’s the only way it can really help since it can track position just rotation. Anyways, just take the error by subtracting the currentGyroValue by the startingGyroValue (you can’t just zero a gyro, so take the current value it’s at before moving). Then divide that by two to get a number that both sides of the base can use (I don’t like it when one side of the base is always trying to readjust to match the other side, otherwise known as a master-slave system). Then multiply that number by some kP scaling constant (this constant will have to be tuned). You get a value for one side of the base however, you can’t use that for the other side because depending on circumstances the other side needs to speed up or slow down. So just multiply the value calculated by negative one. Here is some code that does everything that I described:
void driveChassisStraight(int setPoint, int speed, int direction) {
int gyroSetPoint = chassisGyroSensorValue; //take gyro value robot is in before driving
int leftChassisSpeed = speed;
int rightChassisSpeed = speed;
while(leftChassisEncoder < setPoint && rightChassisEncoder < setPoint) { /*while the distance isn't
met*/
driveLeftChassis(leftChassisSpeed * direction);
driveRightChassis(rightChassisSpeed * direction);
wait1Msec(50); //loop twenty times per second
leftChassisSpeed += (((chassisGyroSensorValue - gyroSetPoint) / 2) / goingStraightKP);
rightChassisSpeed += -(((chassisGyroSensorValue - gyroSetPoint) / 2) / goingStraightKP);
/*get error and average it (divide by two) then use scaling factor and right is negative and notice
how the speeds aren't equal to the calculation but the calculation is added to the speed*/
}
driveLeftChassis(0);
driveRightChassis(0);
SensorValue[leftBackChassisEncoder] = 0;
SensorValue[rightBackChassisEncoder] = 0;
wait1Msec(20);
}
Many of the principles discussed on this forum before surrounding gyros in other language derivatives (think RobotC) apply within PROS as well. As an initial launch into PROS specific gyro implementation, have a read of the API functions regarding gyros: https://pros.cs.purdue.edu/api/#gyroGet (on in your projects api file).