# Should I use gyro or encoders for robot moving in straight line?

I've recently succeeded in building my first collision-avoidance Arduino robot with 2 DC motors, and it works pretty well. However, it doesn't move in a straight line yet, when it should. I'm now studying which method should I implement to make the robot go straight. I've heard about using IMU or encoders with feedback control. I pretty much understand how to use the encoders, but I'm not sure about the gyro. Should I use just one of those or a combination of them?

The problem here is that you are trying to drive in a straight line without any external reference. Wheels slip (making encoders give error), gyro's drive (so there's error there to), magnetometers have issues with nearby magnets, and GPS may not work on the scale you want it to. So what are we left with? Sensor fusion.

If we use a combination of an encoders (to measure the actual wheel speed) with a gyro (to measure the short-term rotation changes), with a magnetometer (to achieve long-term direction management).

Hang on, this is getting expensive and hard. How long did you want to drive in a straight line for? 1m, 100m, 100km? What terrain. If it's a tabletop, encoders or a gyro would work. If it's off road, you need more.

Encoders

An encoder gives ... rotational velocity of the wheel. You can integrate this into rotational angle of the wheel to determine how far you've moved. You can compare this with the other wheel's distance to figure out whether the robot has rotated. At high speeds, you need to poll the encoder really fast, or set up interrupts.

Gyro

Don't ask me how a solid state gyro works, but it gives you the rotation change of the robot directly. Unfortunately, this include the rotation of the earth, and so gyro's drift over time. In my experience, with a small RC car-sized robot over ten minutes, the drive of even a cheap gyro is negligible. Once again you're integrating though, so you have to poll it quite fast, and most gyro's talk over i2c, which can be a bit of a pain.

Note that the accuracy of a gyro depends on it's mounting as well. If it's subject to lots of vibration, it will drift faster.

Magnetometer/compass

With some soft iron, you can shield them from your motors just fine, and they provide and absolute orientation relative to earth. Sounds great? Yup, worked fine one one of my robots, but on the competetion day, someone else had three magnets capable of lifting 25kg each. My robot went into orbit around it. But, if you can ensure that there aren't going to be massive magnets near your robot (other than the robots own motors, which you can shield and then calibrate out to some extent), you can get ~2-3 degree accuracy with very little effort.

Fusing

If you spend a couple more dollars, you can find a 10DOF board that includes a gyroscope, magnetometer (and accelerometer and barometer). Then you can apply some sensor filtering to combine the long-term accuracy of the magnetometer (assuming you drive past the magnets) with the short term (ok, ten minutes or so) sub-degree accuracy of a gyro. Where did the encoders go? Well, I tend to deal with vehicles with tracks (which slip) or offroad (which slip), so I haven't worked with encoders as much as the others.

The physical device that I use in my hobby projects is the GY-87 10DOF board, which uses the MPU6050 accelerometer-gyro and the HMC5883L magnetometer. You speak i2c to the MPU6050, and the MPU6050 can speak to the HMC5883L for you.

• Fusion is the best way to go for sure. I've got only one concern about encoders' output: normally, they provide position feedback. Hence, the corresponding velocity needs to be estimated somehow. This task could be easy with high (thus expensive) resolution encoders and simple differentiation, or tougher with state-space or SG filtering. Jun 12, 2016 at 9:09
• It all depends how accurate you need to go. If you have a 1024 tics per rev encoder and a 5cm radius wheel you get an accuracy of 0.3mm. A cheap \$4 encoder is 24 pulses, or about 1.3cm. So you use a smaller wheel or measure before gear reduction. And to go from the tics of an encoder to a position or velocity isn't actually very hard. I've yet to meet something that standard quadrature decoding didn't work fine for. Jun 12, 2016 at 9:38

Either is going to require feedback control (PID or some subset thereof) to function correctly. I personally have had the most success with encoders, however, I haven't spent a ton of time with accelerometers, but I would assume unless you have an accurate one it may be kind of a pain. Encoders are usually built into motors and are fairly idiot proof.

There is no good substitute for knowing exactly what your wheels are doing. Even with a gyro, you may not get a straight line, as all gyros have drift (and the ones with low drift are expensive). Also, a gyro won't allow you to accurately control wheel speed and won't tell you when a wheel is slipping or stuck. Encoders will buy you much more than a gyro.