commands.py
Go to the documentation of this file.
1 from abc import ABCMeta, abstractmethod
2 import importlib
3 import rospy
4 import roslib.message
5 from rospy_message_converter import message_converter
6 
7 def convert_dictionary_to_ros_request(service_type, dictionary):
8  service_class = roslib.message.get_service_class(service_type)
9  request = service_class._request_class()
10  request_fields = dict(message_converter._get_message_fields(request))
11 
12  for field_name, field_value in dictionary.items():
13  if field_name in request_fields:
14  field_type = request_fields[field_name]
15  field_value = message_converter._convert_to_ros_type(field_type, field_value)
16  setattr(request, field_name, field_value)
17  else:
18  error_message = 'ROS message type "{0}" has no field named "{1}"'\
19  .format(message_type, field_name)
20  raise ValueError(error_message)
21  return request
22 
23 class CommandBase(object):
24  """ base class for specifying the command at the time of menu click. """
25  __metaclass__ = ABCMeta
26  def __init__(self):
27  pass
28 
29  @abstractmethod
30  def __call__(self, args, feedback=None):
31  return None
32 
34  """ Publish topic """
35  def __init__(self):
36  self._pubs = {}
37  self._msgs = {}
38  def __call__(self, args, feedback=None):
39  rospy.loginfo('topic pub: ' + str(args))
40  name = args['name']
41  mtype = args['type']
42  msg = args['data']
43  if not name in self._pubs:
44  self._pubs[name] = rospy.Publisher(name,
45  roslib.message.get_message_class(mtype),
46  queue_size=1,
47  latch=True)
48  if not name in self._msgs:
49  self._msgs[name] = message_converter.convert_dictionary_to_ros_message(mtype, msg)
50  self._pubs[name].publish(self._msgs[name])
51 
53  """ Call service """
54  def __init__(self):
55  self._srvs = {}
56  self._reqs = {}
57  def __call__(self, args, feedback=None):
58  rospy.loginfo('service call: ' + str(args))
59  name = args['name']
60  mtype = args['type']
61  req = args['data']
62  if not name in self._srvs:
63  self._srvs[name] = rospy.ServiceProxy(name,
64  roslib.message.get_service_class(mtype))
65  if not name in self._reqs:
66  self._reqs[name] = convert_dictionary_to_ros_request(mtype, req)
67  self._srvs[name](self._reqs[name])
68 
70  """ Call python function """
71  def __init__(self):
72  self._mods = {}
73  def __call__(self, args, feedback=None):
74  rospy.loginfo('python function: ' + str(args))
75  mod = args['module']
76  func = args['func']
77  fa = args['args']
78  if not mod in self._mods:
79  self._mods[mod] = importlib.import_module(mod)
80  getattr(self._mods[mod], func)(**fa)
def __call__(self, args, feedback=None)
Definition: commands.py:57
def __call__(self, args, feedback=None)
Definition: commands.py:38
def __call__(self, args, feedback=None)
Definition: commands.py:30
def convert_dictionary_to_ros_request(service_type, dictionary)
Definition: commands.py:7
def __call__(self, args, feedback=None)
Definition: commands.py:73


ez_interactive_marker
Author(s):
autogenerated on Mon Jun 10 2019 13:15:06