Problem with publishing to cmd_vel and incorrect distances from laser

Hi guys,

I am having 2 issues with part 1 of the rosject.
The first problem: I seem to be reading incorrect distances form the laser. From what I understand I should be checking the first values of ranges for the right and the values around 360 for the front. but when I do this I seem to get correct values for the right but not for the front.

The second problem: I don’t think I am publishing correctly to cmd_vel as when I launch the pkg the robot just moves straight even though it should be turning. The print messages in the code are being printed correctly.

Any help would be really great I have been stuck on this project for a while…
thanks in advance!

 #! /usr/bin/env python

import rospy

from sensor_msgs.msg import LaserScan

from geometry_msgs.msg import Twist

def callback(msg): # get the laser scan message 

    right_dist = min(min(msg.ranges[0:30]), 10)

    front_dist = min(min(msg.ranges[350:370]), 10)

    print("right dist is %f" % (right_dist))

    print("front dist is %f" % (front_dist))

    move = Twist()

    if right_dist > 0.30 and front_dist > 0.50:

        move.angular.z = -0.03

        move.linear.x = 0.03

        print("turning to wall")

    elif right_dist < 0.20 and front_dist > 0.50:

        move.angular.z = 0.03

        move.linear.x = 0.03

        print("turning away from wall")

    elif front_dist > 0.50:

        move.linear.x = 0.03

        print("moving forward")

    elif front_dist < 0.50:

        move.angular.z = 0.04

        move.linear.x = 0.03

        print("turning with wall")



rospy.init_node('Wall_follower') # we create a node called wall follower

pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)

right_dist = None

front_dist = None

sub = rospy.Subscriber('/scan', LaserScan, callback) # we subscribe to laser scan and put info in msg

rate = rospy.Rate(2)

move = Twist()

while not rospy.is_shutdown():




Hi, you can test where ranges value corresponds with the front of the robot by facing a wall and looking for where the smallest value is.

As for the cmd_vel issue, I would get rid of the linear velocity whenever you are turning to start to debug what might be happening. Does the robot move correctly when you launch turtlebot3_teleop? Also, make sure that other terminals aren’t interfering with the cmd_vel that your program is publishing.

1 Like

Thanks for the great tips, they helped me solve the problems and finish this section of the rosject!

1 Like

I’ve taken the same approach. Kinda stuck with calculations. Could you possibly share a abit more clue or hint on how you solved it? I would appreciate that. Thanks

Please explain your approach. What calculations are you stuck with?

Below is my approach. It works as per requirement but not that smooth though.

def callback(msg): # get the laser scan message


front_range = min(msg.ranges[155:175])

right_range = min(msg.ranges[0:30])

left_range = min(msg.ranges[340:359])

print("Front ", front_range)

print("Right ", right_range)

print("Left ", left_range)

if front_range > 0.5 and right_range > 0.3:

    move.angular.z = - 0.1

    move.linear.x = 0.05

    print("Turn Right to wall ")

elif front_range > 0.5 and right_range < 0.3 and right_range > 0.2:

    move.angular.z = 0.00

    move.linear.x = 0.05

    print("Go Straight")


elif front_range > 0.5:

    move.angular.z = 0.00

    move.linear.x = 0.05

    print("Go Straight")

elif front_range < 0.5 or right_range < 0.2:

    move.angular.z = 0.5

    move.linear.x = 0.01

    print("Turn Left away from wall")

Is there a way to improve performance?

I’m sorry, I don’t know what you mean by “smooth”. If you mean that you want the robot to move along the wall without making little turns, you could use different laser readings, closer to each other, in order for the motors to perform a smaller correction. This could make it look like it’s going more “straight”

1 Like