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

Source Code for Script script-demo_shavenhaircut_py

  1  from joy import * 
  2   
3 -class ShaveNHaircutPlan( Plan ):
4 """ 5 ShaveNHaircutPlan shows a simple example of sequential composition: 6 its behavior is to run the shave plan followed by the haircut plan. 7 """
8 - def __init__(self,app,shave,haircut,*arg,**kw):
9 Plan.__init__(self,app,*arg,**kw) 10 self.shave = shave 11 self.haircut = haircut
12
13 - def behavior( self ):
14 progress("Both: starting 'Shave' sequence") 15 yield self.shave 16 progress("Both: starting 'Haircut' sequence") 17 yield self.haircut 18 progress("Both: done")
19
20 -class ShaveNHaircutApp( JoyApp ):
21 # Load both patterns from their CSV files 22 SHAVE = loadCSV("shave.csv") 23 HAIRCUT = loadCSV("haircut.csv") 24
25 - def __init__(self,shaveSpec,hairSpec,*arg,**kw):
26 JoyApp.__init__(self, *arg,**kw) 27 self.shaveSpec = shaveSpec 28 self.hairSpec = hairSpec
29
30 - def onStart(self):
31 # 32 # Use a sheetplan to realize SHAVE, outputting to self.shaveSpec 33 # 34 self.shaveplan = SheetPlan(self, self.SHAVE, x=self.shaveSpec ) 35 # give us start and stop messages; in your own code you can omit these 36 self.shaveplan.onStart = lambda : progress("Shave: starting") 37 self.shaveplan.onStop = lambda : progress("Shave: done") 38 # 39 # Use a sheetplan to realize H, outputting to s 40 # 41 self.hairplan = SheetPlan(self, self.HAIRCUT, x=self.hairSpec ) 42 # give us start and stop messages; in your own code you can omit these 43 self.hairplan.onStart = lambda : progress("Haircut: starting") 44 self.hairplan.onStop = lambda : progress("Haircut: done") 45 # 46 # Set up a ShaveNHaircutPlan using both of the previous plans 47 # 48 self.both = ShaveNHaircutPlan(self, self.shaveplan, self.hairplan)
49
50 - def onEvent(self,evt):
51 if evt.type != KEYDOWN: 52 return 53 # assertion: must be a KEYDOWN event 54 if evt.key == K_s: 55 if ( not self.shaveplan.isRunning() 56 and not self.both.isRunning() ): 57 self.shaveplan.start() 58 elif evt.key == K_h: 59 if ( not self.hairplan.isRunning() 60 and not self.both.isRunning() ): 61 self.hairplan.start() 62 elif evt.key == K_b: 63 if ( not self.shaveplan.isRunning() 64 and not self.hairplan.isRunning() 65 and not self.both.isRunning() ): 66 self.both.start() 67 elif evt.key == K_ESCAPE: 68 self.stop()
69 70 if __name__=="__main__": 71 robot = None 72 scr = None 73 shaveSpec = "#shave " 74 hairSpec = "#haircut " 75 args = list(sys.argv[1:]) 76 while args: 77 arg = args.pop(0) 78 if arg=='--mod-count' or arg=='-c': 79 N = int(args.pop(0)) 80 robot = dict(count=N) 81 elif arg=='--shave' or arg=='-s': 82 shaveSpec = args.pop(0) 83 if shaveSpec[:1]==">": scr = {} 84 elif arg=='--haircut' or arg=='-h': 85 hairSpec = args.pop(0) 86 if hairSpec[:1]==">": scr = {} 87 elif arg=='--help' or arg=='-h': 88 sys.stdout.write(""" 89 Usage: %s [options] 90 91 'Shave and a Haircut' example of running Plan-s in parallel and in 92 sequence. The example shows that plans can run in parallel, and that 93 Plan behaviors (e.g. the ShaveNHaircutPlan defined here) can use other 94 plans as sub-behaviors, thereby "calling them" sequentially. 95 96 When running, the demo uses the keyboard. The keys are: 97 's' -- start "Shave" 98 'h' -- start "Haircut" 99 'b' -- start "Both", calling "Shave" and "Haircut" in sequence 100 'escape' -- exit program 101 102 Options: 103 --mod-count <number> | -c <number> 104 Search for specified number of modules at startup 105 106 --shave <spec> | -s <spec> 107 --haircut <spec> | -h <spec> 108 Specify the output setter to use for 'shave' (resp. 'haircut') 109 110 Typical <spec> values would be: 111 '#shave ' -- to print messages to the terminal with '#shave ' as prefix 112 '>x' -- send to Scratch sensor 'x' 113 'Nx3C/@set_pos' -- send to position of CKBot servo module with ID 0x3C 114 115 NOTE: to use robot modules you MUST also specify a -c option 116 117 """ % sys.argv[0]) 118 sys.exit(1) 119 # ENDS cmdline parsing loop 120 121 app = ShaveNHaircutApp(shaveSpec,hairSpec,robot=robot,scr=scr) 122 app.run() 123