# ROS2 Nodes
## 0. Post Details
- Reference: [Section 1~4, ROS 2 Topics - Make Your Nodes Communicate Between Each Other, ROS 2 For Beginners (ROS Jazzy - 2025), Edouard Renard, Udemy.](https://www.udemy.com/course/ros2-for-beginners)
- Post by: Jedd Yang
- Dates:
- 2025-09-11 (Started)
- [x] 2025-09-15 (Deadline)
- Keywords: Robotics, ROS2
## 1. Install ROS2 and Setup Your Environment
```bash=
sudo snap install code --classic # install vs code
sudo apt install terminator # install terminator
```
## 2. Write Your First ROS2 Program
### 2.1 Create package
Modularized block organizing nodes. A node is the basic unit in ROS2, a subprogram responsible for only one task.
> [!Important]
> Always build in `~/ros_ws` lest dir issue (multiple found);
> Always open VS code in `~/ros_ws/src` lest the dependency issue.
```bash=
# create workspace
mkdir ~/ros_ws
cd ros_ws/
colcon build
# create pkg
cd ~/ros_ws/src
# create python pkg
ros2 pkg create my_py_pkg --build-type ament_python --dependencies rclpy
# create cpp pkg
ros2 pkg create my_cpp_pkg --build-type ament_cmake --dependencies rclcpp
# build (certain packages)
colcon build --packages-select my_py_pkg
# config the environmental params
nvim ~/.bashrc
# add: source ~/ros_ws/install/setup.bash
# source
source ~/.bashrc
```
### 2.2 Create node
A node is language-agnostic, meaning you can write ROS2 nodes in different languages and they can still communicate. This is because they all call their own APIs which are built on top of rcl, ROS client libraries, written in C.

#### 2.2.1 Python node
Now the package has been created, we create nodes
```bash=
cd ros2_ws/src/my_py_pkg/
cd my_py_pkg/
touch my_first_node.py
cd ../..
code .
```
Find `my_first_node.py` in VS Code, and write:
```python=
#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
class MyNode(Node):
def __init__(self):
super().__init__("py_test")
self.get_logger().info("hello world")
self.create_timer(1.0, self._timer_callback)
def _timer_callback(self):
self.get_logger().info("hello")
def main():
rclpy.init(args = args)
node = MyNode()
rclpy.spin(node) # to keep the node alive
rclpy.shutdown()
if __name__ == "__main__":
main()
```
To install the node and create executables, go to `setup.py` in VS Code, and configure:
```python=
# ...
entry_points={
'console_scripts': [
# {executable name} = {package name}.{file name}:{function name}
"py_node = my_py_pkg.my_first_node:main"
]
}
```
Now build, source, and run.
> [!Tip]
> Always, build, source, and run, very important, remember.
```bash=
cd ~/ros2_ws/
colcon build --packages-select my_py_pkg
source ~/.bashrc # to update environmental parameters
ros2 run my_py_pkg py_node # run ros2 node
```
#### 2.2.2 C++ node
Now the package has been created, we create nodes
```bash=
cd ros2_ws/src/my_cpp_pkg/
cd my_cpp_pkg/
touch my_first_node.cpp
cd ../..
code .
```
Find `my_first_node.cpp` in VS Code, and write:
```cpp=
#include "rclcpp/rclcpp.hpp"
class MyNode:
public rclcpp::Node{
public:
MyNode() : Node("cpp_test"){
RCLCPP_INFO(this->get_logger(), "hello world");
timer_ = this->create_wall_timer(std::chrono::seconds(1),
std::bind(&MyNode::timerCallback, this));
}
private:
void timerCallback(){
RCLCPP_INFO(this->get_logger(), "hello %d", counter_);
counter_++;
}
rclcpp::TimerBase::SharedPtr timer_;
int counter_;
};
int main(int argc, char **argv){
rclcpp::init(argc, argv);
auto node = std::make_shared<MyNode>();
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
```
To install the node and create executables, go to `CMakeLists.txt` in VS Code, and configure:
```cmake=
# ...
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
add_executable(cpp_node src/my_first_node.cpp)
ament_target_dependencies(cpp_node rclcpp)
install(TARGETS
cpp_node # executable name
DESTINATION lib/${PROJECT_NAME}
)
ament_package()
```
Now build, source, and run.
> [!Tip]
> Again, always, build, source, and run, very important, remember.
```bash=
cd ~/ros2_ws/
colcon build --packages-select my_cpp_pkg
source ~/.bashrc # to update environmental parameters
ros2 run my_cpp_pkg cpp_node # run ros2 node
```
Congrats, you created your first node in ROS2.
## 3. Introduction to ROS2 Tools
```bash=
# Rename a node at runtime: rename py_node to abc
ros2 run my_py_pkg py_node --ros-args -r __node:=abc
# visualize the nodes
rqt_graph
```