# originbot **Repository Path**: ailearncoder/originbot ## Basic Information - **Project Name**: originbot - **Description**: originbot小车 手机远程 设备端代码 - **Primary Language**: C++ - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-01-17 - **Last Updated**: 2023-01-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: ros2 ## README - 如何使用包下面的include目录? 在 ROS 2 包中使用 include 目录需要在 CMakeLists.txt 文件中设置 include 路径。 首先,需要在 CMakeLists.txt 文件中设置包的 include 路径。例如,要在包名为 my_package 的包中设置 include 路径 include,可以在 CMakeLists.txt 文件中添加如下内容: ``` ament_target_dependencies(my_package "rclcpp" ) target_include_directories(my_package PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ) ``` 然后,就可以在包的代码中使用 ```#include ``` 的形式包含包中的头文件了。 注意,如果你希望其他包也能够使用该包的头文件,需要在 CMakeLists.txt 文件中使用 ament_export_include_directories() 函数暴露 include 路径。 例如,要暴露包名为 my_package 的包的 include 路径,可以在 CMakeLists.txt 文件中添加如下内容: ``` ament_export_include_directories(include) ``` 这样,其他包就可以使用 find_package() 和 ament_target_dependencies() 函数来设置依赖项,并使用 #include 的形式包含包中的头文件了。 - ros2 功能包 添加google protobuf 编译 在 ROS 2 功能包中使用 Google Protobuf 需要完成以下几步: 安装 Google Protobuf 库和头文件。在 Ubuntu 系统中,可以使用以下命令安装: ``` sudo apt-get install libprotobuf-dev protobuf-compiler ``` 在包的 CMakeLists.txt 文件中设置依赖项。需要使用 ament_target_dependencies() 函数设置包的依赖项,如以下代码所示: ``` ament_target_dependencies(my_package "protobuf" ) ``` 生成 Protobuf 消息文件。首先需要使用 protoc 工具将 .proto 文件编译成 .pb.h 和 .pb.cc 文件。然后需要在 CMakeLists.txt 文件中使用 add_message_files() 函数将消息文件添加到包中。例如,要将包含目录 msg 下的所有消息文件添加到包中,可以使用以下代码: ``` add_message_files( DIRECTORY msg FILES MyMessage1.msg MyMessage2.msg ) ``` 生成 Protobuf 消息 C++ 代码。需要使用 generate_messages() 函数生成 Protobuf 消息的 C++ 代码。这个函数需要传入一个或多个消息文件的名称,用于指定需要生成的消息类型。例如,要生成前面添加的两个消息文件 MyMessage1.msg 和 MyMessage2.msg 的 C++ 代码,可以使用以下代码: ``` generate_messages( DEPENDENCIES std_msgs ) ``` 注意,这里的 std_msgs 是指定依赖的消息包。如果你的消息文件中使用了其他消息类型,需要将其也添加到依赖项中。 最后,需要在 C++ 代码中包含生成的头文件,并使用相应的 Protobuf 消息类型。例如,要使用 MyMessage1 消息类型,可以在代码中添加如下内容: ``` #include "my_package/msg/MyMessage1.hpp" int main() { my_package::msg::MyMessage1 message; // ... } ``` 具体使用方法可以参考 Protobuf 官方文档和示例代码。 ### 常用命令 ``` #!/bin/bash export COLCON_CURRENT_PREFIX=./install source ./install/local_setup.sh ros2 run phone_control remote_video ros2 run phone_control_c phone_control_c --ros-args --log-level info colcon build --packages-select phone_control_c 创建功能包 如何在ROS2中创建一个功能包呢?我们可以使用这个指令: $ ros2 pkg create --build-type ros2命令中: pkg:表示功能包相关的功能; create:表示创建功能包; build-type:表示新创建的功能包是C++还是Python的,如果使用C++或者C,那这里就跟ament_cmake,如果使用Python,就跟ament_python; package_name:新建功能包的名字。 比如在终端中分别创建C++和Python版本的功能包: $ cd ~/dev_ws/src $ ros2 pkg create --build-type ament_cmake learning_pkg_c # C++ $ ros2 pkg create --build-type ament_python learning_pkg_python # Python ros2 pkg list protoc --cpp_out=. ./src/phone_control_c/src/h26x_frame.proto ```