jupyter-ros icon indicating copy to clipboard operation
jupyter-ros copied to clipboard

Is it possible to have two nodes created in one Notebook?

Open pitosalas opened this issue 6 years ago • 7 comments

I am still trying to figure out the threading model of this approach.

pitosalas avatar Jul 03 '19 02:07 pitosalas

Hi, I think it would be more common to have one node per notebook (maybe the node should actually be started automatically with jupyter-ros...)

But you can definitely have more than one subscriber (runs async in it's own thread) or publisher in the same notebook. For publisher, I had a small decorator to run the publisher in a thread so that you can do something like:

@rate(10)
rospy.publish("/mytopic", my_msg)

but this part of the code isn't ready yet. If you want to work on something like this that would be awesome.

What's your use case for two nodes?

wolfv avatar Jul 03 '19 07:07 wolfv

The use case is teaching: have a simple node that is publishing and another simple nose that subscribes and print. The two are used together in explaining the basics.

It’s probably the most common first example. Basically it’s these two, in one notebook:

Node 1

import rospy
from std_msgs.msg import Int32

# Make this into a ROS node.
rospy.init_node('topic_publisher')

# Prepare to publish topic `counter`
pub = rospy.Publisher('counter', Int32, queue_size=10)

# sleep at 2 loops per second
rate = rospy.Rate(2)
count = 0

# loop until ^c
while not rospy.is_shutdown():
    pub.publish(count)
    count += 1
    rate.sleep()

NODE 2:
import rospy
from std_msgs.msg import Int32

# define function is called each time the message is published (by some other node)
def callback(msg):
   print "The square is " + str(msg.data*msg.data)

rospy.init_node('topic_subscriber')
sub = rospy.Subscriber('counter', Int32, callback)

# Wait for published topics, exit on ^c
rospy.spin()

You say that each node has its own thread, but I don’t understand. How does notebook or jupyter-ros know where one node ends and the new one starts?

Also I don’t understand decorators or the example you cite in your email.

pitosalas avatar Jul 03 '19 11:07 pitosalas

Any response to these questions. A clarificaiton of the threading and node creation model would be very useful.

pitosalas avatar Jul 07 '19 19:07 pitosalas

yeah so this would be most simple to do in either two seperate notebooks (very convenient with JupyterLab since you can just create two tabs and put them next to each other) or you can open two Notebooks in two browser tabs.

Also you don't need two nodes for a subscriber and publisher. You can just create the subscriber in the notebook first, and then later create the publisher. The rospy subscriber automatically runs in a (Python) thread.

However, the publisher doesn't. You need to manually create a thread, or use the following magic (that has not yet been released):

https://github.com/RoboStack/jupyter-ros/blob/master/jupyros/ipy.py

That would allow you to create a cell like this:

%thread_cell

for i in range(0, 1000):
    print("Hello from a thread")
    rospy.publish("...")

But it hasn't been released because, IIRC, there were some problems. If you have some PR to make this functionality better that would be awesome! :) w

wolfv avatar Jul 08 '19 14:07 wolfv

Hi Wolf,

Also you don't need two nodes for a subscriber and publisher.

I know but I am trying to teach the concept and that is the most natural way to explain it as a first example. But if its not possible then that’s fine/

If you have some PR to make this functionality better that would be awesome! :

I would be happy to but I don’t understand how it all works yet :)

Pito

w

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/RoboStack/jupyter-ros/issues/27?email_source=notifications&email_token=AAAK5CMZKMO7NQQFOORI35LP6NGETA5CNFSM4H5BGF32YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZNJALA#issuecomment-509251628, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAK5CL5DH7UVNRJMVNJXM3P6NGETANCNFSM4H5BGF3Q.

pitosalas avatar Jul 08 '19 21:07 pitosalas

As far as I know you also can't have two nodes in a single python script ... So it's not even a limitation of Jupyter, but rather of rospy that you need two python processes for two real nodes.

wolfv avatar Jul 09 '19 02:07 wolfv

Yes but one of the advantages of using Jupiter Notebook would be to allow better exposition of the concepts in one notebook. Anyway, it’s fine.

ps. Would you prefer I correspond to you directly or continue to post issues that are really questions? Also I just checked out the Jupiter Support in the vscode python package from Microsoft. It’s a very nice implementation. I will be trying jupyter-ros with it and see how it goes.

Pito Salas Brandeis Computer Science Volen 134

On Jul 8, 2019, at 10:09 PM, Wolf Vollprecht [email protected] wrote:

As far as I know you also can't have two nodes in a single python script ... So it's not even a limitation of Jupyter, but rather of rospy that you need two python processes for two real nodes.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/RoboStack/jupyter-ros/issues/27?email_source=notifications&email_token=AAAK5CMNS27HTVGO2PCAUBTP6PXMXA5CNFSM4H5BGF32YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZO3NIQ#issuecomment-509458082, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAK5CMQLJGIAF6LFMCA6RTP6PXMXANCNFSM4H5BGF3Q.

pitosalas avatar Jul 09 '19 17:07 pitosalas