Connect IR Sensor to ROS2

Summary

Connect IR Sensor to ROS2

Why

  • Outdated documentation of how to do it - rosserial is ROS1, ros2arduino was updated 2 years ago
  • Approachable python interface vs more complex frameworks - micro-ros, arduino-cli, johnny-five requires nodejs install
  • No need to code in Arduino C - enabled by the Firmata protocol

Software

  • ROS2 Humble
  • pyFirmata
  • Arduino IDE

Hardware

Steps:

  1. Wire IR sensor using this diagram

  2. On the arduino IDE, upload File > Examples > Firmata > StandardFirmata to the Arduino Uno board (following this tutorial)

  3. Get device location from the lower right part of Arduino IDE, for me it was /dev/ttyACM0

  4. Install pyfirmata
    sudo apt install python3-pip
    python3 -m pip install pyfirmata
    python3
    
  5. Modify device location in the script ir_sensor_pyfirmata.py.

  6. Test if the range readings are read by pyfirmata by running the script.

  7. Test message publishing and subscribing using this tutorial and replacing the publisher and subscriber functions with the scripts in this repo.
    • publisher_member_function.py
    • subscriber_member_function.py

Bonus:

docker run -it --net=host --device /dev/dri/ --device /dev/ttyACM0 -e DISPLAY=$DISPLAY -v $HOME/.Xauthority:/root/.Xauthority:ro osrf/ros:humble-desktop-nav2

References




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Evaluating physical workflows with VLMs
  • Autonomy from scratch
  • Conversation Prompts App
  • Simulate robot navigation inside Docker
  • Python and Arduino via Smartphones