Mastering ROS for Robotics Programming Second Edition

13
Mastering ROS for Robotics Programming Second Edition Design, build, and simulate complex robots using the Robot Operating System Lentin Joseph Jonathan Cacace - -: \ä#i\ I JT •.<;.-.,:•.•:•,•>>.—..:,: :"'^>&^.ü' f.--*- t&VSfrtä&m -'• W> «SWS! S«i ." BIRMINGHAM - MUMBAI

Transcript of Mastering ROS for Robotics Programming Second Edition

Page 1: Mastering ROS for Robotics Programming Second Edition

Mastering ROS for Robotics Programming Second Edition

Design, build, and simulate complex robots using the Robot Operating System

Lentin Joseph

Jonathan Cacace

- -: \ ä # i \ I JT •.<;.-.,:•.•:•,•>>.—..:,: : " ' ^ > & ^ . ü ' f.--*- t&VSfrtä&m -'• W> «SWS! S«i ."

BIRMINGHAM - MUMBAI

Page 2: Mastering ROS for Robotics Programming Second Edition

Table of Contents Preface i_

Chapter 1: Introduction to ROS 7

Why should we learn ROS? 7

Why we prefer ROS for robots 8

Why some do not prefer ROS for robots 10

Understanding the ROS filesystem level 11

ROS packages 13

ROS metapackages 15 ROS messages 16 The ROS services 18

Understanding the ROS computation graph level 19 ROS nodes 21 ROS messages 22 ROS topics 23 ROS services 24 ROS bags 25 The ROS Master 26 Using the ROS parameter 28

ROS community level 29

What are the prerequisites for starting with ROS? 30 Running the ROS Master and the ROS parameter server 30

Checking the roscore command output 32

Questions 33

Summary 34

Chapter 2: Getting Started with ROS Programming 35

Creating a ROS package 35 Working with ROS topics 38 Creating ROS nodes 38 Building the nodes 41

Adding custom msg and srv files 44

Page 3: Mastering ROS for Robotics Programming Second Edition

Table of Contents

Working with ROS services 47 Working with ROS actionlib 52

Creating the ROS action server 53 Creating the ROS action client 55

Building the ROS action server and client 56

Creating launch files 59

Applications of topics, services, and actionlib 62

Maintaining the ROS package 62

Releasing your ROS package 63

Preparing the ROS package for the release 64

Releasing our package 64

Creating a Wiki page for your ROS package 67

Questions 70

Summary 71

Chapter 3: Working with 3D Robot Modeling in ROS 73

ROS packages for robot modeling 74

Understanding robot modeling using URDF 75

Creating the ROS package for the robot description 79

Creating our first URDF model 79

Explaining the URDF file 82

Visualizing the 3D robot model in RViz 84

Interacting with pan-and-tilt joints 85

Adding physical and collision properties to a URDF model 86

Understanding robot modeling using xacro 87 Using properties 88 Using the math expression 88 Using macros 88

Converting xacro to URDF 89

Creating the robot description for a seven DOF robot manipulator 90 Arm specification 91

Type of joints 91

Explaining the xacro model of the seven DOF arm 92

Using constants 92 Using macros 92

Page 4: Mastering ROS for Robotics Programming Second Edition

Table of Contents

Including other xacro files 93 Using meshes in the link 94 Working with the robot gripper 94 Viewing the seven DOF arm in RViz 95

Understanding joint state publisher 97 Understanding robot state publisher 97

Creating a robot model for the differential drive mobile robot 99

Questions 104

Summary 104

Chapter 4: Simulating Robots Using ROS and Gazebo 105

Simulating the robotic arm using Gazebo and ROS 106

Creating the robotic arm simulation model for Gazebo 107

Adding colors and textures to the Gazebo robot model 110 Adding transmission tags to actuate the model 110 Adding the gazebo_ros_control plugin 111 Adding a 3D vision sensor to Gazebo 111

Simulating the robotic arm with Xtion Pro 113 Visualizing the 3D sensor data 114

Moving robot joints using ROS controllers in Gazebo 116 Understanding the ros_control packages 117 Different types of ROS controllers and hardware interfaces 117 How the ROS controller interacts with Gazebo 118 Interfacing joint state controllers and joint position controllers to the arm 120 Launching the ROS controllers with Gazebo 121 Moving the robot joints 123

Simulating a differential wheeled robot in Gazebo 124 Adding the laser scanner to Gazebo 126 Moving the mobile robot in Gazebo 127 Adding joint state publishers in the launch file 129

Adding the ROS teleop node 129

Questions 131

Summary 132

Chapter 5: Simulating Robots Using ROS and V-REP 133

Setting up V-REP with ROS 134

Understanding the vrep_plugin 138

[ i i i ]

Page 5: Mastering ROS for Robotics Programming Second Edition

