ROS2的RCLPY库中的速率和睡眠函数

2024-09-27 07:22:07 发布

您现在位置:Python中文网/ 问答频道 /正文

最近我开始学习ROS2,但我遇到了一个问题,我创建了一个包&;定义了一个节点

#! /usr/bin/env python
import rospy
rospy.init_node("simple_node")
rate = rospy.Rate(2) # We create a Rate object of 2Hz
while not rospy.is_shutdown(): # Endless loop until Ctrl + C
    print("Help me body, you are my only hope")
    rate.sleep()
# We sleep the needed time to maintain the Rate fixed above
# This program creates an endless loop that repeats itself 2 times per second (2Hz) 
# until somebody presses Ctrl + C in the Shell

因此,我需要将上面的ROS1代码转换为ROS2,为此,我用RCLPY替换了ROSPY库,并将其编码如下:

import rclpy
def main(args=None):
    rclpy.init()
    myfirstnode = rclpy.create_node('simple_node')
    print("Help me body, you are my only hope")

if __name__ == '__main__':
    main()

现在,我想使用RCLPY实现下面给定的代码段,但我无法获得所需的所有函数,我得到了rospy.Rate(2)的RCLPY替换,它是rclpy.create_node('simple_node').create_rate(2)

while not rospy.is_shutdown():
    print("Help me body, you are my only hope")
    rate.sleep()

请推荐rospy.is_shutdown()rospy.Rate(2).sleep()函数的RCLPY替代品


Tags: noderateiscreatehelpbodysleepsimple

热门问题