Using Encoders with the Python Library

Introduction

Enocders are most often used as part of a feedback system to control speed, position and distance in a motor controller. However they can also be used by code interfacing with a RoboClaw motor controller. The RoboClaw’s Python library had functions that can be used to read, set and reset encoders as well as carry out positioning commands.

Materials

(1) RoboClaw motor controller

(1) Pololu 25mm gearmotor with encoder * **

(1) Raspberry Pi 3

(6) female to male 0.1″ jumper wires

(2) female to female jumper wires

(1) computer with BasicMicro Motion Studio installed

(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 tutorial to step 7 to wire and test the motors and encoders.

2. Follow this tutorial to step 14 to connect and test the Raspberry Pi in packet serial mode. Do not repeat steps already done in the tutorial linked in step 1.

Using the Python Library Functions

The RoboClaw Python library has a range of functions used to read, set and reset the encoder counts of attached encoders. Below, the functions are listed as well as examples of their usage. Noe that in the usage examples below all of the functions are called on an existing RoboClaw object.

Reading the Current Encoder Value

There are two functions use to read the encoders attached to a RoboClaw, one for motor channel one and another for motor channel two. Simply pass the address of the RoboClaw and store the value returned in a variable.

ReadEncM1(address)

ReadEncM2(address)

Code example:

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”.

motor_1_count = roboclaw.ReadEncM1(0x80)

Setting the Encoder Count

There are two functions for setting the encoder counts manually, one for each motor channel. The address of the RoboClaw must be passed to the function as well as the count to be set.

SetEncM1(address,val)

SetEncM2(address,val)

Code example:

In this example “SetEncM1” is called on the RoboClaw object to set the value of the encoder counts for channel 1. The address of the RoboClaw hardware and the desired count are passed as arguments to the function.

roboclaw.SetEncM1(0x80, 10000)

Resetting Both Encoders

Both encoder can be reset to zero with the function below. It can be useful to do this before certain operations such as calling position functions.

ResetEncoders(address)

Code example:

In this example the function “ResetEncoders” is called on the RoboClaw object. This resets both encoder channel counts to 0. The only argument passed to the function is the hardware address of the RoboClaw in use.

roboclaw.ResetEncoders(0x80)

Set the Motor Position

One or both motors can be set to a specific position based on encoder counts. Before using these functions each motor has to have its position tuning parameters set in Motion Studio as well as minimum and maximum position values set.

SpeedAccelDeccelPositionM1(address,accel,speed,deccel,position,buffer)

SpeedAccelDeccelPositionM2(address,accel,speed,deccel,position,buffer)

SpeedAccelDeccelPositionM1M2(address,accel1,speed1,deccel1,position1,accel2,speed2,deccel2,position2,buffer)

Code example:

In this example the “SpeedAccelDeccelPositionM1” function is called on the RoboClaw object to position the motor attached to channel 1 on the RoboClaw. The hardware address is passed as the first argument to the function as well as acceleration, decceleration, speed and positon values. The final argument to the function of “1” is the buffer value that tells the RoboClaw to execute the postion command immediately.

roboclaw.SpeedAccelDeccelPositionM1(0x80,10000,2000,10000,15000,1)

Example Code

1. Download or clone the repository from here.

To clone the repository enter the following in the terminal:

git clone https://github.com/basicmicro/using_encoders_python.git

2. Unzip the repository if it was downloaded, if cloned navigate to the directory is was cloned to.

3. Run the code by entering the following command in a terminal window:

python using_encoders_python.py

Figure 1: The example code for this Application Note.

Explanation of the sample code

The example code starts by creating a RoboClaw object and calling its “Open()” function to begin communication with the RoboClaw hardware. Then the encoder for motor channel 1 is read and the result printed. From there the encoder count for motor channel 1 is set to 10000 counts and the count read and printed again to demonstrate that setting the encoder count works properly. Next the “ResetEncoders” function is called to reset both counters to zero and once again the value is read and printed. Finally motor channel 1 is sent a position command. The attached motor should run until it reaches the requested position and then stop.