ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to define GPIO pins in ROS on a Raspberry Pi 2?

asked 2016-04-25 07:13:16 -0600

mukut_noob gravatar image

I am using a Raspberry pi 2 with ubuntu 14.04 and ros Indigo installed. I want to make a robot which would make a map of the surroundings and then autonomously navigate in it. I have sonars and no other sensors. So my question is how can write the GPIO code in ROS node thats going to publish and populate my range/sensor.msg so that my Raspberry Pi can communicate with the sonars?

It would also be help full if you can take a look at this question and see if you can help.

Thanks & Regards, Mukut

edit retag flag offensive close merge delete



It depends on your sonar sensors. What kind of output signal do they have? Analog signal, SPI, I2C, USB? A good starting point would be wiringPi if you want to use the GPIO

JohnDoe2991 gravatar image JohnDoe2991  ( 2016-04-25 09:33:30 -0600 )edit

Thanks john for your fast reply! I am using a HC-SR04. It works fine with the digital pins of my Raspberry pi. So I think it's digital.

mukut_noob gravatar image mukut_noob  ( 2016-04-25 12:01:49 -0600 )edit

Yes, it is a digital signal, where you can measure the time and calculate the distance to the object. A short google search also found this Pi driver. But either with this driver or wiringPi, you have tow write the node yourself.

JohnDoe2991 gravatar image JohnDoe2991  ( 2016-04-25 12:35:11 -0600 )edit

Okay, so all I have to do is to include this driver in the node right? Also can you take a look at my other question?

mukut_noob gravatar image mukut_noob  ( 2016-04-25 20:58:00 -0600 )edit

You need to compile and load the driver first. It is documented on the github page. I cannot answer your second question, I don't know anything about mapping.

JohnDoe2991 gravatar image JohnDoe2991  ( 2016-04-26 00:31:06 -0600 )edit

So it can also be written in python? It would be the same? And in that case I need not need any external GPIO library like the wiring Pi because it has its own library, RPi.GPIO. Is it possible?

mukut_noob gravatar image mukut_noob  ( 2016-04-26 21:15:02 -0600 )edit

It should be possible. I haven't used RPi.GPIO yet, but after a short google search I found that it also supports interrupts (described here). In theory it will work, but I don't know how precise it will be.

JohnDoe2991 gravatar image JohnDoe2991  ( 2016-04-27 00:23:53 -0600 )edit

okay. So its better with wiringpi.

mukut_noob gravatar image mukut_noob  ( 2016-04-27 01:08:31 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-04-27 02:54:36 -0600

JohnDoe2991 gravatar image

Summary of the comments: To use the Pi's GPIO in ROS, you have to use a third party library, for example wiringPi (C++) or RPi.GPIO (Python). Both examples support interrupts (examples: wiringPi, RPi.GPIO), which you need to measure the time the sound signal of your HC-SR04 needs to travel to an object and back to the sensor.

Alternatively, there's a driver for the Raspberry Pi and the HC-SR04 here (Github). I have never used this driver, so I cannot say if it works or not.

Also think about the fact, that the default Raspbian is not a realtime operating system. The measured times are not consistent and may vary depending on the Pi's CPU load. You should test the variation in your system before trusting any measurements.

edit flag offensive delete link more


Yes I have tested it and I am using Ubuntu 14.04 on my Pi. And I will be use either wiringpi or RPi.GPIO. I think it would be more precise.

mukut_noob gravatar image mukut_noob  ( 2016-04-27 04:43:41 -0600 )edit

Question Tools

1 follower


Asked: 2016-04-25 07:13:16 -0600

Seen: 4,011 times

Last updated: Apr 27 '16