This example shows how to move one servo using the interactive IPython 3.0 widgets from Jupyter (known before as ipython notebooks). This notebook is only a "hello world" example, but it opens the door to control small servo-driven robots from Jupyter. It is quite useful when researching on gait generation. The locomotion algorithms can be tested easily on real robots
The bq zum BT-328 board is compatible with arduino, so this example can also be tested with arduino boards
Read these instructions for installing the latest Ipython 3.0. Once installed, check that you alredy have installed the 3.0 version. Open the console and type ipython --version:
ipython --version
3.0.0
Read These instructions
Depending on your OS you may chose different methods:
sudo apt-get install python-serial
conda install pyserial
pip install pyserial
:Download or clone the zum-servos github repo It contains the firmware, python classes and example notebooks for moving the servos
Open the zum_servos_fw.ino firmware with the arduino IDE and upload it into the zum / arduino board. It is locate in the folder: zum-servos/firmware/zum_servos_fw
Launch the Ipython notebook from the zum-servos/python folder. Open the zum-servos-example1 notebook (this one you are reading :-)
In the example python code, change the serial port name where your board is connected. In Linux the default name is /dev/ttyUSB0. In Windows: COM1, COM2...
Run the notebook and enjoy moving the servo from the bottom slider!!!
The python class Servo communicates with the zum-servos firmware by the USB serial line. The Servo objects have the method set_pos(ang) to set the servo position. When this method is invoked, a command is sent to the zum board by serial communication. The firmware process it and moves the servo to the given position
The code is quite simple. First the serial port is opened (Important: baud rate should be set to 19200). Then a servo object is created. Finally the set_pos() method of the servo is called by the interactive Ipython 3.0 function to display the slider
In [7]:
from serial import Serial
Import the Servo class. It is needed for creating the Servo objects
In [8]:
from Servo import Servo
Import the IPython 3 interact function. It is needed for creating the Interactive slider that moves the servo
In [9]:
from IPython.html.widgets import interact
Open the serial port
Change the serial device name. In linux, by default, it is /dev/ttyUSB0. In Windows, it should be COM1, COM2 ...
In [10]:
sp = Serial("/dev/ttyUSB0", 19200)
Create a servo object. It is linked to the serial port already opened
In [11]:
a = Servo(sp, dir = 'a')
Interactive widget for moving the servo
In [12]:
w1 = interact(a.set_pos, pos = (-90, 90))
Example of a simple servo sequence generation
In [28]:
import time
#-- Sequence of angles
seq = [40, 0, 20, -40, -80, 0]
#-- Repeat the sequence n times
for n in range(2):
for ang in seq:
a.pos = ang
time.sleep(0.8)
In [ ]: