In the realm of robotic applications, the Robot Operating System (ROS) has emerged as a pivotal framework that engineers and enthusiasts utilize to foster communication among various hardware and software components of a robot. With the advent of ROS2, the ecosystem has witnessed enhanced capabilities, ensuring robustness and scalability. In this tutorial, we are about to embark on a journey to craft minimal ROS2 nodes employing Python and C++, the two popular languages supported by ROS2.
Crafting a Minimal ROS2 Node with Python
Creating a minimal node with Python is a straightforward task. It serves as the cornerstone for building more complex functionalities as you progress in your ROS2 adventure. Here’s how you can create a minimal ROS2 node using Python:
import rclpy
from rclpy.node import Node
class SimpleNode(Node):
def __init__(self):
super().__init__('simple_node_name')
self.create_timer(0.2, self.timer_callback)
def timer_callback(self):
self.get_logger().info("Greetings from ROS2")
def main(args=None):
rclpy.init(args=args)
node_instance = SimpleNode()
rclpy.spin(node_instance)
rclpy.shutdown()
if __name__ == '__main__':
main()
In the code above:
- Import the necessary libraries.
- Define a class,
SimpleNode
, inheriting from theNode
class provided byrclpy
. - Within the constructor
__init__
, initialize the node with a namesimple_node_name
. - Create a timer with a callback function
timer_callback
to be triggered every 0.2 seconds. - Define the
timer_callback
method to log a message “Greetings from ROS2”. - Define a
main
function to initialize ROS2 communications, instantiate the node, spin the node to keep it running, and finally shutdown ROS2 communications upon exit.
Running this script will launch a ROS2 node named simple_node_name
, logging “Greetings from ROS2” to the console at a 5Hz rate.
Constructing a Basic ROS2 Node in C++
Transitioning to C++, ROS2 nodes are constructed similarly, albeit with syntax and library differences. Here’s a simplistic node example in C++:
#include "rclcpp/rclcpp.hpp"
class BasicNode : public rclcpp::Node
{
public:
BasicNode() : Node("basic_node_name")
{
timer_ = this->create_wall_timer(
std::chrono::milliseconds(200),
std::bind(&BasicNode::timerCallback, this));
}
private:
void timerCallback()
{
RCLCPP_INFO(this->get_logger(), "Hello from BasicNode in ROS2");
}
rclcpp::TimerBase::SharedPtr timer_;
};
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
auto node_instance = std::make_shared<BasicNode>();
rclcpp::spin(node_instance);
rclcpp::shutdown();
return 0;
}
Dissecting the snippet:
- Node Initialization: The
BasicNode
class, inheriting fromrclcpp::Node
, encapsulates a ROS2 node. - Timer Creation: A timer is instantiated, similar to the Python example, but with C++ syntax.
- Logging: The
timerCallback
method logs a message, employingRCLCPP_INFO
. - Node Spinning and Shutdown: Similar to the Python version, the node is spun to process callbacks and then shut down.
Compile and run this C++ script in a ROS2 environment, and watch the log messages flow in the console.