UBUNTU 20.04 + ROS2 rolling
ROS+开发工具一键安装脚本
wget http://fishros.com/install -O fishros && bash fishros
cd src
ros2 pkg creat --build-type ament_cmake cpp_pubsub
cd cpp_pubsub
在src内创建节点
名为publisher_member_function.cpp
和subscriber_member_function.cpp
#include
#include
#include
#include
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "util/msg/num.hpp"
using namespace std::chrono_literals;
/* This example creates a subclass of Node and uses std::bind() to register a
* member function as a callback from the timer. */
class MinimalPublisher : public rclcpp::Node
{
public:
// MinimalPublisher()
// : Node("minimal_publisher"), count_(0)
// {
// publisher_ = this->create_publisher("topic", 10);
// timer_ = this->create_wall_timer(
// 500ms, std::bind(&MinimalPublisher::timer_callback, this));
// }
MinimalPublisher()
: Node("minimal_publisher"), count_(0)
{
publisher_ = this->create_publisher<util::msg::Num>("/test/NumMsg", 10);
timer_ = this->create_wall_timer(
500ms, std::bind(&MinimalPublisher::timer_callback, this));
}
private:
// void timer_callback()
// {
// auto message = std_msgs::msg::String();
// message.data = "Hello, world! " + std::to_string(count_++);
// RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
// publisher_->publish(message);
// }
void timer_callback()
{
auto message = util::msg::Num();
message.num_test = 3;
// RCLCPP_INFO(this->get_logger(), "Publishing: '%f'", message.num_test.c_str());
std::cout << "running talker" << std::endl;
publisher_->publish(message);
}
rclcpp::TimerBase::SharedPtr timer_;
// rclcpp::Publisher::SharedPtr publisher_;
rclcpp::Publisher<util::msg::Num>::SharedPtr publisher_;
size_t count_;
};
int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalPublisher>());
rclcpp::shutdown();
return 0;
}
#include
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "util/msg/num.hpp"
using std::placeholders::_1;
class MinimalSubscriber : public rclcpp::Node
{
// public:
// MinimalSubscriber()
// : Node("minimal_subscriber")
// {
// subscription_ = this->create_subscription(
// "topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
// }
// private:
// void topic_callback(const std_msgs::msg::String & msg) const
// {
// RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
// }
// rclcpp::Subscription::SharedPtr subscription_;
public:
MinimalSubscriber()
: Node("minimal_subscriber")
{
subscription_ = this->create_subscription<util::msg::Num>(
"/test/NumMsg", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
}
private:
void topic_callback(const util::msg::Num &msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%ld'", msg.num_test);
}
rclcpp::Subscription<util::msg::Num>::SharedPtr subscription_;
};
int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalSubscriber>());
rclcpp::shutdown();
return 0;
}
修改xml和cmakelist
<?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>cpp_pubsub</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="hbjzcrn@foxmail.com">crn</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>std_msgs</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<depend>util</depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
cmake_minimum_required(VERSION 3.5)
project(cpp_pubsub)
# 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_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(util REQUIRED)
add_executable(talker src/publisher_member_function.cpp)
add_executable(listener src/subscriber_member_function.cpp)
ament_target_dependencies(listener rclcpp std_msgs util)
ament_target_dependencies(talker rclcpp std_msgs util)
install(TARGETS
talker
listener
DESTINATION lib/${PROJECT_NAME})
ament_package()
编译后运行
colcon build --packages-select cpp_pubsub
. install/setup.bash
ros2 run cpp_pubsub talker
新建一个终端
. install/setup.bash
ros2 run cpp_pubsub listener
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)