Gyro angle increasing while robot is static

We’re trying to get the angle during Teleop mode. It worked in the past perfectly, and now every time we enable (and disable) the reading on the Driver Station LCD is rapidly decreasing 1/100th every second. But if the gyro feels like it, it will rapidly increase 1/100th every second.

This is how we’re initiating it:


#include "WPILib.h"

/**
 * This is a demo program showing the use of the RobotBase class.
 * The SimpleRobot class is the base of a robot application that will automatically call your
 * Autonomous and OperatorControl methods at the right time as controlled by the switches on
 * the driver station or the field controls.
 */ 
class RobotDemo : public SimpleRobot
{
	RobotDrive myRobot; // robot drive system
	Joystick stick1, stick2; // only joystick
	ADXL345_I2C adxl;
	Gyro gyro;
	Relay *blinkylight;

public:
	RobotDemo(void):
		myRobot(2, 3),	// these must be initialized in the same order
		stick1(1),		// as they are declared above.
		stick2(2),
		adxl(1, ADXL345_I2C::kRange_2G),
		gyro(2)
	{
		myRobot.SetExpiration(0.1);
		blinkylight = new Relay(4);
	}

And this is how we’re utilizing it:


void OperatorControl(void)
	{
		gyro.Reset();
		gyro.SetSensitivity(0.007);
		DriverStationLCD *dsLCD = DriverStationLCD::GetInstance();
		myRobot.SetSafetyEnabled(true);
		bool motorinv = false;
		
		while (true)
		{
			blinkylight->Set(Relay::kForward);
			/*
			ADXL345_I2C::AllAxes ax = adxl.GetAccelerations();
			//printf("X: %f, Y: %f, Z: %f", ax.XAxis, ax.YAxis, ax.ZAxis);
			dsLCD->PrintfLine(DriverStationLCD::kUser_Line1, "X: %f", ax.XAxis);
			dsLCD->PrintfLine(DriverStationLCD::kUser_Line2, "Y: %f", ax.YAxis);
			dsLCD->PrintfLine(DriverStationLCD::kUser_Line3, "Z: %f", ax.ZAxis);
			*/
			dsLCD->PrintfLine(DriverStationLCD::kUser_Line1, "X: %f",
					adxl.GetAcceleration(ADXL345_I2C::kAxis_X));
			dsLCD->PrintfLine(DriverStationLCD::kUser_Line2, "Y: %f",
					adxl.GetAcceleration(ADXL345_I2C::kAxis_Y));
			dsLCD->PrintfLine(DriverStationLCD::kUser_Line3, "Z: %f",
					adxl.GetAcceleration(ADXL345_I2C::kAxis_Z));
			dsLCD->PrintfLine(DriverStationLCD::kUser_Line4, "Gyro: %f", gyro.GetAngle());
			dsLCD->PrintfLine(DriverStationLCD::kUser_Line6, "Motorinv: %d", motorinv);
			dsLCD->UpdateLCD();
			
			if(stick1.GetRawButton(2) || stick2.GetRawButton(2))
			{
				if(motorinv == false)
				{
					motorinv = true;
				}
				else if(motorinv == true)
				{
					motorinv = false;
				}
			}
			myRobot.SetInvertedMotor(myRobot.kRearLeftMotor, motorinv);
			myRobot.SetInvertedMotor(myRobot.kRearRightMotor, motorinv);
			if((stick1.GetTrigger() == true) && (stick2.GetTrigger() == true))
			{
				myRobot.TankDrive(stick1,stick2);
			}
			else if(stick1.GetTrigger() == true && (stick2.GetTrigger() == false))
			{
				myRobot.ArcadeDrive(stick1);
				}
			else if(stick2.GetTrigger() == true && (stick1.GetTrigger() == false))
			{
				myRobot.ArcadeDrive(stick2);
			}
			else
			{
				myRobot.TankDrive(0.0, 0.0);
			}
			Wait(0.005);				// wait for a motor update time
		}
	}
};

START_ROBOT_CLASS(RobotDemo);

I guess the question is, what are we doing wrong to get such extreme values? Like I said, this previously worked and we switched out gyros - same problem.

Thank you in advance!

In the past when I’ve used gyros, this happens when you set the sensitivity incorrectly.

Changing .01 degrees per second is a pretty good drift rate for the gyros that I’ve worked with. I’m used to more like 0.05 degrees per second.

Is the gyro calibrating with a still robot, e.g., hands-off, no compressor or other vibrating object running.

Seconded…

I think your gyro is working just fine… probably better than ours! :smiley:

*If the drift is bothersome during an extended practice or demo session, add a button to your code so the driver can re-zero the gyro whenever the robot is pointing in the zero direction.

Either this or just reset the gyro before you do an action.

That works only if the “action” is intended to be relative to the current position, not if you are using the gyro for true heading (e.g. field-centric control).

Thank you for all of your help everyone!

We figured it out:
The problem was the wire that provides power for the gyro wasn’t plugged in…
But the code above does work, excluding the sensitivity. So we changed that to 0.05 and the gyro works perfectly now.