Using ROS to control GPIO pins on a Raspberry Pi 3

As the title says, it was time for me to document how to do the "simple" task of using ROS (Robot Operating System) to turn an LED on/off (well, we can do a LOT more than that by using the GPIO pins, but as a baseline, it's a starter).

Because I've installed ROS onto the Raspberry Pi itself, I can create ROS nodes directly on the RPi.
So let's get on with it and create a simple demo to blink an LED using ROS topics from the RPi.

First step, we have to download "wiringPi"
$ git clone git://git.drogon.net/wiringPi
$ cd wiringPi
$ sudo ./build

The interesting thing is that everyone decides how they are going to refer to the GPIO pins on the RPi...I just keep track of the physical pin (as that doesn't change!)
And here is the GPIO pin layout in relation to wiringPi:
https://projects.drogon.net/raspberry-pi/wiringpi/pins/

Now that we've installed the library we're going to be using, let's switch to creating a ROS package for the LED blink demo.
I've already created a workspace previously, as I was using the ROS ROBOTICS PROJECTS PDF as a walkthrough for the USB web cam sample (which worked great from an Ubuntu VMware, but fails on a real RPi...).
Previously, I was using the RPi to connect to the Servo controller and have used quite a few of the GPIOs already, so, I just decided to unplug a GPIO and re-purpose it for this example.
I plugged into physical PINs 6 and 10, so that is actually GPIO 12 and 20...

As I had already created a workspace I could just re-use it, but I'll re-document it here just incase I have to rebuild the SD Card (again!)

$ mkdir -p ~/ros_project_dependencies_ws/src
$ cd ~/ros_project_dependencies_ws/
$ catkin_make
$ source devel/setup.bash
(also remember to add that source command to the end of the ./bashrc file)

$ catkin_create_pkg ros_wiring_example roscpp std_msgs
$ cd ros_wiring_examples
$ mkdir src

Now create a file called blink.cpp, with the following content:

That code will SUBSCRIBE to a topic called /led_blink which is a Boolean type.  If the value is TRUE, the LED will turn on, otherwise it will be off.

Navigate up to the ros_wiring_examples folder and you need to edit the CMakeLists.txt file to be lie so:

Now, we need to re-run the catkin_make command, so:

$ cd ~/ros_project_dependencies_ws/
$ catkin_make
$ source devel/setup.bash

That's it, we're done.  We can now run and test this.  One thing to remember, on the RPi, when you are interacting with the GPIO, you need to be the root user.

TERM1:
$ roscore


TERM2:
$ sudo -s
$ cd ~/ros_project_dependencies_ws/build/ros_wiring_examples
NOTE that the folder is BUILD and not in /src
$ ./blink_led
(this will execute the compiled/built blink_led application built from the blink_led.cpp from above)

If we take a look at the RPi, we see the LED is "off":



TERM3:  (sending a "1" will turn on and a "0" will turn off)
$ rostopic pub /led_blink std_msgs/Bool 1


Now, if we look again, we see the LED is "on":


$ rostopic pub /led_blink std_msgs/Bool 0


and if we take a look at the blink_led app output, we can see that the console log shows the behaviour from above:



yay! we can now turn an LED on and off by publishing a value to a rostopic.... now....that's the baseline that we can build upwards from!  Controlling servos, sending signals to perform alsorts of actions can now happen!....

Comments

Post a Comment