Table of Contents

Interacting with V-REP using ROS services 140 Interacting with V-REP using ROS topics 142

Simulating the robotic arm using V-REP and ROS 147 Adding the ROS interface to V-REP joint controllers 150

Simulating a differential wheeled robot in V-REP 154 Adding a laser sensor to V-REP 157 Adding a 3D vision sensor to V-REP 160

Questions 162

Summary 162

Chapter 6: Using the ROS Movelt! and Navigation Stack 163

Installing Movelt! 164

Movelt! architecture 164 The move_group node 165 Motion planning using Movelt! 167 Motion planning request adapters 169 Movelt! planning scene 169 Movelt! kinematics handling 170 Movelt! collision checking 171

Generating Movelt! configuration package using the Setup Assistant tool 172

Step 1 - Launching the Setup Assistant tool 172 Step 2 - Generating the Self-Collision matrix 174 Step 3 - Adding virtual joints 175 Step 4 - Adding planning groups 176 Step 5 - Adding the robot poses 177 Step 6 - Setting up the robot end effector 178 Step 7 - Adding passive joints 179 Step 8 - Author information 179 Step 9 - Generating configuration files 179

Motion planning of robot in RViz using Movelt! configuration package 181

Using the RViz Motion Planning plugin 182 Interfacing the Movelt! configuration package to Gazebo 185

Step 1 - Writing the controller configuration file for Movelt! 186 Step 2 - Creating the controller launch files 187 Step 3 - Creating the controller configuration file for Gazebo 187

[ i v ]

Page 6: Mastering ROS for Robotics Programming Second Edition

Table of Contents

Step 4 - Creating the launch file for Gazebo trajectory controllers 189 Step 5 - Debugging the Gazebo- Movelt! interface 190

Understanding the ROS Navigation stack 192 ROS Navigation hardware requirements 192 Working with Navigation packages 194

Understanding the move_base node 194 Working of Navigation stack 196 Localizing on the map 196 Sending a goal and path planning 196 Collision recovery behavior 196 Sending the command velocity 196

Installing the ROS Navigation stack 197

Building a map using SLAM 197 Creating a launch file for gmapping 197 Running SLAM on the differential drive robot 200 Implementing autonomous navigation using amcl and a static map 204 Creating an amcl launch file 204

Questions 207

Summary 208

Chapter 7: Working with pluginlib, Nodelets, and Gazebo Plugins 209

Understanding pluginlib 210 Creating plugins for the calculator application using pluginlib 210

Working with the pluginlib_calculator package 212 Step 1 - Creating the calculator_base header file 212 Step 2 - Creating the calculator_plugins header file 213 Step 3 - Exporting plugins using the calculator_plugins.cpp 214 Step 4 - Implementing the plugin loader using the calculatorjoader.cpp 214 Step 5 - Creating the plugin description file: calculator_plugins.xml 215 Step 6 - Registering the plugin with the ROS package system 216 Step 7 - Editing the CMakeLists.txt file 216 Step 8 - Querying the list of plugins in a package 216 Step 9 - Running the plugin loader 217

Understanding ROS nodelets 217

Creating a nodelet 218 Step 1 - Creating a package for a nodelet 218 Step 2 - Creating the hello_world.cpp nodelet 218 Step 3 - Explanation of hello_world.cpp 218

Page 7: Mastering ROS for Robotics Programming Second Edition

Table of Contents

Step 4 - Creating the plugin description file 220 Step 5 - Adding the export tag in package.xml 220 Step 6 - Editing CMakeLists.txt 220 Step 7 - Building and running nodelets 221 Step 8 - Creating launch files for nodelets 223

Understanding the Gazebo plugins 225 Creating a basic world plugin 225

Quest ions 22g Summary 230

Chapter 8: Writ ing ROS Controllers and Visualization Plugins 231 Understanding ros_control packages 233

The controllerjnterface package 233 Initializating the controller 234 Starting the ROS controller 234 Updating the ROS controller 235 Stopping the controller 235

The controller_manager 235 Writ ing a basic joint controller in ROS 236

Step 1 - Creating the controller package 236 Step 2 - Creating the controller header file 237 Step 3 - Creating the controller source file 237 Step 4 - Explaining the controller source file 238 Step 5 - Creating the plugin description file 240 Step 6 - Updating package.xml 240 Step 7 - Updating CMakeLists.txt 240 Step 8 - Building the controller 240 Step 9 - Writing the controller configuration file 241 Step 10 - Writing the launch file for the controller 241 Step 11 - Running the controller along with the seven dof arm in Gazebo 242

Understanding the ROS visualization tool (RViz) and its plugins 244 Displays panel 246 RViz toolbar 246 Views 246 Time panel 246 Dockable panels 246

Writ ing an RViz plugin for teleoperation 247

[ v i ]

