ros_parameters.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # License: BSD
4 # https://raw.github.com/robotics-in-concert/rocon_multimaster/license/LICENSE
5 #
6 
7 import os.path
8 import re
9 
10 import rosparam
11 import rospy
12 from gateway_msgs.msg import Rule, RemoteRule
13 
14 from . import utils
15 
16 ###############################################################################
17 # Functions
18 ###############################################################################
19 
20 
22  '''
23  Returns the gateway parameters from the ros param server.
24  Most of these should be fairly self explanatory.
25  '''
26  param = {}
27 
28  # Check if there is a user provided custom configuration
29  try:
30  custom_configuration_file = rospy.get_param('~custom_rules_file')
31  if custom_configuration_file:
32  if os.path.isfile(custom_configuration_file):
33  loaded_parameters = rosparam.load_file(custom_configuration_file, default_namespace=rospy.resolve_name(rospy.get_name())) # this sets the private namespace
34  for params, ns in loaded_parameters:
35  # need to merge whatever default rules are already on the param server with these settings
36  existing_parameters = rosparam.get_param(ns)
37  for p, v in params.iteritems():
38  if p in ['default_flips', 'default_advertisements', 'default_pulls'] and p in existing_parameters:
39  existing_parameters[p] += v
40  else:
41  existing_parameters[p] = v
42  rosparam.upload_params(ns, existing_parameters, verbose=True)
43  else:
44  rospy.logwarn("Gateway : user provided configuration file could not be found [%s]" % custom_configuration_file)
45  except KeyError:
46  # Not an error, just no need to load a custom configuration
47  pass
48 
49  # Hub
50  param['hub_uri'] = rospy.get_param('~hub_uri', '')
51  # Convert these back to accepting lists once https://github.com/ros/ros_comm/pull/218
52  # goes through, for now we use semi-colon separated lists.
53  param['hub_whitelist'] = rospy.get_param('~hub_whitelist', [])
54  param['hub_blacklist'] = rospy.get_param('~hub_blacklist', [])
55 
56  # Gateway
57  param['name'] = rospy.get_param('~name', 'gateway')
58  param['watch_loop_period'] = rospy.get_param('~watch_loop_period', 10) # in seconds
59 
60  # Blacklist used for advertise all, flip all and pull all commands
61  param['default_blacklist'] = rospy.get_param('~default_blacklist', []) # list of Rule objects
62 
63  # Used to block/permit remote gateway's from flipping to this gateway.
64  param['firewall'] = rospy.get_param('~firewall', True)
65 
66  # The gateway can automagically detect zeroconf, but sometimes you want to force it off
67  param['disable_zeroconf'] = rospy.get_param('~disable_zeroconf', False)
68 
69  # The gateway uses uui'd to guarantee uniqueness, but this can be disabled
70  # if you want clean names without uuid's (but you have to manually
71  # guarantee uniqueness)
72  param['disable_uuids'] = rospy.get_param('~disable_uuids', False)
73 
74  # Make everything publicly available (excepting the default blacklist)
75  param['advertise_all'] = rospy.get_param('~advertise_all', []) # boolean
76 
77  # Topics and services for pre-initialisation/configuration
78  param['default_advertisements'] = rospy.get_param('~default_advertisements', []) # list of Rule objects
79  param['default_flips'] = rospy.get_param('~default_flips', []) # list of RemoteRule objects
80  param['default_pulls'] = rospy.get_param('~default_pulls', []) # list of RemoteRule objects
81 
82  # Network interface name (to be used when there are multiple active interfaces))
83  param['network_interface'] = rospy.get_param('~network_interface', '') # string
84 
85  return param
86 
87 
88 def generate_rules(param):
89  '''
90  Converts a param of the suitable type (see default_blacklist.yaml)
91  into a dictionary of Rule types.
92 
93  @return all rules as gateway_msgs.msg.Rule objects in our usual keyed dictionary format
94  @rtype type keyed dictionary of Rule lists
95  '''
96  rules = utils.create_empty_connection_type_dictionary()
97  for value in param:
98  rule = Rule()
99  rule.name = value['name']
100  # maybe also check for '' here?
101  pattern = re.compile("None", re.IGNORECASE)
102  if pattern.match(value['node']):
103  rule.node = '' # ROS Message fields should not be None, maybe not a problem here though, see below
104  else:
105  rule.node = value['node']
106  rule.type = value['type']
107  rules[rule.type].append(rule)
108  return rules
109 
110 
112  '''
113  Converts a param of the suitable type (default_flips, default_pulls) into
114  a list of RemoteRule objects and a list of target gateways for flip_all/pull_all.
115 
116  @param yaml object
117  @type complicated
118 
119  @return list of remote rules
120  @return RemoteRule[]
121  '''
122  remote_rules = []
123  all_targets = []
124  pattern = re.compile("None", re.IGNORECASE)
125  for remote_rule in param:
126  if 'rule' in remote_rule:
127  # maybe also check for '' here?
128  node = None if pattern.match(remote_rule['rule']['node']) else remote_rule['rule']['node']
129  remote_rules.append(RemoteRule(remote_rule['gateway'],
130  Rule(remote_rule['rule']['type'],
131  remote_rule['rule']['name'],
132  node
133  )
134  )
135  )
136  else:
137  all_targets.append(remote_rule['gateway'])
138  return remote_rules, all_targets
def setup_ros_parameters()
Functions.


rocon_gateway
Author(s): Daniel Stonier , Jihoon Lee , Piyush Khandelwal
autogenerated on Mon Jun 10 2019 14:40:10