本篇是关于ROS学习笔记 关于赵老师的2.2.4_话题通信_自定义接口消息_接口文件_哔哩哔哩_bilibili
1、创建工作空间
mkdir -p ws02_plumbing/src
cd ws02_plumbing
colcon build
2、创建功能包 在src文件夹中
ros2 pkg create --build-type ament_cmake base_interfaces
3、进入vscode
进入ws02-src文件夹
cd src
4、创建cpp文件和创建py文件
ros2 pkg create cpp01_topic --build-type ament_cmake --dependencies rclcpp std_msgs base_interfaces_demo --node-name demo01_talker_str
ros2 pkg create py01_topic --build-type ament_python --dependencies rclpy std_msgs base_interfaces_demo --node-name demo01_talker_str_py
5、运行
1、ros2 run cpp01_topic demo01_talker_str
2、在另外一个终端内进行,先执行install:ros2 run cpp01_topic demo02_listener_str
关于cpp下的 talker.cpp:(demo01_talker_str
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using namespace std::chrono_literals;
class Talker: public rclcpp::Node{
public:
Talker():Node("talker_node_cpp"),count(0){
RCLCPP_INFO(this->get_logger(),"sender create");
publisher_ = this->create_publisher<std_msgs::msg::String>("chatter",10);
// use: ros2 topic echo /chatter 检测接收方 是否接受到
timer_ = this->create_wall_timer(1s,std::bind(&Talker::on_timer,this));
}
private:
void on_timer(){
auto message = std_msgs::msg::String();
message.data = "hello world"+std::to_string(count++);
RCLCPP_INFO(this->get_logger(),"send information:%s",message.data.c_str());
publisher_->publish(message);
}
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
rclcpp::TimerBase::SharedPtr timer_;
size_t count;
};
int main(int argc, char ** argv)
{
rclcpp::init(argc,argv);
rclcpp::spin(std::make_shared<Talker>());
rclcpp::shutdown();
return 0;
}
listen.cpp:(demo02_listener_str
// listen 订阅发布的消息 并且在终端输出
// 1、包含头文件 2、初始化客户端 3、自定义节点类 4、调用spin函数 并且传入节点指针 5、支援释放
// 3.1、创建订约方 3、2 解释并且输出数据
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
class Listener: public rclcpp::Node{
public:
Listener():Node("Listen_node_cpp"){
RCLCPP_INFO(this->get_logger(),"receive create");
subscription_ = this->create_subscription<std_msgs::msg::String>("chatter",10,std::bind(&Listener::do_cb,this,std::placeholders::_1));
}
private:
void do_cb(const std_msgs::msg::String &msg){
RCLCPP_INFO(this->get_logger(),"receive:%s",msg.data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};
int main(int argc, char const *argv[])
{
rclcpp::init(argc,argv);
rclcpp::spin(std::make_shared<Listener>());
rclcpp::shutdown();
/* code */
return 0;
}
Cmakelist:
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(baseinterfaces_demo REQUIRED)
add_executable(demo01_talker_str src/demo01_talker_str.cpp)
add_executable(demo02_listener_str src/demo02_listener_str.cpp)
target_include_directories(demo01_talker_str PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_compile_features(demo01_talker_str PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17
ament_target_dependencies(
demo01_talker_str
"rclcpp"
"std_msgs"
"baseinterfaces_demo"
)
ament_target_dependencies(
demo02_listener_str
"rclcpp"
"std_msgs"
"baseinterfaces_demo"
)
install(TARGETS demo01_talker_str
DESTINATION lib/${PROJECT_NAME})
install(TARGETS demo02_listener_str
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()
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="xwh@todo.todo">xwh</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>std_msgs</depend>
<depend>baseinterfaces_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>
5351

被折叠的 条评论
为什么被折叠?



