0
$\begingroup$

Rosanswers logo

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

Originally posted by SupermanPrime01 on ROS Answers with karma: 27 on 2017-04-09

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by gstavrinos with karma: 641 on 2017-04-10

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by SupermanPrime01 on 2017-04-10:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.