Mastering ROS for Robotics Programming(Second Edition)
上QQ阅读APP看书,第一时间看更新

Adding the ROS teleop node

The ROS teleop node publishes the ROS Twist command by taking keyboard inputs. From this node, we can generate both linear and angular velocity, and there is already a standard teleop node implementation available; we can simply reuse the node.

The teleop is implemented in the diff_wheeled_robot_control package. The script folder contains the diff_wheeled_robot_key node, which is the teleop node. As per usual, you can get this package from the code provided with the book or download it from the following link:

$ git clone  https://github.com/jocacace/diff_wheeled_robot_control.git  

To successfully compile and use this package, you may need to install the joy_node package:

$ sudo apt-get install ros-kinetic-joy

Here is the launch file called keyboard_teleop.launch to start the teleop node:

 <launch> 
  <!-- differential_teleop_key already has its own built in velocity smoother --> 
  <node pkg="diff_wheeled_robot_control" type="diff_wheeled_robot_key" name="diff_wheeled_robot_key"  output="screen"> 
 
    <param name="scale_linear" value="0.5" type="double"/> 
    <param name="scale_angular" value="1.5" type="double"/> 
    <remap from="turtlebot_teleop_keyboard/cmd_vel" to="/cmd_vel"/>  
  </node> 
</launch> 

Let's start moving the robot.

Launch Gazebo with complete simulation settings, using the following command:

$ roslaunch diff_wheeled_robot_gazebo diff_wheeled_gazebo_full.launch  

Start the teleop node:

$ roslaunch diff_wheeled_robot_control keyboard_teleop.launch  

Start RViz to visualize the robot state and laser data:

$ rosrun rviz rviz  

Add Fixed Frame : /odom, add Laser Scan , and the topic as /scan to view the laser scan data, and add the Robot model to view the robot model.

In the teleop terminal, we can use some keys (U, I, O, J, K, L, M, ",", ".") for direction adjustment and other keys (q, z, w, x, e, c, k, space key) for speed adjustments. Here is the screenshot showing the robot moving in Gazebo using the teleop and its visualization in RViz.

We can add primitive shapes from the Gazebo toolbar to the robot environment or we can add objects from the online library, which is on the left-side panel:

Figure 13: Moving differential drive robot in Gazebo using teleoperation

The robot will only move when we press the appropriate key inside the teleop node terminal. If this terminal is not active, pressing the key will not move the robot. If everything works well, we can explore the area using the robot and visualizing the laser data in RViz.