Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accepting (min,max,step_size) arguments during creating publishers #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions jupyros/ros_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import numpy as np
import threading
import subprocess, yaml, os
def add_widgets(msg_instance, widget_dict, widget_list, prefix=''):
import warnings
sliders_defaults = {'float':(0,10,0.1), 'int':(0,10,1)}
def add_widgets(msg_instance, widget_dict, widget_list, minval, maxval, step_size, prefix=''):
"""
Adds widgets.

Expand All @@ -33,23 +35,32 @@ def add_widgets(msg_instance, widget_dict, widget_list, prefix=''):
w_box = widgets.HBox([widgets.Label(value='Image path:'), w])
widget_list.append(w_box)
return widget_dict, widget_list

if msg_instance._type in sliders_defaults:
try:
minval, maxval, step_size = sliders_defaults[msg_instance._type]
except ValueError as err:
warnings.warn(err.__str__(), Warning)
warnings.warn("Using the default values instead of The message's Defaults. Make sure you have assigned a 3 valued tuple(min,max,step_size) to the slider_defaults dictionary", Warning)
for idx, slot in enumerate(msg_instance.__slots__):
attr = getattr(msg_instance, slot)
s_t = msg_instance._slot_types[idx]
w = None

if s_t in ['float32', 'float64']:
w = widgets.FloatSlider()
if minval == maxval:
minval, maxval, step_size = sliders_defaults['float']
w = widgets.FloatSlider(min=minval, max=maxval, step=step_size)
if s_t in ['int8', 'uint8', 'int32', 'uint32', 'int64', 'uint64']:
w = widgets.IntSlider()
if minval == maxval:
minval, maxval, step_size = sliders_defaults['int']
w = widgets.IntSlider(min=minval, max=maxval, step=step_size)
if s_t in ['string']:
w = widgets.Text()

if isinstance(attr, Message):
widget_list.append(widgets.Label(value=slot))
widget_dict[slot] = {}
add_widgets(attr, widget_dict[slot], widget_list, slot)
add_widgets(attr, widget_dict[slot], widget_list, minval, maxval, step_size, slot)

if w:
widget_dict[slot] = w
Expand Down Expand Up @@ -81,7 +92,7 @@ def img_to_msg(imgpath):
imgmsg = bridge.cv2_to_imgmsg(img)
return imgmsg

def publish(topic, msg_type):
def publish(topic, msg_type, minval=0, maxval=0, step_size=1):
"""
Create a form widget for message type msg_type.
This function analyzes the fields of msg_type and creates
Expand All @@ -92,7 +103,10 @@ def publish(topic, msg_type):

@param msg_type The message type
@param topic The topic name to publish to

@param minval minimum value for sliders
@param maxval maximum value for sliders
@param step_size steps size value for sliders

@return jupyter widget for display
"""
publisher = rospy.Publisher(topic, msg_type, queue_size=10)
Expand All @@ -108,8 +122,7 @@ def latch_value_change(arg):
publisher.impl.is_latch = arg['new']

latch_check.observe(latch_value_change, 'value')

add_widgets(msg_type(), widget_dict, widget_list)
add_widgets(msg_type(), widget_dict, widget_list, minval=minval, maxval=maxval, step_size=step_size)
send_btn = widgets.Button(description="Send Message")

def send_msg(arg):
Expand Down