Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # A simple ROS publisher node in Python
- import rospy
- from std_msgs.msg import String
- from geometry_msgs.msg import Twist
- class Publisher:
- def __init__(self):
- self.pub = rospy.Publisher('chatter', String, queue_size=10)
- rospy.init_node('publisher_node', anonymous=True)
- self.rate = rospy.Rate(10) # hz
- self.ctrl_c = False
- rospy.on_shutdown(self.shutdownhook)
- rospy.loginfo("publisher node is active...")
- def shutdownhook(self):
- self.shutdown_function()
- self.ctrl_c = True
- def shutdown_function(self):
- print("stopping publisher node at: {}".format(rospy.get_time()))
- def main_loop(self):
- while not self.ctrl_c:
- pub = rospy.Publisher("topic_name", Twist, queue_size=10)
- vel_cmd = Twist()
- publisher_message = "rospy time is: {}".format(rospy.get_time())
- vel_cmd.linear.x = 5.0
- vel_cmd.angular.z = 5.0
- self.pub.publish(publisher_message)
- self.rate.sleep()
- if __name__ == '__main__':
- publisher_instance = Publisher()
- try:
- publisher_instance.main_loop()
- except rospy.ROSInterruptException:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement