The most recent version of this article can be found at this link.
Introduction
The RoboClaw uses encoders as part of a feedback system that allow control of motor speed and position. Many motors include encoders as part of the assembly. In this Application Note we’ll be wiring up and testing a popular gearmotor from Pololu that has a built-in encoder. The motor and encoder combination we'll be looking at is the 25mm metal geatmotor from Pololu. This instructions in this Application Note apply to all of the motor and encoder combinations in this series.
Materials
(1) RoboClaw motor controller
(1) battery for RoboClaw
(2) Pololu 25D metal gearmotors with encoders *
(12) male to female jumper wires
(1) computer with BasicMicro Motion Studio installed
(1) micro USB cable
(1) small screwdriver
* Pololu product link: Pololu Gearmotor
Discussion
Beyond basic wiring of the encoder there are a few things to know about how encoders work. As you may have noticed each encoder has two outputs. All quadrature encoders have two output channels often labeled A and B. The signal coming from these outputs are a pulse train of square waves. Each pulse corresponds to one mark on the encoder disk, this is what is counted by the quadrate decoder on the RoboClaw. The outputs from both channels are offset from each other by 90 degrees. The decoder then can look to see which channel leads the other and from that determine which direction the motor is rotating.
An aspect of an encoder to pay attention to is the number of counts per revolution your encoder provides. The encoder used in this Application Note provides 48 counts per revolution and coupled with the gearbox the encoder outputs 976 counts per revolution. The higher the number of counts per revolution the more precise the control of the gearbox, which means that the motor controller will be able to more precisely control the position or velocity of the motor.
A problem that will often be encountered when dealing with encoders is electrical noise. Noise can appear on the encoder output lines due to external signals that are nearby or due to improper filtering at the encoder. When noise appears on an encoder output the decoder will often count noise spikes as encoder counts and cause problems with the motor controller maintaining velocity and position control. Keeping the encoder lines away from other wires, particularly those carrying large amounts of power or rapidly changing signals, helps to reduce noise as does keeping the encoder output wires as short as possible.
Let's get started
1. See this tutorial and follow the Application Note to step 10. Note that the motor power wires on the Pololu motors are the red and black wires on the wiring harness.
2. Be sure you’ve disconnected power from the RoboClaw.
3. Locate the EN1, EN2 and encoder power headers on the RoboClaw. The photo below shows where these are located. Each encoder has two power wires and two wires of output for a total of 4 wires that need to be connected to the RoboClaw. Wire the encoder according to the table below. The encoder for each motor needs to wired to the correct encoder header. Motor channel M1 used ENC1 and motor channel M2 used ENC.
Motor Wire | Function | RoboClaw board |
Red | Motor power | M1A/M2A terminal |
Black | Motor power | M1B/M2B terminal |
Green | Encoder Ground | (-) pin on encoder power header |
Blue | Encoder Vcc | (+) pin on encoder power header |
Yellow | Encoder A output | ENC1/ENC2 header inside pin |
White | Encoder B output | ENC1/ENC2 header outside pin |
Figure 1
Enocder power and input headers.
4. Repeat the same process for the second motor if you have one. Remember to wire the encoder for a given motor channel to the correct encoder channel.
5. Reconnect power to the RoboClaw.
6. Open up BasicMicro Motion Studio and connect the RoboClaw like you’ve done in the App Note at the beginning of this one. Click on “PWM Settings” Use the sliders labeled “Motor 1” and “Motor 2” to move the motors forwards and backwards. Moving the slider up should move the motor forwards and down backwards. If the encoders are working properly the “M1 Encoder” and “M2 Encoder” values at the top of the window should increment when the motor moves in the forwards direction and decrement in the backwards direction.
Figure 2
BasicMicro Motion Studio encoder count and motor contol testing.
7. If the encoder counts don’t correspond to the motor direction power down the RoboClaw and reverse the connections on either the ENC1 or ENC2 header depending on which encoder isn’t working properly. The reason these two connections need to be reversed is that the RoboClaw uses the signals from both encoder channels to determine which direction the motor is rotating. If channel A and channel B of the encoder are reversed the motor controller thinks the motor is rotating the opposite direction that it is physcially rotating. If the encoder count doesn’t change at all in Motion Studio check to see if you’ve reversed the power connections for the encoder. If the encoder power has been reversed it’s possible that the encoders are now damaged.
Reading encoders with the Arduino
An Arduino or any other microcontroller can be used to read data from and about the encoders. Below is a small snippet of code that shows how to do this. If you would like to try this yourself, see this article about using the Arduino with the RoboClaw.
Figure 3
Arduino serial console output.
The functions used in the code below are:
uint32_t ReadEncM1(uint8_t address, uint8_t *status=NULL,bool *valid=NULL);
uint32_t ReadEncM2(uint8_t address, uint8_t *status=NULL,bool *valid=NULL);
uint32_t ReadSpeedM1(uint8_t address, uint8_t *status=NULL,bool *valid=NULL);
uint32_t ReadSpeedM2(uint8_t address, uint8_t *status=NULL,bool *valid=NULL);
These functions return the encoder count and speed as integers. You simply need to pass them the address of the RoboClaw in use and optionally variables to hold the status and valid values.
Figure 4
Arduino code to read encoder values.
Link to code sample
Tuning
After wiring the encoders and checking to make sure they’re working properly the motor and encoder combination can be tuned in BaiscMicro Motion Studio. If precise position and speed control isn't needed for a project there is no need do a tuning routine. However if precise control of speed and/or position is needed the motor and encoder comination must be tuned. There is an auto tune function to take care of this or you can manually do this yourself. Future articles will cover how to do both of these.