Waiting for callback before control loop
Hello, I have a node where I'm using lidar data to control a turtlebot. I'm subscribed to the /scan topic, and storing the distance and angle from the lidar callback inside class variables, and these are updated with every callback. These class variables are initialized to 0. Sometimes the first callback happens before the control loop, and then everything runs fine. But sometimes the callback does not happen before the control loop and this causes the values to keep their initial value on the first control loop iteration, which can cause issues.
Here is a simplified version of the code:
class move:
def __init__(self):
self.pub = rospy.Publisher("tb3_1/cmd_vel",Twist,queue_size=100)
self.sub2 = rospy.Subscriber("tb3_1/scan",LaserScan,self.lidar_callback)
self.lidar_dist = 0
self.lidar_angle = 0
def control(self):
move = Twist()
--- Computation of control equations---
def lidar_callback(self,msg):
---calculate distance and angle---
self.lidar_dist = distance
self.lidar_angle = angle
if __name__ == '__main__':
rospy.init_node("lidar_follower",disable_signals=True)
rate = rospy.Rate(100)
obj = move()
while not rospy.is_shutdown():
obj.control()
rate.sleep()
Thanks in advance