Post date: Dec 10, 2018 12:04:11 AM
What is an Encoder?
Encoders can be a powerful tool for navigating the robot on the field during the autonomous period of the match. An encoder is anything that converts information from one format to another. Many motors such as the REV Core Hex, TETRiX TorqueNADO, and AndyMark NeveRest DC motors come with an encoder. Specifically, these encoders are known as rotary encoders. Rotary encoders are electromechanical devices that track the rotation of a motor shaft. There are many different types of encoders including Optical and Magnetic Encoders. Magnetic Encoders have a magnet which is attached to the shaft of the motor and use two Hall Effect sensors (for more information of Hall effects sensors see: https://www.explainthatstuff.com/hall-effect-sensors.html) to detect the change in the magnetic field as the motor shaft is rotating. These types of encoders are used most of the time in the DC Motors we use for FTC. The two Hall-effect sensors generate this output:
As seen in the image above, there are four stages (a,b,c, and d) for each period which is why this type of encoder is known as a a quadrature encoder. Each of these stages is a seperate “count” or “tick” measured by the encoders. The number of counts per revolution of the motor shaft can be found in the specifications for each motor. For example, the REV Robotics Version 2 DC motors which we use have the Counts per Revolution at the Output Shaft specified to be 1120. This means that for every rotation of the motor shaft, the encoder will measure 1120 “ticks” or “counts”. The TorqueNADO TETRIX motors have a specified Counts per Revolution of 1440. You can find this specified online for whatever motors you are using. This number is necessary for when you go ahead using your encoders for navigation.
Moving Forwards and Backwards Using Encoders
When a robot completes one complete revolution, how far has it moved? On one full rotation, it has travelled a distance equal to the circumference of the wheel, or d, where d is the diameter of the wheel. Using this, we can find the number of inches travelled per rotation which is d.
This then needs to be converted into the number of counts our encoders should measure. Therefore:
will give you the number of counts the encoder needs to measure per inch. This means that given the number of inches we want the robot to travel, we can turn the motors until the desired number of encoder “ticks” are counted.
Coding Forward and Backwards Movement Using Encoders
In the sample codes provided by FTC, there is a code called PushbotAutoDriveByEncoder_Linear. In this code, a method called encoderDrive is included. This method takes four parameters, speed, leftInches, rightInches, and timeOut. Left inches and right inches tell how many inches the right and left wheels should be moving forward/backwards. There are a few things to notice in this code. First, the constants at the top demonstrate the exact calculation we did earlier. Using the counts per motor rev (aka counts per rotation), and wheel diameter, they found the number of counts per inch. This is the only information that is really needed when moving using the encoder. In the method encoder drive, the motors are set to RunToPosition Mode. The current position of the motors are received and then the intended position of the motor is calculated. The intended position of the robot is the the current position plus the number of counts it must move further. Therefore it will be,
Once the finalMotorPosition is calculated, one simply needs to call the setTargetPosition method of the motor for the robot to move to that desired final position. All of this is done in the encoderDrive method given in the sample code. If we give a negative number for the desired inches to move, it will move backwards since the final motor position will be less than the initial position. Using this, simply calling the method encoderDrive with the correct number of inches forward/backward you would like the robot to move is all you need to do!
Turning Using Encoders
Let’s define the diameter of the large circle, representing the path of the robot rotation to be D. When each wheel turns 1 full rotation, they travel a total distance that is equal to the circumference of the wheel. Therefore, if the diameter of each wheel is d, the distance travelled by the wheel in one rotation is (pi)d. We want to be able to figure out how many degrees was turned by the robot after this rotation. In other words, we are finding the fraction of the turn that was completed after one rotation. The fraction of the path completed is: ((pi)d)/((pi)D), where D is the diameter of the circular path the robot takes. This diameter would be equal to the distance from one corner of the robot to the opposite corner. In order to find the degrees, where theta is the degrees turned,
Therefore:
Now we have found the number of degrees turned per rotation and now the number of counts per degree can be found.
Our motors have 1120 counts per rotation. So our calculations would look like this:
You would use the specifications for your motors to find the number of counts per degree.
Coding Rotation Using Encoders
To code this rotation, you must create a method very similar to the encoderDrive method used to move forward and backwards. However, rather than passing leftInches and rightInches, we want to pass the degrees we want to turn. Also, instead of using the constant counts per inch, we use the constant counts per degree (which we found above). Just as before, we must set the motors to RunToPosition Mode and find a new target position. Now, our target position is found like this:
Now, simply call the setTargetPosition method for each motor involved in the turn and the robot will turn to the desired angle you want.
Things to Watch Out For
Encoders are a powerful tool. With this, you’ll be able to have full control over the movement of the robot all over the field. However, there are just some things you want to watch out for when using encoders. First, when turning, depending on the number of wheels you use, you may have to set different code for different motors. For example, we use four wheels on our robot. To turn right (for example), we turn the two right wheels backwards and the two left wheels forward. Therefore, the target position for the left wheels is:
While the target position for the right wheels is:
If the wheels don’t turn in opposite directions, the robot will simply go forward.
Another thing to look out for is the effect of speed. The faster speed you set for the motors to turn, the more inaccurate your movements will be. This is because while the motors run to a certain position, even after the motors stop turning, the robot only stops after a little bit of time (braking distance). Typically the distance moved during this time is very small (and it isn’t a big deal). However, if the speed is really fast, the braking distance increases and it may become a problem.
Finally, the sample code includes another constant called the drive gear reduction. If you are using a different gear ratio (other than 1:1), you will need to include this in your code. This is because when the motor turns 1 full rotation, the wheel didn’t turn a full rotation because of the gear ratio. Therefore, when calculating the counts per inch (or counts per degrees), you need to take that factor into consideration. Counts per inch, for example, would thus be found like this:
With that, you’re all set to code using encoders. You’ll be able to completely control the robot’s position on the field at any time!
Sources:
https://www.machinedesign.com/sensors/basics-rotary-encoders-overview-and-new-technologies-0
http://www.revrobotics.com/content/docs/Encoder-Guide.pdf
https://ftc-tricks.com/dc-motors/