Hello everyone, My team wants to use encoders to know how far we have driven and gyro to turn to a certain angle. the issues that we are having are with the encoders one doesn’t count and the other never stops counting. The Gyro we cant get to turn to a degree. We have only gotten it to turn with in 5-15 degrees of the target. We are not using PID because I am unsure of where to start with that or where to put the code in a command based program. Any help is greatly appreciated!

Gyro info:

Gyro: Analog Devices, Gyro Board (am-3555)

Encoders Info:

The Encoders: Encoder Kit AMT103-V (fc17-033)

we are using OI ports

there is no gear ratios between the encoder and wheel (they are on the same shaft)

we are using arcade drive during teleop period

we 6 inch wheels

the motor controllers are the 2015 Talon KOP controllers and those are using PWM.

You’ve accidentally invented a P-only PID algorithim.

Your P-value is 1/90.

The interesting mistake I see is you assume that if you’re within 10 degrees of your target, you are 10 degrees away. That’s probably not helping. Set a minimum speed if you must, not a minimum movement.

Anyway, your logic is perfectly fine. It just doesn’t use I or D, and so won’t be as incredibly accurate.

The Math.abs(turnAngle - gyroAngle) value will always be greater than or equal to 0, so your rotation will never finish. Try starting with a larger acceptable error value (like 5 degrees) to make sure your code reaches the end point and then try adjusting downwards.

I encourage you to try using the built-in PID functions in wpilib. They really do a lot of the work for you. KauiLabs has good sample code that can be easily adapted to any gyro on their examples site.

Take a look at this class. It implements turning using two rates, one for “coarse turning” when the actual heading is >10 degrees off the desired heading, and “fine turning” when the heading is <10 degrees off of desired. There’s a 2 degree deadband on either side of the desired heading.

Too precise and your robot will either turn very very slowly or it will never get there. You want to go to 57 degrees, but the robot will correct and overshoot and hit 59, then 52, then 60, then 54, …