How do I ensure that callback2 only gets called once? When I call the service, the callback2 keeps running over and over even after a response of True has been given.
Note: This project requires the use of services and I don’t have any python knowledge beyond what has been introduced in the course up to this point.
#! /usr/bin/env python import rospy from geometry_msgs.msg import Twist from sensor_msgs.msg import LaserScan from srv_message_pkg.srv import FindWall, FindWallResponse import time def callback2(msg): move = Twist() minimum = min(msg.ranges) index = msg.ranges.index(minimum) if index < 180: move.angular.z = -3.14/18.0 else: move.angular.z = 3.14/18.0 print("index: ", index) print("dist: ", minimum) pub.publish(move) time.sleep(abs(180-index)/10.0) move.angular.z = 0 move.linear.x = 0.1 pub.publish(move) time.sleep(minimum * 5) move.linear.x = 0 pub.publish(move) def callback(request): sub = rospy.Subscriber("/scan", LaserScan, callback2) response = FindWallResponse() response.wallfound = True time.sleep(20) return response rospy.init_node("find_wall_server") my_service = rospy.Service('/find_wall', FindWall, callback) pub = rospy.Publisher("/cmd_vel", Twist, queue_size=1) rospy.spin()