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

cd Drone
mkdir src
cd src

State Vector Publisher

Create a package

In Drone/src, execute:

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):

<exec_depend>rclpy</exec_depend>
<exec_depend>std_msgs</exec_depend>

Add an entry point

In setup.py, add the following line within the console_scripts brackets of the entry_points field:

entry_points={
        'console_scripts': [
                'talker = py_pubsub.publisher_member_function:main',
        ],
}

Check setup.cfg

Check that setup.cfg has the following content:

[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:

colcon build --packages-select state_vector_pubsub

Open a new terminal, navigate to Drone, and source the setup files:

source install/setup.bash

Now run the talker node:

ros2 run state_vector_pubsub talker

Action Server

Create an interface package

In Drone/src, execute:

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:

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:

find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "action/Commands.action"
)

Add the required dependencies to package.xml:

<buildtool_depend>rosidl_default_generators</buildtool_depend>
<member_of_group>rosidl_interface_packages</member_of_group>

Build the package by executing the following commands:

# 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:

source install/setup.bash

Now run the server:

python3 src.commands_action_interface.src.commands_action_server.py