ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

range in rviz?

asked 2018-06-24 17:31:01 -0600

jarain78 gravatar image

updated 2018-06-25 00:21:29 -0600

jayess gravatar image

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
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-06-25 01:02:28 -0600

NEngelhard gravatar image

updated 2018-06-25 01:06:43 -0600

You're on the right path, but this looks suspicious:

min_range = 2.0 
max_range = 2.0

And regarding code quality (no duplicated code in range_infrared_sensor_?)

def publish_range(ir_value, publisher): 
    range_msg = Range()
    range_msg.header.stamp = rospy.Time.now()
    range_msg.header.frame_id = "/base_link"
    range_msg.radiation_type = 0
    range_msg.field_of_view = 1.0
    range_msg.min_range = min_range
    range_msg.max_range = max_range
    range_msg.range = ir_value
    publisher.publish(range_msg)

and the call

publish_range(float(splited_data[0]), ir1_pub)

(or even put the publishers also in a list)

edit flag offensive delete link more

Comments

Hi, thanks for your answer, I solved my problem. :) ;)

Thanks

jarain78 gravatar image jarain78  ( 2018-06-26 09:16:10 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-06-24 17:31:01 -0600

Seen: 754 times

Last updated: Jun 25 '18