Move the real TurtleBot

There are a number of ways to move the TurtleBot using ROS. In this section, we present the following three methods:

  • Using the keyboard
  • Using ROS terminal window commands
  • Using a Python script

Using keyboard teleoperation to move TurtleBot

In a new terminal window, launch the TurtleBot keyboard teleop program on the remote computer:

$ roslaunch turtlebot_teleop keyboard_teleop.launch

The output is as follows:

... logging to /home/turtlebot/.ros/log/b662ab4a-c22e-11e5-b730-6c71d9a711bd/roslaunch-turtlebot-0428-26633.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.
started roslaunch server http://192.168.11.123:58861/
SUMMARY
========
PARAMETERS
 * /rosdistro: indigo
 * /rosversion: 1.11.16
 * /turtlebot_teleop_keyboard/scale_angular: 1.5
 * /turtlebot_teleop_keyboard/scale_linear: 0.5
NODES
  /
    turtlebot_teleop_keyboard (turtlebot_teleop/turtlebot_teleop_key)
ROS_MASTER_URI=http://192.168.11.123:11311
core service [/rosout] found
process[turtlebot_teleop_keyboard-1]: started with pid [26653]
Control Your Turtlebot!
---------------------------
Moving around:
   u    i    o
   j    k    l
   m    ,    .
q/z : increase/decrease max speeds by 10%
w/x : increase/decrease only linear speed by 10%
e/c : increase/decrease only angular speed by 10%
space key, k : force stop
anything else : stop smoothly
CTRL-C to quit
currently:  speed 0.2  turn 1

We have left the entire output from the launch because it is useful to know the parameters and nodes involved when a package is launched. Make sure that the TurtleBot base and the netbook batteries are sufficiently charged so you can move the TurtleBot around to become familiar with its capabilities for straight-line motion and rotation. Now try the i key to move the TurtleBot forward or the , key to drive backward. The speed is 0.2 meters/second.

Using ROS commands to move TurtleBot around

There are a number of ways to control the TurtleBot movement other than using the keyboard. There are several ROS commands that are useful to move and monitor the TurtleBot in motion:

  • rostopic pub is used to publish commands to move the TurtleBot
  • rostopic echo is used to display the messages sent

After the TurtleBot has been brought up with the minimal launch command, the rostopic pub command can be used to move and turn the TurtleBot. To move the TurtleBot forward, issue this command from the remote computer:

$ rostopic pub -r 10 /mobile_base/commands/velocity geometry_msgs/Twist '{linear: {x: 0.2}}'

TurtleBot should move forward continuously at 0.2 meters/second until you press Ctrl + C while the focus is on the active window.

This command publishes (pub) the /mobile_base/commands/velocity topic at the rate of 10 times per second. The –r variable indicates that the rate is repeated. To send the message once, use -1 instead of –r.

To move the TurtleBot backward, issue the following command:

$ rostopic pub -r 20 /mobile_base/commands/velocity geometry_msgs/Twist '{linear: {x: -0.2}}'

Always press Ctrl + C to stop TurtleBot.

To cause the robot to turn in a circle requires some forward velocity and angular velocity, which the following command shows:

$ rostopic pub -r 10 /mobile_base/commands/velocity geometry_msgs/Twist '{linear: {x: 0.2}, angular: {x: 0, y: 0, z: 1.0}}'

The linear speed is 0.2 meters/second and the rotation is 1.0 radian (about 57 degrees) per second.

To view the messages sent, type the following command in a separate terminal window:

$ rostopic echo /mobile_base/commands/velocity

The output is as follows:

  linear:
  x: 0.2
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 1.0
---

The message repeats the linear velocity and angular rotation values being sent 10 times a second. Use Ctrl + C to stop the display.

Writing your first Python script to control TurtleBot

We will present a simple Python script to move the TurtleBot in this section. The basic approach to create a script begins with a design. The design should detail the activity to be accomplished. For example, a script can command TurtleBot to move straight ahead, make several turns, and then stop. The next step is to determine the commands for TurtleBot to accomplish the tasks. Finally, a script is written and tested to see whether TurtleBot responds in the expected way. The remote computer will execute the Python script and TurtleBot will move as directed if the script is correctly written.

In terms of the TurtleBot commands that will be used, we can summarize the process as follows:

  • Design the program outlining the activities of TurtleBot when the script is executed
  • Determine the nodes, topics, and messages to be sent (published) or received (subscribed) from the TurtleBot during the activity
  • Study the ROS Python tutorials and examples to determine the way to write Python statements that send or receive messages between the remote computer and the TurtleBot

