Ros Environment in root

Viewed 3620

I have a ros (kinetic) environment set up on a raspberry pi 3 and am trying to get ros to execute upon startup via a simple bash script which calls roslaunch. Ros works in the user domain but fails when called from root. Here is my launch_ros.sh script:

#!/bin/bash
source /home/pi/ros_catkin_ws/devel/setup.bash
export PYTHONPATH=/opt/ros/kinetic/lib/python2.7/dist-packages
roslaunch my_pkg pkg_launch.launch

When I run sudo /home/pi/Desktop/ros_launch.sh the roscore crashes with

ERROR: cannot launch node of type [rosout/rosout]: can't locate node
[rosout] in package [rosout] failed to start core service [/rosout]
The traceback for the exception was written to the log file

But, if I comment out

source /home/pi/ros_catkin_ws/devel/setup.bash

and execute /home/pi/Desktop/ros_launch.sh, ros works fine.

Also worthy of noting is if I leave the above source line uncommented when running in the user domain I get the same error as I do in the root. I think this might be pointing me to the solution but I am still very new to ros. Has anyone come across this issue and found a solution?

4 Answers

In order to run a node as root after changing your shell to root using commands like sudo -i, You can source your current bash profile thats located inside your normal user .bashrc and use it inside root shell.

Try the following code:

#!/bin/bash
source /opt/ros/kinetic/setup.bash
source /home/pi/ros_catkin_ws/devel/setup.bash
export PYTHONPATH=/opt/ros/kinetic/lib/python2.7/dist-packages  
roslaunch my_pkg pkg_launch.launch

You need to source your workspace devel to be able to find your own package. But, you need to source ROS devel to be able to use roscd, roslaunch, ...

In the code below I added:

source /opt/ros/kinetic/setup.bash

to source ROS and be able to use it.

PS: If it's still not working you should try a short delay before running roslaunch.

I was accidentally in a conda environment (base only) and it was messing up big-time. Try disabling any Python virtual environments.

It really worked and it inspired me to report that I didn't have rosnode as a program when I was planning to call ROS scripts in my own applications. Calling source .bashrc directly from the Raspberry Pi's system would refresh the terminal, but there was no way for my program to take over. The solution was to place the required ROS environment scripts in a separate script like name init_env.sh and then call source init_env.sh before any other ROS scripts were executed.

Related