Here is the program I have written. The function is to make the turtlesim complete one revolution, with a fixed angular velocity and speed.
#!/usr/bin/env python import rospy from geometry_msgs.msg import Twist from turtlesim.msg import Pose from math import * def pose_callback(msg): print(msg.x,msg.y,msg.theta) return def main(): speed = 10 angular_vel= 5 rospy.init_node('turtle_revolve', anonymous=True) rate= rospy.Rate(10) velocity_publisher = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10) vel_msg = Twist() rospy.Subscriber("/turtle1/pose", Pose, pose_callback) posi = Pose() #Setting the time of the start of the node to be t1 t1= rospy.Time.now().to_sec() #Initial positions x and y x = posi.x y= posi.y vel_msg.linear.x = speed vel_msg.angular.z = angular_vel rospy.logwarn(posi.x) rospy.logwarn(posi.y) while not rospy.is_shutdown(): velocity_publisher.publish(vel_msg) #Another time measurement t2 is taken for every iteration t2 = rospy.Time.now().to_sec() # difference in time measurements t1 and t2 t_diff = t2 - t1 #If the time delay between measurements is greater than 0.5 secs and theta has reached 0 , #it means that the circle has been completed if (t_diff >0.5 and posi.theta == 0.0 ): break rospy.logwarn("Completing circle") if __name__ == '__main__': try: main() except rospy.ROSInterruptException: pass
Now my reasoning here is that theta will become zero again when it reaches the initial position and x, y should be equal to the initial values. I tried to find out x, y with posi = Pose () , after subscribing to turtle1/Pose . However all values in posi, i.e x,y, theta etc are zero. Ive confirmed this with multiple print/logwarn statements that i removed. It seems posi is returning zero values continuosly.
However I have confirmed using rostopic in the terminal that the Pose topic is printing values. Furthermore the pose values are correctly being displayed by the pose_callback function. So the zero values are only in the main function. Could anyone point out anything wrong in the code ? Why does posi have all values zero all the time ?