Page 8: Mastering ROS for Robotics Programming Second Edition

Table of Contents

Methodology of building the RViz plugin 247 Step 1 - Creating the RViz plugin package 248 Step 2 - Creating the RViz plugin header file 248 Step 3 - Creating the RViz plugin definition 250 Step 4 - Creating the plugin description file 251 Step 5 - Adding the export tags in package.xml 251 Step 6 - Editing CMakeL_ists.txt 252 Step 7 - Building and loading plugins 253

Questions 255

Summary 255

Chapter 9: Interfacing I/O Boards, Sensors, and Actuators to ROS 257

Understanding the Arduino-ROS interface 258

What is the Arduino-ROS interface? 259

Understanding the rosserial package in ROS 260 Installing rosserial packages on Ubuntu 16.04 262

Understanding ROS node APIs in Arduino 267 ROS - Arduino Publisher and Subscriber example 269 Arduino-ROS, example - blink LED and push button 273 Arduino-ROS, example - Accelerometer ADXL 335 276 Arduino-ROS, example - ultrasonic distance sensor 280

Equations to find distance using the ultrasonic range sensor 281 Arduino-ROS example - Odometry Publisher 284

Interfacing non-Arduino boards to ROS 286 Setting ROS on Odroid-XU4 and Raspberry Pi 2 286

How to install an OS image to Odroid-XU4 and Raspberry Pi 2 289 Installation in Windows 289 Installation in Linux 291

Connecting to Odroid-XU4 and Raspberry Pi 2 from a PC 291 Configuring an Ethernet hotspot for Odroid-XU4 and Raspberry Pi 2 292

Installing Wiring Pi on Odroid-XU4 294 Installing Wiring Pi on Raspberry Pi 2 296

Blinking LED using ROS on Raspberry Pi 2 297 Push button + blink LED using ROS on Raspberry Pi 2 299 Running examples in Raspberry Pi 2 302

Interfacing DYNAMIXEL actuators to ROS 304

Questions 304

[v i i ]

Page 9: Mastering ROS for Robotics Programming Second Edition

Table of Contents

S u m m a r y 305

Chapter 10: Programming Vision Sensors Using ROS, Open CV, and PCL 307

Understanding ROS - OpenCV interfacing packages 308 Understanding ROS - PCL interfacing packages 308

Installing ROS perception 309 Interfacing USB webcams in ROS 311 Working with ROS camera calibration 315

Converting images between ROS and OpenCV using cv_bridge 318 Image processing using ROS and OpenCV 319

Step 1 - Creating a ROS package for the experiment 319 Step 2 - Creating source files 319 Step 3 - Explanation of the code 319

Publishing and subscribing images using image_transport 320 Converting OpenCV to ROS images using cv_bridge 321 Finding edges on the image 323 Visualizing raw and edge-detected-images 323

Step 4 - Editing the CMakeLists.txt file 323 Step 5 - Building and running an example 324

Interfacing Kinect and Asus Xtion Pro in ROS 325 Interfacing Intel Real Sense camera with ROS 328

Working with a point cloud to a laser scan package 331 Interfacing Hokuyo Laser in ROS 334 Working with point cloud data 336

How to publish a point cloud 337 How to subscribe and process the point cloud 339 Writing data to a Point Cloud Data (PCD) file 340 Reading and publishing a point cloud from a PCD file 341

Working with AR Marker detection for object pose estimation 344 Using AprilTag with ROS 345

Questions 351 S u m m a r y 351

Chapter 11: Building and Interfacing Differential Drive Mobile Robot Hardware in ROS 353

Introducing to Chefbot - a DIY mobile robot and its hardware configuration 354

[ v i i i ]

Page 10: Mastering ROS for Robotics Programming Second Edition

Table of Contents

Flashing Chefbot firmware using Energia IDE 357 Serial data sending protocol from LaunchPad to PC 359 Serial data sending protocol from PC to Launchpad 359

Discussing Chefbot interface packages on ROS 360 Computing odometry from encoder ticks 367 Computing motor velocities from ROS twist message 369

Running the robot standalone launch file using C++ nodes 371 Configuring the Navigation stack for Chefbot 371 Configuring the gmapping node 371 Configuring the Navigation stack packages 373

Common configuration local_costmap and global_costmap 374 Configuring global costmap parameters 375 Configuring local costmap parameters 376 Configuring base local planner parameters 377 Configuring DWA local planner parameters 378 Configuring move_base node parameters 378

Understanding AMCL 381 Understanding RViz for working with the Navigation stack 384

2D Pose Estimate button 384 Visualizing the particle cloud 386 The 2D Nav Goal button 387 Displaying the static map 388 Displaying the robot footprint 389 Displaying the global and local cost map 390 Displaying the global plan, the local plan, and the planner plan 391 The current goal 392

