(ROS) for Absolute Beginners Robotics Programming Made Easy

Lentin Joseph Robot Operating System (ROS) for Absolute Beginners: Robotics Programming Made Easy

Lentin Joseph Cheerakathil House Aluva, Kerala, India

ISBN-13 (pbk): 978-1-4842-3404-4 ISBN-13 (electronic): 978-1-4842-3405-1 https://doi.org/10.1007/978-1-4842-3405-1 Library of Congress Control Number: 2018945056 Copyright © Lentin Joseph 2018, corrected publication 2018 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Natalie Pao Development Editor: James Markham Coordinating Editor: Jessica Vakili Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit http://www.apress. com/rights-permissions. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842-3404-4. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper I dedicate this book to my parents, C. G. Joseph and Jancy Joseph, for giving me strong support in making this project happen.

The original version of the book FM and Cover was revised. An erratum for the book FM and the Cover can be found at https://doi.org/10.1007/978-1-4842-3405-1_7 Table of Contents

About the Author ���������������������������������������������������������������������������������xi About the Technical Reviewer �����������������������������������������������������������xiii

Chapter 1: Getting Started with for Robotics ������������������1 Getting Started with GNU/Linux ����������������������������������������������������������������������������1 What Is Ubuntu? ����������������������������������������������������������������������������������������������2 Why Ubuntu for Robotics? �������������������������������������������������������������������������������3 Installing Ubuntu ���������������������������������������������������������������������������������������������������3 Recommended PC Requirements ��������������������������������������������������������������������4 Downloading Ubuntu ���������������������������������������������������������������������������������������4 Installing VirtualBox �����������������������������������������������������������������������������������������5 Creating a VirtualBox Machine ������������������������������������������������������������������������6 Installing Ubuntu on VirtualBox ���������������������������������������������������������������������18 Installing Ubuntu on a PC ������������������������������������������������������������������������������28 Playing with the Ubuntu Graphical User Interface ����������������������������������������������29 The Ubuntu File System ���������������������������������������������������������������������������������30 Useful Ubuntu Applications ����������������������������������������������������������������������������33 Getting Started with Shell Commands ����������������������������������������������������������������34 Terminal Commands Cheat Sheet �����������������������������������������������������������������35 Summary ������������������������������������������������������������������������������������������������������������54

v Table of Contents

Chapter 2: Fundamentals of C++ for Robotics Programming ������������55 Getting Started with C++ ������������������������������������������������������������������������������������56 Timeline: The C++ Language ������������������������������������������������������������������������56 C/C++ in Ubuntu Linux ���������������������������������������������������������������������������������������56 Introduction to GCC and G++ Compilers �������������������������������������������������������57 Installing C/C++ Compiler �����������������������������������������������������������������������������57 Verifying Installation ��������������������������������������������������������������������������������������58 Introduction to GNU Project Debugger (GDB) �������������������������������������������������59 Learning OOP Concepts from Examples �������������������������������������������������������������69 The Differences Between Classes and Structs ����������������������������������������������69 C++ Classes and Objects ������������������������������������������������������������������������������73 Class Access Modifier �����������������������������������������������������������������������������������75 C++ Inheritance ��������������������������������������������������������������������������������������������76 C++ Files and Streams ���������������������������������������������������������������������������������82 in C++ ��������������������������������������������������������������������������������������84 C++ Exception Handling ��������������������������������������������������������������������������������85 C++ Standard Template Libraries �����������������������������������������������������������������88 Building a C++ Project ����������������������������������������������������������������������������������������88 Creating a Linux Makefile ������������������������������������������������������������������������������88 Creating a CMake File �����������������������������������������������������������������������������������92 Summary ������������������������������������������������������������������������������������������������������������94

Chapter 3: Fundamentals of Python for Robotics Programming ��������95 Getting Started with Python ��������������������������������������������������������������������������������96 Timeline: The Python Language ���������������������������������������������������������������������96 Python in Ubuntu Linux ���������������������������������������������������������������������������������������97 Introduction to Python Interpreter �����������������������������������������������������������������97 Installing Python on Ubuntu 16.04 LTS ����������������������������������������������������������98

vi Table of Contents

Verifying Python Installation ��������������������������������������������������������������������������98 Writing Your First Code ����������������������������������������������������������������������������������99 Understanding Python Basics ����������������������������������������������������������������������103 Summary ����������������������������������������������������������������������������������������������������������126

