Creating and building ROS2 in Drone =================================== If there is a problem with the action or the topic in the drone, you need to delete and create the action and topic again. Remove all the content of the ``Drone`` folder except the ``src`` folder and the ``Logs`` folder. Create a workspace ------------------ .. code-block:: bash cd Drone mkdir src cd src State Vector Publisher ---------------------- Create a package ~~~~~~~~~~~~~~~~ In ``Drone/src``, execute: .. code-block:: bash ros2 pkg create --build-type ament_python --license Apache-2.0 state_vector_pubsub Import the publisher node ~~~~~~~~~~~~~~~~~~~~~~~~~ Navigate into ``Drone/src/state_vector_pubsub/state_vector_pubsub``. Then, move into the directory the ``.py`` modules that are in ``src/state_vector_pubsub/state_vector_pubsub``. Add dependencies ~~~~~~~~~~~~~~~~ Add the following dependencies into ``package.xml`` (``Drone/src/state_vector_pubsub``): .. code-block:: xml rclpy std_msgs Add an entry point ~~~~~~~~~~~~~~~~~~ In ``setup.py``, add the following line within the ``console_scripts`` brackets of the ``entry_points`` field: .. code-block:: python entry_points={ 'console_scripts': [ 'talker = py_pubsub.publisher_member_function:main', ], } Check ``setup.cfg`` ~~~~~~~~~~~~~~~~~~~ Check that ``setup.cfg`` has the following content: .. code-block:: ini [develop] script_dir=$base/lib/state_vector_pubsub [install] install_scripts=$base/lib/state_vector_pubsub Build and run ~~~~~~~~~~~~~ Still in the root of your workspace, ``Drone``, build your new package: .. code-block:: bash colcon build --packages-select state_vector_pubsub Open a new terminal, navigate to ``Drone``, and source the setup files: .. code-block:: bash source install/setup.bash Now run the talker node: .. code-block:: bash ros2 run state_vector_pubsub talker Action Server ------------- Create an interface package ~~~~~~~~~~~~~~~~~~~~~~~~~~~ In ``Drone/src``, execute: .. code-block:: bash ros2 pkg create --license Apache-2.0 commands_action_interface Restore the action ~~~~~~~~~~~~~~~~~~ Create an ``action`` directory in the ROS 2 package ``commands_action_interface``: .. code-block:: bash cd commands_action_interface mkdir action Move ``Commands.action`` from ``src/commands_action_interface/action`` into ``Drone/src/commands_action_interface/action``. Building the action ~~~~~~~~~~~~~~~~~~~ Add the following lines to ``CMakeLists.txt`` before the ``ament_package()`` line: .. code-block:: cmake find_package(rosidl_default_generators REQUIRED) rosidl_generate_interfaces(${PROJECT_NAME} "action/Commands.action" ) Add the required dependencies to ``package.xml``: .. code-block:: xml rosidl_default_generators rosidl_interface_packages Build the package by executing the following commands: .. code-block:: bash # Change to the root of the workspace cd ~/Drone # Build colcon build Run the server ~~~~~~~~~~~~~~ Move all the ``.py`` modules from ``src/commands_action_interface/src`` to ``Drone/src/commands_action_interface/src``. To run the server, open a new terminal, navigate to ``Drone``, and source the setup files: .. code-block:: bash source install/setup.bash Now run the server: .. code-block:: bash python3 src.commands_action_interface.src.commands_action_server.py