web analytics

Servo Widget Python Example Code

This code uses the Servo Widget Class


import servoWidget
import time
import random

# super simple program for wireless servo widget
slave = 1
servo = 0

# instantiate class
master = servoWidget.MasterController("COM5")

# set the number of slaves scanned to 8 so we get REALLY fast updates (max is 64)
master.setNumSlaves(8)

# loop through all servos on slave # 1

while(1):
    print "slave", slave, "servo", servo

    master.setServoSpeed(slave, servo, 300)

    # set servo position, wait, and bring it back
    master.setServoPosition(slave, servo, 900)

    # do it by altering the postion a bit at a time
    for i in range(1000, 2000, 10):
        master.setServoPosition(slave, servo, i)

##    time.sleep(1)

    # read analog data here, note offsets, first four bytes are tossed
    data = master.getSlaveData(slave)
    
    d0=ord(data[4])
    d1=ord(data[5])
    d = (d1<<8) | d0
    print "analog inputs", d,

    d0=ord(data[6])
    d1=ord(data[7])
    d = (d1<<8) | d0
    print d,

    d0=ord(data[8])
    d1=ord(data[9])
    d = (d1<<8) | d0
    print d
    
    servo = servo + 1
    if servo > 5:
       servo = 0
       if slave == 4:
          slave = 1
       else:
          slave = 4
       
    time.sleep(1)