Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # A simple ROS publisher node in Python
  3.  
  4. import rospy
  5. from std_msgs.msg import String
  6. from geometry_msgs.msg import Twist
  7.  
  8. class Publisher:
  9.    
  10.     def __init__(self):
  11.         self.pub = rospy.Publisher('chatter', String, queue_size=10)
  12.         rospy.init_node('publisher_node', anonymous=True)
  13.         self.rate = rospy.Rate(10) # hz
  14.                
  15.         self.ctrl_c = False
  16.         rospy.on_shutdown(self.shutdownhook)
  17.        
  18.         rospy.loginfo("publisher node is active...")
  19.  
  20.     def shutdownhook(self):
  21.         self.shutdown_function()
  22.         self.ctrl_c = True
  23.  
  24.     def shutdown_function(self):
  25.         print("stopping publisher node at: {}".format(rospy.get_time()))
  26.  
  27.     def main_loop(self):
  28.         while not self.ctrl_c:
  29.            
  30.             pub = rospy.Publisher("topic_name", Twist, queue_size=10)
  31.             vel_cmd = Twist()
  32.             publisher_message = "rospy time is: {}".format(rospy.get_time())
  33.             vel_cmd.linear.x = 5.0
  34.             vel_cmd.angular.z = 5.0
  35.             self.pub.publish(publisher_message)
  36.             self.rate.sleep()
  37.  
  38. if __name__ == '__main__':
  39.     publisher_instance = Publisher()
  40.     try:
  41.         publisher_instance.main_loop()
  42.     except rospy.ROSInterruptException:
  43.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement