terça-feira, 5 de abril de 2022

ROS 2: Hello World!




Package Creation

    Well, it's time to do the same code as we did for the DDS layer, but now for ROS 2 instead.

    The first thing we need to do to create ROS-2 code is to build a package. A package works as a container for your code to be run and shared among other ROS-2 users / machines / projects.

    To create a package, first we need to have a workspace set up. 


    Workspace

        First you need to choose a path for your workspace. For this example, let's make one called ws. The name doesn't matter.

mkdir -p ws/src              
cd ws/

        Notice that we also created a subdir called src. This dir is to be holding our package code. It is considered good practice having a subdir called src to hold the packages in a workspace.

        Next, let's activate ROS-2 shell env

source /opt/ros/galactic/setup.bash

    Package building


    
    Nice, ok, so now let's move on to the package creation and building.  For C++ programming, we need to have at least two files :

  • package.xml    -   meta information about our package
  • CMakeList.txt -   describes how to compile our package's code
        Let's do it all by issuing the following command

ros2 pkg create --build-type ament_cmake --node-name hello_node hello_world
        This creates the basic structure of a package named hello_world for a node called hello_node for us. 




        Now let's build the package to test if everything is working as expected. The following command will build every package you have in your workspace. That's fine for now, because we only have our hello_world package

cd ..
colcon build               
Starting >>> hello_world
Finished <<< hello_world [1.42s]                     

Summary: 1 package finished [1.77s]

        If you wish to build only a specific package, you should use the command bellow. Let's try it now too.

colcon build --packages-select (package name) 


    
    Finally, let's test our package by running it on a node. Remember that we are simulating a robot processing unit such as a microcontroller, so the terminal (which is a OS process) will work as that processor to run our node.

        In order to differentiate it from other processes that are potentially running in parallel in our robot, we'll setup our terminal to point out to our hello_world package / hello_node node  "session", then we'll ask ros to run the code


. install/local_setup.bash
ros2 run hello_world hello_node
        ROS ran our code and then returned it's output:  "hello world hello_world package"

        Let's go and change it so we get a better hello world message. In hello_node.cpp:

#include <cstdio>

int main(int argc, char ** argv)
{
(void) argc;
(void) argv;

printf("Hello world from ROS!\n");
return 0;
}


      Now rebuild and run it to test it:

colcon build --packages-select --allow-overriding hello_world
ros2 run hello_world hello_node
Hello world from ROS!



Nenhum comentário:

Postar um comentário

ROS2: Executables, Plugins and Components

    Introduction       In ROS-2, we have basically three types of code-run generation:  an executable, a plugin and a component.       An ex...