You know an Arduino program by heart: globals, a setup() that runs once, a loop() that runs forever. One board owns everything.
Add a camera, a lidar or a second microcontroller and that ends. Two programs must exchange data, one must be replaceable without reflashing the other, and you need to see what flows without a Serial.println() in every file.
What changes when you move from a sketch to a node
A sketch is one program, one loop, one processor. A node is a process, and a robot runs several at once.
You lose the loop. You write callbacks and an executor calls them. A timer fires every 100 ms; a subscription fires when a message lands; neither knows about the other. Rebuilding loop() with while True and time.sleep() blocks the executor and stops every other callback in that node.
You lose the flash step. Build, source, run. The build writes into a workspace; the source step tells your shell where those files are.
You lose the direct wire. A sensor and its reader used to share a variable. Now they share a topic name, and the publisher does not know who listens, which is why a broken robot fails silently instead of printing garbage.
What to install, and what it costs you in time
Two paths.
Ubuntu 24.04 LTS with ROS 2 Jazzy Jalisco. Jazzy reached general availability on 23 May 2024, targets Ubuntu 24.04, and is supported until May 2029 under REP 2001. Ubuntu 24.04 has standard support to May 2029 and extended security maintenance to 2034, so the windows line up.
sudo apt install ros-jazzy-desktop
source /opt/ros/jazzy/setup.bash
echo "source /opt/ros/jazzy/setup.bash" >> ~/.bashrcAdd the ROS 2 apt repository first. That helper package’s command has changed more than once, so copy the current lines from the official Ubuntu install page for Jazzy rather than from a blog.
Windows 11 with WSL2. If the laptop must run Windows for college work or MATLAB, do not dual boot in exam week. Install WSL2, run Ubuntu 24.04 inside it, and follow the same steps. WSL 2 runs Linux GUI applications through WSLg, so RViz opens in a normal window. It needs Windows 11 or Windows 10 build 19044 or newer plus a matching GPU driver.
The hardware you have. These are rules of thumb from running the stack, not published requirements. On WSL2 the Linux side shares memory with Windows, so a 4 GB laptop installs and then thrashes; 8 GB is workable with the browser closed; 16 GB is comfortable once a simulator is involved. Ubuntu asks for at least 25 GB of disk, and ROS 2, a workspace and a simulator all add to that.
Raspberry Pi 5. It comes in 2 GB, 4 GB, 8 GB and 16 GB versions. In practice, a headless robot board is comfortable at 4 GB, and a Pi driving a monitor and RViz should start at 8 GB. Jazzy is published for arm64 as well as amd64, so the same apt steps work.
Budget a free evening, most of it waiting on downloads.
The five concepts you need before any code
Five words carry almost all of ROS 2.
- Node. A process that does one job and has a name. Example: a
lidar_drivernode publishes scans, and nothing else knows how that sensor works. - Topic. A named stream that publishers write and subscribers read without waiting. Example:
/scancarries ranges to mapping and obstacle avoidance at once. - Service. A request and response. Example:
/reset_odometryreturns success once the wheel counters are zeroed. - Action. A long job with feedback that can be cancelled. Example: driving to the dock reports distance remaining and stops cleanly when cancelled.
- Parameter. A named value read at startup and changeable without editing code. Example:
wheel_radiuslets one node serve a 65 mm and a 100 mm chassis.
Streaming data goes on a topic, a quick lookup in a service, anything slow and stoppable is an action, and anything you would hard-code is a parameter.
Your first node, line by line
Create a workspace and an empty Python package:
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_python py_distance --dependencies rclpy std_msgsThis fakes an infrared sensor:
import rclpy
from rclpy.node import Node
from std_msgs.msg import Float32
class DistancePublisher(Node):
def __init__(self):
super().__init__('distance_publisher')
self.publisher_ = self.create_publisher(Float32, '/ir_distance_cm', 10)
self.timer = self.create_timer(0.1, self.publish_distance)
self.get_logger().info('distance publisher started')
def publish_distance(self):
msg = Float32()
msg.data = 42.0
self.publisher_.publish(msg)
def main(args=None):
rclpy.init(args=args)
node = DistancePublisher()
try:
rclpy.spin(node)
finally:
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()The imports. rclpy is the Python client library. Node is the base class that gives you logging, timers and the publisher factory. Float32 comes from std_msgs, and message types always arrive as package.msg.Type.
The constructor. super().__init__('distance_publisher') registers the node name. create_publisher(Float32, '/ir_distance_cm', 10) takes the type, the topic and a queue depth of 10. create_timer(0.1, self.publish_distance) calls your method every 0.1 s, so the topic runs near 10 Hz.
The main guard. rclpy.init() starts the middleware. rclpy.spin(node) hands control to the executor, which waits for timer ticks and calls your callbacks. Without spin the program exits having published nothing. The try/finally destroys the node and shuts down on Ctrl+C.
The subscriber is the same skeleton with create_subscription:
import rclpy
from rclpy.node import Node
from std_msgs.msg import Float32
class DistanceSubscriber(Node):
def __init__(self):
super().__init__('distance_subscriber')
self.subscription = self.create_subscription(
Float32, '/ir_distance_cm', self.on_distance, 10)
def on_distance(self, msg):
self.get_logger().info(f'distance {msg.data:.1f} cm')
def main(args=None):
rclpy.init(args=args)
node = DistanceSubscriber()
try:
rclpy.spin(node)
finally:
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()The callback runs on the executor thread while spin runs, so it must be fast. Both scripts need entry points in setup.py under console_scripts. Then build and source from the workspace root, installing python3-colcon-common-extensions if colcon is missing.
colcon build --symlink-install
source install/setup.bashSeeing the graph: ros2 node list / topic list / topic echo / topic hz
With both nodes running, open a third terminal and look at the system, not the code.
ros2 node list
ros2 topic list -t
ros2 topic echo /ir_distance_cm
ros2 topic hz /ir_distance_cmnode list shows what is alive. topic list -t adds message types. topic echo prints messages as they arrive, and topic hz prints the measured rate with minimum, maximum and standard deviation, which is how you find your 10 Hz timer actually running at 9.4 Hz. topic info --verbose adds publisher count, subscriber count and quality of service.
“The topic is silent” covers three faults. Check the publisher is in node list. Check the publisher count, because a subscription alone makes a topic appear with zero publishers. Check the type on both sides, since Float32 and Float64 will not connect.
One naming detail saves hours: ir_distance_cm resolves relative to the node’s namespace, /ir_distance_cm is absolute. Mixing them creates two topics with almost the same name. Past a handful of nodes, namespacing becomes the problem that robot fleet management software solves at plant scale.
A minimal differential-drive base in ROS 2
The standard way to command a base is geometry_msgs/msg/Twist on /cmd_vel. Twist holds linear and angular, each with x, y and z. A differential-drive robot uses two: linear.x in metres per second, angular.z in radians per second, positive counter-clockwise.
The other four must be zero. linear.y is sideways motion and needs mecanum wheels. linear.z needs a drone. angular.x and angular.y are roll and pitch, which a two-wheeled base cannot produce on purpose. A base that ignores them drives straight when a navigation stack expects it to crab.
import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist
class CmdVelPublisher(Node):
def __init__(self):
super().__init__('cmd_vel_publisher')
self.publisher_ = self.create_publisher(Twist, '/cmd_vel', 10)
self.timer = self.create_timer(0.1, self.send_speed)
def send_speed(self):
msg = Twist()
msg.linear.x = 0.15
msg.angular.z = 0.5
self.publisher_.publish(msg)
def main(args=None):
rclpy.init(args=args)
node = CmdVelPublisher()
try:
rclpy.spin(node)
finally:
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()With wheel radius r and wheel separation L, each wheel’s linear speed is linear.x plus or minus angular.z * L / 2, and dividing by r gives radians per second. At linear.x = 0.15, angular.z = 0.5, r = 0.0325 m and L = 0.16 m, the right wheel needs 0.19 m/s and the left 0.11 m/s, about 5.8 and 3.4 rad/s.
Then the part that separates a demo from a robot. /cmd_vel is a request. A working base reads encoders, runs a closed loop so measured speed tracks the request, integrates counts into a pose, publishes /odom and broadcasts the odom to base_link transform. Without it, the distance you command is not the distance you travel, the error grows every second, and every map built on that odometry is slightly wrong. Counts per revolution depends on encoder and gearbox, so measure it by hand.
Where Arduino and ESP32 fit
You do not discard the microcontroller. You move it down a layer.
micro-ROS runs a ROS 2 client library on the board, making it a real node rather than a device behind a custom protocol. It talks over a transport, usually serial, and the ESP32 is supported by the Arduino library for micro-ROS, which provides USB serial transports. Use it when the MCU owns hard real-time work: PWM, encoder interrupts, an IMU sampled at a fixed rate.
Do not try full ROS 2 on an 8-bit AVR. The client libraries assume far more memory than an Uno has. An ESP32 hosts micro-ROS comfortably but will not run RViz.
The cheap option is a serial bridge. The Arduino prints L120,R118 for wheel ticks; a Python node on the Pi reads the port with pyserial and publishes a proper message, and a /cmd_vel subscriber writes V150,W20 back. It costs nothing and shows what micro-ROS would do, but you own the protocol and every edge case.
Board choice is separate: see Arduino vs ESP32 for robotics.
The three errors that stop beginners
TF frame mismatch. The symptom is “Lookup would require extrapolation into the future”, or a missing frame, from RViz or a navigation node. Someone asked for a frame nobody published, usually because the strings differ: one node publishes base_link, another asks for base_footprint, or one has a leading slash and the other does not. Run ros2 run tf2_tools view_frames, then tf2_echo on one pair. Fix the strings, not the maths.
QoS mismatch. The symptom is the worst kind: the topic is listed, topic info shows one publisher and one subscriber, both processes are healthy, and the callback never fires. ROS 2 connects them only when the subscriber’s request is not more demanding than the publisher’s offer, and both default to reliable. Camera and lidar drivers often publish best effort, so a subscriber insisting on reliable never connects. Compare both sides with ros2 topic info /scan --verbose.
Workspace not sourced. The symptom is Package 'py_distance' not found, or ros2 run listing packages you installed. It works in the terminal where you built and fails in every new one, because sourcing is per shell. Source /opt/ros/jazzy/setup.bash and install/setup.bash in each terminal, or add both to ~/.bashrc. echo $ROS_DISTRO should print jazzy.
What to learn next, and in what order
- URDF and robot_state_publisher. Describe your chassis as a link and joint tree and view it in RViz. Until you see the model, the transform tree stays abstract.
- Odometry and transforms. Publish
/odomand theodomtobase_linktransform from encoder counts, then push the robot by hand and watch the model move. - Simulation. Load the same URDF into Gazebo and drive it with the same publisher. With no hardware, this is the fastest way to keep going, and learning robotics without hardware covers the software-only route. A simulated robot is not an industrial digital twin; the robot cell digital twin guide explains that boundary.
- Navigation. Nav2 wants a map, a transform tree and a base that accepts velocity commands.
- Hardware. Move the stack onto the chassis one subsystem at a time: motors, encoders, sensors. For a college project, the final year robotics projects guide has scopes that fit a semester.
Can I learn ROS 2 on Windows without installing Ubuntu?
Yes, through WSL2. Install Ubuntu 24.04 inside it, add the ROS 2 apt repository, and install Jazzy as you would natively. WSL 2 supports Linux GUI applications, so RViz opens in a normal window on Windows 11 or Windows 10 build 19044 or newer.
How much RAM do I need for ROS 2?
As a rule of thumb, 8 GB is workable for a couple of nodes plus RViz, 16 GB is comfortable once you add Gazebo, and 4 GB installs but thrashes. On WSL2 the Linux side competes with Windows for the same physical memory, so total system RAM matters more than the number you give the virtual machine.
Is ROS 2 free to use for a commercial project?
The core ROS 2 code is open source under the Apache 2.0 licence, so there is no licence fee for shipping a product. That is not free of cost: you still pay for integration engineering, and packages in the wider ecosystem carry their own licences. Read every dependency's licence.
Do I need a Raspberry Pi to learn ROS 2?
No. Everything here runs on a laptop, and the first steps of the learning path need no hardware. A Pi matters when the stack has to travel with the robot, and a Pi 5 with 8 GB handles nodes, a camera and RViz on a monitor.
What is the difference between ROS 2 and micro-ROS?
ROS 2 is the full framework on a computer with an operating system, Python or C++ client libraries, and the standard tools. micro-ROS is a port of the client library to microcontrollers, with a much smaller footprint and a transport that carries messages to a full ROS 2 machine. micro-ROS makes the board a node; a serial bridge makes it a peripheral.
Which ROS 2 version should I install?
Start with the long-term-support release that matches your Ubuntu version. Jazzy Jalisco targets Ubuntu 24.04 and is supported until May 2029. Check the ROS 2 releases page before installing, because a newer LTS may exist by now.
Sources and further reading
- ROS 2 documentation, ROS 2 Jazzy Jalisco release notes: general availability on 23 May 2024, Ubuntu 24.04 Noble on amd64 and arm64, and the May 2029 end of support under REP 2001.
- ROS 2 documentation, Ubuntu (deb) installation for Jazzy: current apt repository setup and the
ros-jazzy-desktoppackage. - ROS 2 documentation, Writing a simple publisher and subscriber (Python): the rclpy structure used above.
- ROS 2 documentation, Creating your first ROS 2 package:
ros2 pkg create,colcon buildandconsole_scripts. - ROS 2 documentation, Understanding ROS 2 topics:
node list,topic list,echo,hzandinfo. - ROS 2 documentation, About quality of service settings: compatibility rules, reliable as the default, and what happens when profiles do not match.
- ROS 2 documentation, Introduction to tf2: coordinate frames and the transform tree.
- ROS 2 documentation, About actions and about parameters: the definitions behind two of the five concepts.
- ROS 2 Special Interest Group, REP 105: Coordinate Frames for Mobile Platforms: the
map,odomandbase_linkconventions. - ROS 2, common_interfaces: the repository that defines
geometry_msgs/Twistand the standard message packages. - ROS 2, geometry2: the tf2 implementation behind
view_framesandtf2_echo. - ROS 2, rclpy and rclcpp: the client libraries, both released under Apache 2.0.
- micro-ROS, project site (now hosted at micro.vulcanexus.org) and the micro-ROS Arduino library: the ESP32 port and its USB serial transports.
- Canonical, Ubuntu release cycle: standard support to May 2029 and ESM to May 2034 for 24.04 LTS, and the desktop installation guide with its 25 GB storage requirement.
- Raspberry Pi, Raspberry Pi 5 product page: the 2 GB, 4 GB, 8 GB and 16 GB variants.
- Microsoft, Run Linux GUI apps with WSL: WSL 2 GUI support and the Windows build requirement.