Chapter 4: Kick-Starting Robot Programming Using ROS ����������������127 What Is Robot Programming? ���������������������������������������������������������������������������127 Why Robot Programming Is Different ����������������������������������������������������������129 Getting Started with ROS ����������������������������������������������������������������������������������132 The ROS Equation ����������������������������������������������������������������������������������������135 Robot Programming Before and After ROS ��������������������������������������������������135 The History of ROS ��������������������������������������������������������������������������������������135 Before and After ROS �����������������������������������������������������������������������������������138 Why Use ROS? ���������������������������������������������������������������������������������������������138 Installing ROS ����������������������������������������������������������������������������������������������139 and Sensors Supporting ROS ����������������������������������������������������������146 Popular ROS Computing Platforms ��������������������������������������������������������������148 ROS Architecture and Concepts �������������������������������������������������������������������149 The ROS File System �����������������������������������������������������������������������������������152 ROS Computation Concepts �������������������������������������������������������������������������154 The ROS Community ������������������������������������������������������������������������������������155 ROS Command Tools �����������������������������������������������������������������������������������155 ROS Demo: Hello World Example �����������������������������������������������������������������160 ROS Demo: turtlesim �����������������������������������������������������������������������������������162 ROS GUI Tools: Rviz and Rqt ������������������������������������������������������������������������168 Summary ����������������������������������������������������������������������������������������������������������170

vii Table of Contents

Chapter 5: Programming with ROS ���������������������������������������������������171 Programming Using ROS �����������������������������������������������������������������������������������171 Creating a ROS Workspace and Package ����������������������������������������������������������172 ROS Build System ����������������������������������������������������������������������������������������176 ROS Catkin Workspace ��������������������������������������������������������������������������������177 Creating a ROS Package ������������������������������������������������������������������������������178 Using ROS Client Libraries ��������������������������������������������������������������������������������180 roscpp and rospy �����������������������������������������������������������������������������������������181 The Hello World Example Using ROS �����������������������������������������������������������189 Programming turtlesim Using rospy ������������������������������������������������������������204 Programming TurtleBot Simulation Using rospy ������������������������������������������221 Programming Embedded Boards Using ROS ����������������������������������������������������227 Interfacing Arduino with ROS ����������������������������������������������������������������������228 Installing ROS on a Raspberry Pi �����������������������������������������������������������������234 Summary ����������������������������������������������������������������������������������������������������������236

Chapter 6: Robotics Project Using ROS ��������������������������������������������237 Getting Started with Wheeled Robots ���������������������������������������������������������������237 Differential Drive Robot Kinematics ������������������������������������������������������������������238 Building Robot Hardware ����������������������������������������������������������������������������������242 Buying Robot Components ��������������������������������������������������������������������������243 Block Diagram of the Robot �������������������������������������������������������������������������249 Assembling Robot Hardware �����������������������������������������������������������������������251 Creating a 3D ROS Model Using URDF ��������������������������������������������������������������252 Programming Robot Firmware ��������������������������������������������������������������������������257 Programming Robot Using ROS ������������������������������������������������������������������������262 Creating a Bluetooth-ROS Driver for the Robot �������������������������������������������263 The Teleop Node ������������������������������������������������������������������������������������������268 viii Table of Contents

The Twist Message to Motor Velocity Node �������������������������������������������������269 The Odometry Node �������������������������������������������������������������������������������������270 The Dead-Reckoning Node ��������������������������������������������������������������������������270 Final Run �����������������������������������������������������������������������������������������������������������272 Summary ����������������������������������������������������������������������������������������������������������274

Erratum �����������������������������������������������������������������������������������������������E1 Index �������������������������������������������������������������������������������������������������275

ix About the Author

Lentin Joseph is an author and a robotics entrepreneur from India. He runs a robotics software company called Qbotics Labs. He has seven years of experience in the robotics domain, especially in the Robot Operating System, OpenCV, and PCL. He has authored four books in ROS, including Learning Robotics Using Python (Packt Publishing, 2015), Mastering ROS for Robotics Programming (first & second edition) (Packt Publishing, 2015), and ROS Robotics Projects (Packt Publishing, 2015). He is currently doing research for the Robotics Institute at Carnegie Mellon University.

xi About the Technical Reviewer

Massimo Nardone has more than 22 years of experiences in security, web/mobile development, the cloud, and IT architecture. His true IT passions are security and Android. He has been programming and teaching how to program with Android, Perl, PHP, Java, VB, Python, C/C++, and MySQL for more than 20 years. He holds a Master of Science degree in computing science from the University of Salerno, Italy. He has worked as a project manager, software engineer, research engineer, chief security architect, information security manager, PCI/SCADA auditor, and senior lead IT security/cloud/SCADA architect for many years. His technical skills include security, Android, Cloud, Java, MySQL, Drupal, Cobol, Perl, web and mobile development, MongoDB, D3, Joomla, Couchbase, C/C++, WebGL, Python, Pro Rails, Django CMS, Jekyll, Scratch, and more. He is currently the chief information security officer (CISO) at Cargotec Oyj. He worked as a visiting lecturer and supervisor for exercises at the Networking Laboratory of the Helsinki University of Technology (Aalto University). He holds four international patents (in the PKI, SIP, SAML, and Proxy areas).

xiii