Most of the time it relies on ROS nodes to publish appropriate twist messages. As a simple example, let's say we want to program the robot to move forward 1m, rotate 180 degrees, and then return to the start position. we will try to accomplish this task in a different way, which is a good representation of the different levels of Ros movement control .
1. Estimate distance and angle by timing and constant speed
Our first attempt was to use timed twist commands to let the robot take a certain amount of time to move forward a certain distance, rotated 180 degrees, and then at the same speed as the same length of the forward movement, not surprisingly it will return to the beginning of the position. Finally, we'll let the robot rotate 180 degrees back to its original direction.
This script can be found in the timed_out_and_back.py under subdirectory rbx1_nav/nodes.
2. Timing forward and back to motion on the Arbotix simulator
To ensure that the simulated turtlebot back to the beginning, press "Ctrl-c" to stop the running startup file in the simulated Turtlebot, and then run it again with the following command:
Roslaunch Rbx1_bringup Fake_turtlebot.launch
If you want, you can replace Fake_turtlebot.launch with a file that corresponds to pi robot or your own robot, which doesn't make a difference.
If the Rviz is not running, then let it start running:
find Rbx1_nav '/sim.rviz
or press the reset button to remove the odometry arrow left in the previous section.
Finally run the timed_out_and_back.py node:
Rosrun Rbx1_nav timed_out_and_back.py
3. Timed forward and return to motion script
The following is a complete script that ticks forward and returns a node.
#!/usr/bin/Envpythonimport rospyfrom geometry_msgs.msg import twistfrom Math import Piclass outandback (): Def __init__ (self): # Give the Node A name Rospy.init_node ('Out_and_back', anonymous=False) # Set Rospy to execute a shutdownfunctionWhen exiting Rospy.on_shutdown (self.shutdown) # Publisher to control the robot's speedSelf.cmd_vel = Rospy. Publisher ('/cmd_vel', Twist, queue_size=1# How fast would we update the robot's movement?Rate = -# Set The equivalent ROS rate variable R=Rospy. Rate # Set The forward linear0.2meters per second linear_speed=0.2# Set the travel distance to1.0meters Goal_distance=1.0# HowLongShould it take the US to get there?linear_duration= Goal_distance/Linear_speed # Set the rotation speed to1.0radians per second angular_speed=1.0# Set The rotation angle to Pi radians ( thedegrees) Goal_angle=Pi # howLongShould it take to rotate?angular_duration= Goal_angle/Angular_speed # Loop through the legs of the trip forIinchRange2): # Initialize the movement command Move_cmd=Twist () # Set The forward speed move_cmd.linear.x=Linear_speed # Move forward forA TimeTo go the desired distance ticks=int(Linear_duration *Rate ) forTinchRange (Ticks): Self.cmd_vel.publish (Move_cmd) R.Sleep() # Stop the robot before the rotation move_cmd=Twist () self.cmd_vel.publish (Move_cmd) rospy.Sleep(1) # Now rotate left roughly thedegrees # Set the angular speed move_cmd.angular.z=Angular_speed # Rotate forA TimeTo go thedegrees Ticks=int(Goal_angle *Rate ) forTinchRange (Ticks): Self.cmd_vel.publish (Move_cmd) R.Sleep() # Stop the robot before the next leg move_cmd=Twist () self.cmd_vel.publish (Move_cmd) rospy.Sleep(1) # Stop the Robot Self.cmd_vel.publish (Twist ()) def shutdown (self): # Always stop the robot when shutting down the node. Rospy.loginfo ("stopping the robot ...") Self.cmd_vel.publish (Twist ()) Rospy.Sleep(1) if__name__ = ='__main__': Try:outandback () Except:rospy.loginfo ("out-and-back node terminated.")View Code
---control mobile Dock 5