Hello everyone. I’m trying to do the second part of the rosject for Ros in 5 days. My problem is in creating the FindWall server: the robot in the simulation is supposed to take the data from the /scan topic, find the minimum value of the ranges vector given by the topic and rotate up to the point where its frontal scan (ranges[180]) is the minimum value. Then it should approach it and use the same logic to put the wall to its right.
To follow this logic my idea was to use a while cycle to rotate the robot while the ranges[180] value is bigger than the minimum (plus a tolerance term, since it seems that the laser values tend to change over time even if the robot is not moving) and then stop:
#! /usr/bin/env python
import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
def callback (msg):
print(msg.ranges[180])
a = min(msg.ranges)
print(a)
while msg.ranges[180]>a+0.05:
tw_var.angular.z = 0.3
pub.publish(tw_var)
tw_var.angular.z = 0.0
if msg.ranges[180]>0.3:
tw_var.linear.x = 0.1
pub.publish(tw_var)
else:
b = msg.ranges[180]
while msg.ranges[90]>b+0.05:
tw_var.angular.z = 0.3
pub.publish(tw_var)
tw_var.angular.z = 0.0
pub.publish(tw_var)
rospy.init_node("my_rosject1_node")
sub = rospy.Subscriber("/scan",LaserScan,callback)
pub = rospy.Publisher("/cmd_vel",Twist,queue_size=1)
tw_var = Twist()
rospy.spin()
Obviously I still haven’t written the part about the server, but I think that in order to do that I first have to solve this problem.
My problem is that the readings of the laser are not updated inside the while cycle, which means that the robot ends up in a loop and rotates for a virtually infinite time. This doesn’t happen when I use the if commands.
Did I miss something obvious? How can I solve this?
Thanks in advance for your help!