Ask Your Question
0

how do i run straight line command over again in turtlesim straight line tutorial using python

asked 2017-04-09 12:47:15 -0500

SupermanPrime01 gravatar image

updated 2017-04-09 12:48:41 -0500

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
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-04-10 01:23:53 -0500

updated 2017-04-10 01:24:59 -0500

A simple working solution is the following:

#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
import time

def move(velocity_publisher):
    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

    #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__':
    # Starts a new node
    rospy.init_node('robot_cleaner', anonymous=True)
    velocity_publisher = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
    try:
     timeout =  time.time() + 60
     while time.time() < timeout:
         move(velocity_publisher)

    except rospy.ROSInterruptException:
     pass

As of why your implementation is not working, you use while not rospy.is_shutdown(): inside the move function and you are preventing it from returning, since this while loop will stop only when the node is killed. Also notice that the node and publisher definitions are in main and not in the move function. We do not want to define those everytime we call the move function.

edit flag offensive delete link more

Comments

Thank you for providing the code. When people explain it to me, I have trouble visualizing it in my mind. Thankfully, you've provided the code and I can match the code with your description.

SupermanPrime01 gravatar image SupermanPrime01  ( 2017-04-10 14:08:54 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-04-09 12:47:15 -0500

Seen: 761 times

Last updated: Apr 10 '17