定频率发布信息1
通过定时器实现
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class Simple_Node(Node):
def __init__(self):
super().__init__('simple_node')
# Create a timer that will gate the node actions twice a second
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.node_callback)
#
def node_callback(self):
self.get_logger().info('simple_node is alive')
def main(args=None):
rclpy.init(args=args)
my_node = Simple_Node() # instantiate my simple_node
rclpy.spin(my_node) # execute simple_node
# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
my_node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
CPP实现
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
class SimpleNode : public rclcpp::Node {
public:
SimpleNode() : Node("simple_node"), count_(0) {
// 创建发布者
publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
// 创建定时器,每秒触发一次
timer_ = this->create_wall_timer(std::chrono::seconds(1),
std::bind(&SimpleNode::timer_callback, this));
}
private:
void timer_callback() {