You may not understand what I do… but it is still awesome.

Python code for servoblaster

Assuming you have servoblaster working now, here is some starter code that can be used to send echo commands to the console
from subprocess import call
from time import sleep

def stop(time):
print(“stopleft”)
call (["echo 0=150 > /dev/servoblaster"], shell=True)
print(“stopright”)
call (["echo 1=150 > /dev/servoblaster"], shell=True)
sleep(time)

def call_command(servo,pulsewidth):
call(["echo "+str(servo)+"="+str(pulsewidth)+" > /dev/servoblaster"], shell=True)

def forwards(time):
call_command(0,200)
call_command(1,100)
sleep(time)
stop(0.1)

def back(time):
call_command(0,100)
call_command(1,200)
sleep(time)
stop(0.1)

def left(time):
call_command(0,200)
sleep(time)
stop(0.1)

def right(time):
call_command(1,100)
sleep(time)
stop(0.1)
#Edit instruction here using the definitions forward back left right sleep

forwards(2)
back(2)

 

One thought on “Python code for servoblaster

  • Fishy 2407 says:

    #Pibots advanced controls
    from subprocess import call
    from time import sleep

    def stop(time):
    call (["echo 0=150 > /dev/servoblaster"], shell=True)
    call (["echo 1=150 > /dev/servoblaster"], shell=True)
    sleep(time)

    def call_command(servo,pulsewidth):
    call(["echo "+str(servo)+"="+str(pulsewidth)+" > /dev/servoblaster"], shell=True)

    def forwards(time):
    call_command(0,200)
    call_command(1,100)
    sleep(time)
    stop(0.1)

    def back(time):
    call_command(0,100)
    call_command(1,200)
    sleep(time)
    stop(0.1)

    def left(time):
    call_command(0,200)
    sleep(time)
    stop(0.1)

    def right(time):
    call_command(1,100)
    sleep(time)
    stop(0.1)
    #now talk to the user
    #——————————————————————————————————————————-
    import easygui as eg
    while True:
    command=eg.enterbox(“which way do you want the bot to go?”)
    timing=eg.enterbox(“For how long?”)
    #——————————————————————————————————————————-
    if command == forwards:
    forwards(timing)
    elif command== back:
    back(timing)
    elif command==left:
    left(timing)
    elif command==right:
    right(timing)
    elif command==stop:
    stop(timing)
    elif command==exit:
    break
    #——————————————————————————————————————————-

    This new script uses easy Gui to let you tell it to do things so that you can make it up as you go along (with thanks to mr mac for the origional code)

Leave a Reply