0

最近我开始学习 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 实现下面给出的代码片段,但我无法获得所需的所有功能,我有 RCLPY 替代品rospy.Rate(2),它是rclpy.create_node('simple_node').create_rate(2).

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

请建议功能的 RCLPY 替代品rospy.is_shutdown()rospy.Rate(2).sleep()

4

1 回答 1

0

你可以从你的节点创建一个 Rate 对象:

self._loop_rate = self.create_rate(loop_rate, self.get_clock())

见: https ://github.com/ros2/rclpy/blob/master/rclpy/rclpy/node.py

然后在一个循环中,您可以调用:

self._loop_rate.sleep()

见: https ://github.com/ros2/rclpy/blob/master/rclpy/rclpy/timer.py

于 2020-10-08T18:07:14.813 回答