(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
Using the Arduino Library Functions
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”.
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.
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.
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.
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.
Running the Sample Code
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.
Figure 1: 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.
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.