当前位置:网站首页>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)
边栏推荐
猜你喜欢
MMDetection 使用示例:从入门到出门
"No title"
ptables基本语法使用规则
Investigation and Research Based on the Involution Behavior of College Students
防火墙基础之防火墙做出口设备安全防护
CPU突然飙高系统反应慢,是怎么导致的?有什么办法排查?
EuROC dataset format and related codes
合宙Cat1 4G模块Air724UG配置RNDIS网卡或PPP拨号,通过RNDIS网卡使开发板上网(以RV1126/1109开发板为例)
开发那些事儿:如何通过EasyCVR平台获取监控现场的人流量统计数据?
在表格数据集上训练变分自编码器 (VAE)示例
随机推荐
After EasyCVR is locally connected to the national standard device to map the public network, the local device cannot play and cascade the solution
"No title"
容器化 | 在 NFS 备份恢复 RadonDB MySQL 集群数据
Error when using sourcemap for reporting an error: Can‘t resolve original location of error.
GBase8s存储过程
MySQL安装教程(详细)
EuROC 数据集格式及相关代码
关于使用腾讯云HiFlow场景连接器每天提醒签到打卡
【web自动化测试】playwright安装失败怎么办
天呐,七夕我收到9份告白~
Babbitt | Metaverse daily must-read: Weibo animation will recruit all kinds of virtual idols around the world and provide support for them...
PHP代码审计9—代码执行漏洞
EasyCVR如何通过接口调用设备录像的倍速回放?
Develop those things: How to obtain the traffic statistics of the monitoring site through the EasyCVR platform?
Alibaba Cloud International Edition uses ROS to build WordPress tutorial
gbase8s创建RANGE分片表
golang安装和基础配置
基于激励的需求响应计划下弹性微电网的短期可靠性和经济性评估(Matlab代码实现)
C#爬虫之通过Selenium获取浏览器请求响应结果
Hezhou Cat1 4G module Air724UG is configured with RNDIS network card or PPP dial-up, and the development board is connected to the Internet through the RNDIS network card (taking the RV1126/1109 devel