simulated_range_sensors.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License");
00006 # you may not use this file except in compliance with the License.
00007 # You may obtain a copy of the License at
00008 #
00009 #   http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS,
00013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 # See the License for the specific language governing permissions and
00015 # limitations under the License.
00016 
00017 
00018 import rospy
00019 from sensor_msgs.msg import *
00020 
00021 class GazeboVirtualRangeSensor():
00022 
00023         def __init__(self, laser_topic, range_topic):
00024                 rospy.Subscriber(laser_topic, LaserScan, self.laser_callback)
00025 #               rospy.logdebug("subscribed to laser topic: %s", laser_topic)
00026                 self.pub = rospy.Publisher(range_topic, Range, queue_size=1)
00027 #               rospy.loginfo("topic '" + range_topic + "' advertised")
00028                 rospy.loginfo("adding sensor converting from topic '" + laser_topic + "' to topic '" + range_topic + "'")
00029 
00030         def laser_callback(self, msg):
00031                 sensor_range = Range()
00032 
00033                 # set header
00034                 sensor_range.header = msg.header
00035 
00036                 # set range from average out of laser message
00037                 if len(msg.ranges) > 0:
00038                         sensor_range.range = sum(msg.ranges)/len(msg.ranges)
00039                 else:
00040                         sensor_range.range = 0
00041 
00042                 # set min and max range
00043                 sensor_range.min_range = msg.range_min
00044                 sensor_range.max_range = msg.range_max
00045 
00046                 # set field of view
00047                 sensor_range.field_of_view = msg.angle_max - msg.angle_min
00048 
00049                 # set radiation type to IR (=1)
00050                 sensor_range.radiation_type = 1
00051 
00052                 # publish range
00053                 self.pub.publish(sensor_range)
00054 
00055 if __name__ == "__main__":
00056         rospy.init_node('tactile_sensors')
00057         rospy.sleep(0.5)
00058 
00059         if not rospy.has_param('~sensors'):
00060                 rospy.logerr("no sensors specified")
00061                 exit(1)
00062 
00063         sensors = rospy.get_param('~sensors')
00064 
00065         if type(sensors) != list:
00066                 rospy.logerr("sensors parameter not a list")
00067                 exit(1)
00068 
00069         for sensor in sensors:
00070                 GazeboVirtualRangeSensor(sensor['laser_topic'], sensor['range_topic'])
00071 
00072         rospy.spin()


cob_phidgets
Author(s): Florian Weisshardt
autogenerated on Sat Jun 8 2019 21:02:14