compliance.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import rospy
3 import numpy as np
4 import yaml
5 from .. import utils
6 
7 
9  def __init__(self):
10  # open sensor_compliance/bounding_boxes.yaml and all the boxes defined
11  self.boxes = find_boxes('sensor_compliance/bounding_boxes.yaml')
12  # look at all sensors in sensors directory and get the default params
13  self.sensors_dir = rospy.get_param('sensors_dir') + '/'
14  self.default_parameters = utils.get_macros(self.sensors_dir)
15  self.dir = rospy.get_param('compliance_dir') + '/'
16  self.numeric = yaml.safe_load(open(self.dir +
17  'sensor_compliance/numeric.yaml'))
18  return
19 
20  def param_compliance(self, sensor_type, params={}):
21  # ie: given an instance of sensor_type = 'wamv_camera'
22  # with parameters = params, is this camera in compliance
23  # check if the sensor is allowed
24  params = params.copy()
25  if sensor_type not in self.default_parameters:
26  rospy.logerr('%s is not defined anywhere under %s' %
27  (sensor_type, self.dir))
28  assert sensor_type in self.default_parameters,\
29  '%s is not defined anywhere under %s' % (sensor_type, self.dir)
30  for i in params:
31  if i not in self.numeric[sensor_type]['allowed_params']:
32  rospy.logerr('%s parameter specification of %s not permitted' %
33  (i, sensor_type))
34 
35  # add the default params to params if not specified
36  for i in self.default_parameters[sensor_type]:
37  if i not in params:
38  params[i] = self.default_parameters[sensor_type][i]
39  if 'x' and 'y' and 'z' in params:
40  xyz = np.array([float(params['x']),
41  float(params['y']),
42  float(params['z'])])
43  for box in self.boxes:
44  if box.fit(xyz):
45  return True
46  rospy.logerr('%s %s is out of bounds' %
47  (sensor_type, params['name']))
48  rospy.logerr('%s %s is at xyz=(%s, %s, %s), %s' %
49  (sensor_type, params['name'],
50  xyz[0], xyz[1], xyz[2],
51  'must fit in at least one of the following boxes ' +
52  'with remaining space:'))
53  for box in self.boxes:
54  rospy.logerr(' %s' % str(box))
55  return False
56  else:
57  return True
58 
59  def number_compliance(self, sensor_type, n):
60  # ie: are n wamv_cameras allowed?
61  if n > self.numeric[sensor_type]['num']:
62  rospy.logerr('Too many %s requested' % sensor_type)
63  rospy.logerr(' maximum of %s %s allowed' %
64  (self.numeric[sensor_type]['num'], sensor_type))
65  return False
66  return True
67 
68 
70  def __init__(self):
71  # open thruster_compliance/bounding_boxes.yaml and the boxes defined
72  self.boxes = find_boxes('thruster_compliance/bounding_boxes.yaml')
73  # look at all sensors in sensors directory and get the default params
74  self.thrusters_dir = rospy.get_param('thrusters_dir') + '/'
75  self.default_parameters = utils.get_macros(self.thrusters_dir)
76  self.dir = rospy.get_param('compliance_dir') + '/'
77  self.numeric = yaml.safe_load(open(self.dir +
78  'thruster_compliance/numeric.yaml'))
79  return
80 
81  def param_compliance(self, thruster_type, params={}):
82  # ie: given an instance of thruster_type = 'engine'
83  # with parameters = params, is this engine in compliance
84  # check if the thruster is allowed
85 
86  params = params.copy()
87  if thruster_type not in self.default_parameters:
88  rospy.logerr('%s is not defined anywhere under %s' %
89  (thruster_type, self.dir))
90  assert thruster_type in self.default_parameters,\
91  '%s is not defined anywhere under %s' % \
92  (thruster_type, self.dir)
93  for i in params:
94  if i not in self.numeric[thruster_type]['allowed_params']:
95  rospy.logerr('%s parameter specification of not permitted' %
96  (i, thruster_type))
97  assert False
98 
99  # add the default params to params if not specified
100  for i in self.default_parameters[thruster_type]:
101  if i not in params:
102  params[i] = self.default_parameters[thruster_type][i]
103  # right now the ONLY compliance check we have is to make sure that
104  # the sensors are in at least one of the boxes
105  xyz = np.array([float(j) for j in [i for i in
106  params['position'].split(' ')
107  if i != '']])
108  for box in self.boxes:
109  if box.fit(xyz):
110  return True
111  rospy.logerr('%s %s is out of bounds' %
112  (thruster_type, params['prefix']))
113  rospy.logerr('%s %s is at xyz=(%s, %s, %s), %s' %
114  (thruster_type, params['prefix'],
115  xyz[0], xyz[1], xyz[2],
116  'it must fit in at least one of the following boxes ' +
117  'with remaining space:'))
118  for box in self.boxes:
119  rospy.logerr(' %s' % str(box))
120  return False
121 
122  def number_compliance(self, thruster_type, n):
123  if n > self.numeric[thruster_type]['num']:
124  rospy.logerr('Too many %s requested' % thruster_type)
125  rospy.logerr(' maximum of %s %s allowed' %
126  (self.numeric[thruster_type]['num'], thruster_type))
127  return False
128  return True
129 
130 
131 class Box:
132  def __init__(self, name, pose, size, space):
133  self.name = name
134 
135  self.pose = np.array([float(j) for j in [i for i in pose.split(' ')
136  if i != '']])
137  self.size = np.array([float(j) for j in [i for i in size.split(' ')
138  if i != '']])
139  self.space = int(space)
140  return
141 
142  def fit(self, pose):
143  pose = pose-self.pose[:3]
144  for idx, i in enumerate(pose):
145  if abs(i) > self.size[idx]/2:
146  return False
147  # if space is -1, unlimited things
148  if self.space == -1:
149  return True
150  elif self.space == 0:
151  return False
152  elif self.space > 0:
153  self.space -= 1
154  return True
155 
156  def __str__(self):
157  return '<Box name:%s x:[%s, %s] y:[%s,%s] z:[%s,%s]\
158  remaining_space:%s>' % \
159  (self.name,
160  (self.pose[0] + self.size[0]/2),
161  (self.pose[0] - self.size[0]/2),
162  (self.pose[1] + self.size[1]/2),
163  (self.pose[1] - self.size[1]/2),
164  (self.pose[2] + self.size[2]/2),
165  (self.pose[2] - self.size[2]/2),
166  self.space)
167 
168 
169 def find_boxes(box_yaml):
170  addrs = rospy.get_param('compliance_dir') + '/' + box_yaml
171  box_def = yaml.safe_load(open(addrs))
172  boxes = []
173 
174  for name, properties in box_def.iteritems():
175  boxes.append(Box(str(name),
176  properties['pose'],
177  properties['size'],
178  properties['capacity']))
179  return boxes


vrx_gazebo
Author(s): Brian Bingham , Carlos Aguero
autogenerated on Thu May 7 2020 03:54:56