RIG InMoov Project
Functions | Variables
servo_demo Namespace Reference

Documentation for the servo_demo.py script. More...

Functions

def connect (port)
 Sets the global ser variable. More...
 
def sweep (initial, servos, starts, ends, steps)
 Produces complex command chains. More...
 
def unsweep (servos, ends, steps)
 Produces a command chain that resets all servos to 0. More...
 
def servowrite (commands, delay)
 Writes commands to the serial port. More...
 
def gesture (initial, servos, starts, ends, steps, delay)
 Sets the servos to a position, waits for input, then resets them to 0. More...
 
def reset ()
 Immediately sets the servos to 0. More...
 
def cmd ()
 Sends the input to the serial port until a blank line is given. More...
 
def dump (servos=6)
 Retrieves information about the board. More...
 
def peace (delay=0)
 Makes a peace sign. More...
 
def ok (delay=0.01)
 Makes an ok sign. More...
 
def grab (delay=0.02)
 Makes a fist. More...
 
def rockon (delay=0.01)
 Makes a rock on sign. More...
 
def wiggle (n=90, delay=0.01, wiggles=1)
 Wiggles the fingers. More...
 
def count ()
 Counts to 5 on the fingers. More...
 
def demo ()
 Performs all movements. More...
 
def getarrow ()
 
def calibrate (s=1)
 Calibrate a servo. More...
 
def calibrate_all ()
 

Variables

int CANCEL_SIGNAL = 255
 
int DUMP_SIGNAL = 253
 
int START_RESPONSE = 252
 
int END_RESPONSE = 251
 
int RHAND_ID = 181
 The identification byte of the right hand board.
 
 ser = serial.Serial()
 The serial connection for input and output. More...
 

Detailed Description

Documentation for the servo_demo.py script.

An example script for controlling the right hand. This script can also be used to calibrate the bot. Please see connect, cmd, and calibrate. This script requires python with a library called pyserial. After instally python 3, pyserial can be installed by calling

1 pip install pyserial

Or through your package manager. Example:

1 sudo apt-get install python-pyserial

Once you have the library installed, you must have your PYTHONPATH variable set to the location of the library.

1 export PYTHONPATH=/usr/lib/python2.7/site-packages

Replace 2.7 with your version of python (type in /usr/lib/python and press tab once or twice).

This may vary from system to system. See the python and pyserial websites for help.

To run the script, execute

1 python -i servo_demo.py

or in python

1 import servo_demo

This will allow you to call the functions from a command interpreter. It is recommended you experiment with all functions and view the source to understand how you might produce commands. Also try demo() while connected.

The movements included are quickly written and for demo purposes only.

See also
Introduction for the servo codes.

Function Documentation

def servo_demo.calibrate (   s = 1)

Calibrate a servo.

Takes one argument, which is the id of a server. Introduction has the servo codes.

Use the left and right arrow keys to adjust the value of the servo. Use any other key to exit.

Enter the maximum values into the limit array in your settings file.

Note that if a servo isn't moving the correct way, for example if a finger isn't flat at 0 and curved at 180, you must set reverse to 1 at the servo index.

Definition at line 375 of file servo_demo.py.

def servo_demo.cmd ( )

Sends the input to the serial port until a blank line is given.

Must enter integers between 0-255.

See also
Arduino Protocol

Definition at line 229 of file servo_demo.py.

def servo_demo.connect (   port)

Sets the global ser variable.

Call this to connect to the board.

1 connect ('/dev/ttyUSB0')

Replace /dev/ttyUSB0 with the name of the port your board is connected to. Usually this is /dev/ttyUSB* for the arduino nano or /dev/ttyACM* for the arduino uno. This is the same port used in Installation.

Note that you must be part of the dialout group in Linux. Enter this command to add yourself to a group.

1 usermod -a -G dialout $USER

You must logout to apply group changes. If you cannot logout, you can run the script as root.

1 sudo su
2 export PYTHONPATH=/usr/lib/python2.7/site-packages
3 python -i example/servo_demo.py

