sonarmite.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 import roslib; roslib.load_manifest('kingfisher_node')
00004 import rospy
00005 
00006 from sensor_msgs.msg import Range
00007 from std_msgs.msg import Float32
00008 import serial, select
00009 
00010 ser = None
00011 
00012 def _shutdown():
00013     global ser
00014     rospy.loginfo("Sonar shutting down.")
00015     rospy.loginfo("Closing Sonar serial port.")
00016     ser.close()
00017 
00018 def serial_lines(ser, brk="\n"):
00019     buf = ""
00020     while True:
00021         rlist, _, _ = select.select([ ser ], [], [], 1.0)
00022         if not rlist:
00023             continue
00024         new = ser.read(ser.inWaiting())
00025         buf += new
00026         if brk in new:
00027             msg, buf = buf.split(brk)[-2:]
00028             yield msg
00029 
00030 if __name__ == '__main__':
00031     #global ser
00032     rospy.init_node('sonarmite')
00033     range_pub = rospy.Publisher('depth', Range)
00034     quality_pub = rospy.Publisher('quality', Float32)
00035     range_msg = Range(radiation_type=Range.ULTRASOUND, 
00036                       field_of_view=0.26,
00037                       min_range=0.2, 
00038                       max_range=100.0)
00039     range_msg.header.frame_id = "sonar"
00040 
00041     port = rospy.get_param('~port', '/dev/ttyUSB0')
00042     baud = rospy.get_param('~baud', 9600)
00043     quality_threshold = rospy.get_param('~quality_threshold', 0.54)
00044     
00045     rospy.on_shutdown(_shutdown)
00046 
00047     try:
00048         ser = serial.Serial(port=port, baudrate=baud, timeout=.5)
00049         lines = serial_lines(ser)
00050 
00051         while not rospy.is_shutdown(): 
00052             data = lines.next()
00053             try:
00054                 fields = data.split()
00055 
00056                 quality = float(fields[6]) / 128.0
00057                 quality_pub.publish(quality)
00058 
00059                 if quality >= quality_threshold:
00060                     range_msg.range = float(fields[1])
00061                     range_msg.header.stamp = rospy.Time.now()
00062                     range_pub.publish(range_msg)
00063             except ValueError as e:
00064                 rospy.logerr(str(e))
00065                 continue
00066 
00067         rospy.loginfo('Closing Digital Compass Serial port')
00068         ser.close()
00069 
00070     except rospy.ROSInterruptException:
00071         pass


kingfisher_node
Author(s): Mike Purvis
autogenerated on Sat Dec 28 2013 17:08:10