Doubt in exercise_31

Hi, I have a small doubt in exercise_31. If we see in the image below, in the self.laser_forward = msg.ranges[359], how did we know that the ranges is a part of msg.

I mean once we typed ros2 interface show sensor_msgs/msg/LaserScan commnad, then we can see that ranges is a part of LaserScan message which is inside msg. Then why didnt we use
self.laser_forward = LaserScan.ranges[359] instead of self.laser_forward = msg.ranges[359].

Hi @navjotsinghsodhi52 ,

The simplest explanation to your question will be that the LaserScan is a message container, whereas the msg acquired from laser_callback is the actual LaserScan object containing the current scan values.

If you do LaserScan.ranges[359] you will always get 0.0. Because this is an empty message container initialized with zeros for all values.
If you do msg.ranges[359] you will get the current laser scan range value at index 359 which will most probably not be 0.0 exactly.

In simpler terms, LaserScan is like int and msg is like the value of int.
If you do int a = 5, to acquire the value of a, you will need to use a. You cannot do int = 10 - this will be invalid operation!

I hope this is clear.


1 Like

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