from joy.decl import *
from joy import Plan, JoyApp, StickFilter
# Create Buggy Plan, print "Buggy started" to terminal, and zero wheels
class Buggy( Plan ):
def __init__(self,*arg,**kw):
Plan.__init__(self,*arg,**kw)
def onStart( self ):
progress("Buggy started")
def onStop( self ):
progress("Stopping")
r = self.app.robot.at
r.lwheel.set_speed(0)
r.rwheel.set_speed(0)
# Watch for joystick movements, use these to set left/right wheel speed, and print wheel speed to terminal
def behavior( self ):
oncePer = self.app.onceEvery(0.5)
while True:
yield
sf = self.app.sf
lspeed = sf.getValue('joy0axis1')
rspeed = sf.getValue('joy0axis4')
r = self.app.robot.at
r.lwheel.set_speed(lspeed*200)
r.rwheel.set_speed(rspeed*200)
if oncePer():
progress("Buggy: left wheel %6f right wheel %6f"
% (lspeed,rspeed))
# Create BuggyApp JoyApp, a cluster that has two modules
class BuggyApp( JoyApp ):
def __init__(self,robot=dict(count=2),*arg,**kw):
JoyApp.__init__(self,robot=robot,*arg,**kw)
#Create StickFilter Plan, tell it to watch for two joysticks, and to process their signals through the low-pass filter 20x/sec
def onStart(self):
sf = StickFilter(self,dt=0.05)
sf.setLowpass( 'joy0axis1',10)
sf.setLowpass( 'joy0axis4',10)
sf.start()
self.sf = sf
self.ma = Buggy(self)
#Create onEvent Plan (the top-level event handler), tell it to watch for joystick motion and pass it to StickFilter Plan.
def onEvent(self,evt):
if evt.type == JOYAXISMOTION:
self.sf.push(evt)
# The dead-man switch: if the joystick button is held down the robot goes, otherwise it does not
elif evt.type==JOYBUTTONDOWN and evt.joy==0 and evt.button==0:
self.ma.stop()
elif evt.type==JOYBUTTONUP and evt.joy==0 and evt.button==0:
self.ma.start()
# Prevent robot-position events from printing to terminal, but print everything else
elif evt.type==CKBOTPOSITION:
return
JoyApp.onEvent(self,evt)
#Stop in an orderly fashion
def onStop(self):
self.ma.onStop()
#Boilerplate and housekeeping
if __name__=="__main__":
app = BuggyApp()
app.run()