Go to the documentation of this file.00001
00002
00003
00004 from __future__ import print_function
00005 import rospy, math
00006 from sensor_msgs.msg import LaserScan
00007
00008
00009 def range_to_led(range_value):
00010 try:
00011 distance = int(range_value[0] * 1000)
00012 if distance < 4: distance = 8 - distance
00013
00014
00015 led_value = int(761000 / math.pow(distance, 1.66))
00016 if led_value > 4000: led_value = 4000
00017 if led_value < 15: led_value = 15
00018 except:
00019 led_value = 15
00020 return led_value
00021
00022
00023 def write_to_file(data):
00024 try:
00025 with open("/dev/rtlightsensor0", "w") as f:
00026 print("%d %d %d %d" % tuple(data), file=f)
00027 except:
00028 rospy.logerr("failed to open rtlightsensor0")
00029
00030
00031 def sensor1_callback(data):
00032 led_val[0] = range_to_led(data.ranges)
00033
00034
00035 def sensor2_callback(data):
00036 led_val[1] = range_to_led(data.ranges)
00037
00038
00039 def sensor3_callback(data):
00040 led_val[2] = range_to_led(data.ranges)
00041
00042
00043 def sensor4_callback(data):
00044 led_val[3] = range_to_led(data.ranges)
00045 write_to_file(led_val)
00046
00047
00048 def listener():
00049 rospy.Subscriber(rospy.get_namespace() + "rf_scan", LaserScan, sensor1_callback)
00050 rospy.Subscriber(rospy.get_namespace() + "rs_scan", LaserScan, sensor2_callback)
00051 rospy.Subscriber(rospy.get_namespace() + "ls_scan", LaserScan, sensor3_callback)
00052 rospy.Subscriber(rospy.get_namespace() + "lf_scan", LaserScan, sensor4_callback)
00053
00054
00055 if __name__ == "__main__":
00056 led_val = [15, 15, 15, 15]
00057 rospy.init_node("sensor_data_converter", anonymous=True)
00058 listener()
00059 rospy.spin()