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
pub.publish(twist)
rospy.loginfo('Distance from front wall {distance}'.format(distance=frontSensor))
rate.sleep()
rospy.init_node('turtlebot_follow_wall_node')
sub = rospy.Subscriber('/scan', LaserScan, callback)
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
rospy.spin()
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:
1.011608600616455
---
0.9516257643699646
---
0.9455939531326294
---
0.8709417581558228
---
0.8126998543739319
---
0.6968746185302734
---
0.5924617648124695
---
0.5130252838134766
---
0.39601925015449524
---
0.2969437539577484
---
0.2104492485523224
---
inf
---
inf
What do you think my mistake here is?
p/s : Thank you for the quick reply. I really appreciate it