I am trying to connect Jupyter Notebook (provided by Binder) with ROS in my laptop using rosbridge server from rosbridge_suite
.
Previously I was able to connect it to Google Colab by using following script(using roslibjs
),
from IPython.display import Javascript, display
from google.colab import output
display(Javascript(url="https://cdn.jsdelivr.net/npm/roslib@1/build/roslib.js"))
output.eval_js('''
// Connecting to ROS
// -----------------
var ros = new ROSLIB.Ros({
url : 'ws://localhost:9090'
});
ros.on('connection', function() {
console.log('Connected to websocket server Hi da.');
});
ros.on('error', function(error) {
console.log('Error connecting to websocket server: ', error);
});
ros.on('close', function() {
console.log('Connection to websocket server closed.');
});
// Subscribing to a Topic
var listener = new ROSLIB.Topic({
ros : ros,
name : '/Geomagic/joint_states',
messageType : 'sensor_msgs/JointState'
});
listener.subscribe(function(message) {
console.log('Received message on ' + listener.name + ': ' + message.position);
google.colab.kernel.invokeFunction('notebook.Concat', [message.position], {});
//listener.unsubscribe();
});
''');
But I wasn't able to use it in Colab as after connecting and accessing the data for few minutes I experienced frequent runtime disconnects. I don't know whether it happens because of any security issues. So I was trying to move to Jupyter Notebook provided by Binder.
So I am trying to find an alternative for eval_js
as in google colab. No solutions mentioned here worked for me.
Inspite of alternative I need to know How I can connect ROS using roslibjs
with Binder notebooks.
Note: I am very new to javascript and I have not used or learnt it earlier. Google Colab runtime disconnect issues happened in Chromium,Chromium(Incognito) and Firefox(without any 3rd party plugins) as well also I didn't ran a ML model to use all of the Colab Resources.
UPDATE: (12/07/23)
Without eval_js
I was able to connect to rosbridge server by running the following script (as a HTML) in (Google Colab)
from IPython.display import Javascript, display, HTML
htm = """
<script language="javascript" type="text/javascript" src="https://cdn.jsdelivr.net/npm/roslib@1/build/roslib.js"></script>
<script>
// Connecting to ROS
// -----------------
var ros = new ROSLIB.Ros({
url : 'ws://localhost:9090'
});
ros.on('connection', function() {
console.log('Connected to websocket server Hi da.');
});
ros.on('error', function(error) {
console.log('Error connecting to websocket server: ', error);
});
ros.on('close', function() {
console.log('Connection to websocket server closed.');
});
// Subscribing to a Topic
var listener = new ROSLIB.Topic({
ros : ros,
name : '/chatter',
messageType : 'std_msgs/String'
});
listener.subscribe(function(message) {
console.log('Received message on ' + listener.name + ': ' + message.data);
//google.colab.kernel.invokeFunction('notebook.Concat', [message.data], {});
//listener.unsubscribe();
});
</script>
"""
display(HTML(htm))
But when I tried the same in Binder's Jupyter Notebook it doesn't work even though I use IPython's display and HTML only.