How can I program a gyro?

I have no idea where to start when it comes to programming. I was wondering where I have to write the code for the gyro, just to move straight. Do I put the code in the DriveTrain subsystem or in a command?Also, after that, I’d like to be able to put it into my Autonomous command. If anyone can help that would be appreciated.

You want to make a new PIDCommand that, in the usePIDOutput method, adds the output of the loop to one side of the drive and subtracts it from the other. Assuming you’re using a NavX as your gyro, returnPIDInput should just return [navx].pidGet().

Starting on line 34: https://github.com/FIRST-Team-2557-The-SOTABots/FRC_Robot/blob/bugfix/2016/Robot/src/main/java/org/usfirst/frc/team2557/robot/subsystems/Chassis.java

That was code I wrote last year. It is an extremely simplified PID (only using the P term). The “0.03” is a constant, known as the proportion constant (Kp). It’s a magic number, unfortunately :-/ You need to guess and check the value for your robot, it’s different for each one. The value for this year’s bot was 0.5. If the robot is swerving, your number is too high, so just slowly decrease the constant until it drives straight with minimal swerve.

If you are using the NavX you need to create your own reset the gyro yourself. My team made code for driving straight with the NavX, if you want to use for reference: https://github.com/FIRST-Team-2557-The-SOTABots/FRC_Robot2.0/blob/dev/2017/FRC_Robot2.0/src/org/usfirst/frc/team2557/robot/subsystems/Chassis_sub.java starting on line 72.

Do yourself a favor and understand the code instead of copy-pasting it. When you run into issues this way you can figure out the solution.

Unrelated to the gyro, but I can see in your code that you have a class called EulerDistanceEstimator that estimates distance using accelerometer data. Has this worked well for you, as in has it had a low enough amount of error that you can actually use it for autonomous?

Would this go under a new subsystem? Or in the “DriveTrain” one?

@Poseidon5817
It doesn’t work very well. There is about ±1 foot of error, due to the fact that you are doing double-integration (the error in the accelerometer is too great, even a small error makes a big error two integrals up). Use it as a last resort; you may find that timed drives are more accurate than using acceleration. If you have encoders then absolutely use those; those are a miracle. Even if you have just one encoder (one on one side of your chassis) just use the drive straight with a gyro PID and you can get some amazing autos. Our code-base from 2016 was based on one single encoder (since the other side broke and we couldn’t replace it) and it worked very well.

@Eric_Dao
We had it in the subsystem for our drive train (chassis), but if I could do the code over again I would put it inside a command. It makes for some spaghetti code by putting it in a subsystem. Have getters for the gyro and setters for the robot drive and do that actual PID logic inside a command (either timed-based or distance-based for telling when the command has finished).

How would I now call this command in the Autonomous command?

You can use Command Groups, which let you combine multiple commands into a single command. For example, here is our code last year for the Chival de Frise: FRC_Robot/Robot/src/main/java/org/usfirst/frc/team2557/robot/commands/autonomous/Auto_ChivalDeFrise.java at bugfix/2016 · FIRST-Team-2557-The-SOTABots/FRC_Robot · GitHub

If you’re asking how you actually run the command in autonomous then you have some research to do. Your “autonomousInit” in Robot.java would look something like this:


public void autonomousInit() {
    Command autoCmd = new Auto_For_Whatever_Command();
    autoCmd.start();
}

The WPI docs can give you more information on that front. You can also look through the code of other teams to see how they did it.

Thanks for the help, but I’m just wondering where in the DriveTrain.java do I put the Kp = 0.03 and the gyro.getangle. Then after that I’d like to know how I can use those functions to make the bot not veer off while driving straight in auto.Thanks!

I don’t want to give you an exact implementation of the code, for educational benefit (I hope you understand). You can look at code on GitHub for my team or other teams to see where they put their functions and how they use the functions.

In general you would make a static variable for Kp (public static final double Kp = 0.03) and a getter for gyro.getAngle (which is a function that returns the value of gyro.getAngle). Those go inside your DriveTrain class which is inside DriveTrain.java:


public class DriveTrain extends SubSystem {
    // This is where functions/static variables go
}

Ah I see. Thanks for your help!