Example 1: Moving one servo connected to the zum bt-328 board

Introduction

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

Stuff needed

  • IPython 3.0 installed
  • Python pyserial library installed
  • A web browser
  • One Futaba 3003 servo (or any other compatible)
  • Control board: bq zum BT-328 board or any other arduino compatible board
  • USB cable

Steps

1 Install Ipython 3.0

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

2 Install the Pyserial python library

Read These instructions

Depending on your OS you may chose different methods:

  • Ubuntu: sudo apt-get install python-serial
  • Anaconda environment: conda install pyserial
  • PyPI: pip install pyserial:

3 Download the zum servos project

Download or clone the zum-servos github repo It contains the firmware, python classes and example notebooks for moving the servos

4 Hardware connections

  • Connect the Servo to the PIN 9 of the zum / arduino board
  • Connect the board to the computer with the USB cable

5 Upload the zum-servos firmware into the board

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

6 Launch the Ipython notebook

Launch the Ipython notebook from the zum-servos/python folder. Open the zum-servos-example1 notebook (this one you are reading :-)

7 Configure the serial port

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

8 run the notebook

Run the notebook and enjoy moving the servo from the bottom slider!!!

How it works

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

The python code

Import the Serial class. This is needed for opening the serial port.


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 [ ]: