The RoboClaw normally uses encoders in an automatic closed loop control scheme to maintain speed and position. However there are situations where more custom control from a micrcontroller is called for. This Application Note covers using an Arduino to read, set and reset encoders using the BasicMicro Arduino library.
(1) RoboClaw motor controller
(1) Arduino Uno
(1) Pololu gear motor with quadrate encoder * **
(3) female to male 0.1″ jumper cable
(1) computer with BasicMicro Motion Studio installed
(1) micro USB cable for the RoboClaw
(1) USB cable for the Arduino
(1) small screwdriver
* Link to Pololu gearmotor with encoder
** Note that any motor with a built-in quadrate encoder can be used for this Application Note
Let’s Get Started
1. Follow this App Note to step 17 to configure the RoboClaw and Arduino.
Using the Arduino Library Functions
Below is a listing of all of the encoder functions available in the Arduino library. Note that all of the functions are called on an exisiting RoboClaw object created prevously. An App Note on the basics of using the library, including creating the RoboClaw object is available here.
There are two function used to read the current encoder values. As per their naming, one is used to read channel 1 and the other channel 2. Only the address of the RoboClaw needs to be passed to these functions.
In this example “ReadEncM1” is called on the RoboClaw object to read the current encoder count, the address of the RoboClaw hardware is passed as the only argument to the function. The value returned is saved to the variable “motor_1_count”.
int motor_1_count = roboclaw.ReadEncM1(0x80)
These two functions are used to read the speed of the motor in encoder counts, there is one function for each channel. The only argument to these functions is the address of the RoboClaw.
In this example “ReadSpeedM1” is called on the RoboClaw object to read the speed of motor 1 in encoder counts. The only argument passed to the function is the address of the RoboClaw.
int motor_1_speed = roboclaw.ReadSpeedM1(0x80)
These functions set their respective channel’s encoder count to a given value. The address of the RoboClaw and the value to be set must be passed to them.
In this example the count of the channel 1 encoder is set to 10,000 counts. The address of the RoboClaw is passed as well as the count to set.
Passing the address of the RoboClaw to this function and calling it causes both encoder channels to be set to zero.
In this example the encoder counts for both channels is set to zero by calling “ResetEncoders” with the address of the RoboClaw passed to the function.
motor_1_count = roboclaw.ResetEncoders(0x80)
These functions are used to set the motor for a given channel to a specific position in encoder counts. The address of the RoboClaw, an acceleration value, a deccelertion value, speed, position and flag value must be passed to the functions. The flag value determines whether or not the command is executed immediately or stored for later execution. A value of “0” stores the command and a value of “1” starts the positioning command immediately. The “M1M2” version of these functions allows for both motors be positioned independently and at the same time.
SpeedAccelDeccelPositionM1(address, accel, speed, deccel, position, flag)
SpeedAccelDeccelPositionM2(address, accel, speed, deccel, position, flag)
SpeedAccelDeccelPositionM1M2(address, accel1, speed1, deccel1, position1, accel2, speed2, deccel2, position2, flag)
In this example the position of motor 1 is set to 15000 counts. The address of the RoboClaw is passed as the first argument to the function. The acceleration and deceleration values are set to 10000 and the speed value set to 2000. The flag argument is set to “1” so that the command executes immediately.
motor_1_count = roboclaw.SpeedAccelDeccelPositionM1(0x80,10000,2000,10000,15000,1)
Running the Sample Code
There is sample code available to demonstrate the use of the functions detailed in this App Note. Follow the instructions below to obtain the code and test it with the Arduino and RoboClaw configured in step 1.
1. Download the RoboClaw Arduino library from here.
2. Add the RoboClaw Arduino library to the Arduino IDE. Note the location the RoboClaw library was downloaded to and then open the Arduino IDE. In the top menu click on “Sketch” and the go to “Include Library” and then “Add .Zip Library”. In the dialog box that opens select the downloaded .zip file and the click “Open”. The library has now been added to the Arduino installation and can be referenced from sketches.
Dialog for adding .zip libraries to the Arduino IDE installation.
3. Download or clone the sample code for this Application Note from here.
If Git is installed on the machine in use it can be cloned by entering the following command in the terminal.
git clone https://github.com/basicmicro/using_encoders_arduino.git
4. Open the example code in the Arduino IDE and upload it to the Arduino board.
5. The Arduino should now be busy running the example code and operating the attached RoboClaw.
Explanation of Example Code
The example code starts by reading the value of the encoder attached to channel 1 and printing the value to the serial monitor. It then proceeds to set the encoder count of channel 1 to 10000 counts, reads the value and prints it to demonstrate that setting the value works. From there it starts motor 1 running at half speed and then read and prints the speed value, the motor is then stopped. Next both encoders are set to zero by calling “ResetEncoders” and the encoder value read and printed. Lastly a position command is called to position motor 1 to a position of 15000 counts.
Troubleshooting the Example
If there is no response from the RoboClaw to the commands the Arduino is sending there are a few things to check. First ensure there is a ground connection between the Arduino and the RoboClaw, without it the two boards cannot communicate properly. Finally check to make sure the RX and TX connections between the Arduino and RoboClaw are not reverse. Pin 10 of the Arduino should be attached to the RoboClaw’s S2 signal pin and pin 11 of the Arduino attached to the RoboClaw’s S1 signal pin.