ros2创建package, launch file, custom message教程

本笔记是按照视频https://www.bilibili.com/video/BV19E411H7NN?p=1做的,记录一下一些ros2的基本操作

创建workspace

mkdir -p ros2_ws/src
cd ros2_ws
colcon build
source install/local_setup.bash

使用C++创建ros2 package

1. 创建package

cd ros2_ws/src
ros2 pkg create ros2_cpp_pkg --build-type ament_cmake --dependencies rclcpp

2.  编写cpp文件

在ros2_cpp_pkg文件夹下的src文件夹内创建文件 ros2_cpp_node.cpp

这个文件里的代码如下:

#include "rclcpp/rclcpp.hpp"

int main(int argc, char* argv[]){
    rclcpp::init(argc,argv);
    auto node = rclcpp::Node::make_shared("ObiWan");
    
    RCLCPP_INFO(node->get_logger(),
                "Help me Obi-Wan Kenobi, you're my only hope");
    rclcpp::shutdown();

    return 0;
}

3.在CMAKELIST里添加相关内容

在CMAKELIST的末尾添加以下内容

add_executable(cpp_code src/ros2_cpp_node.cpp)
ament_target_dependencies(cpp_code rclcpp)

install(TARGETS
  cpp_code
  DESTINATION lib/${PROJECT_NAME}
  )

4. 重新build再运行setup脚本

在ros_ws文件目录下执行

colcon build --symlink-install
source install/setup.bash

5.运行测试

ros2 run ros2_cpp_pkg cpp_node

结果:

ljh@lll:~/桌面/ros2workspace/ros2_ws$ ros2 run ros2_cpp_pkg cpp_code
[INFO] [ObiWan]: Help me Obi-Wan Kenobi, you're my only hope

创建一个ros2 launch file

1. 创建launch文件夹和文件

在ros2_cpp_pkg文件夹下创建launch文件夹, 并在launch文件夹下创建ros2_cpp_code.launch.py文件

# ros2_cpp_code.launch.py
from launch import LaunchDescription
import launch_ros.actions

def generate_launnch_description():
    return LaunchDescription([
        launch_ros.actions.Node(
            package='ros2_cpp_pkg', node_executable='cpp_code', output='screen'
        ),
    ])

2. 在CMAKELISTS里面添加luanch file

在末尾添加

install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)

3. 重新build再运行setup脚本

在ros_ws文件目录下执行

colcon build --symlink-install
source install/setup.bash

4. 运行测试

ros2 launch ros2_cpp_pkg ros2_cpp_code.launch.py

结果:

ljh@lll:~/桌面/ros2workspace/ros2_ws$ ros2 launch ros2_cpp_pkg ros2_cpp_code.launch.py
[INFO] [launch]: process[cpp_code-1]: started with pid [18865]
[INFO] [ObiWan]: Help me Obi-Wan Kenobi, you're my only hope
[INFO] [launch]: process[cpp_code-1]: process has finished cleanly

创建ros2 custom message

1. 创建package

在ros2_ws/src 文件夹下,创建ros2_msg 这个 pacakge

ros2 pkg create ros2_msg --build-type ament_cmake --dependencies rclcpp std_msgs

2. 创建msg文件

在ros2_msg 文件夹下创建 msg文件夹,再在msg文件夹下创建MyMsg.msg文件

文件内容如下:

int32 day
string month
int32 year

3. 修改CMakeLists.txt

修改后的文件如下

cmake_minimum_required(VERSION 3.5)
project(ros2_msg)

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(builtin_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # remove the line when a copyright and license is present in all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # remove the line when this package is a git repo
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

rosidl_generate_interfaces(${PROJECT_NAME}
  "msg/MyMsg.msg"
  DEPENDENCIES builtin_interfaces
)

ament_package()

4. 修改package.xml

修改后的文件如下

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>ros2_msg</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="674478778@qq.com">ljh</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <depend>rclcpp</depend>
  <depend>std_msgs</depend>
  <build_depend>builtin_interfaces</build_depend>
  <build_depend>rosidl_default_generators</build_depend>
  <exec_depend>builtin_interfaces</exec_depend>
  <exec_depend>rosidl_default_generators</exec_depend>

  <member_of_group>rosidl_interface_packages</member_of_group>


  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

5. 重新build再运行setup脚本

在ros_ws文件目录下执行

colcon build --symlink-install
source install/setup.bash

6. 测试

一个终端运行:

ros2 topic pub /test_topic ros2_msg/MyMsg

另一个终端运行

ros2 topic echo /test_topic 

测试结果分别如下:

publisher: beginning loop
publishing #1: ros2_msg.msg.MyMsg(day=21, month='Jan', year=2077)

publishing #2: ros2_msg.msg.MyMsg(day=21, month='Jan', year=2077)

publishing #3: ros2_msg.msg.MyMsg(day=21, month='Jan', year=2077)

publishing #4: ros2_msg.msg.MyMsg(day=21, month='Jan', year=2077)
day: 21
month: Jan
year: 2077

day: 21
month: Jan
year: 2077

day: 21
month: Jan
year: 2077
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值