control.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
4 # enable debugging
5 import cgi, cgitb
6 import os
7 import pexpect
8 import time
9 cgitb.enable()
10 
11 import popen2
12 
13 
14 print "Content-type: text/html"
15 print
16 
17 print "RESULT"
18 
19 ros_distro = "ROS_DISTRO" #This gets overwritten by the install script
20 
21 form = cgi.FieldStorage()
22 message = form.getvalue("action", "NO_ACTION")
23 
24 def run(fun):
25  out = ""
26  v = popen2.popen4(fun)
27  for i in v[0]:
28  out = out + i
29  return out
30 
31 def run_as_robot(command):
32  run = "su applications -c \"" + command + "\""
33  #print "C", run
34  child = pexpect.spawn(run)
35  result = child.expect(["ssword:", "(yes/no)?"])
36  child.sendline("willow")
37  out = ""
38  for i in child.readlines():
39  out = out + i
40  return out
41 
42 
43 
44 if (message == "GET_STATE"):
45  print "USERS"
46  active_user = "(UNKNOWN)"
47  dead_users = ""
48  message = ""
49  for i in run("robot users --no-plist").split("\n"):
50  if (i.find("Active User:") != -1):
51  active_user = i.split(":")[1].strip()
52  if (i.find("Message:") != -1):
53  message = i.split(":")[1].strip()
54  if (i.find("*") != -1):
55  dead_users = dead_users + i[i.find("*") + 1:i.find("(")].strip() + ","
56 
57  #print "ACTIVE_USER: ", active_user
58  #print "INACTIVE_USERS:", dead_users.strip().strip(",")
59  #print "MESSAGE:", message
60 
61  if (active_user == "applications"):
62  print "STATE_VALID"
63  elif (active_user == "" or active_user == "None"):
64  print "STATE_OFF"
65  else:
66  print "STATE_IN_USE"
67  print "USER:", active_user
68  if (message != ""):
69  print "MESSAGE:", message #TODO: no newlines!
70 
71  #print "PROCESSES:"
72  #processes = ""
73  #for i in run_as_robot("robot plist").split("\n"):
74  # if (i.strip() != "Password:" and i.strip() != "The following processes are running:" and i.strip() != "" and i.strip() != "No processes running."):
75  # l = i.strip().split(None)
76  # if (len(l) > 5):
77  # print l[5], "from", l[4]
78  # else:
79  # print "INVALID:", i.strip()
80 
81  print
82 elif (message == "STOP_ROBOT"):
83  print "STOPPING_ROBOT"
84  result = run_as_robot("yes | robot claim -m 'stopping the robot' ; yes | robot stop ; yes | robot release")
85  print result
86  print "DONE"
87 elif (message == "START_ROBOT"):
88  print "STARTING_APP_MAN"
89  print run_as_robot("yes | robot claim -m 'running applications platform'")
90  print run_as_robot(". /opt/ros/" + ros_distro + "/setup.bash ; . ~/.bashrc ; export ROS_ENV_LOADER=/opt/ros/" + ros_distro + "/env.sh ; nohup roslaunch pr2_app_manager whole_pr2_apps.launch > ~/run.txt &")
91  # Wait for master to become available
92  import socket
93  s = socket.socket()
94  connect = 0
95  # Wait for a maximum of 30 seconds
96  while connect < 30:
97  try:
98  s.connect(('localhost', 11311))
99  connect = 30
100  s.close()
101  except:
102  time.sleep(1)
103  connect += 1
104  print "DONE"
105 else:
106  print "REJECT_COMMAND"
107  print "action = STOP_ROBOT,START_ROBOT,GET_STATE"
108 
def run_as_robot(command)
Definition: control.py:31
def run(fun)
Definition: control.py:24


pr2_app_manager
Author(s): Vijay Pradeep
autogenerated on Sat Feb 27 2021 04:00:58