Skip to content
Snippets Groups Projects
ros_widgets.py 1.92 KiB
Newer Older
Wolf Vollprecht's avatar
Wolf Vollprecht committed
import rospy
import std_msgs
import ipywidgets as widgets

def _add_widgets(msg_type, form_list, widget_list):
    """
    Adds widgets.
    
    @param msg_type The message type
    @param form_list The form list
    @param widget_list The widget list
    
    @return form_list and widget_list
    """
    for idx, slot in enumerate(msg_type.__slots__):
        attr = getattr(msg_type, slot)
        s_t = msg_type._slot_types[idx]
        w = None

        if s_t in ['float32', 'float64']:
            w = widgets.FloatSlider()
        if s_t in ['int8', 'uint8', 'int32', 'uint32', 'int64', 'uint64']:
            w = widgets.IntSlider()
        if s_t in ['string']:
            w = widgets.Text()
        if w == None:
            add_widgets(s_t, form_list, widget_list)
        form_list.append(w)
        w_box = widgets.HBox([widgets.Label(value=slot), w])
        widget_list.append(w_box)
    return form_list, widget_list

def widget_for_msg(msg_type, publish_to):
    """
    Create a form widget for message type msg_type.
    This function analyzes the fields of msg_type and creates
    an appropriate widget.
    A publisher is automatically created which publishes to the
    topic given as publish_to parameter. This allows pressing the 
    "Send Message" button to send the message to ROS.
    
    @param msg_type The message type
    @param publish_to The topic name to publish to
    
    @return jupyter widget for display
    """
    widget_list, form_list = [], []
    publisher = rospy.Publisher(publish_to, msg_type, queue_size=10)

    form_list, widget_list = _add_widgets(msg_type, [], [])
    send_btn = widgets.Button(description="Send Message")
    
    def send_msg(arg):
        arg_list = [wdg.value for wdg in form_list]
        msg = msg_type(*arg_list)
        publisher.publish(msg)
    
    send_btn.on_click(send_msg)
    widget_list.append(send_btn)
    vbox = widgets.VBox(children=widget_list)
    
    return vbox