I’ve always been a robotics geek and have started exploring ROS. This might help as a ‘how to get started’ post if you’ve wondered how to use ROS with the Raspberry Pi and want to stick to Python.
Using ROS is likely absolute overkill for most home projects but I am working up to quite a complex project involving different sensors, GPS and at least two motors, so ROS is actually a good fit for where I’m headed.
It will also allow me to keep everything very decoupled and simple. ROS has a heap of features that will come in handy in the future e.g. logging, data play back, data management, test framework, state and config management and more.
I intuitively like the architecture because I know it will keep things simple and it has decent Python libraries which will make for speedy development. I don’t want to be writing a framework, rather I want to focus on motor, sensor and logic code.
ROS only supports up to Python 2.7 which’ll be a problem if your Raspberry Pi HATs or pHATs have supporting Python 3 libs only.
I’m going to assume you know some programming, are comfortable on the linux shell, know the basics about Raspberry PI GPIO pins so won’t do much extra explanation.
Here we go…
Raspberry PI setup.
Put simple push switches on pins 18 and 24 on a breadboard.
Raspberry PI ROS installation
Follow the instructions over here.
If something fails, it’ll most likely to be an environment variable issue, e.g. for me this solved packages not being found.
Also, I only installed the shell version, not the full desktop version.
The Python code
See my code in GitHub. Everything other than the scripts directory was autogenerated by a ROS.
I created a new ROS package first as explained here, my two python scripts live in the scripts directory. The code is based on this tutorial code.
You can configure a launch file #, but the simplest way to test this is to run the following in three separate shells:
All from within catkin workspace directory:
Run the ROS OS.
Run the talker
rosrun test talker.py
Run the listener
rosrun test listener.py
Press one one of the buttons and the output might look like:
pi@raspberrypi:~/catkin_ws/src/test/scripts $ rosrun test listener.py [INFO] [1556918461.361843]: heading 360 [INFO] [1556918461.964700]: heading 15 [INFO] [1556918462.867729]: heading 30 [INFO] [1556918463.667803]: heading 15 [INFO] [1556918464.167762]: heading 0 [INFO] [1556918464.372864]: heading 345 [INFO] [1556918464.778167]: heading 330
‘heading’ is just a variable name that corresponds to a compass heading where the buttons turn left or right deducting or subtracting 15 degrees from the previous value.
In this example, we see ROS decoupling sensor reading code from the action code. It would be possible to add more hardware to the PI and wire it together with more publisher/subscriber code and custom logic. See more on publishers and subscribers.
e.g. instead of just outputting data in the listener like this:
[INFO] [1556918464.372864]: heading 345 [INFO] [1556918464.778167]: heading 330
logic could be triggered to drive a motor, move a servo or trigger a solenoid. With a more complicated robot, ROS is ideal to keep the application simple and all parts decoupled, and allow incremental development.