当前位置:网站首页>ros2订阅esp32发布的电池电压数据
ros2订阅esp32发布的电池电压数据
2022-08-04 18:34:00 【zhangrelay】
一个最简单的订阅和发布案例如下:
pub-

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using namespace std::chrono_literals;
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
auto node = rclcpp::Node::make_shared("simple_publisher");
auto pub = node->create_publisher<std_msgs::msg::String>("/my_message", 10);
std_msgs::msg::String myMessage;
size_t counter{0};
rclcpp::WallRate loop_rate(500ms);
while (rclcpp::ok())
{
myMessage.data = "Hello, ros2 world! " + std::to_string(counter++);
RCLCPP_INFO(node->get_logger(), "Publishing: '%s'", myMessage.data.c_str());
try
{
pub->publish(myMessage);
rclcpp::spin_some(node);
} catch (const rclcpp::exceptions::RCLError & e)
{
RCLCPP_ERROR(node->get_logger(), "Errore type : %s", e.what());
}
loop_rate.sleep();
}
rclcpp::shutdown();
return 0;
}sub-

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
std::shared_ptr<rclcpp::Node> node = nullptr;
void TopicCallback(const std_msgs::msg::String::SharedPtr msg)
{
RCLCPP_INFO(node->get_logger(), "I heard the message : '%s'", msg->data.c_str());
}
int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
node = std::make_shared<rclcpp::Node>("simple_subscriber");
auto sub = node->create_subscription<std_msgs::msg::String>("/my_message", 10, TopicCallback);
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}esp32参考:
esp32发布机器人电池电压到ros2(micro-ros+CoCube)
数据类型是float32,需要修改,开启float32发布和订阅:
发布

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/float32.hpp"
using namespace std::chrono_literals;
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
auto node = rclcpp::Node::make_shared("simple_publisher");
auto pub = node->create_publisher<std_msgs::msg::Float32>("/my_message", 10);
std_msgs::msg::Float32 myMessage;
rclcpp::WallRate loop_rate(500ms);
while (rclcpp::ok())
{
myMessage.data++;
RCLCPP_INFO(node->get_logger(), "Publishing: '%f'", myMessage.data);
try
{
pub->publish(myMessage);
rclcpp::spin_some(node);
} catch (const rclcpp::exceptions::RCLError & e)
{
RCLCPP_ERROR(node->get_logger(), "Errore type : %s", e.what());
}
loop_rate.sleep();
}
rclcpp::shutdown();
return 0;
}
订阅

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/float32.hpp"
std::shared_ptr<rclcpp::Node> node = nullptr;
void TopicCallback(const std_msgs::msg::Float32::SharedPtr msg)
{
RCLCPP_INFO(node->get_logger(), "I heard the message : '%f'", msg->data);
}
int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
node = std::make_shared<rclcpp::Node>("simple_subscriber");
auto sub = node->create_subscription<std_msgs::msg::Float32>("/my_message", 10, TopicCallback);
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
再稍作修改,可以得到订阅esp32机器人电池电压的数据:
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/float32.hpp"
std::shared_ptr<rclcpp::Node> node = nullptr;
void TopicCallback(const std_msgs::msg::Float32::SharedPtr msg)
{
RCLCPP_INFO(node->get_logger(), "Robot battery voltage : '%f'", msg->data);
}
int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
node = std::make_shared<rclcpp::Node>("battery_sub");
auto sub = node->create_subscription<std_msgs::msg::Float32>("/robot_battery", 1, TopicCallback);
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
还遇到一些小问题^_^
#include <cstdio> #include <memory> #include "rclcpp/rclcpp.hpp" #include "rclcpp_components/register_node_macro.hpp" #include "std_msgs/msg/string.hpp" #include "demo_nodes_cpp/visibility_control.h" namespace demo_nodes_cpp { class ListenerBestEffort : public rclcpp::Node { public: DEMO_NODES_CPP_PUBLIC explicit ListenerBestEffort(const rclcpp::NodeOptions & options) : Node("listener", options) { setvbuf(stdout, NULL, _IONBF, BUFSIZ); auto callback = [this](std_msgs::msg::String::ConstSharedPtr msg) -> void { RCLCPP_INFO(this->get_logger(), "I heard: [%s]", msg->data.c_str()); }; sub_ = create_subscription<std_msgs::msg::String>("chatter", rclcpp::SensorDataQoS(), callback); } private: rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub_; }; } // namespace demo_nodes_cpp RCLCPP_COMPONENTS_REGISTER_NODE(demo_nodes_cpp::ListenerBestEffort)
边栏推荐
猜你喜欢

"No title"

How does EasyCVR call the double-speed playback of device recording through the interface?

群友求助,一周没有搞定的需求,3分钟就解决了?

Develop those things: How to obtain the traffic statistics of the monitoring site through the EasyCVR platform?

网站设计师:Nicepage 4.15 Crack By Xacker

Documentary on Security Reinforcement of Network Range Monitoring System (1)—SSL/TLS Encrypted Transmission of Log Data

DHCP&OSPF组合实验演示(Huawei路由交换设备配置)

关于使用腾讯云HiFlow场景连接器每天提醒签到打卡

12. SAP ABAP OData 服务如何支持 $select 有选择性地仅读取部分模型字段值

什么是网站监控,网站监控软件有什么用?
随机推荐
Iptables防火墙基础知识介绍
袋鼠云思枢:数驹DTengine,助力企业构建高效的流批一体数据湖计算平台
通配符SSL证书不支持多域名吗?
golang安装和基础配置
八猴渲染器是什么?它能干什么?八猴软件的界面讲解
Homework 8.3 Thread Synchronization Mutex Condition Variables
Usage of collect_list in Scala105-Spark.sql
【填空题】130道面试填空题
FE01_OneHot-Scala应用
EuROC dataset format and related codes
当前最快的实例分割模型:YOLACT 和 YOLACT++
buuctf(探险1)
How does the intelligent video surveillance platform EasyCVR use the interface to export iframe addresses in batches?
群友求助,一周没有搞定的需求,3分钟就解决了?
C#爬虫之通过Selenium获取浏览器请求响应结果
用Excel绘制统计图
动态数组底层是如何实现的
Babbitt | Metaverse daily must-read: Weibo animation will recruit all kinds of virtual idols around the world and provide support for them...
什么是网站监控,网站监控软件有什么用?
Enterprise survey correlation analysis case