Ask Your Question
1

Raspberry Pi 2, ROS and GPIO access

asked 2015-08-24 23:11:28 -0500

rbaleksandar gravatar image

updated 2015-08-24 23:24:26 -0500

Hi!

I just created two nodes:

  • One is on my notebook to send status updates of a LED (the message is of type Bool)
  • The other is on my RPi2 and sets the GPIO 17 to high/low according to value in the Bool message received

Both platforms are running ROS Indigo (notebook has Debian Jessie and RPi2 has Raspbian Wheezy). I'm also using only Python (especially on the RPi2 this seems to be the much easier way to do things related to the GPIO).

My problem is the GPIO access requires superuser privileges, which rosrun doesn't seem to like (in fact rosrun obviously is not a known command when using sudo). My question is in what best way am I to handle this situation? Should ROS on such an embedded system actually be installed as root because of all the low level access to the hardware underneath or is it possible to execute rosrun in superuser temporarily just for that the time the specific node that requires such privileges is running?

edit retag flag offensive close merge delete

5 Answers

Sort by ยป oldest newest most voted
1

answered 2015-08-27 13:57:00 -0500

rbaleksandar gravatar image

updated 2016-04-07 16:32:45 -0500

After a long struggle I finally got it. There is actually no need to use sudo at all (or modify user/user groups) if you use the wiringPi library. It has a command line utility called gpio which has an export function that makes the GPIOs accessible without the need for sudo both in Python and C/C++.

UPDATE: Since Raspbian Jessie the access to the GPIOs no longer requires sudo.

edit flag offensive delete link more

Comments

dont we have to subscribe the gpio pin values or something ? Can u directly access the gpio pins inside the ros node, just by using the library, and using its name at header file? thanks for giving easy answer, by the way.

dinesh gravatar imagedinesh ( 2016-05-30 13:18:30 -0500 )edit

If I recall correctly you don't have to use any command line tools but you do have to set the pins you want to use as ins or outs. Code-wise you can put the calls to the wiringPi library anywhere in your node since this has nothing to do with ROS.

rbaleksandar gravatar imagerbaleksandar ( 2016-05-30 14:17:50 -0500 )edit

How you control the pins remotely is a different topic. You can create a service for example, which runs on your ROS on the Pi and then trigger it remotely from your PC where another ROS is running (don't forget the master-slave thing with multiple ROS in the same network).

rbaleksandar gravatar imagerbaleksandar ( 2016-05-30 14:18:52 -0500 )edit

I have the same issue but exporting the pin is not helping: $ gpio export 17 out, same error: wiringPinSetup: Must be root. gpio v 2.32 and ROS kinetic. What am I missing?

zweistein gravatar imagezweistein ( 2018-04-13 07:53:15 -0500 )edit
1

answered 2015-08-25 01:17:03 -0500

mgruhler gravatar image

You shouldn't run ROS as root anywhere. The problem why rosrun cannot be found using sudo is that you haven't set up ROS for this.

Why not allow access to the (required) low level devices? See this link about general unix file permissions and this link about how to use udev rules on an RPi for setting file permissions for the GPIOs (on the very bottom of the page).

edit flag offensive delete link more

Comments

This seems to offer a solution for a single application. Is there a way to enable non-root access to all GPIOs? Also I've read that Python has issues with such things (not to mention that one needs to modify the root configuration files like .bashrc to add the ROS and catkin workspace paths).

rbaleksandar gravatar imagerbaleksandar ( 2015-08-25 11:09:57 -0500 )edit

No, this is not for an application, but for a user group (second link). Thus, all members of this group have access, with all programs/applications they want.

Python has "issues with such things"? Whicht things?

And excatly why you would need to change the root .bashrc, sudo is not the way to go

mgruhler gravatar imagemgruhler ( 2015-08-26 01:55:38 -0500 )edit

I did change to root's .bashrc. I added 3 paths there - the one in /opt/ros/.../dist-packages, another one from the catkin_ws (again dist-packages for Python2.7) and finally the one from /usr/lib/.../dist-packages. It is still complaining. I even changed my root to keep the PYTHONPATH (->sudoers)

rbaleksandar gravatar imagerbaleksandar ( 2015-08-26 03:14:16 -0500 )edit

The script changes the group of the files to the group passed in ( gpio ) and changes the permissions mode of the files to 0660

Ah, I missed that.

rbaleksandar gravatar imagerbaleksandar ( 2015-08-26 03:14:39 -0500 )edit
0

answered 2015-08-27 15:23:12 -0500

duck-development gravatar image

The Anwer is simpel

  1. sudo su
  2. source /opt/ros/indigo/setup.bash
  3. source /home/pi/catkin_ws/devel/setup.bash
  4. rosrun ...

or

  1. sudo -E rosrun ...

Greet Anton

edit flag offensive delete link more

Comments

I tried this today early in the morning (before I found the better solution that I've posted here - no sudo at all!) and it worked but I had difficulties with Python scripts using rospy (added option to preserve the PYTHONPATH for sudoers, added all dist-packages paths related to ROS but...Meh...

rbaleksandar gravatar imagerbaleksandar ( 2015-08-27 16:55:46 -0500 )edit

Thinking I'm actually going to write a ROS article in the tutorials section and even record a video on YouTube for this considering the amount of posts I've found only where people were stuck on this.

rbaleksandar gravatar imagerbaleksandar ( 2015-08-27 16:56:47 -0500 )edit

Never use sudo with ROS (I know, never say never). Especially not for hardware access. I've never come across a situation that actually required it. As @mig wrote, just give the user access to the hardware by giving him permissions and / or adding to the appropriate group.

gvdhoorn gravatar imagegvdhoorn ( 2015-08-28 06:34:55 -0500 )edit

As I've mentioned already - the gpio utility provides access to the pins without any sudo or even the need to tinker with usergroups and such. It goes through /sys/class/.

rbaleksandar gravatar imagerbaleksandar ( 2015-08-28 06:57:35 -0500 )edit

I was replying to the answer by @duck-development, not your post. Also: note that going through sysfs adds (quite) some latency, so may not be suitable for all applications.

gvdhoorn gravatar imagegvdhoorn ( 2015-08-28 07:08:13 -0500 )edit
0

answered 2015-12-06 13:26:57 -0500

If you use the Jessie version of Raspbian access to gpio from python no longer needs sudo permission by default

edit flag offensive delete link more
0

answered 2016-03-14 10:04:29 -0500

Cerin gravatar image

By default, yes, GPIO requires superuser, but you can easily change this by doing:

sudo su -
usermod -a -G gpio <youruser>
echo 'SUBSYSTEM==\"gpio*\", PROGRAM=\"/bin/sh -c 'chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio; chown -R root:gpio /sys/devices/virtual/gpio && chmod -R 770 /sys/devices/virtual/gpio'\"' > /etc/udev/rules.d/99-com.rules

i.e. Add your user to the gpio group and then create a udev rule to automatically give that group permission to access the GPIO hardware.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2015-08-24 23:11:28 -0500

Seen: 3,257 times

Last updated: Apr 07 '16