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

Source Code for Script script-demo_cyclePlan_py

 1  from joy import * 
 2   
3 -class CyclePlanApp( JoyApp ):
4 - def __init__(self,*arg,**kw):
5 JoyApp.__init__(self,scr={},*arg,**kw)
6
7 - def onStart( self ):
8 # Define callbacks moving cat to waypoints 9 def wayPt1(self): 10 self.app.scr.sensorUpdate(catX=100,catY=100)
11 def wayPt2(self): 12 self.app.scr.sensorUpdate(catX=100,catY=-100)
13 def wayPt3(self): 14 self.app.scr.sensorUpdate(catX=-100,catY=-100) 15 def wayPt4(self): 16 self.app.scr.sensorUpdate(catX=-100,catY=100) 17 CYCLE = { 18 0.0 : wayPt1, 19 0.2 : wayPt2, 20 0.6 : wayPt3, 21 0.8 : wayPt4 22 } 23 self.plan = CyclePlan(self, CYCLE) 24 self.plan.onStart = curry(progress,">>> START") 25 self.plan.onStop = curry(progress,">>> STOP") 26 self.freq = 1.0 27 self.rate = 0.05 28 self.limit = 1/0.45 29 30
31 - def onEvent(self, evt):
32 if evt.type==KEYDOWN: 33 if evt.key in [ord('q'),27]: # 'q' and [esc] stop program 34 self.stop() 35 # 36 elif evt.key==ord(' '): # [space] stops cycles 37 self.plan.setPeriod(0) 38 progress('Period changed to %s' % str(self.plan.period)) 39 # 40 elif evt.key in (ord(','),ord('.')): 41 f = self.freq 42 # Change frequency up/down in range -limit..limit hz 43 if evt.key==ord(','): 44 f = (1-self.rate)*f - self.rate*self.limit 45 else: 46 f = (1-self.rate)*f + self.rate*self.limit 47 if abs(f) < 1.0/self.limit: 48 self.plan.setPeriod( 0 ) 49 else: 50 self.plan.setPeriod( 1/f ) 51 self.freq = f 52 progress('Period changed to %g, %.2f Hz' % (self.plan.period,f)) 53 # 54 elif evt.key==ord('h'): 55 progress( "HELP: ',' to decrease/reverse period; '.' opposite; ' ' stop; 'q' end program; 'h' this help; other keys start Plan" ) 56 # 57 else: # any other key 58 progress( "Starting cycles...." ) 59 self.plan.start() 60 if evt.type!=TIMEREVENT: 61 JoyApp.onEvent(self,evt)
62 63 if __name__=="__main__": 64 print """ 65 Demo of SheetPlan class 66 ----------------------- 67 68 Use this demo with the demo-plans.sb Scratch project. 69 70 When any key is pressed, starts a SheetPlan making the 71 cat move around a square. 72 73 The application can be terminated with 'q' or [esc] 74 """ 75 import joy 76 joy.DEBUG[:]=[] 77 app=CyclePlanApp() 78 app.run() 79