ros2 底盘控制程序实践

定频率发布信息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() {
   
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值