Saturday, October 27, 2018

ROSPy node templates

Spin and Rate

spin() and rate() are resposible to handle communication events, e.g. arriving messages. If you are subscribing messages, services or actions you must call them to process the events.

Using rospy.rate

  • rospy rate
#! /usr/bin/env python import rospy from datetime import datetime NODE_NAME = "reading_laser" def my_shutdown(): rospy.loginfo("Shutdown node") def main(): rospy.init_node(NODE_NAME) rospy.loginfo("Init node: {}".format(NODE_NAME)) rospy.on_shutdown(my_shutdown) rate = rospy.Rate(1) while not rospy.is_shutdown(): rate.sleep() rospy.loginfo(datetime.now().strftime("%b %d %Y %H:%M:%S")) if __name__ == "__main__": main()
  • Loop run every one second (rate 1 Hz)
  • Run Node (don't forget to chmod)
[INFO] [1540546445.840602]: Init node: reading_laser [INFO] [1540546446.842748]: Oct 26 2018 12:34:06 [INFO] [1540546447.842572]: Oct 26 2018 12:34:07 [INFO] [1540546448.842655]: Oct 26 2018 12:34:08

Using rospy.spin

  • main thread wait for shutdown (ctrl-c)
  • Other event are invoked
#! /usr/bin/env python import rospy from datetime import datetime NODE_NAME = "reading_laser" def my_shutdown(): rospy.loginfo("Shutdown node") def main(): rospy.init_node(NODE_NAME) rospy.loginfo("Init node: {}".format(NODE_NAME)) rospy.on_shutdown(my_shutdown) while not rospy.is_shutdown(): rospy.spin() if __name__ == "__main__": main()

No comments: