Giter Club home page Giter Club logo

simple_node's Introduction

Simple Node

This wrap uses the spin of an executor in a new thread. As a result, the calls of services and actions can be used synchronously. Besides, this node also includes predefined action servers and one client to make its use more user-friendly to developers.

Examples

Node

This example shows how to create a node. To block the node, join_spin is used, which blocks the main thread till the thread of the spin ends.

from simple_node import Node
import rclpy

class MyNode(Node):
    def __init__(self):
        super().__init__("my_node")

def main():
    rclpy.init()
    node = MyNode()
    node.join_spin()
    rclpy.shutdown()


if __name__ == "__main__":
    main()

Action Server

The following example shows how to create an action server that treats only one goal at the same time. If a new goal is received, the server aborts the current goal and starts treating the new one.

class MyNode(Node):
    def __init__(self):
        super().__init__("my_node")

        self.__action_server = self.create_action_server(ActionType,
                                                         "action_name",
                                                         self.__execute_cb)

    def __execute_server(self, goal_handle):
        result = ActionType.Result()

        succeed = self.do_something(goal_handle.request)

        if self.__action_server.is_canceled():
            self.__action_server.wait_for_canceling()
            goal_handle.canceled()

        else:
            if succeed:
                goal_handle.succeed()
            else:
                goal_handle.abort()

        return result

Action Client

The following example shows how to create an action client.

class MyNode(Node):
    def __init__(self):
        super().__init__("my_node")

        self.__action_client = self.create_action_client(
            ActionType, "action_name")

        goal = ActionType.Goal()

        self.__action_client.wait_for_server()
        self.__action_client.send_goal(goal)
        self.__action_client.wait_for_result()

        self.__action_client.is_succeeded():

simple_node's People

Contributors

mgonzs13 avatar

Stargazers

Furkan GÜLTEK avatar  avatar  avatar David Sobrín Hidalgo  avatar Linda TJ avatar Napuh avatar Alberto avatar

Watchers

 avatar  avatar

simple_node's Issues

Created node name remap

When a node is created from within another ROS2 node which was launched with a remapped name and namespace, the created simple_node gets its name andnamespace remapped b global args.

Example Piece of Code:

from rclpy.node import Node
from simple_node import Node as SimpleNode

self.__actionState_node = SimpleNode(_node.get_name() + "_" + _state_name + "_action_node", _node.get_namespace())
self.__another_node = Node(_node.get_name() + "_" + _state_name + "_another_node", namespace=_node.get_namespace(), use_global_arguments=False)
        ActionState.__init__(self=self,
                             node=self.__actionState_node,
                            action_type=_action_type,
                            action_name=_action_name,
                            create_goal_handler=_create_goal_handler,
                            outcomes=_outcomes,
                            result_handler=_result_handler)

If the prcess is launched as follow:

ros2 run package node --ros-args -r __node:=name_remapped

In the above case self.__actionState_node will be named "name_remapped" in any case, while self.__another_node won't beacuase of the use global_arguments=False
In the init method of the simple node only name and namepsace arguments are accepted.

A fix could be to rewrite the init method as follows:

def __init__(self, node_name: str,
                        context = None,
                        cli_args = None,
                        namespace = "",
                        use_global_arguments = True,
                        enable_rosout = True,
                        start_parameter_services = True,
                        parameter_overrides = None,
                        allow_undeclared_parameters = False,
                        automatically_declare_parameters_from_overrides = False, 
                        executor = None):

        super().__init__(node_name,
                        context = context,
                        cli_args = cli_args,
                        namespace = namespace,
                        use_global_arguments = use_global_arguments,
                        enable_rosout=enable_rosout,
                        start_parameter_services=start_parameter_services,
                        parameter_overrides=parameter_overrides,
                        allow_undeclared_parameters=allow_undeclared_parameters,
                        automatically_declare_parameters_from_overrides=automatically_declare_parameters_from_overrides
                        )

        if not executor:
            self._executor = MultiThreadedExecutor()
        else:
            self._executor = executor

        self._spin_thread = Thread(target=self._run_executor, daemon=True)
        self._spin_thread.start()

        self._wake_thread = Thread(target=self._wake_node, daemon=True)
        self._wake_thread.start()

In this way it is possible to have more controll on all ROS2 node init arguments

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.