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

How to use ROS in Colab?

asked 2020-08-31 09:28:29 -0500

Fares Mhaya gravatar image

Now I'm a complete newbie when it comes to both ROS and Colab. I found this code a while ago, and I want to use it in Google Colab because using the drive makes things easy due to the immensely large dataset I have to deal with. The problem is, I don't know how to import and use ROS libraries (e.g. rospy, roslib) in Colab. Can anybody help me out with that? I've got ROS Melodic installed on ubuntu 18.04.5 LTS by the way.

edit retag flag offensive close merge delete

Comments

I have no experience with Google Colab, but it appears to host Jupyter notebooks.

That would make this Python.

To get access to rospy and related packages without having to install ROS, you could try and see whether rospypi/simple suffices.

And/or take a look at something like RoboStack/jupyter-ros.

gvdhoorn gravatar image gvdhoorn  ( 2020-09-01 02:44:16 -0500 )edit

Hello. I'm trying to do the same as you did: run ROS in Colab. Did you get any progress? If so, could you share maybe your notebook or any tips? Thanks!

Gustavo Lima gravatar image Gustavo Lima  ( 2021-01-02 13:23:05 -0500 )edit

Rospy simple seem to work, but since we don't have a master it's kinda useless. If you have a valid external IP, I suppose you can just test it, otherwise you need something like this: https://imadelhanafi.com/posts/google... , which is basically a tunnel to connect colab to your local network. I will see if I can make this work.

fbelmonteklein gravatar image fbelmonteklein  ( 2021-02-05 06:04:21 -0500 )edit

I'd like to mention the ROS Development Studio: https://app.theconstructsim.com/ perfect solution for collaberation, and you don't have the hassle of installing & configuring ros. (not ideal for your largedataset, but worth a mention in this topic)

crnewton gravatar image crnewton  ( 2021-02-05 07:42:17 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2021-02-19 12:55:50 -0500

vschmidt gravatar image

I built on the answer above and added a run script that sets the ROS_MASTER_URI and sources the ROS environment. It has to be run in each cell, but this seems to work.

!sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

!sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

!sudo apt update

!sudo apt install ros-melodic-desktop-full

!pip install --extra-index-url https://rospypi.github.io/simple/ rospy

import rospy
### wait, we need a master:
!echo ""
!printf -- '#!/bin/bash\n' > ros_start.sh
!printf -- 'source /opt/ros/melodic/setup.bash\n' >> ros_start.sh
!printf -- '/opt/ros/melodic/bin/roscore' >> ros_start.sh
!chmod +x ros_start.sh
!cat ros_start.sh

# Create a utility to set ROS_MASTER_URI and source the environment.
with open("run",'w') as f:
  f.write('#!/bin/bash\n')
  f.write('ROS_MASTER_URI="0.0.0.0"\n')
  f.write('source /opt/ros/melodic/setup.bash\n')
  f.write('exec $@\n')

f.close()
!chmod +x run
!echo ""
!cat run

Once this executes, one can then do the following:

import subprocess
import os
prc = subprocess.Popen([os.getcwd()+"/ros_start.sh"])
# Init the cell with 'run'
!./run
###now we have a master!
rospy.get_published_topics()

--

[['/rosout_agg', 'rosgraph_msgs/Log']]

Or you can execute ROS command line arguments like this:

!./run rostopic list
/rosout
/rosout_agg

And here is an example creating a node, publisher and subscriber: [Although I admit, I am not sure why I'm getting doublets of messages.]

!./run

from std_msgs.msg import String

rospy.init_node('test_node')
P = rospy.Publisher('/test',String,queue_size=10)
def testcb(data):
  print("Got %s" % data.data)

S = rospy.Subscriber('test', String, callback=testcb)

for i in range(10):
  P.publish(str(i))

-

WARNING: cannot load logging configuration file, logging is disabled
Got 0
Got 0
Got 1
Got 1
Got 2
Got 2
Got 3
Got 3
Got 4
Got 4
Got 5
Got 5
Got 6
Got 6
Got 7
Got 7
Got 8
Got 8
Got 9
Got 9
edit flag offensive delete link more
1

answered 2021-02-05 06:37:25 -0500

fbelmonteklein gravatar image

updated 2021-02-28 17:09:47 -0500

Well, a colab notebook is basically a docker instance, so you can install ROS in a colab notebook!

What I did:

!sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

!sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

!sudo apt update

!sudo apt install ros-melodic-desktop-full

!pip install --extra-index-url https://rospypi.github.io/simple/ rospy

import rospy
### wait, we need a master:

!printf -- '#!/bin/bash\n' > ros_start.sh
!printf -- 'source /opt/ros/melodic/setup.bash\n' >> ros_start.sh
!printf -- '/opt/ros/melodic/bin/roscore' >> ros_start.sh
!chmod +x ros_start.sh
!cat ros_start.sh

import subprocess
import os
prc = subprocess.Popen([os.getcwd()+"/ros_start.sh"])

Here (as pointed out by shandilya1998 and Gustavo Lima) you need to wait a bit since subprocess is asynchronous and the master will take some time to load.

###when we have a master the following line should work.
rospy.get_published_topics()

Here is the gist for it, if you want to try it out yourself. https://gist.github.com/frederico-kle...

edit flag offensive delete link more

Comments

1

I tried using your approach, but after the installation is done, when I try to run the master, I am getting the following error- OSError: [Errno 99] Cannot assign requested address

shandilya1998 gravatar image shandilya1998  ( 2021-02-15 23:42:33 -0500 )edit
1

Thanks for the help. I got the same error that @shandilya1998 said

Gustavo Lima gravatar image Gustavo Lima  ( 2021-02-27 11:16:23 -0500 )edit

You are right. There is something strange happening, because after some time it works. I think maybe the roscore is still loading on the background when you ask for the topics. I've updated the answer to show this.

fbelmonteklein gravatar image fbelmonteklein  ( 2021-02-28 17:04:34 -0500 )edit

Question Tools

3 followers

Stats

Asked: 2020-08-31 09:28:29 -0500

Seen: 3,362 times

Last updated: Feb 28 '21