The driver layer: communication between firmware and ROS2
Atlasbot · 17/8/2026 · 12 min read
Article The panorama of robot software puts "Communication" as the layer between Firmware and ROS2. This is Driver Layer — not a motor driver (discussed in article Motor Driver), but a software driver: a piece of code that runs on the main computer (ROS2), acting as a translator between the firmware's own protocol and the standard topic/message ecosystem of ROS2.
Key concepts
Firmware (running on MCU) and ROS2 (running on SBC) are two completely different worlds — MCU knows nothing about DDS, topics, or message types of ROS2; it only knows how to read/write bytes via UART. The driver layer stands in the middle, doing exactly one thing: translate correctly, translate fast enough between two different data formats, does not contain control logic (that is the job of Nav2/PID learned in the Motor Control lesson).
How it works
Uplink (firmware → ROS2):
Read raw data via Serial (e.g. "e 1523 1489")
→ parse into number → package into standard message (EncoderVals)
→ publish to topic ROS2
Downstream (ROS2 → firmware):
subscribe /cmd_vel (ROS2 standard Twist message)
→ calculate PWM commands for each wheel
→ packaged into a simple text command string (e.g. "o 100 100")
→ sent via Serial to firmware
This is exactly the architecture used in the Diff Robot project (showcase section of this page): package diff_serial_bridge reads/writes via Serial UART 57600 baud with a simple text script, easy to debug directly through the terminal.
A notable design detail (also from the Diff Robot project): instead of directly using the existing ROS2 data type for encoder data/motor commands, the project defines its own message type (MotorCommand, EncoderVals, MotorVels in package diff_serial_mgs) — making the interface between the driver layer and other nodes clear clear semantics, easy to expand fields without breaking code that is using standard data types elsewhere in the system.
In summary: Mixing control logic into this layer (for example, arbitrarily smoothing the speed in the driver instead of letting the upper layer decide) is a common architectural error, making the system difficult to debug because the logic is scattered and unclear.
Technical details
Formulas
The time it takes to transmit a message via Serial depends directly on the baud rate and the number of bytes that need to be sent — which determines whether a text or binary protocol is more suitable at the desired communication frequency:
Transmission_time ≈ (Number of bit_messages) / Baud_rate
For example message text "e 1523 1489\n" (13 characters ≈ 130 bits with start/stop bit):
At 57600 baud: 130 / 57600 ≈ 2.3ms
For example, equivalent binary message (2 numbers int16 + header + checksum ≈ 6 bytes = 60 bits):
At 57600 baud: 60 / 57600 ≈ 1ms
At a sending frequency of 50Hz (20ms cycle), both protocols have plenty of time space —
The difference only becomes significant when the communication frequency increases to a few hundred Hz
Parameters
| Text protocol | Binary protocol | |
|---|---|---|
| Easy to debug | It's very easy — open the serial terminal, type in the command, and read it right away | Difficult — requires separate decoding tool |
| Bandwidth | More expensive (each number is multiple ASCII bytes) | More economical (each number is a fixed number of bytes) |
| Parsing speed | Slower (must parse the string) | Faster (direct reading of byte structure) |
| Suitable | Small robot, moderate communication frequency (below ~100Hz) | Robots need high bandwidth and many large frequency sensors |
Practical applications
For most small/medium sized AMRs, a simple text protocol is a reasonable choice — the ability to debug directly by typing manual commands through the serial terminal during development is much more valuable than the few percent of bandwidth saved from the binary protocol, unless the robot really needs to transmit large amounts of data (e.g. camera images) over that same Serial channel — as calculated in the Formula section, the difference in transmission time between the two protocols is only really significant when the communication frequency exceeds the Typical 50-100Hz of encoder data/motor commands.
Practical example
def thoi_gian_truyen(so_bit, baud_rate):
return so_bit / baud_rate * 1000 # ms
baud = 57600
# Text: "e 1523 1489\n" ~ 13 characters, ~10 bits each (start+8data+stop)
t_text = thoi_gian_truyen(13 * 10, baud)
# Binary: header(1) + 2x int16(4) + checksum(1) = 6 byte = 48 bit + framing overhead
t_binary = thoi_gian_truyen(60, baud)
print(f"Text protocol: {t_text:.2f} ms/message")
print(f"Binary protocol: {t_binary:.2f} ms/message")
print(f"So voi chu ky 20ms (50Hz): text con du {20-t_text:.1f}ms, binary con du {20-t_binary:.1f}ms")
Output:
Text protocol: 2.26 ms/message
Binary protocol: 1.04 ms/message
So voi chu ky 20ms (50Hz): text con du 17.7ms, binary con du 19.0ms
Both protocols have a lot of extra time compared to the 50Hz cycle - numerical proof for choosing the text protocol because it is easy to debug without a significant trade-off in speed at the typical communication frequency of small-medium sized AMR.
Common mistakes
- Insert control logic into the driver layer — as stated in the Summary section, the driver should only translate data, should not arbitrarily smooth the speed or make decisions to change the upper control layer.
- No mechanism to detect packet loss/timeout — Serial does not guarantee data arrival on time or in the right order in all cases — electrical noise, loose cables, or full buffers can cause data packet loss or delay; Missing timeout/checksum causes the odometry to be permanently off without any way to detect it.
- Using the generic standard ROS2 data type directly instead of a separate message type with clear semantics — makes the code harder to read (you have to guess which field contains what) and difficult to extend new data without breaking other places that are using the same data type.
- Reading Serial in a blocking fashion on the same main processing thread — if reading Serial data blocks (blocks) the main thread waiting for incoming data, the entire driver node can hang when the Serial connection fails, having a knock-on effect on other nodes that depend on data from it.
Conclusion
The Driver Layer is the thinnest but most easily abused layer in the entire AMR software architecture — the discipline of keeping it doing just one thing (translating data, not making decisions) makes the system much easier to debug when things go wrong. The next article in the category goes into the ROS2 Layer — the communication infrastructure layer that the driver layer has just learned to "talk" to.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.