Go Beyond the Blink: When Your Robot Starts to Feel
The first time your robot reacts rather than merely moves, something energizes you. The sensors pulse; the gears hum; for once the robot knows, not only acts. In this blog, we’ll discuss how to collect raw sensor data for robotics with ROS 2. From encoder ticks to IMU readings, this is where autonomy starts. Want to have your robot listen to the planet?
1. Wire the “Throttle Cable” — L298D ↔ ESP32
Analogy: The ESP32 is the driver’s hand, the L298D is the gas pedal.
Why It Matters: Before your robot can read the world, it must first be able to move through it. Connecting the ESP32’s GPIO pins (17–20) to the L298D motor driver establishes the basic motor interface.
How: Flash a basic C++ sketch that delivers a forward PWM pulse:
analogWrite(17, 128); // IN1
analogWrite(18, 0); // IN2
analogWrite(19, 128); // IN3
analogWrite(20, 0); // IN4
This delivers a balanced 50% PWM to both motors—enough to see the wheels turn in place.
This foundational connection ensures your robot can collect raw sensor data for robotics and respond to drive commands in real-time, enabling motion tests and eventual mapping.
2. Install micro-ROS — The “Walkie-Talkie” Between Brains
Analogy: A two-way radio letting the Pi shout orders and hear back.
Why It Matters: You can’t collect data if your microcontroller and your main compute unit (like a Raspberry Pi) don’t speak the same protocol. micro-ROS bridges that gap by integrating low-power embedded systems into the ROS 2 ecosystem.
How:
- Build micro-ROS firmware with colcon build on the ESP32 side.
- On the Pi, launch the agent:
ros2 run micro_ros_agent micro_ros_agent serial --dev /dev/ttyUSB0
3. Translate /cmd_vel to Motor PWM
Analogy: GPS voice (“turn left”) becomes a nudge on the steering wheel.
Why It Matters: Commanding motion with /cmd_vel (the standard ROS velocity command topic) means your robot is now responding to high-level input—just like steering a car.
How: Subscribe to /cmd_vel, then map the linear.x and angular.z components to left/right motor PWM values. Here’s the pseudocode:
left_pwm = speed + rotation;
right_pwm = speed - rotation;
Then set PWM using analogWrite() accordingly.
This simple transform converts velocity commands into direct action, helping your robot collect raw sensor data for robotics and ground its mobility.
4. Measure Reaction Time
Analogy: Stopwatch from driver shout to wheel spin.
Why It Matters: Responsiveness is critical for real-time robotics. Latency in command execution affects path accuracy and control stability. Measuring round-trip time helps fine-tune performance.
How: Publish a quick /cmd_vel, then listen on /motor_state or a custom feedback topic. Use ROS 2 timestamps to measure round-trip delay:
ros2 topic echo /motor_state
You’re aiming for <80 ms. If not, investigate buffer sizes, Wi-Fi stability, or firmware bottlenecks.
5. IMU, The Robot’s Inner Ear
Analogy: Your sense of balance when eyes are closed.
Why It Matters: Inertial Measurement Units (IMUs) provide angular velocity and linear acceleration—essential for orientation, stabilization, and inertial navigation. You can read more about IMU fundamentals in robotics in this primer by VectorNav, a leading provider of precision inertial sensors.
How:
- Connect via I2C (typically SDA/SCL pins)
- Calibrate flat
- Publish data to /imu
On the Pi:
ros2 topic echo /imu
Visualize using the RViz IMU plugin to verify pitch, roll, and yaw. This data enables accurate motion estimation even in the absence of external localization.
6. Encoders – The Odometer
Analogy: Car trip-meter counting wheel clicks.
Why It Matters: Encoders track wheel rotation, essential for measuring distance traveled. It’s your robot’s main dead-reckoning tool—indispensable for environments without GPS.
How: Use interrupt service routines (ISRs) on the ESP32 to read encoder channels A and B. Convert tick counts to distance using known wheel circumference and gear ratio.
void IRAM_ATTR encoderISR() {
ticks++;
}
Publish to /wheel_odom as a ROS 2 topic, and monitor:
ros2 topic echo /wheel_odom
7. Broadcast the Full Telemetry Pack
Analogy: Group chat channels—the Pi listens on “drive” and reads “feelings.”
Why It Matters: Collecting raw sensor data in robotics isn’t useful unless it’s shared in real time. ROS 2’s pub-sub architecture makes this seamless.
How: On ESP32:
- Publish: /imu_raw, /wheel_odom
- Subscribe: /cmd_vel
Spin at 50 Hz using rclc_executor for responsive telemetry. Each message completes a feedback loop that enhances your robot’s responsiveness and self-awareness.
8. Quick Track Test
How:
- Drive with:
ros2 run teleop_twist_keyboard teleop_twist_keyboard
- Watch /imu change as you turn.
- Record session:
ros2 bag record /imu /wheel_odom /cmd_vel
This creates a full-time log of your robot’s responses, essential for debugging, replay, and testing algorithms like SLAM.
9. Why It Matters for Autonomy
This step unlocks the sensory foundation of autonomous navigation:
- Motion: ROS 2’s /cmd_vel becomes actual PWM motor movement.
- Perception: Sensors feed live information into the control loop.
- Odometry: Wheel and IMU data feed into SLAM and localization.
Without real-time sensor data collection, autonomy is guesswork. By collecting raw sensor data for robotics, you’re building a robot that understands both where it is and where it’s going. The fusion of encoder and IMU data creates reliable odometry through kinematic equations, which will soon anchor your robot to a digital map.
What’s Next After You Collect Raw Sensor Data for Robotics: From Raw Data to Maps
Now that you can collect raw sensor data for robotics from your robot using ROS 2, you’re ready to map the world. Next in the series: Mapping 101, using LiDAR and odometry to build SLAM maps.
Want to build smarter, more autonomous machines? Robotisim offers in-depth ROS 2 insights, expert blogs, and real-world engineering strategies to help you integrate sensors, actuators, and autonomy in your next robotics project.
Explore robotics deeper at Robotisim, and take the next step toward building intelligent, sensor-driven robots that navigate with confidence.