# Encoder and gyro Help

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)

1. we are using OI ports

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

3. we are using arcade drive during teleop period

4. we 6 inch wheels

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

-Sean, Director of Robotics, Team 5709

Then what are you using?

We tried to come up with are own logic at first but it’s not working. TurnAnlge = 90

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

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.

OK thanks I’ll add a I and D to what i have and If anyone else can give any pointers with the encoders that’d be great!

You will also need to adjust how you determine when you are done turning. You have the following expression:

``````
if (Math.abs(turnAngle - gyroAngle) < 0) {
// Done turning code ...
} else {
// Not done turning code ...
}
``````

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.

So I have made a attempt at the built in PID Controller class. Will this work?

Sometimes a PID is overkill for gyro turning.

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.

I want the gyro to be a precise as possible. I see overkill as a good thing. Also how would i go about using PID for encoders?

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, …