Pololu Python Library
- July 16th, 2009
- Posted in My Projects . Robotics . Software
- By Carlitos
- Write comment
I started writing a library for controlling the Pololu motor controllers with a computer trough a serial port.
I’m writing this in Python so the code can be cross-platform but I would be very glad to have some feedback about running it on other OSs than Linux. Actually, any feedback would be very welcome.
As of now it can interface with the Pololu Micro Serial Servo Controller that I got form RobotShop. I am planning to use this code in my upcoming project RobotShop is sponsoring. I will supplement this library as I get newer hardware to work with.
I know there is already a python interface for it but I really wanted to have an object oriented way of managing motors (i.e. they can be instantiated and controlled more easily).
You can download the library here: lib_pololu.py (you will need to change the extension of the file to .py instead of txt).
In order to properly use this library you will require:
If you use a civilized OS you may be able to get all this by typing this in a command prompt:
sudo apt-get install idle python-serial
Here is a sample script that will use the library in order to control a servo: servo_example.py (you will need to change the extension of the file to .py instead of txt).
# Assuming that the file is at your/path/to/the/library/lib_pololu.py
import sys
sys.path.append(‘/your/path/to/the/library’)
#Import the lib_pololu module
import lib_pololu
#Import the serial communication module that should already be installed
import serial
#Open serial port
port = serial.Serial(‘/dev/ttyUSB0′)
port.baudrate=2400 #set an appropriate baudrate
#Create a motor assuming that the motor is connected to the connector
#number 0 on the controller. The two numbers (1150 and 4650) are
#the calibration values corresponding to the 0 ans 180 deg positions
#respectively. These are the numbers that should be sent to the servos
#using command 4 with the methos send_command() in lib_pololu
motor = lib_pololu.Servo(port, 0, 1150, 4650)
#Playing around with the motor
motor.set_pos(45) #sets the posotion of the motors in degerees.
motor.set_speed(100) #sets the speed at a value between 0 and 127
Note for Redmond OS (aka Window$) users: you will need the Win32 Python extension for pyserial to work.










No comments yet.