ENPM702 Introductory Robot Programming L11 PDF
Document Details
Uploaded by Deleted User
University of Maryland
2024
Z. Kootbally
Tags
Summary
This document contains lecture notes on ROS (Robot Operating System) part 3, covering topics such as publishers and subscribers. It's part of ENPM702: Introductory Robot Programming, taught in Fall 2024 at the University of Maryland.
Full Transcript
ENPM702: Introductory Robot Programming L11: ROS (Part 3) version 1.1 Lecturer: Z. Kootbally School: University of Maryland Semester/Year: Fall/2024 2024/11/13 Table of Contents Write a Simple Publisher Ɓ...
ENPM702: Introductory Robot Programming L11: ROS (Part 3) version 1.1 Lecturer: Z. Kootbally School: University of Maryland Semester/Year: Fall/2024 2024/11/13 Table of Contents Write a Simple Publisher Ɓ Learning Objectives Publish at Regular Intervals Ɓ Publishers Ɓ Subscribers Messages Write Subscribers Topics Ɓ Next Class version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 1 27 Changelog v1.0: Original version. version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 2 27 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 1.1 ENPM702 | L11: Robot Operating System (Part 3) 3 27 Learning Objectives ` Learning Objectives By the end of this lecture, you will be able to: Messages and Topics: Gain knowledge about message structures and how data is transmitted through topics. Publishers: 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. Subscribers: Learn to write and implement a subscriber in ROS 2 to retrieve LIDAR scans and the robot position. version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 4 27 Publishers Part I ͽ Publishers version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 5 27 Publishers ` Publishers The primary purpose of a publisher is to make information produced by a node available to any other nodes. Topics – Publishers send messages on topics. Messages – The data sent by publishers are structured as messages. Asynchronous – The publisher can keep generating and sending messages without waiting for any acknowledgement or response from subscribers. Quality of Service (QoS) – QoS settings determine how the communication is handled (reliability, durability, etc). version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 6 27 Publishers Ī Messages δ Messages A message is a structured data type that is used by nodes to communicate with each other over topics, services, or actions. Interface Definition Language (IDL) *.msg *.srv *.action #include *_msgs my_package import version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 7 27 Publishers Ī Messages ` Commands List all interfaces installed on your system: ı ros2 interface list Get the structure of an interface: ı ros2 interface show e.g., ı ros2 interface show geometry_msgs∕msg∕Twist Get all the interfaces from a package: ıros2 interface package e.g., ı ros2 interface package geometry_msgs version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 8 27 Publishers Ī Messages ` Usage 1. Understand the message structure. e.g., ı ros2 interface show example_interfaces∕msg∕String 2. Include the message in your program. e.g., #include 3. Instantiate a message object. e.g., auto message = example_interfaces::msg::String(); 4. Fill out the message fields. e.g., message.data = "Hello"; 5. Publish the message. e.g., publisher_->publish(message); The package ˜ example_interfaces contains all basic message types (string, float, inte- ger, etc). Check with ı ros2 interface package example_interfaces version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 9 27 Publishers Ī Topics δ Topics A topic is a communication channel used for data exchange between nodes. ` Structure Name: Each topic has a unique name that acts as an identifier, such as t /camera/image_raw for a camera feed. Message Type: Topics are associated with specific message types that define the structure of the transmitted data, like m sensor_msgs/msg/Image for image data. /camera/image_raw sensor_msgs/msg/Image sensor_msgs/msg/Image version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 10 27 Publishers Ī Topics ` Commands List all active topics: ı ros2 topic list Get data published on a topic: ı ros2 topic echo Get information on a topic: ı ros2 topic info Get a topic’s type: ı ros2 topic type Get the average publishing rate: ı ros2 topic hz version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 11 27 Publishers Ī Write a Simple Publisher é ToDo - Write a Simple Publisher Objective: Publish a single m Twist message (linear.x=1.0 and angular.z=0.5) to t cmd_vel Package ˜ bot_publisher Files u bot_publisher.cpp and u bot_publisher.hpp Class BotPublisher Executable bot_pub Node n bot_publisher Topic t /cmd_vel Message m geometry_msgs/msg/Twist Ͻ Don’t forget the dependencies when creating the package. version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 12 27 Publishers Ī Write a Simple Publisher Write the class BotPublisher in u bot_publisher/include/bot_publisher.hpp #include Create a publisher attribute: rclcpp::Publisher::SharedPtr velocity_publisher_; Initialize velocity_publisher_: velocity_publisher_ = this->create_publisher("cmd_vel", 10); ↪ Build the message and publish it. auto message = geometry_msgs::msg::Twist(); message.linear.x = 1.0; message.angular.z= 0.5; velocity_publisher_->publish(message); version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 13 27 Publishers Ī Write a Simple Publisher Initialize velocity_publisher_: velocity_publisher_ = this->create_publisher("cmd_vel", 10); This line instantiates a publisher object using the message type geometry_msgs::msg::Twist, with the name topic "cmd_vel" and having a queue size of 10. The second argument of the method create_publisher() is the Quality of Service (QoS) profile setting. The value 10 specifies the size of the message queue for the publisher. This is related to the QoS setting history with the policy keep_last. The publisher maintains a buffer for the 10 most recent messages. Older messages are dropped if the queue exceeds this size, favoring recent information. This reduces latency and memory usage, especially in high-frequency publishing environments. version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 14 27 Publishers Ī Write a Simple Publisher Write the main() function in u main.cpp #include "bot_publisher.hpp" int main(int argc, char **argv) { rclcpp::init(argc, argv); auto node = std::make_shared("bot_publisher"); rclcpp::spin(node); rclcpp::shutdown(); } version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 15 27 Publishers Ī Write a Simple Publisher Edit u CMakeLists.txt # Path to the include folder include_directories(include/bot_publisher) # Create the executable add_executable(bot_pub src/bot_publisher.cpp src/main.cpp) # Set the target dependencies ament_target_dependencies(bot_pub rclcpp geometry_msgs) # Install the target install(TARGETS bot_pub DESTINATION lib/${PROJECT_NAME} ) version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 16 27 Publishers Ī Write a Simple Publisher Compile and execute. ı colcon build --packages-select bot_publisher ı ros2 run bot_publisher bot_pub Introspect. ı rqt_graph ı ros2 topic echo cmd_vel version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 17 27 Publishers Ī Publish at Regular Intervals é ToDo - Publish at Regular Intervals Objective: Modify the class BotPublisher to publish a m Twist message (linear.x=1.0 and angular.z=0.5) to t cmd_vel every 300 ms. Move the code that builds and publishes m Twist messages into a callback function. Use create_wall_timer() and pass in the callback function from the previous step. version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 18 27 Subscribers Part II ͽ Subscribers version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 19 27 Subscribers ` Subscribers A subscriber is an object that receives messages from a topic. The subscriber declares the type of message that it expects to receive, along with a callback function that is invoked to handle the incoming data. Topics – Subscribers receive messages from topics. Messages – Subscribers can process messages with callback functions. Quality of Service (QoS) – Subscribers can define their own QoS setting. version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 20 27 Subscribers Ī Write Subscribers é ToDo - Write Subscribers Objective: Create two subscribers: 1) to retrieve the position of the turtlebot from t odom ( m nav_msgs/msg/Odometry ), and 2) to receive LiDAR data from t scan ( m sensor_msgs/msg/LaserScan ). Package ˜ bot_subscriber Files u bot_subscriber.cpp and u bot_subscriber.hpp Class BotSubscriber Executable bot_sub Node n bot_subscriber Topics t /odom & t /scan Message m nav_msgs/msg/Odometry & m sensor_msgs/msg/LaserScan Ͻ Don’t forget the dependencies when creating the package. version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 21 27 Subscribers Ī Write Subscribers Write the class BotSubscriber in u bot_subscriber.hpp #include #include Declare two subscriber objects: rclcpp::Subscription::SharedPtr odom_sub_; rclcpp::Subscription::SharedPtr scan_sub_; Initialize the subscriber attributes. odom_sub_ = this->create_subscription("odom", 10, std::bind(&BotSubscriber::odom_callback, this, std::placeholders::_1)); scan_sub_ = this->create_subscription("scan", 10, std::bind(&BotSubscriber::scan_callback, this, std::placeholders::_1)); Declare the callbacks. void BotSubscriber::odom_callback(const nav_msgs::msg::Odometry::SharedPtr msg) void BotSubscriber::scan_callback(const sensor_msgs::msg::LaserScan::SharedPtr msg) Implement the callbacks in u bot_subscriber.cpp Simply log a message in each callback. version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 22 27 Subscribers Ī Write Subscribers Write the main() function in u main.cpp #include "bot_subscriber.hpp" int main(int argc, char **argv) { rclcpp::init(argc, argv); auto node = std::make_shared("bot_subscriber"); rclcpp::spin(node); rclcpp::shutdown(); } version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 23 27 Subscribers Ī Write Subscribers Edit u CMakeLists.txt # Path to the include folder include_directories(include/bot_subscriber) # Create the executable add_executable(bot_sub src/bot_subscriber.cpp src/main.cpp) # Set the target dependencies ament_target_dependencies(bot_sub rclcpp nav_msgs sensor_msgs) # Install the target install(TARGETS bot_sub DESTINATION lib/${PROJECT_NAME} ) version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 24 27 Subscribers Ī Write Subscribers Compile and execute. ı colcon build --packages-select bot_subscriber ı ros2 run bot_subscriber bot_sub Introspect. ı rqt_graph version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 25 27 Subscribers Ī Write Subscribers Ș Exercise #1 In a new package, ˜ bot_controller, integrate the publisher and subscribers we developed in this course to achieve the following: Move the robot forward with a linear velocity of 0.1 m/s. Stop the robot when the front beam detects an obstacle at a distance of 0.2 m. version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 26 27 Next Class ` Next Lecture Lecture 12: ROS (Part 4) version 1.1 ENPM702 | L11: Robot Operating System (Part 3) 27 27