icub-client
drive_control_rpc.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import yarp
4 
5 #Open network
6 yarp.Network.init()
7 
8 # Open the RPC port
9 toHomeo = yarp.Port()
10 homeoPortName="/manager/toHomeostasis/rpc"+":o"
11 toHomeo.open(homeoPortName)
12 
13 toAllo = yarp.Port()
14 alloPortName="/manager/toAllostasis/rpc"+":o"
15 toAllo.open(alloPortName)
16 
17 toBM = yarp.Port()
18 BMPortName="/manager/BehaviorManager/rpc"+":o"
19 toBM.open(BMPortName)
20 
21 toPlanner = yarp.Port()
22 plannerPortName="/manager/toPlanner/rpc:o"
23 toPlanner.open(plannerPortName)
24 
25 homeoRPC = "/homeostasis/rpc"
26 alloRPC = "/AllostaticController/rpc"
27 BMRPC = "/BehaviorManager/rpc"
28 plannerRPC = "/planner/rpc"
29 
30 print yarp.Network.connect(homeoPortName,homeoRPC)
31 print yarp.Network.connect(alloPortName,alloRPC)
32 print yarp.Network.connect(BMPortName,BMRPC)
33 print yarp.Network.connect(plannerPortName,plannerRPC)
34 
36  cmd = yarp.Bottle()
37  rply = yarp.Bottle()
38  cmd.clear()
39  cmd.addString('names')
40  toHomeo.write(cmd,rply)
41  driveList = rply.toString().strip('()').split(' ')
42  print driveList
43  return driveList
44 
46  cmd = yarp.Bottle()
47  rply = yarp.Bottle()
48  cmd.clear()
49  cmd.addString('names')
50  toBM.write(cmd,rply)
51  behList = rply.toString().strip('()').split(' ')
52  print behList
53  return behList
54 
56  print "Interrupting and closing ports"
57  # interrupting
58  toHomeo.interrupt()
59  toAllo.interrupt()
60  toBM.interrupt()
61  toPlanner.interrupt()
62  # closing
63  toHomeo.close()
64  toAllo.close()
65  toBM.close()
66  toPlanner.close()
67 
68 
69 def trigger_behavior(behavior):
70  # Prepare command
71  cmd = yarp.Bottle()
72  cmd.clear()
73  cmd.addString('force')
74  cmd.addString(behavior)
75  cmd.addString('bottom')
76  # Send command
77  if not yarp.Network.isConnected(homeoPortName,homeoRPC):
78  print yarp.Network.connect(homeoPortName,homeoRPC)
79  yarp.Time.delay(0.1)
80  toHomeo.write(cmd)
81  yarp.Time.delay(2.)
82  reset_all()
83  freeze_all()
84 
85 
86 def launch_behavior(behavior):
87  # Prepare command
88  cmd = yarp.Bottle()
89  cmd.clear()
90  cmd.addString(behavior)
91  # Send command
92  if not yarp.Network.isConnected(BMPortName,BMRPC):
93  print yarp.Network.connect(BMPortName,BMRPC)
94  yarp.Time.delay(0.1)
95  toHomeo.write(cmd)
96  yarp.Time.delay(2.)
97  reset_all()
98  freeze_all()
99 def freeze_all():
100  # Prepare command
101  cmd = yarp.Bottle()
102  cmd.clear()
103  cmd.addString('freeze')
104  cmd.addString('all')
105  # Send command
106  if not yarp.Network.isConnected(homeoPortName,homeoRPC):
107  print yarp.Network.connect(homeoPortName,homeoRPC)
108  yarp.Time.delay(0.1)
109  toHomeo.write(cmd)
110 
112  # Prepare command
113  cmd = yarp.Bottle()
114  cmd.clear()
115  cmd.addString('unfreeze')
116  cmd.addString('all')
117  # Send command
118  if not yarp.Network.isConnected(homeoPortName,homeoRPC):
119  print yarp.Network.connect(homeoPortName,homeoRPC)
120  yarp.Time.delay(0.1)
121  toHomeo.write(cmd)
122 
123 def reset_all():
124  # Prepare command
125  cmd = yarp.Bottle()
126  cmd.clear()
127  cmd.addString('par')
128  cmd.addString('all')
129  cmd.addString('reset')
130  # Send command
131  if not yarp.Network.isConnected(homeoPortName,homeoRPC):
132  print yarp.Network.connect(homeoPortName,homeoRPC)
133  yarp.Time.delay(0.1)
134  toHomeo.write(cmd)
135 
136 def narrate():
137  trigger_behavior("narrate")
138 
139 def pointing():
140  trigger_behavior("pointing")
141 
142 def tagging():
143  trigger_behavior("tagging")
144 
145 def test():
146  trigger_behavior("test")
147 
149  # Prepare command
150  cmd_allo = yarp.Bottle()
151  cmd_allo.clear()
152  cmd_allo.addString('manual')
153  cmd_allo.addString('on')
154  # Send command to allostatic controller
155  if not yarp.Network.isConnected(alloPortName,alloRPC):
156  print yarp.Network.connect(alloPortName,alloRPC)
157  yarp.Time.delay(0.1)
158  toAllo.write(cmd_allo)
159 
160  # Send command to planner
161  cmd_planner = yarp.Bottle()
162  cmd_planner.clear()
163  cmd_planner.addString('manual')
164  cmd_planner.addString('on')
165  if not yarp.Network.isConnected(plannerPortName,plannerRPC):
166  print yarp.Network.connect(plannerPortName,plannerRPC)
167  yarp.Time.delay(0.1)
168  toPlanner.write(cmd_planner)
169 
170  yarp.Time.delay(0.1)
171  freeze_all()
172  yarp.Time.delay(0.1)
173  reset_all()
174 
175 
177  # Prepare command
178  cmd = yarp.Bottle()
179  cmd.clear()
180  cmd.addString('manual')
181  cmd.addString('off')
182  # Send command
183  if not yarp.Network.isConnected(alloPortName,alloRPC):
184  print yarp.Network.connect(alloPortName,alloRPC)
185  yarp.Time.delay(0.1)
186  toAllo.write(cmd)
187 
188  # Send command to planner
189  cmd_planner = yarp.Bottle()
190  cmd_planner.clear()
191  cmd_planner.addString('manual')
192  cmd_planner.addString('off')
193  if not yarp.Network.isConnected(plannerPortName,plannerRPC):
194  print yarp.Network.connect(plannerPortName,plannerRPC)
195  yarp.Time.delay(0.1)
196  toPlanner.write(cmd_planner)
197 
198  yarp.Time.delay(0.1)
199  reset_all()
200  yarp.Time.delay(0.1)
201  unfreeze_all()
def launch_behavior(behavior)
def trigger_behavior(behavior)