创建接口工作空间
必须-p递归创建
mkdir -p ws01_plumbing/src
cd ws01_plumbing/
colcon build
创建接口功能包
cd src/
ros2 pkg create --build-type ament_cmake base_interfaces_demo
warning就对了
topic题1
ws01_plumbing/src下
创建依赖于pkg_interfaces的包
ros2 pkg create cpp01_topic --build-type ament_cmake --dependencies rclcpp std_msgs base_interfaces_demo
这里视频和文档里面不一样,视频先发布了一个demo01_talker_str节点(与cpp文件同名)
ws01_plumbing/src/cpp01_topic/src/demo01_talker.cpp
头文件红线includepath加上
/opt/ros/humble/include/**
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using namespace std::chrono_literals;
// 3.定义节点类;class MinimalPublisher : public rclcpp::Node
{
public:
MinimalPublisher()
: Node("minimal_publisher"), count_(0)
{
// 3-1.创建发布方;
publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
// 3-2.创建定时器;
timer_=this->create_wall_timer(500ms,std::bind(&MinimalPublisher::timer_callback,
this));
}
private:
void timer_callback()
{
// 3-3.组织消息并发布。
private:
void timer_callback(){
auto message = std_msgs::msg::String();
message.data = "Hello, world! " + std::to_string(count_++);
RCLCPP_INFO(this->get_logger(), "发布的消息:'%s'", message.data.c_str());
publisher_->publish(message);
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
size_t count_;
};
int main(int argc, char * argv[]){
// 2.初始化ROS2客户端;
rclcpp::init(argc, argv);
// 4.调用spin函数,并传入节点对象指针。
rclcpp::spin(std::make_shared<MinimalPublisher>());
// 5.释放资源;
rclcpp::shutdown();
return 0;
}
这里可以build一下
ws01_plumbing下
colcon build
ws01_plumbing/src/cpp01_topic/src/demo02_listener.cpp
// 1.包含头文件;
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using std::placeholders::_1;
// 3.定义节点类;
class MinimalSubscriber : public rclcpp::Node
{
public:
MinimalSubscriber()
: Node("minimal_subscriber")
{
// 3-1.创建订阅方;
subscription_ = this->create_subscription<std_msgs::msg::String>("topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
}
private:
// 3-2.处理订阅到的消息;
void topic_callback(const std_msgs::msg::String & msg) const
{
RCLCPP_INFO(this->get_logger(), "订阅的消息: '%s'", msg.data.c_str()); }
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};
int main(int argc, char * argv[])
{
// 2.初始化 ROS2 客户端;
rclcpp::init(argc, argv);
// 4.调用spin函数,并传入节点对象指针。 rclcpp::spin(std::make_shared<MinimalSubscriber>());
// 5.释放资源;
rclcpp::shutdown();
return 0;
}
ws01_plumbing/src/cpp01_topic/src/package.xml无需编辑
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>cpp01_topic</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="shuling@todo.todo">shuling</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>std_msgs</depend>
<depend>base_interfaces_demo</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
ws01_plumbing/src/cpp01_topic/src/CMakelists.txt
add_executable
依赖std_msgs
cmake_minimum_required(VERSION 3.8)
project(cpp01_topic)
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(base_interfaces_demo REQUIRED)
add_executable(demo01_talker src/demo01_talker.cpp) ament_target_dependencies(
demo01_talker
"rclcpp"
"std_msgs"
)
add_executable(demo02_listener src/demo02_listener.cpp) ament_target_dependencies(
demo02_listener
"rclcpp"
"std_msgs"
)
install(TARGETS
demo01_talker
demo02_listener
DESTINATION lib/${PROJECT_NAME})
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
ws01_plumbing下
colcon build --packages-select base_interfaces_demo
colcon build --packages-select cpp01_topic