ENPM702 L10: ROS (Part 2) PDF
Document Details
Uploaded by WonderfulSuprematism
University of Maryland
2024
Z. Kootbally
Tags
Summary
This document is a lecture on introductory robot programming using ROS (Robot Operating System). The lecture covers various topics such as node creation, spinning nodes, callbacks, ROS scheduling, and the Turtlebot. It is part of a Fall 2024 semester course at the University of Maryland.
Full Transcript
ENPM702: Introductory Robot Programming L10: ROS (Part 2) version 2.0 Lecturer: Z. Kootbally School: University of Maryland Semester/Year: Fall/2024 2024/11/12 Table of Contents Spawn Ɓ Learning Obj...
ENPM702: Introductory Robot Programming L10: ROS (Part 2) version 2.0 Lecturer: Z. Kootbally School: University of Maryland Semester/Year: Fall/2024 2024/11/12 Table of Contents Spawn Ɓ Learning Objectives Teleoperation Ɓ Node Ɓ Callbacks Write a Node Callables Spinning a Node Ɓ ROS Scheduling Write a Node with OOP create_wall_timer Ɓ Turtlebot Velocity Ɓ Next Class Packages Ɓ Appendix A version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 1 54 Changelog v2.0: Added sections on callbacks and ROS scheduler. v1.0: Original version. version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 2 54 Convention IJ Syntax. ` General Important notes. u file Ì folder link Ŧ ı command Best practices. ƍ example δ terminology question Ͻ ` ROS Warning. n node t topic : m message Summary. Ď Tips. version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 3 54 Learning Objectives ` Learning Objectives By the end of this lecture, you will be able to: Write a Node: Understand how to create a basic ROS 2 node and implement it in code. Spinning a Node: Learn the importance and process of spinning a node to keep it responsive and operational. Write a Node with OOP: Implement nodes using object-oriented programming principles to manage more complex structures and functionalities. Publishers: Understand the concept of publishers in ROS 2 and how to create them. Messages and Topics: Gain knowledge about message structures and how data is transmitted through topics. Write a Publisher: Learn to write and implement a publisher in ROS 2 to control robot actions, including publishing specific data types like m geometry_msgs/msg/Twist. version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 4 54 Node Part I ͽ Nodes version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 5 54 Node δ Nodes A node is a small program (e.g., written in C++) that executes some relatively simple task or process. Nodes can publish data (publishers), receive data (subscribers), or do both (publishers/subscribers). version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 6 54 Node Ī Write a Node é ToDo - Write a Node In the package ˜ first_package write the node n hello which prints Hello in the terminal. 1. Write the node in u first_package/src/main.cpp 2. Build: Edit u CMakeLists.txt to build and install the executable. ı cd ~∕ros702_ws ı colcon build --packages-select first_package ı source ~∕ros702_ws∕install∕setup. 3. Run: ı ros2 run first_package hello (can be run from anywhere). version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 7 54 Node Ī Write a Node é ToDo - Write a Node 1. Write the node in u first_package/src/main.cpp #include int main(int argc, char **argv) { rclcpp::init(argc, argv); auto node = std::make_shared("hello"); RCLCPP_INFO_STREAM(node->get_logger(), "Hello"); // rclcpp::spin(node); rclcpp::shutdown(); } rclcpp::init(argc, argv) initializes any global resources needed by the middleware and the client library and handles client library-related command-line argument parsing. auto node = std::make_shared("hello"); creates a shared pointer to a Node object. The name of the node is n hello. RCLCPP_INFO_STREAM(node->get_logger(), "Hello"); logs the message Hello. rclcpp::shutdown() invalidates all nodes and their constituent parts, causing them to shut down. It also destroys any global resources created during the original call to rclcpp::init(argc, argv). version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 8 54 Node Ī Write a Node Ī ROS Logging ` ROS Logging In ROS 2, logging is handled by the rcutils logging system. It provides a way to output messages with various severity levels (DEBUG, INFO, WARN, ERROR, and FATAL) to the console. 1. Include the rclcpp logging header in your source file. #include 2. Use the logging macros provided by rclcpp to log messages at different severity levels. RCLCPP_DEBUG(node->get_logger(), "This is a debug message."); RCLCPP_INFO(node->get_logger(), "This is an info message."); RCLCPP_WARN(node->get_logger(), "This is a warning message."); RCLCPP_ERROR(node->get_logger(), "This is an error message."); RCLCPP_FATAL(node->get_logger(), "This is a fatal message."); version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 9 54 Node Ī Write a Node é ToDo - Write a Node 2. Build: Edit u CMakeLists.txt to build and install the executable. #other code add_executable(hello src/main.cpp) ament_target_dependencies(hello rclcpp) install(TARGETS hello DESTINATION lib/${PROJECT_NAME}) ı cd ~∕ros702_ws ı colcon build --packages-select first_package ı source ~∕ros702_ws∕install∕setup. version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 10 54 Node Ī Write a Node é ToDo - Write a Node 3. Run: ı ros2 run first_package hello (can be run from anywhere). version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 11 54 Node Ī Spinning a Node ` Spinning a Node Spinning a node refers to the process of keeping a node actively running so that it can respond to events and execute callbacks. Spinning a node involves running an execution loop that processes callbacks. The simplest way to spin a node: rclcpp::spin() This function blocks execution and keeps the node alive until it is terminated by rclcpp::shutdown() or an external signal like Ctrl + C version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 12 54 Node Ī Spinning a Node ` Ctrl + C When Ctrl + C is pressed, it is interpreted as a signal to gracefully shut down nodes and stop the running processes. This is specifically tied to handling the SIGINT (Signal Interrupt) signal in Linux and Unix-based systems. Signal Handling: ROS 2 sets up a handler for SIGINT when rclcpp::init() is called. This allows the application to intercept the signal when Ctrl + C is pressed. Graceful Shutdown: When Ctrl + C is detected, rclcpp::shutdown() is automatically called. This function performs several important cleanup steps: Stops the Node Spinning: If any nodes are currently spinning with rclcpp::spin() or through an executor, spinning is interrupted, and the process exits the event loop. Releases Resources: Any allocated resources related to the node and communication are released, such as publishers, subscribers, and services. Shuts Down the ROS 2 Context: This step ensures that all nodes and associated threads terminate correctly, preventing resource leaks and ensuring clean application exits. version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 13 54 Node Ī Spinning a Node é ToDo - Spin the Node 1. Use rclcpp::spin() to spin the node. 2. Recompile: ı colcon build --packages-select first_package 3. Run the node: ı ros2 run first_package hello 4. Use Ctrl + C to stop the node from spinning. version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 14 54 Node Ī Write a Node with OOP é ToDo - Write a Node with OOP Complex nodes often require intricate structures, including various attributes and methods. Implementing functionalities such as data publishing, data reception, creating service clients and servers, and developing action clients and servers benefits significantly from an object-oriented programming (OOP) approach. 1. Write: The class FirstNode in u first_package/include/first_node.hpp The methods (if any) in u first_package/src/first_node.cpp FirstNode instantiation in u first_package/src/main.cpp 2. Build: Edit u CMakeLists.txt to build and install the executable. ı cd ~∕ros702_ws ı colcon build --packages-select first_package ı source ~∕ros702_ws∕install∕setup. 3. Run: ı ros2 run first_package hello (can be run from anywhere). version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 15 54 Node Ī Write a Node with OOP é ToDo - Write a Node with OOP 1. Write: The class FirstNode in u first_package/include/first_node.hpp #include class FirstNode: public rclcpp::Node { public: FirstNode(std::string node_name): Node(node_name){ RCLCPP_INFO_STREAM(this->get_logger(), "Hello"); } }; The methods (if any) in u first_package/src/first_node.cpp The current class does not have any methods. FirstNode instantiation in u first_package/src/main.cpp #include "first_package/first_node.hpp" int main(int argc, char **argv) { rclcpp::init(argc, argv); auto node = std::make_shared("hello"); rclcpp::spin(node); rclcpp::shutdown(); } version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 16 54 Node Ī Write a Node with OOP é ToDo - Write a Node with OOP 2. Build: Edit u CMakeLists.txt to build and install the executable. # add path to the include directory include_directories(include) # add first_node.cpp add_executable(hello src/main.cpp src/first_node.cpp) ament_target_dependencies(hello rclcpp) install(TARGETS hello DESTINATION lib/${PROJECT_NAME}) ı cd ~∕ros702_ws ı colcon build --packages-select first_package version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 17 54 Node Ī Write a Node with OOP é ToDo - Write a Node with OOP 3. Run: ı ros2 run first_package hello (can be run from anywhere). version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 18 54 Turtlebot Part II ͽ Turtlebot version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 19 54 Turtlebot ` Turtlebot The Turtlebot is a differential wheeled robot, i.e., its movement is based on two separately driven wheels placed on either side of the robot body. ó Resources Read about the origin of the Turtlebot. Read about the origin of “Turtle” in robotics. version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 20 54 Turtlebot Ī Velocity ` Velocity Translational velocity 𝑣 (m/s) on the wheel 𝑥 axis: +𝑣 moves the robot forward and −𝑣 moves the robot backward. Angular velocity 𝜔 (rad/s) on the wheel 𝑧 axis: +𝜔 rotates the robot CCW and −𝜔 rotates the robot CW. right wheel positive rotation left wheel To move the Turtlebot you need to send messages of type m geometry_msgs/msg/Twist to the Topic t /cmd_vel version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 21 54 Turtlebot Ī Packages ` Packages Install the packages needed for the Turtlebot. ı sudo apt install 'ros--turtlebot3*' ı sudo apt install 'ros--turtlebot3-*' Add one of the following lines in u.bashrc|.zshrc export TURTLEBOT3_MODEL=burger export TURTLEBOT3_MODEL=waffle # prefer this one export TURTLEBOT3_MODEL=waffle_pi Source u.bashrc|.zshrc version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 22 54 Turtlebot Ī Spawn ` Spawn Spawn the robot in RViz. ı ros2 launch turtlebot3_fake_node turtlebot3_fake_node.launch.py Spawn the robot in Gazebo. You may need to run ı source ∕usr∕share∕gazebo∕setup. ı ros2 launch turtlebot3_gazebo Optionally, start RViz. ı ros2 launch turtlebot3_fake_node rviz2.launch.py version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 23 54 Turtlebot Ī Teleoperation ` Teleoperation Spawn the Turtlebot in RViz. ı ros2 launch turtlebot3_fake_node turtlebot3_fake_node.launch.py Know issue with the rendering. Move the robot around with your keyboard. ı ros2 run turtlebot3_teleop teleop_keyboard Visualize messages published on t /cmd_vel Visualize the type of messages published to t /cmd_vel Visualize the structure of the messages published on t /cmd_vel Use the tool ros2 interface show version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 24 54 Callbacks Part III ͽ Callbacks version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 25 54 Callbacks ` Callbacks A callback (or callback function) g is a function provided as an argument to another function f. This enables the function f to “call back” or execute the function g at a specified point during its own execution. Callback functions are commonly used for event handling, asynchronous processing, or simply to add flexibility by allowing user-defined behavior within a function. IJ f(, g( version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 28 54 Callbacks Ī Callables Ī std::function Ī Function Pointer δ Function Pointer A function pointer is a pointer that holds the address of a function and enables you to call the function through the pointer. IJ return_type (*pointer_name)(parameter_type_1, parameter_type_2,...); version 2.0 ENPM702 | L10: Robot Operating System (Part 2) 29 54 Callbacks Ī Callables Ī std::function Ī Function Pointer IJ1 ƍ Example 1 int add(int a, int b) { return a + b; } 2 3 int main(){ 4 // Declaring a function pointer and assigning it to 'add' 5 int (*ptr)(int, int) = &add; // ptr is now a callable 6 7 // Call add through ptr 8 int result_dereference = (*ptr)(5, 10); // Using dereference 9 int result_direct = ptr(5, 10); // Direct invocation 10 11 std::cout