RealRobotLab Turtlebot3 LaserScan not functioning

Hi, I am having trouble with the /scan topic in realrobotlab simulation. The /scan topic seems to not be updating with time. Here is an example of what I mean :

Basically in that screenshot, I am printing the ‘front sensor’ value from the ranges. The robot is actually moving in a straight line, yet the sensor reading is not changing.

One odd thing about this is that, when I re-run my simulation, the front sensor value will be updated to its current position but if the robot is moving anywhere in the simulation, the sensor value is still unchanged.

Does it have anything to do with the rate I am printing my sensor value?

It sounds like your code assigns the “front distance” variable once, but never updates it. Double check you code and see if that is the case

1 Like
frontSensor = msg.ranges[180]
while frontSensor > 0.5:

    if msg.ranges[90] < 0.3 and msg.ranges[90] > 0.2 :
        diff = msg.ranges[120] - msg.ranges[60]

        if diff > 0.15:
            # We have to rotate anti-clockwise

        elif diff < -0.5:
            # We have to rotate clockwise

            rospy.loginfo('Distance from front wall {distance}'.format(distance=frontSensor))
            twist = Twist()
            rate = rospy.Rate(1)
            twist.linear.x = 0.1
            twist.angular.z = 0


#Then initiate node, publisher and subscriber

Its not the cleanest of all code, but hopefully it’s not messy enough for you to understand it. This is basically my callback function for my rospy.Subscriber. As you can see, my program is stuck inside that else loop, and I thought that it will update the variable for every loop?

Please do correct me if I’m wrong.

Ah, I see your issue

So you aren’t getting trapped in the else block, as much as the while loop. From the example output you posted earlier, your initial value for frontSensor was 1.468… Which is is greater than 0.5, so you enter the while loop. All good so far.

However, nothing in the while loop every changes the value of frontSensor, so the code never escapes the while loop. Because you never finish the loop, your callback function never completes. Because the callback never finishes, it never gets called again. Because the callback is only called once, it only updates the values of frontSensor once. Does that make sense?

My advice is to avoid the while loop entirely. In this case you expect the callback to be called very frequently so you can (sort of) think of the callback function behaving like a loop.

Does that clear things up?

1 Like

Yeah, that makes perfect sense. That’s what I thought too tbh.

However, I’m still struggling with it. Not really sure what went wrong tbh.

So, I changed my callback function to this and make it simpler without the loop to test it:

 #! /usr/bin/env python

import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
import time
import numpy as np

def callback(msg):

    rate = rospy.Rate(1)

    frontSensor = msg.ranges[180]

    if msg.ranges[90] < 0.3 :

        twist = Twist()
        rate = rospy.Rate(1)
        twist.linear.x = 0.5
        twist.angular.z = 0
        rospy.loginfo('Distance from front wall {distance}'.format(distance=frontSensor))

sub = rospy.Subscriber('/scan', LaserScan, callback)
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)

I removed all the possible loops there is and just made a simpler one and positioned my robot in a position that satisfies those conditions.
However, it still doesnt update my frontSensor as shown below:

[INFO] [1623374896.069053, 2417.032000]: Distance from front wall 1.032407283782959
[INFO] [1623374897.142459, 2418.032000]: Distance from front wall 1.0337927341461182
[INFO] [1623374898.208466, 2419.032000]: Distance from front wall 1.0265597105026245
[INFO] [1623374899.241514, 2420.032000]: Distance from front wall 1.029232144355774
[INFO] [1623374900.351034, 2421.032000]: Distance from front wall 1.0195547342300415
[INFO] [1623374901.527919, 2422.032000]: Distance from front wall 1.0373893976211548

Also, I also ‘echo’-ed the rostopic that reads the sensor msgs. Below is the command line I use:

# In WebShelll #1
rostopic echo /scan/ranges[180]   # To get the front reading

# Output I received when I run my python script:


What do you think my mistake here is?

p/s : Thank you for the quick reply. I really appreciate it :grinning_face_with_smiling_eyes:

1 Like

What jumps out at me is that you are still using rospy.Rate(). Rates are typically only used with loops. I would try removing that. Typically, the rate of your callback function will be determined by the rate that messages are received. (i.e. you shouldn’t expect it to be consistent)

The other weird thing is that frontSensor is getting updated, but very slowly. Again, I am suspicious of that imposed delay.

p.s. I didn’t realize you could use rostopic to view just a specific field from a message. Thanks for that tip I will definitely use it!


Hmmm, I tried removing it, but it is still not giving me the results I wanted. Maybe I should just re-write the whole code again, and see what would work. Speaking of which, do you have some guidelines or tips that might be useful for me to perform the lab? Any guidelines (eg get reading from front sensor and check the distance) or tips would be helpful and it may help other people too!

p.s : No problem! Glad that it was helpful!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.