📜 ⬆️ ⬇️

Using GPIO in Raspberry Pi from ROS

Good afternoon, dear readers of Habr!

Recently, studying books on the practical use of ROS, I learned about the interesting possibility of using GPIO ports available on Raspberry Pi from ROS. In this article I want to talk about how this is possible. To access the GPIO ports on the Raspberry Pi board, we will use the Wiring Pi library. Who are interested in this topic, please under the cat.

Wiring Pi installation


First, install the Wiring Pi library on the Raspberry Pi:

git clone git://git.drogon.net/wiringPi cd wiringPi ./build 

The GPIO port assignment scheme for the Raspberry Pi can be found in the article .
')

Blink LED Example


We will make a simple example of LED blinking with the help of commands that will be published in the ROS topic. We connect the LED to the pins on the Raspberry Pi in this way: a short leg (cathode) to the GND port, a long leg (anode) to the GPIO18 port (pin number 12 in the diagram).

We will have this connection:

image

Create a ROS package:

 catkin_create_pkg ros_wiring_example 

Create a C ++ file with the following code:

 #include "ros/ros.h" #include "std_msgs/Bool.h" #include <iostream> //Wiring Pi header #include "wiringPi.h" //Wiring PI first pin #define LED 1 //Callback to blink the LED according to the topic value void blink_callback(const std_msgs::Bool::ConstPtr& msg) { if(msg->data == 1){ digitalWrite (LED, HIGH) ; ROS_INFO("LED ON"); } if(msg->data == 0){ digitalWrite (LED, LOW) ; ROS_INFO("LED OFF"); } } int main(int argc, char** argv) { ros::init(argc, argv,"blink_led"); ROS_INFO("Started RPi Blink Node"); //Setting WiringPi wiringPiSetup (); //Setting LED pin as output pinMode(LED, OUTPUT); ros::NodeHandle n; ros::Subscriber sub = n.subscribe("blink_led",10,blink_callback); ros::spin(); } 

Here we include the library file wiringPi.h and add initialization by calling the method wiringPiSetup (). The pinMode () and digitalWrite () methods perform the same thing as the methods of the same name used for the Arduino.
Create a CMakeLists.txt file with the following content:

 cmake_minimum_required(VERSION 2.8.3) project(ros_wiring_examples) find_package(catkin REQUIRED COMPONENTS roscpp std_msgs ) find_package(Boost REQUIRED COMPONENTS system) //Include directory of wiring Pi set(wiringPi_include "/usr/local/include") include_directories( ${catkin_INCLUDE_DIRS} ${wiringPi_include} ) //Link directory of wiring Pi LINK_DIRECTORIES("/usr/local/lib") add_executable(blink_led src/blink.cpp) target_link_libraries(blink_led ${catkin_LIBRARIES} wiringPi ) 

We will have simple logic. Create a subscriber to the topic blink_led type Boolean. If the value is 1, then we light the LED. If 0 comes, then the LED is off.
Next, compile the package:

 cd ~/<catkin_ws> catkin_make source devel/setup.bash 

Finally, run the node:

 roscore 

We need to login as root to start the node:

 sudo -s cd /home/pi/<catkin_ws> rosrun ros_wiring_example blink_led 

Send a command to turn on the LED:

 rostopic pub /blink_led std_msgs/Bool 1 

Send a command to turn off the LED:

 rostopic pub /blink_led std_msgs/Bool 0 

Everything is very simple. We simply specify the topic blink_led, the message type std_msgs / Bool and the value: 0 or 1.

Now you can use all the power of GPIO ports on the Raspberry Pi in your projects. We can connect ultrasonic distance sensors, servomotors and so on. and manage them or retrieve data from them in ROS nodes without the need to connect additional Arduino boards.

Good luck with your Raspberry Pi projects and see you soon!

Source: https://habr.com/ru/post/369679/


All Articles