Hello
I am currently on the Rosject from ROS in 5 Days Python part 2.
When I run my code, I sometimes get this error:
Here is the part of the code which causes the error:
class Find_Nearest_Wall():
def init(self):
# start publisher, subscriber and server
self.pub = rospy.Publisher(‘/cmd_vel’, Twist, queue_size=1)
self.move = Twist()
self.sub = rospy.Subscriber(‘/scan’, LaserScan, self.sub_callback)
self.rate = rospy.Rate(1)
self.my_service = rospy.Service(“/find_wall”, FindWall, self.server_callback)
rospy.loginfo(“The service: /find_wall is ready”)
def sub_callback(self, data):
# get laser data
self.laser = data.ranges
def server_callback(self, request):
print("server_callback started with {",request,"}")
# init values
i=0
min_laser = float('inf')
min_index = 0
# get robot standing still
self.move.linear.x = 0
self.move.angular.z = 0
self.pub.publish(self.move)
# loop through all laserbeams to find the smallest
for i in range(0,len(self.laser)):
if self.laser[i] < min_laser:
min_laser = self.laser[i]
min_index = i
So I mean my self.laser gets initiated in the sub_callback(self, data). So therefore I should be able to use it in the server_callback() function.
What is weird, is that if I launch my code multiple times then after the 3th or 4th attempt it then works without error.
Meaning I do the following in the simulation:
- Launch the code → Error message → Ctrl C to stop the code
- Launch again the code → Error message → Ctrl C to stop the code
- Launch again (3th or 4th attempt) → No Error Message → Code runs like it is supposed to do.
So I don’t really know why I am getting this error messages. Sometimes it does also work on the 1st attempt without any error.
So I don’t know why I get this error message and why then suddently not any more after a few attempts, without having moved the robot.
So I would be thankful for any help.