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 quadrature encoder. The motor and encoder combination we’ll be looking at is the 25mm metal gearmotor from Pololu. This instructions in this Application Note apply to all of the motor and quadrature encoder combinations in this series.
(1) battery for RoboClaw
(4) male to female jumper wires
(1) computer with BasicMicro Motion Studio installed
(1) micro USB cable
(1) small screwdriver
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 quadrature decoder on the RoboClaw. The outputs from both channels are out of phase from each other by 90 degrees. The decoder determines by direction of rotation by looking at which channel’s pulse train leads the other.
Quadrature encoders are rated by the number of output pulses per revolution, this is the number of pulses that will be output every time the shaft completes one full turn. 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.
Changing the connectors on the wiring harness
The Pololu gearmotor comes with a 6 pin connector on it’s wiring harness. To make connecting the various wires to the appropriate locations on the RoboClaw the wiring will be divided up in to 3 pairs of wires and a 2 pin connector added to each pair.
1. Identify the existing wiring pairs based on the table below. Each of these pair will be place in to a 2 pin connector.
|Yellow||Encoder channel A|
|White||Encoder channel B|
Figure 1: The wiring harness with the stock 6 pin connector.
2. Remove the existing connector from the wiring harness by lifting up the small tab holding in each wire and pulling the wires out one by one.
Figure 2: The wiring harness with the connector removed.
3. For each pair of wires in the table insert the wires for the pair into an empty 2 pin connector. When inserting a wire orient the open side of the crimp connector facing the hole in the flat side of the connector. If the wire has been inserted correctly it will click in to place and will not pull out of the connector. Repeat this process for all of the pair in the harness. Be sure to wire the correct wires together or damage to hardware by occur.
Figure 3: The connectors before installation.
Figure 4: The wiring harness with connectors on the appropriate pairs.
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 of each motor will need to be wired to the correct encoder header. Motor channel M1 uses ENC1 and motor channel M2 uses 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: Motor and encoder with wiring harness.
Figure 2: Completed wiring for one encoder channel.
4. If a second motor is present repeat the process. Remember to ensure the encoder is wired properly and is paired with 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 3: BasicMicro Motion Studio encoder count and motor control testing.
7. If the encoder counts don’t correspond to the motor direction, the A and B channels are backwards. 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 in the opposite direction than it is physically 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
A microcontroller such as an Arduino can be used to read data from and about the encoders. Below is a small snippet of code that shows how to do this, it can be downloaded from here. If you would like to try this yourself, see this article about using the Arduino with the RoboClaw.
Figure 4: Arduino serial console output.
The functions used in the code below are:
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 5: Arduino code to read encoder values.
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. An Application Note is available that covers auto-tuning. If precise position and speed control isn’t required for a project tuning can be skipped. However if precise control of speed and/or position is needed the motor and encoder combination must be tuned. Motion Studio provides an auto tune function to take care of this or you can manually do this yourself.