range in rviz?
Hi everyone, I'm new to ROS and have a related question about it. how can I read the distance of an ultrasound sensor using rviz range? I don't know what to do. I have already registered the sensor. The arduino sends the data but when I run the rviz , add the range and in the topic select the register, I can't see anything.
This is my code.
Regards
import rospy
import serial
from std_msgs.msg import String
from sensor_msgs.msg import Range
import roslib; roslib.load_manifest('rviz')
from geometry_msgs.msg import Twist
ser = serial.Serial('/dev/ttyUSB0', 9600)
# Init Node
#------------------------------------------------------------------
rospy.init_node('robot')
# Node Register
#------------------------------------------------------------------
robot_pub = rospy.Publisher('uart', String)
ir1_pub = rospy.Publisher('ir1', Range)
ir2_pub = rospy.Publisher('ir2', Range)
ir3_pub = rospy.Publisher('ir3', Range)
array_ir_pub = rospy.Publisher('array_ir', Range)
twist_pub = rospy.Publisher('/cmd_vel', Twist)
ranges = [float('NaN'), 1.0, -float('Inf'), 3.0, float('Inf')]
move = Twist()
rate = rospy.Rate(1)
min_range = 2.0
max_range = 2.0
# Infrared Sensors
#------------------------------------------------------------------
def range_infrared_sensor_1(ir_value):
ir1_range = Range()
ir1_range.header.stamp = rospy.Time.now()
ir1_range.header.frame_id = "/base_link"
ir1_range.radiation_type = 0
ir1_range.field_of_view = 1.0
ir1_range.min_range = min_range
ir1_range.max_range = max_range
ir1_range.range = ir_value
ir1_pub.publish(ir1_range)
#print "ir2: {0}".format(ir1_range.range)
def range_infrared_sensor_2(ir_value):
ir2_range = Range()
ir2_range.header.stamp = rospy.Time.now()
ir2_range.header.frame_id = "/base_link"
ir2_range.radiation_type = 0
ir2_range.field_of_view = 1.0
ir2_range.min_range = min_range
ir2_range.max_range = max_range
ir2_range.range = ir_value
ir2_pub.publish(ir2_range)
#print "ir2: {0}".format(ir1_range.range)
def range_infrared_sensor_3(ir_value):
ir3_range = Range()
ir3_range.header.stamp = rospy.Time.now()
ir3_range.header.frame_id = "/base_link"
ir3_range.radiation_type = 0
ir3_range.field_of_view = 1.0
ir3_range.min_range = min_range
ir3_range.max_range = max_range
ir3_range.range = ir_value
ir3_pub.publish(ir3_range)
#print "ir3: {0}".format(ir1_range.range)
def range_array_infrared_sensor(ir_left, ir_center, ir_right):
ir_array = [ir_left, ir_center, ir_right]
array_ir_range = Range()
array_ir_range.header.stamp = rospy.Time.now()
array_ir_range.header.frame_id = "/base_link"
array_ir_range.radiation_type = 0
array_ir_range.field_of_view = 1.0
array_ir_range.min_range = min_range
array_ir_range.max_range = max_range
array_ir_range.range = ir_array
array_ir_pub.publish(array_ir_range)
#------------------------------------------------------------------
def move_robot():
move.linear.x = 1
move.linear.z = 1
robot_pub.publish(move)
rate.sleep()
def read_and_write_data_to_robot():
data= ser.readline()
rospy.loginfo(data)
data_sent_by_arduino = String(data)
#data_split = data_sent_by_arduino.split(' ')
splited_data = data.split(' ')
robot_pub.publish(data_sent_by_arduino)
rospy.sleep(0.1)
#ser.write('c 128 128 0')
range_infrared_sensor_2(float(splited_data[0]))
#range_array_infrared_sensor(float(splited_data[1]), float(splited_data[0]), float(splited_data[2]))
def robot_talker():
while not rospy.is_shutdown():
read_and_write_data_to_robot()
if __name__ == '__main__':
try:
robot_talker()
except rospy.ROSInterruptException:
pass