最近我开始学习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替代品
可以从节点创建速率对象:
见: https://github.com/ros2/rclpy/blob/master/rclpy/rclpy/node.py
然后在循环中,您可以调用:
见: https://github.com/ros2/rclpy/blob/master/rclpy/rclpy/timer.py
相关问题 更多 >
编程相关推荐