Experiment with ROS, Raspberry PI and Python
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.
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 but in my code 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.