Hi everyone this is my first year programming in First Robotics and I need some help programming a gyro in autonomus. My goal is to have our bot drive forward and make a sudden 90 degree turn. I have figured out how to make a sudden turn however, the bot does not stop and it continues to spin. Any help would be greatly appreciated.
Here’s my code:
public void autonomous() {
while (isAutonomous() && isEnabled()) {
SmartDashboard.putNumber(" angle " , gyro1.getAngle());
double angle = gyro1.getAngle() + 90;
//robot.drive(-0.15, angle * Kp); this is for driving straight
robot.drive(-0.10, angle / 30); // the turn
gyro1.reset();
if(angle < 90) {
robot.stopMotor();
}
}
}
The first problem is that you are resetting the gyro in every iteration of your loop. Thus the angle variable is always going to contain the number of degrees that you have turned since the last iteration of the loop (plus 90 degrees).
Unless you are turning really fast, you are never going to get the gyro to read a full 90 degrees.
I’m also not sure why you are adding 90 degrees to the angle each time. If you are turning in the positive direction, your angle will always be greater than 90 degrees. If you are turning in the negative direction, your angle will always be less than 90 degrees because of the above problem. Either way, the magic +90 is probably not what you intended and the stopMotor() method is not going to be called when you want it to be called.
What you probably want is to reset the gyro in the robotInit() method or before your autonomous while loop, then leave it alone.
Thanks for the help I moved the reset into the robotInit and I removed the +90. But I don’t understand what I need to do to fix the stopMotor() issue you were talking about. If you don’t mind elaborating it would be greatly appreciated.