hw_controller.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 import ConfigParser
3 import importlib
4 import itertools
5 import rospy
6 import __builtin__
7 import std_msgs.msg
8 import jason_msgs.msg
9 from pathlib import Path
10 from collections import OrderedDict
11 from threading import Event
12 from threading import RLock
13 
14 def str2bool(v):
15  return v.lower() in ("yes", "true", "t", "1")
16 
17 def setattr_recursive(obj_list, attr_list, value):
18  aux = obj_list.pop()
19  setattr(aux, attr_list.pop(), value)
20  if len(obj_list) > 0:
21  return setattr_recursive(obj_list, attr_list, aux)
22  else:
23  return aux
24 
25 class CommInfo:
26  def __init__(self):
27  self.method = ""
28  self.msg_type = ""
29  self.name = ""
30  self.dependencies = ""
31  self.module = None
32  self.args = []
33  self.params_dict = OrderedDict()
34  self.buf = ""
35  self.latch = "True"
36 
37  def fill_data(self, name, reader):
38  if reader.has_option(name, "method"):
39  self.method = reader.get(name, "method")
40  if reader.has_option(name, "msg_type"):
41  self.msg_type = reader.get(name, "msg_type")
42  if reader.has_option(name, "name"):
43  self.name = reader.get(name, "name")
44  if reader.has_option(name, "dependencies"):
45  self.dependencies = reader.get(name, "dependencies")
46  self.module = importlib.import_module(self.dependencies)
47  if reader.has_option(name, "args"):
48  args_aux = reader.get(name, "args")
49  self.args = [y.split('.') for y in [x.strip() for x in args_aux.split(',')]]
50  if reader.has_option(name, "params_name"):
51  aux_name = reader.get(name, "params_name")
52  if reader.has_option(name, "params_type"):
53  aux_type = reader.get(name, "params_type")
54  self.params_dict = OrderedDict((x.strip(),y.strip()) for x,y in itertools.izip(aux_name.split(','),aux_type.split(',')))
55  else:
56  self.params_dict = OrderedDict((x.strip(),"str") for x in aux_name.split(','))
57  if reader.has_option(name, "buf"):
58  self.buf = reader.get(name, "buf")
59  if reader.has_option(name, "latch"):
60  self.latch = reader.get(name, "latch")
61 
62  def convert_params(self, params):
63 
64  if self.method == "service":
65  msg_type = self.msg_type + "Request"
66  elif self.method == "topic":
67  msg_type = self.msg_type
68  else:
69  return None
70 
71  param_class = getattr(self.module, msg_type)
72  param_instance = param_class()
73  converted = param_instance
74  for p,k in zip(params, self.params_dict):
75  param_attr = k.split('.')
76  obj_list = [param_instance]
77  for attr in param_attr:
78  if hasattr(obj_list[-1], attr):
79  if attr is not param_attr[-1]:
80  obj_list.append(getattr(obj_list[-1], attr))
81  else:
82  value = getattr(__builtin__, self.params_dict[k])(p)
83  converted = setattr_recursive(obj_list, param_attr, value)
84 
85  return converted
86 
87 
89  def __init__(self):
90  self.comm_dict = dict()
91  self.default_path = ""
92  self.comm_len = 0
93 
94  def read_manifest(self, *args):
95  reader = ConfigParser.RawConfigParser()
96 
97  if len(args) > 0 and Path(args[0]).is_file():
98  man_path = Path(args[0])
99  else:
100  man_path = Path(self.default_path)
101 
102  if man_path.is_file():
103  reader.read(str(man_path))
104  self.get_info(reader)
105 
106 
107  def get_info(self, reader):
108  comm_list = reader.sections()
109  self.comm_len = len(comm_list)
110  for comm in comm_list:
111  comm_info = CommInfo()
112  comm_info.fill_data(comm, reader)
113  self.comm_dict[comm] = comm_info
114 
115 
117  def __init__(self):
118  CommController.__init__(self)
119  self.default_path = "actions_manifest"
120 
121  def executable_action(self, action_name):
122  return (action_name in self.comm_dict.keys())
123 
124  def perform_action(self, action_name, params):
125  action_completed = False
126  try:
127  action = self.comm_dict[action_name]
128  converted_params = action.convert_params(params)
129  msg_type = getattr(action.module, action.msg_type)
130  node_namespace = rospy.get_namespace()
131  if action.method == "service":
132  rospy.wait_for_service(node_namespace + (action.name[1:] if action.name.startswith('/') else action.name))
133  try:
134  service_aux = rospy.ServiceProxy(node_namespace + (action.name[1:] if action.name.startswith('/') else action.name), msg_type)
135  service_aux(converted_params)
136  except rospy.ServiceException as e:
137  print("service "+ action.name +" call failed: %s." % e)
138  action_completed = True
139  elif action.method == "topic":
140  latch = str2bool(action.latch)
141  topic_pub = rospy.Publisher(
142  node_namespace + (action.name[1:] if action.name.startswith('/') else action.name),
143  msg_type,
144  queue_size=1,
145  latch=latch)
146 
147  if hasattr(converted_params, "header"):
148  header = std_msgs.msg.Header()
149  header.stamp = rospy.Time.now()
150  setattr(converted_params, "header", header)
151 
152  topic_pub.publish(converted_params)
153  action_completed = True
154  else:
155  print("method " + action.method + " not available.")
156  action_completed = False
157  except KeyError:
158  action_completed = False
159 
160  return action_completed
161 
162 
164  def __init__(self):
165  CommController.__init__(self)
166  self.default_path = "perceptions_manifest"
167  self.subsciber_dict = dict()
168  self.perceptions = dict()
169  self.rate = None
170  self.p_event = Event()
171  self.p_lock = RLock()
172 
173  def get_info(self, reader):
174  default_section = reader.defaults()
175  try:
176  self.rate = float(default_section["rate"])
177  except KeyError:
178  self.rate = 30
179  CommController.get_info(self, reader)
180 
181  def start_perceiving(self):
182  for comm in self.comm_dict.items():
183  name = comm[0]
184  perception = comm[1]
185  node_namespace = rospy.get_namespace()
186  self.subsciber_dict[name] = rospy.Subscriber(
187  node_namespace + (perception.name[1:] if perception.name.startswith('/') else perception.name),
188  getattr(perception.module, perception.msg_type),
189  self.subscriber_callback,
190  name
191  )
192 
193  def subscriber_callback(self, msg, name):
194  perception_param = []
195  for data in self.comm_dict[name].args:
196  obj = msg
197  for d in data:
198  if hasattr(obj, d):
199  obj = getattr(obj, d)
200  if d is data[-1]:
201  perception_param.append(obj)
202  perception_param = map(str, perception_param)
203 
204  perception = jason_msgs.msg.Perception()
205  perception.perception_name = name
206  perception.parameters = perception_param
207  if(self.comm_dict[name].buf == "add"):
208  perception.update = False
209  else:
210  perception.update = True
211 
212  self.p_lock.acquire()
213  self.perceptions[name] = perception
214  self.p_event.set()
215  self.p_lock.release()
def setattr_recursive(obj_list, attr_list, value)
def get_info(self, reader)
def read_manifest(self, args)
def convert_params(self, params)
def perform_action(self, action_name, params)
def executable_action(self, action_name)
def subscriber_callback(self, msg, name)
def fill_data(self, name, reader)


jason_hw_bridge
Author(s): Gustavo R. Silva , Leandro B. Becker , Jomi F. Hubner
autogenerated on Tue Apr 14 2020 03:41:10