#!/usr/bin/env python # license removed for brevity # # Example program that # moves in a square # T3 version # nbp 2021 # # import math import random import rospy # needed for ROS from geometry_msgs.msg import Twist # ROS Twist message motionTopic='/cmd_vel' # turtlebot vel topic # square_node - version 1 # Moves roughly in a square # def square_node(): '''move roughly in a square''' # all ROS 'nodes' (ie your program) have to do the following rospy.init_node('Square_Node', anonymous=True) #register as a ROS publisher for the velocity topic vel_pub = rospy.Publisher(motionTopic, Twist, queue_size=1) # this is how frequently the loop below iterates rospy.sleep(1) rate = rospy.Rate(0.5) # Hz msg = Twist() # new velocity message msg.linear.x,msg.angular.z=0,0 vel_pub.publish(msg) # stop all motors up front while not rospy.is_shutdown(): msg.linear.x = 0 msg.angular.z = math.pi / 2.0 vel_pub.publish(msg) rospy.sleep(1) msg.linear.x = 0.2 msg.angular.z = 0 vel_pub.publish(msg) rospy.sleep(3) return # # This function is called by ROS when you stop ROS # Here we use it to send a zero velocity to robot # in case it was moving when you stopped ROS # def callback_shutdown(): print("Shutting down") pub = rospy.Publisher(motionTopic, Twist, queue_size=1) msg = Twist() msg.angular.z=0.0 msg.linear.x=0.0 pub.publish(msg) rospy.sleep(5) return #-------------------------------MAIN program---------------------- if __name__ == '__main__': try: rospy.on_shutdown(callback_shutdown) square_node() except rospy.ROSInterruptException: pass