This is my webpage to control my Robot. I'm using webiopi also.
I use a pololu servo instead of the Pi. I should change it but it is like that since 2013.
This is webpage using webioPI.
P.S. I changed my IP ! Just put yours.
Code: Select all
#!/usr/bin/env python3
from time import sleep
import webiopi
import RPi.GPIO as GPIO
from select import select
import serial
import ossaudiodev
import wave
from threading import Thread
out = serial.Serial('/dev/ttyAMA0',38400)
CamX = int(3000)
CamY = int(3000)
#set io pin
EnA = 11
EnB = 15
Out1 = 24
Out2 = 21
Out3 = 19
Out4 = 23
GPIO.setmode(GPIO.BOARD)
def Forward():
GPIO.output(Out1,True)
GPIO.output(Out3,True)
GPIO.output(Out2,False)
GPIO.output(Out4,False)
GPIO.output(EnA,True)
GPIO.output(EnB,True)
def Stop():
GPIO.output(Out1,False)
GPIO.output(Out3,False)
GPIO.output(Out2,False)
GPIO.output(Out4,False)
GPIO.output(EnA,True)
GPIO.output(EnB,True)
def Standby():
GPIO.output(Out1,False)
GPIO.output(Out3,False)
GPIO.output(Out2,False)
GPIO.output(Out4,False)
GPIO.output(EnA,False)
GPIO.output(EnB,False)
def Reverse():
GPIO.output(Out2,True)
GPIO.output(Out4,True)
GPIO.output(Out1,False)
GPIO.output(Out3,False)
GPIO.output(EnA,True)
GPIO.output(EnB,True)
def TurnRight():
GPIO.output(Out1,True)
GPIO.output(Out3,False)
GPIO.output(Out2,False)
GPIO.output(Out4,True)
GPIO.output(EnA,True)
GPIO.output(EnB,True)
def TurnLeft():
GPIO.output(Out1,False)
GPIO.output(Out3,True)
GPIO.output(Out2,True)
GPIO.output(Out4,False)
GPIO.output(EnA,True)
GPIO.output(EnB,True)
def ServoSpeed(id , speed):
mystring ='\x80\x01\x01' + chr(id) + chr(speed)
out.write(bytes(mystring,'iso-8859-1'))
print("Servo ",id,"speed = ",id,speed)
def ServoSleep(id):
mystring = '\x80\x01\x00' + chr(id) + chr(15)
out.write(bytes(mystring,'iso-8859-1'))
print("Servo ",id," to sleep")
def Servo( id , ServoPosition):
mystring = '\x80\x01\x04' + chr(id) + chr(ServoPosition >>7) + chr(ServoPosition & 0x7f)
out.write(bytes(mystring,'iso-8859-1'))
print("Servo ",id," = ",ServoPosition)
def CloseJaw():
Servo(2,4400)
def OpenJaw():
Servo(2,1000)
def play(filename):
sound_file = wave.open(filename,'rb')
#print("getting parameters")
(nc, sw, fr, nf, comptype, compname) = sound_file.getparams()
#print("parameters were", (nc, sw, fr, nf, comptype, compname))
sound = ossaudiodev.open('w')
sound.setparameters(ossaudiodev.AFMT_S16_NE, nc, fr)
data = sound_file.readframes(nf)
sound_file.close()
sound.write(data)
sound.close()
def Play1():
thread = Thread(target = play, args=("ialert.wav",))
thread.start()
def Play2():
thread = Thread(target = play, args=("tos-redalert.wav",))
thread.start()
def Play3():
thread = Thread(target = play, args=("s3.wav",))
thread.start()
def Play4():
thread = Thread(target = play, args=("s4.wav",))
thread.start()
def Play5():
thread = Thread(target = play, args=("s5.wav",))
thread.start()
def Play6():
thread = Thread(target = play, args=("s6.wav",))
thread.start()
def Play7():
thread = Thread(target = play, args=("s7.wav",))
thread.start()
def Play8():
thread = Thread(target = play, args=("s8.wav",))
thread.start()
def Play9():
thread = Thread(target = play , args=("s9.wav",))
thread.start()
#============ MAIN ==============
homeX = 2800
homeY = 3000
CamX = homeX
CamY = homeY
def CameraSet():
global CamX
global CamY
Servo(0, CamX+70)
Servo(1, CamY)
def CameraUp():
global CamX
global CamY
CamY=CamY + 100
if CamY > 3600:
CamY = 3600
CameraSet()
def CameraDown():
global CamX
global CamY
CamY=CamY - 100
if CamY < 2200:
CamY = 2200
CameraSet()
def CameraLeft():
global CamX
global CamY
CamX = CamX - 200
if CamX < 800:
CamX = 800
CameraSet()
def CameraRight():
global CamX
global CamY
CamX = CamX + 200
if CamX > 4800:
CamX = 4800
CameraSet()
def CameraHome():
global CamX
global CamY
CamX = homeX
CamY = homeY
CameraSet()
def CameraStop():
CameraSet()
CameraSet()
ServoSpeed(0,32)
ServoSpeed(1,32)
GPIO.setmode(GPIO.BOARD)
# set enable a as output
GPIO.setup(EnA,GPIO.OUT)
GPIO.output(EnA, False)
#set enable b as output
GPIO.setup(EnB,GPIO.OUT)
GPIO.output(EnB, False)
#set enable out 1,2,3 and 4 as output
GPIO.setup(Out1,GPIO.OUT)
GPIO.setup(Out2,GPIO.OUT)
GPIO.setup(Out3,GPIO.OUT)
GPIO.setup(Out4,GPIO.OUT)
GPIO.output(Out1,False)
GPIO.output(Out2,False)
GPIO.output(Out3,False)
GPIO.output(Out4,False)
server = webiopi.Server(
port=8000,
login="robot",
password="r2d2")
server.addMacro(Forward)
server.addMacro(Stop)
server.addMacro(Reverse)
server.addMacro(TurnRight)
server.addMacro(TurnLeft)
server.addMacro(OpenJaw)
server.addMacro(CloseJaw)
server.addMacro(CameraHome)
server.addMacro(CameraUp)
server.addMacro(CameraDown)
server.addMacro(CameraLeft)
server.addMacro(CameraRight)
server.addMacro(CameraStop)
server.addMacro(Play1)
server.addMacro(Play2)
server.addMacro(Play3)
server.addMacro(Play4)
server.addMacro(Play5)
server.addMacro(Play6)
server.addMacro(Play7)
server.addMacro(Play8)
server.addMacro(Play9)
webiopi.runLoop()
server.stop()
I do have a Pi A inside but I think I will redo the robot with a B+ and remove the USB hub, the Pololu servo. My usb battery pack has the battery cell inflated since it is always on for more than 2.5 years now . Also I will replace the L923 motor driver for some power mosfet driver to reduce power lost.
P.S. I do have a speaker on the robot if you wonder why is the keypad on the right. I just added snd_pcm_oss in /etc/modules.