Pololu Python Library
- July 16th, 2009
- 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:
- Python
- Pyserial
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).
# -*- coding: utf-8 -*-##################################################################################Author: Carlos Asmat##Creation Date: 12/07/09##Title: Pololu Module Usage Example##Description: Set of functions allowing the communication with## Pololu devices using the Pololu protocol.####Copyright: Carlos Asmat, 2009##License:## This program is free software: you can redistribute it and/or modify## it under the terms of the GNU General Public License as published by## the Free Software Foundation, either version 3 of the License, or## (at your option) any later version.#### This program is distributed in the hope that it will be useful,## but WITHOUT ANY WARRANTY; without even the implied warranty of## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the## GNU General Public License for more details.#### You should have received a copy of the GNU General Public License## along with this program. If not, see <http://www.gnu.org/licenses/>.####Description:## This is a short script that used the lib_Pololu library.##################################################################################
# Adding the path to the lib_pololu.py file to your modules path.# Assuming that the file is at your/path/to/the/library/lib_pololu.pyimport syssys.path.append('/your/path/to/the/library')
#Import the lib_pololu moduleimport lib_pololu
#Import the serial communication module that should already be installedimport serial
#Open serial portport = 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_pololumotor = lib_pololu.Servo(port, 0, 1150, 4650)
#Playing around with the motormotor.set_pos(45) #sets the posotion of the motors in degerees.motor.set_speed(100) #sets the speed at a value between 0 and 127Note for Redmond OS (aka Window$) users: you will need the Win32 Python extension for pyserial to work.










