Script demo_shavenhaircut_py
|
|
1 from joy import *
2
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
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
21
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
49
51 if evt.type != KEYDOWN:
52 return
53
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
120
121 app = ShaveNHaircutApp(shaveSpec,hairSpec,robot=robot,scr=scr)
122 app.run()
123