How to use a gyro to drive straight during autonomous

Hello there,

My team has our autonomous code ready, but the robot keeps drifting to one side. I was considering using a gyroscope to help us drive straight, but I have no idea how to do that, we have a NavX-MXP gyro and we are using a mecanum drive train.

Are you reading the rate directly from the gyro or calculating your own rate?

We are reading the rate directly from the gyro I believe

Have a look at these WPILib examples on using a gyro to drive straight.
(Note, C++ examples also exist)
Timed Robot
Command
If you want to be really fancy, you can look into using a Trajectory.

The basic premise is as follows:

  • You have a setPoint heading you want to follow
  • You calculate the error in your heading (setPoint - currentHeading)
  • You scale the error down to adjust how strongly the robot tries to correct for the error
  • You use this error as your turn rate (eg driveCartesian(0.0, 0.5, error))
    Note, You may need to negate the error depending on whether the NavX reports positive CW or CCW.

That looks great, I’ll be sure to try it.

1 Like

Ok, so, robot is still drifting to one side instead of going straight backwards. I tried the example program, but it didn’t change anything. At this point I don’t think the NavX sensor is ‘plug and play’ like the electrical team told me. Does anyone know how to set it up properly? Also i’m a little confused about ‘setPoint’ and ‘currentHeading’ mean, what values do I use for them?

setPoint is the direction you want to face, according to the gyro.
currentHeading is the direction you currently are facing according to the gyro.

The NavX is plug and play is the sense that you plug it in the the MXP port, install the libraries, and create an AHRS object.

Do you have any way to measure the sideways drift on the robot? Encoders, drop odometer wheel, etc.

We have encoders.

I would look into implementing a MecanumOdometry object.
Then you could use 2 feedback loops. One for correcting your rotation, the other for correcting your sideways drift.

My one concern is I don’t know how accurate encoders on a Mecanum bot is, at slow speeds in autonomous the slip may be manageable.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.