filter.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 import roslib; roslib.load_manifest('map_laser')
4 import rospy
5 import tf
6 from sensor_msgs.msg import LaserScan
7 from nav_msgs.msg import OccupancyGrid
8 from tf.transformations import euler_from_quaternion
9 from std_msgs.msg import ColorRGBA
10 import math
11 
12 
13 class MapLaser(object):
14  def __init__(self):
15  rospy.init_node('map_laser_filter')
16  self.pub = rospy.Publisher('/base_scan_filter',
17  LaserScan,
18  queue_size=10)
20  self.map = None
21  self.save = None
22  self.sub = rospy.Subscriber('/scan_filtered',
23  LaserScan,
24  self.laser_cb,
25  queue_size=1)
26  self.sub2 = rospy.Subscriber('/map', OccupancyGrid, self.map_cb)
27 
28 
29  def map_cb(self, msg):
30  self.map = msg
31 
32  def get_laser_frame(self, msg):
33  now = msg.header.stamp
34  f2 = msg.header.frame_id
35  f1 = '/map'
36  self.listener.waitForTransform(f1, f2, now, rospy.Duration(.15))
37 
38  return self.listener.lookupTransform(f1, f2, now)
39 
40  def is_occupied(self, x, y):
41  N = 2
42  for dx in range(-N, N + 1):
43  for dy in range(-N, N + 1):
44  index = (x + dx) + (y + dy) * self.map.info.width
45  if index < 0 or index > len(self.map.data):
46  continue
47  value = self.map.data[index]
48  if value > 50 or value < 0:
49  return True
50  return False
51 
52 
53  def laser_cb(self, msg):
54  if self.map is None:
55  return
56  try:
57  (trans, rot) = self.get_laser_frame(msg)
58  self.save = (trans, rot)
59  except Exception: # TODO should list handled exceptions
60  if self.save is None:
61  return
62  (trans, rot) = self.save
63 
64  yaw = euler_from_quaternion(rot)[2]
65 
66  nr = []
67 
68  for (i, d) in enumerate(msg.ranges):
69  if math.isnan(d) or d > msg.range_max or d < msg.range_min:
70  nr.append(msg.range_max + 1.0)
71  continue
72  angle = yaw + msg.angle_min + msg.angle_increment*i
73  dx = math.cos(angle) * d
74  dy = math.sin(angle) * d
75  map_x = trans[0] + dx
76  map_y = trans[1] + dy
77 
78  grid_x = int((map_x - self.map.info.origin.position.x) / self.map.info.resolution)
79  grid_y = int((map_y - self.map.info.origin.position.y) / self.map.info.resolution)
80 
81  if self.is_occupied(grid_x, grid_y):
82  nr.append(msg.range_max + 1.0)
83  else:
84  nr.append(d)
85 
86  msg.ranges = nr
87  self.pub.publish(msg)
88 
89 if __name__ == '__main__':
90  x = MapLaser()
91  rospy.spin()
def get_laser_frame(self, msg)
Definition: filter.py:32
def is_occupied(self, x, y)
Definition: filter.py:40
def map_cb(self, msg)
Definition: filter.py:29
def laser_cb(self, msg)
Definition: filter.py:53
def __init__(self)
Definition: filter.py:14


map_laser
Author(s): David V. Lu!!
autogenerated on Mon Jun 10 2019 13:47:19