how do i run straight line command over again in turtlesim straight line tutorial using python
Hello,
I've gone through the turtlesim movement tutorials and they've helped a lot but I'm unable to get the script to continuously ask me for inputs for 1 minute and execute the movements. I've tried to use a while loop but it doesn't seem to work. When I Ctrl-C the terminal running the script it'll continuously ask me for inputs but the turtle doesn't move. How would I change the python code to have it asks me for inputs more than once and move the turtle multiple times without having to rerun the script?
The code I used is shown below:
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
import time
def move():
# Starts a new node
rospy.init_node('robot_cleaner', anonymous=True)
velocity_publisher = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
vel_msg = Twist()
#Receiveing the user's input
print("Let's move your robot")
speed = input("Input your speed:")
distance = input("Type your distance:")
isForward = input("Foward?: ")#True or False
#Checking if the movement is forward or backwards
if(isForward):
vel_msg.linear.x = abs(speed)
else:
vel_msg.linear.x = -abs(speed)
#Since we are moving just in x-axis
vel_msg.linear.y = 0
vel_msg.linear.z = 0
vel_msg.angular.x = 0
vel_msg.angular.y = 0
vel_msg.angular.z = 0
while not rospy.is_shutdown():
#Setting the current time for distance calculus
t0 = rospy.Time.now().to_sec()
current_distance = 0
#Loop to move the turtle in an specified distance
while(current_distance < distance):
#Publish the velocity
velocity_publisher.publish(vel_msg)
#Takes actual time to velocity calculus
t1=rospy.Time.now().to_sec()
#Calculates distancePoseStamped
current_distance= speed*(t1-t0)
#After the loop, stops the robot
vel_msg.linear.x = 0
#Force the robot to stop
velocity_publisher.publish(vel_msg)
if __name__ == '__main__':
try:
timeout = time.time() + 60
while time.time() < timeout:
move()
except rospy.ROSInterruptException:
pass