Definition at line 72 of file servo_demo.py.

def servo_demo.count ( )

Counts to 5 on the fingers.

See also
sweep()

Definition at line 321 of file servo_demo.py.

def servo_demo.demo ( )

Performs all movements.

Definition at line 329 of file servo_demo.py.

def servo_demo.dump (   servos = 6)

Retrieves information about the board.

The command sends the DUMP_SIGNAL to the serial port and reads back the response. The response is described in servo.ino.

When you write your own similar function, timing is important.

We may need to increase the response delay on the Arduino if you are unable to recieve a response. View the source of this function to understand how it works, but keep in mind it is untested. Also keep in mind that the function is very non-general as it always reads the same amount of bytes instead of looking for the end signal.

Note
While this function could be used to change dynamically between gestures instead of resetting the servos to 0, this function did not exist when this script was first written.

Definition at line 241 of file servo_demo.py.

def servo_demo.gesture (   initial,
  servos,
  starts,
  ends,
  steps,
  delay 
)

Sets the servos to a position, waits for input, then resets them to 0.

See also
sweep() servowrite()

Definition at line 212 of file servo_demo.py.

def servo_demo.grab (   delay = 0.02)

Makes a fist.

See also
gesture()

Definition at line 295 of file servo_demo.py.

def servo_demo.ok (   delay = 0.01)

Makes an ok sign.

See also
gesture()

Definition at line 291 of file servo_demo.py.

def servo_demo.peace (   delay = 0)

Makes a peace sign.

See also
gesture()

Definition at line 287 of file servo_demo.py.

def servo_demo.reset ( )

Immediately sets the servos to 0.

Definition at line 225 of file servo_demo.py.

def servo_demo.rockon (   delay = 0.01)

Makes a rock on sign.

See also
gesture()

Definition at line 299 of file servo_demo.py.

def servo_demo.servowrite (   commands,
  delay 
)

Writes commands to the serial port.

If delay is 0, immediately writes a list of ints to ser as bytes. Otherwise, writes a list of ints to ser as bytes one at a time.

Inserting a delay controls the speed of the movements.

See also
connect()

Definition at line 195 of file servo_demo.py.

def servo_demo.sweep (   initial,
  servos,
  starts,
  ends,
  steps 
)

Produces complex command chains.

The output returned from the function is formatted in such a way that the commands can be transmitted to the serial port at variable speeds.

The parameters must all be lists of the same length whose indexes correspond with servos.

Parameters
initialAn array of initial commands to append to.
servosThe servos to control.
startsThe current positions of the servos.
endsThe desired end positions.
stepsThe values to use to decrement/increment. Converted to absolute value.
Returns
Upon success, returns the generated list of commands. The commands are surrounded by the CANCEL_SIGNAL for syncing. The servos will always reach their exact final destination, unless if steps at the index of the servo is 0. Upon error, raises 'Invalid sweep command'. This indicates non-matching list lengths.
Example
1 sweep ([], [1,2], [180,0], [90,180], [90,45])
Returns
1 [CANCEL_SIGNAL, 1, 180, 2, 0, 1, 90, 2, 45, 2, 90, 2, 135, 2, 180, CANCEL_SIGNAL]
The protocol this follows is described in servo.ino. By intertwining commands in this way, a delay can be inserted between bytes and fluid motion is still preserved.

Definition at line 103 of file servo_demo.py.

def servo_demo.unsweep (   servos,
  ends,
  steps 
)

Produces a command chain that resets all servos to 0.

See also
sweep()

Definition at line 187 of file servo_demo.py.

def servo_demo.wiggle (   n = 90,
  delay = 0.01,
  wiggles = 1 
)

Wiggles the fingers.

See also
sweep()

Definition at line 303 of file servo_demo.py.

Variable Documentation

servo_demo.ser = serial.Serial()

The serial connection for input and output.

Must be initialized.

See also
connect()

Definition at line 70 of file servo_demo.py.