ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
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.
2 | No.2 Revision |
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.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.