Recently I started learning ROS2, but I've encountered one issue, I've created a package & defined a node.
#! /usr/bin/env pythonimport rospyrospy.init_node("simple_node")rate = rospy.Rate(2) # We create a Rate object of 2Hzwhile not rospy.is_shutdown(): # Endless loop until Ctrl + Cprint("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
So, I need to convert above ROS1 code for ROS2, for that I replaced ROSPY library with RCLPY and coded it as below:
import rclpydef main(args=None):rclpy.init()myfirstnode = rclpy.create_node('simple_node')print("Help me body, you are my only hope")if __name__ == '__main__':main()
Now, I want to implement below-given code snippet using RCLPY but I'm not able to get all the functions required, I've got the RCLPY substitute of rospy.Rate(2)
, it is rclpy.create_node('simple_node').create_rate(2)
.
while not rospy.is_shutdown():print("Help me body, you are my only hope")rate.sleep()
Please suggest RCLPY substitutes of the functions rospy.is_shutdown()
and rospy.Rate(2).sleep()
Best Answer
You can create a Rate object from your Node:
self._loop_rate = self.create_rate(loop_rate, self.get_clock())
See:https://github.com/ros2/rclpy/blob/master/rclpy/rclpy/node.py
Then within a loop you can call:
self._loop_rate.sleep()
See:https://github.com/ros2/rclpy/blob/master/rclpy/rclpy/timer.py