ir_ranger.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 """
00004   ir_ranger.py - convert analog stream into range measurements
00005   Copyright (c) 2011 Vanadium Labs LLC.  All right reserved.
00006 
00007   Redistribution and use in source and binary forms, with or without
00008   modification, are permitted provided that the following conditions are met:
00009       * Redistributions of source code must retain the above copyright
00010         notice, this list of conditions and the following disclaimer.
00011       * Redistributions in binary form must reproduce the above copyright
00012         notice, this list of conditions and the following disclaimer in the
00013         documentation and/or other materials provided with the distribution.
00014       * Neither the name of Vanadium Labs LLC nor the names of its 
00015         contributors may be used to endorse or promote products derived 
00016         from this software without specific prior written permission.
00017   
00018   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021   DISCLAIMED. IN NO EVENT SHALL VANADIUM LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024   OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00026   OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00027   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 """
00029 
00030 import rospy
00031 
00032 from sensor_msgs.msg import Range
00033 from arbotix_msgs.msg import Analog
00034 from arbotix_msgs.srv import SetupChannel, SetupChannelRequest
00035 
00036 from arbotix_python.sensors import *
00037 
00038 class ir_ranger:
00039     def __init__(self):
00040         rospy.init_node("ir_ranger")
00041         
00042         # sensor type: choices are A710YK (40-216"), A02YK (8-60"), A21YK (4-30")
00043         self.sensor_t = rospy.get_param("~type","GP2D12")
00044         if self.sensor_t == "A710YK" or self.sensor_t == "ultralong":
00045             self.sensor = gpA710YK()
00046         elif self.sensor_t == "A02YK" or self.sensor_t == "long":
00047             self.sensor = gpA02YK()
00048         else:
00049             self.sensor = gp2d12() 
00050 
00051         # start channel broadcast using SetupAnalogIn
00052         rospy.wait_for_service('arbotix/SetupAnalogIn')
00053         analog_srv = rospy.ServiceProxy('arbotix/SetupAnalogIn', SetupChannel) 
00054         
00055         req = SetupChannelRequest()
00056         req.topic_name = rospy.get_param("~name")
00057         req.pin = rospy.get_param("~pin")
00058         req.rate = int(rospy.get_param("~rate",10))
00059         analog_srv(req)
00060 
00061         # setup a range message to use
00062         self.msg = Range()
00063         self.msg.radiation_type = self.sensor.radiation_type
00064         self.msg.field_of_view = self.sensor.field_of_view
00065         self.msg.min_range = self.sensor.min_range
00066         self.msg.max_range = self.sensor.max_range
00067 
00068         # publish/subscribe
00069         self.pub = rospy.Publisher("ir_range", Range, queue_size=5)
00070         rospy.Subscriber("arbotix/"+req.topic_name, Analog, self.readingCb)
00071 
00072         rospy.spin()
00073         
00074     def readingCb(self, msg):
00075         # convert msg.value into range.range
00076         self.msg.header.stamp = rospy.Time.now()
00077         self.msg.range = self.sensor.convert(msg.value<<2)
00078         self.pub.publish(self.msg)
00079 
00080 if __name__=="__main__":
00081     ir_ranger()
00082 


arbotix_sensors
Author(s): Michael Ferguson
autogenerated on Sat Jun 8 2019 19:34:59