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
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.
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!
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.
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)