Script demo_buggy_py
[hide private]
[frames] | no frames]

Source Code for Script script-demo_buggy_py

 1  from joy import * 
 2   
3 -class Buggy( Plan ):
4 - def __init__(self,*arg,**kw):
5 Plan.__init__(self,*arg,**kw) 6 self.r = self.app.robot.at
7
8 - def onStart( self ):
9 progress("Buggy started")
10
11 - def onStop( self ):
12 progress("Stopping") 13 self.r.lwheel.set_speed(0) 14 self.r.rwheel.set_speed(0)
15
16 - def behavior( self ):
17 oncePer = self.app.onceEvery(0.5) 18 while True: 19 yield 20 # Read joystick from application's StickFilter 21 sf = self.app.sf 22 lspeed = sf.getValue('joy0axis1') 23 rspeed = sf.getValue('joy0axis4') 24 #print(lspeed,rspeed) 25 26 self.r.lwheel.set_speed(lspeed*200) 27 self.r.rwheel.set_speed(rspeed*200) 28 29 if oncePer(): 30 progress("Buggy: left wheel %6f right wheel %6f" 31 % (lspeed,rspeed))
32
33 -class BuggyApp( JoyApp ):
34 - def __init__(self,robot=dict(count=2),*arg,**kw):
35 cfg = dict () 36 JoyApp.__init__(self,robot=robot,cfg=cfg,*arg,**kw)
37
38 - def onStart(self):
39 sf = StickFilter(self,dt=0.05) 40 sf.setLowpass( 'joy0axis1',10) 41 sf.setLowpass( 'joy0axis4',10) 42 sf.start() 43 self.sf = sf 44 self.ma = Buggy(self)
45
46 - def onEvent(self,evt):
47 if evt.type == JOYAXISMOTION: 48 # Forward a copy of the event to the StickFilter plan 49 self.sf.push(evt) 50 # Buttons --> press button0 to stop buggy; release to start 51 elif evt.type==JOYBUTTONDOWN and evt.joy==0 and evt.button==0: 52 self.ma.stop() 53 elif evt.type==JOYBUTTONUP and evt.joy==0 and evt.button==0: 54 self.ma.start() 55 56 # Hide robot position events 57 elif evt.type==CKBOTPOSITION: 58 return 59 JoyApp.onEvent(self,evt)
60
61 - def onStop(self):
62 self.ma.onStop()
63 64 if __name__=="__main__": 65 app = BuggyApp() 66 app.run() 67