Obstacle avoidance using the Navigation stack 392 Working with Chefbot simulation 394

Building a room in Gazebo 394 Adding model files to the Gazebo model folder 395

Sending a goal to the Navigation stack from a ROS node 397

Questions 400

Summary 400

Chapter 12: Exploring the Advanced Capabilities of ROS-Movelt! 401

Motion planning using the move_group C++ interface 402

Motion planning a random path using Movelt! C++ APIs 402

Motion planning a custom path using Movelt! C++ APIs 405

[ ix]

Page 11: Mastering ROS for Robotics Programming Second Edition

Table of Contents

Collision checking with a robot arm using Movelt! 407 Adding a collision object to Movelt! 407 Removing a collision object from the planning scene 412 Attaching a collision object to a robot link 412 Checking self-collisions using Movelt! APIs 413

Working with perception using Movelt! and Gazebo 415 Manipulating objects with Movelt! 424

Working with a robot pick-and-place task using Movelt! 424 Calculating grasp poses with GPD 429 Pick and place action in Gazebo and real robot 432

Understanding DYNAMIXEL ROS Servo controllers for robot hardware interfacing 433

The DYNAMIXEL servos 433 DYNAMIXEL-ROS interface 434

Interfacing 7 DOF DYNAMIXEL-based robotic arm to ROS Movelt! 435 Creating a controller package for a COOL arm robot 436 Movelt! configuration of the COOL Arm 440

Questions 442 Summary 443

Chapter 13: Using ROS in MATLAB and Simulink 445 Getting started with MATLAB and MATLAB-ROS 446

Getting started with the Robotic System Toolbox and ROS-MATLAB interface 447 Starting with ROS topics and MATLAB callback functions 452 Implementing an obstacle avoidance system for a Turtlebot robot 457

Getting started with ROS and Simulink 463 Creating a wave signal integrator in Simulink 463 Working with ROS messages in Simulink 466 Publishing a ROS message in Simulink 466 Subscribing to a ROS topic in Simulink 471

Developing a simple control system in Simulink 474 Configuring the Simulink model 476

Questions 47g Summary 47g

Chapter 14: ROS for Industrial Robots 481

Page 12: Mastering ROS for Robotics Programming Second Edition

Table of Contents

Understanding ROS-lndustrial packages 482

Goals of ROS-lndustrial 482

ROS-lndustrial - a brief history 483

Benefits of ROS-lndustrial 483

Installing ROS-lndustrial packages 483

Block diagram of ROS-lndustrial packages 484

Creating a URDF for an industrial robot 486

Creating the Moveit! configuration for an industrial robot 487

Updating the Moveit! configuration files 491

Testing the Moveit! configuration 493

Installing ROS-lndustrial packages of Universal robotic arms 493

Installing the ROS interface of Universal Robots 494

Understanding the Moveit! configuration of a Universal Robot arm 496

Getting started with real Universal Robots hardware and ROS-I 498

Working with Moveit! configuration of A B B robots 500

Understanding the ROS-lndustrial robot support packages 504

Visualizing the ABB robot model in RViz 505

ROS-lndustrial robot client package 506

Designing industrial robot client nodes 507

ROS-lndustrial robot driver package 508

Understanding the Moveit! IKFast plugin 511

Creating the Moveit! IKFast plugin for the A B B IRB 6640 robot 511

Prerequisites for developing the Moveit! IKFast plugin 512

OpenRave and IK Fast modules 512 Moveit! IKFast 512 Installing the Moveit! IKFast package 513 Installing OpenRave on Ubuntu 16.04 513

Creating the COLLADA file of a robot to work with OpenRave 515

Generating the IKFast CPP file for the IRB 6640 robot 517 Creating the Moveit! IKFast plugin 518

Questions 520

Summary 521

Chapter 15: Troubleshooting and Best Practices in ROS 523

Setting up RoboWare Studio in Ubuntu 524

[xi ]

Page 13: Mastering ROS for Robotics Programming Second Edition

Table of Contents

Installing/uninstalling RoboWare Studio 524 Getting started with RoboWare Studio 524 Create ROS packages in RoboWare Studio 526 Building ROS workspace in RoboWare Studio 529 Executing ROS nodes in RoboWare Studio 530 Starting ROS tools from the RoboWare interface 532 Handling active ROS topics, nodes, and services 533 Creating ROS nodes and classes with RoboWare tools 535 ROS package manager in RoboWare Studio 535

Best practices in ROS 536 ROS C++ coding style guide 537

Standard naming conventions used in ROS 537 Code license agreement 538 ROS code formatting 538 Console output 539

Best practices in the ROS package 540

Important troubleshooting tips in ROS 540

Using roswtf 541

Questions 544

Summary 544

Other Books You May Enjoy 545.

Index 549

[ x i i ]