当前位置:网站首页>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)
边栏推荐
猜你喜欢
”元宇宙“必须具备这些特点
npm配置国内镜像(淘宝镜像)
机器学习——线性回归
Investigation and Research Based on the Involution Behavior of College Students
How does the intelligent video surveillance platform EasyCVR use the interface to export iframe addresses in batches?
Win10只读文件夹怎么删除
LVS负载均衡群集之原理叙述
开发那些事儿:如何通过EasyCVR平台获取监控现场的人流量统计数据?
12. SAP ABAP OData 服务如何支持 $select 有选择性地仅读取部分模型字段值
巴比特 | 元宇宙每日必读:微博动漫将招募全球各类虚拟偶像并为其提供扶持...
随机推荐
leetcode/有效的回文串,含有不需要判断回文的字符
袋鼠云思枢:数驹DTengine,助力企业构建高效的流批一体数据湖计算平台
July 31, 2022 Summary of the third week of summer vacation
22/8/4 记忆化搜索+博弈论
智能视频监控平台EasyCVR如何使用接口批量导出iframe地址?
Enterprise survey correlation analysis case
单行、多行文本超出显示省略号
limux入门3—磁盘与分区管理
实验室专利书写指南
PHP代码审计10—命令执行漏洞
容器化 | 在 NFS 备份恢复 RadonDB MySQL 集群数据
buuctf(探险1)
【web自动化测试】Playwright快速入门,5分钟上手
Develop those things: How to obtain the traffic statistics of the monitoring site through the EasyCVR platform?
网站设计师:Nicepage 4.15 Crack By Xacker
EasyCVR calls the cloud recording API and returns an error and no recording file is generated. What is the reason?
2019年海淀区青少年程序设计挑战活动小学组复赛试题详细答案
”元宇宙“必须具备这些特点
Babbitt | Metaverse daily must-read: Weibo animation will recruit all kinds of virtual idols around the world and provide support for them...
PHP代码审计7—文件上传漏洞