There is a great deal of documentation describing ROS Python scripts. The statement structure is fixed for many operations. The site http://wiki.ros.org/rospy briefly describes rospy, which is called the ROS client library for Python. The purpose is to allow statements written in Python language to interface with ROS topics and services.

The site http://wiki.ros.org/rospy_tutorials contains a list of tutorials. At the top of the tutorial page, there is a choice of distributions of ROS, and Indigo is chosen for our discussions. A specific tutorial that describes many Python statements that are used in a typical script can be found at: http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber(python)

To find the nodes that are active after the keyboard_teleop.launch file is launched, type this command:

$ rosnode list

The output is as follows:

/app_manager
/bumper2pointcloud
/capability_server
/capability_server_nodelet_manager
/cmd_vel_mux
/diagnostic_aggregator
/interactions
/master
/mobile_base
/mobile_base_nodelet_manager
/robot_state_publisher
/rosout
/turtlebot_laptop_battery
/turtlebot_teleop_keyboard
/zeroconf/zeroconf

The nodes are described in the Kobuki tutorial that can be found at: http://wiki.ros.org/kobuki/Tutorials/Kobuki's%20Control%20System

According to the site, the mobile_base node listens for commands, such as velocity, and publishes sensor information. The cmd_vel_mux variable serves to multiplex commands to assure that only one velocity command at a time is relayed to the mobile base.

In the previous example, we used the rostopic pub command to publish the linear and angular geometry_msgs/Twist data in order to move the TurtleBot. The Python script that follows will accomplish essentially the same thing. The script will send a Twist message on the cmd_vel_mux/input/navi topic.

A Python script will be created to move the TurtleBot forward in a simple example. If you are not very familiar with Python, it may be best to study and execute the example script and then refer to the ROS tutorials. The procedure to create an executable script on the remote computer is as follows:

  1. Write the script with the required format for a ROS Python script using an ordinary text editor.
  2. Give the script a name in the <name>.py format and save the script.

We have called our script ControlTurtleBot.py and saved it in our home directory.

To make the script executable, execute the Ubuntu command:

$ chmod +x ControlTurtleBot.py

Make sure that the TurtleBot is ready by running the minimal launch. Then, in a new terminal window, type this command:

$ python ControlTurtleBot.py

In our example, Ctrl + C is used to stop the TurtleBot. The comments in the script explain the statements. The tutorials listed previously give further details of Python scripts written using the ROS conventions:

#!/usr/bin/env python
# Execute as a python script  
# Set linear and angular values of TurtleBot's speed and turning.
import rospy      # Needed to create a ROS node
from geometry_msgs.msg import Twist    # Message that moves base

class ControlTurtleBot():
  def __init__(self):
    # ControlTurtleBot is the name of the node sent to the master
    rospy.init_node('ControlTurtleBot', anonymous=False)

    # Message to screen
    rospy.loginfo(" Press CTRL+c to stop TurtleBot")

    # Keys CNTL + c will stop script   
    rospy.on_shutdown(self.shutdown)
        
    # Publisher will send Twist message on topic 
    # cmd_vel_mux/input/navi
    
    self.cmd_vel = rospy.Publisher('cmd_vel_mux/input/navi', Twist, queue_size=10)
   
    # TurtleBot will receive the message 10 times per second. 
    rate = rospy.Rate(10);   
    # 10 Hz is fine as long as the processing does not exceed
    #   1/10 second.

    # Twist is geometry_msgs for linear and angular velocity
    move_cmd = Twist()
    # Linear speed in x in meters/second is + (forward) or 
    # - (backwards)
    move_cmd.linear.x = 0.3	
    # Modify this value to change speed
    # Turn at 0 radians/s
    move_cmd.angular.z = 0	
    # Modify this value to cause rotation rad/s

    # Loop and TurtleBot will move until you type CNTL+c
    while not rospy.is_shutdown():
    # publish Twist values to TurtleBot node /cmd_vel_mux
    self.cmd_vel.publish(move_cmd)
    # wait for 0.1 seconds (10 HZ) and publish again
    rate.sleep()
    
    
  def shutdown(self):
    # You can stop turtlebot by publishing an empty Twist message 
    rospy.loginfo("Stopping TurtleBot")

    self.cmd_vel.publish(Twist())
    # Give TurtleBot time to stop
    rospy.sleep(1)
 
if __name__ == '__main__':
  try:
    ControlTurtleBot()
  except:
    rospy.loginfo("End of the trip for TurtleBot")
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.61.12