I am using in rviz2 the tool nav2 goal. But I want to cancel the robot's navigation when it is at a distance of less than 2 meters from an obstacle
I want to replicate this button but in a python code. The robot should stop if it is close <2 m from any obstacle. I know the functions BasicNavigator.cancelTask() works but just if you add the goal in code but I need to use the Nav2 Goal tool.
I tried using the Simple Commander API just calling the BasicNavigator.cancelTask() function but is not working I think I am using wrong. Does anyone how to do it ? It should be calling a service but I do not know which one exactly.
#!/usr/bin/env python3
import rclpy
from nav2_simple_commander.robot_navigator import BasicNavigator
from rclpy.node import Node
from rclpy.action import ActionClient
from std_srvs.srv import SetBool
class StopNavigation(Node):
def __init__(self):
super().__init__('stop_navigation')
# Create a BasicNavigator object.
self.navigator = BasicNavigator()
def stop_navigation_client(self):
while rclpy.ok:
self.navigator.waitUntilNav2Active()
client = self.create_client(SetBool, "soft_stop_navigation")
# Wait for the SetBool client to become available.
while not client.wait_for_service(timeout_sec=1.0):
rclpy.spin_once(self)
# Send a request to the SetBool client.
request = SetBool.Request()
request.data = True
future = client.call_async(request)
rclpy.spin_until_future_complete(self, future)
# Check the response.
response = future.result()
print("")
print("future_t: ", response.success)
print("feedback: ",self.navigator.getFeedback())
print("")
if response.success:
print("SetBool service succeeded, Cancel Nav.")
self.navigator.cancelTask()
# Check if the task was successfully canceled
if self.navigator.cancelTask():
print("Task was successfully canceled")
else:
print("Task could not be canceled")
break
else:
print("Continue Nav...")
def main():
# Initialize the ROS node.
rclpy.init()
# Create a MyNode object.
node = StopNavigation()
# Wait for the SetBool client to become available.
node.stop_navigation_client()
rclpy.spin_once(node)
# Shutdown the ROS node.
rclpy.shutdown()
if __name__ == "__main__":
main()
Perhaps you should use the SimpleGoalChecker and modify its
xy_goal_tolerance
parameter?