ros2订阅esp32发布的电池电压数据

网友投稿 905 2022-08-30

ros2订阅esp32发布的电池电压数据

ros2订阅esp32发布的电池电压数据

一个最简单的订阅和发布案例如下:

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("/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 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("simple_subscriber"); auto sub = node->create_subscription("/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("/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 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("simple_subscriber"); auto sub = node->create_subscription("/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 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("battery_sub"); auto sub = node->create_subscription("/robot_battery", 1, TopicCallback); rclcpp::spin(node); rclcpp::shutdown(); return 0;}

还遇到一些小问题^_^

#include #include #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("chatter", rclcpp::SensorDataQoS(), callback); } private: rclcpp::Subscription::SharedPtr sub_; }; } // namespace demo_nodes_cpp RCLCPP_COMPONENTS_REGISTER_NODE(demo_nodes_cpp::ListenerBestEffort)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:codeforces 1003d Coins and Queries
下一篇:TIOBE 发布 2020 年 3 月编程语言排行榜,Go 语言的表现令人惊叹!(tiobe怎么读)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~