ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

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

asked 2023-05-23 20:22:41 -0500

Juan Carlos gravatar image

updated 2023-05-23 20:34:51 -0500

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.

image description

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()
edit retag flag offensive close merge delete

Comments

Perhaps you should use the SimpleGoalChecker and modify its xy_goal_tolerance parameter?

ljaniec gravatar image ljaniec  ( 2023-05-24 02:55:59 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-24 11:32:53 -0500

Looking over the rclpy API docs, unless there are functions undocumented, you cannot do that in python3, but you can in C++. You want to cancel all goals using async_cancel_all_goals even if they were sent from another client (e.g. rviz2).

edit flag offensive delete link more

Comments

Thanks a lot, I use the action client navigate_to_pose. I just create the client and with the function async_cancel_all_goals I was able to stop the robot

rclcpp_action::Client<nav2_msgs::action::NavigateToPose>::SharedPtr cancel_navto_client = rclcpp_action::create_client<nav2_msgs::action::NavigateToPose>(node,"/navigate_to_pose");

cancel_navto_client->async_cancel_all_goals();
Juan Carlos gravatar image Juan Carlos  ( 2023-05-26 14:51:32 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2023-05-23 20:22:41 -0500

Seen: 819 times

Last updated: May 24 '23