Gyro coding issues

So we are trying to set up a gyro for a simple 180 command however we aren’t even getting any response from the gyro we have the data up into SmartDashboard and when we turn the bot we get nothing. However the dashboard even when disabled does occasionally drift a pixel so if its on the dashboard during a match at the end it’ll end up about 90 degrees from starting point.

Here’s the mocked up code for a test bot.

What gyro are you using and how is it wired? From your code it looks like it should be an old analog gyro hooked up to analog input 0… these are little-used in recent years, digital gyros like the ADXRS45x series are far more common.

Yea we have one of the Analog FRC Gyros since its all we got. And its in the little gyro port thingy on the rio. Since we just need the yaw for turning it’ll be plenty.

image

Yea I believe we use the ADXRS450 depicted.

Ah, I see how that could be confusing. “Analog Devices” is the manufacturer, but it doesn’t connect to one of the analog input ports on the roboRIO.

Try this instead of the AnalogGyro class:
https://first.wpi.edu/wpilib/allwpilib/docs/release/java/edu/wpi/first/wpilibj/ADXRS450_Gyro.html

2 Likes

So then this should work. I have no way to test until I get the robot tomorrow.

1 Like

Can’t say for sure.

I suggest the gyro object not be declared static, because its initialization will occur before robotInit is called. Check this out: https://www.baeldung.com/java-static-variables-initialization

I also suggest: reading through the doc page for the ADXRS450_Gyro class, looking up any additional documentation for the gyro and its interface, and making sure your code compiles.

Making it non-static gives said errors

image
image
image

The errors you see mean exactly what they say: the compiler has found three places where gyro was accessed as a member of the class driveTrain, instead of accessing it through an object of type driveTrain.

I strongly recommend renaming the “driveTrain” class to “DriveTrain”, as the latter follows the standard Java class naming practice, which is to capitalize the first letter of class names. “driveTrain” is capitalized as though it is a variable name.

In the Robot class (Robot.java), you don’t have access to the instance of driveTrain that was constructed in RobotContainer. However, you’re only using it to call the calibrate() method on the gyro object. Instead, this call should probably go in the constructor of driveTrain.

Same goes for the SmartDashboard.putData(driveTrain.gyro) call – it would be best done in the driveTrain constructor. Because ADXRS450_Gyro implements Sendable, it’s only necessary to call putData on it once.

In the Rotate command, you have access to the instance of driveTrain, named m_driveTrain. Instead of trying to reference the driveTrain class, m_driveTrain.gyro.getAngle() will give you the result you’re looking for.

I also recommend taking a look at this page, and the example projects linked from it:

2 Likes

Alright i fixed it and pushed to origin.

I have an issue where the displayed gyro doesn’t do anything. However, the turn command rotates about 5 times and stops and the displayed gyro moves about 5-10 degrees.

So I found the issue the axis is up and down is there an easy way to fix that.

Also while I’m here the gyro has a slight drift

Have you tried recalibrating the gyro? Call the calibrate() function on startup to calibrate the gyro. Make sure to keep it completely still.

Yea it probably moving while calibrating. I’m guessing im just going to have to get a SPI cable.

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