Skip to content

Commit 42814d7

Browse files
committed
Added angle text next to the sliders
1 parent a4435a6 commit 42814d7

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

graphics/graphics_canvas.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from vpython import canvas, color, arrow, compound, keysdown, rate, norm, sqrt, cos, button, menu, checkbox, slider
1+
from vpython import canvas, color, arrow, compound, keysdown, rate, norm, sqrt, cos, button, menu, checkbox, slider, \
2+
wtext, degrees
23
from graphics.common_functions import *
34
from graphics.graphics_grid import GraphicsGrid, create_line, create_segmented_line, create_marker
45
from enum import Enum
@@ -59,7 +60,7 @@ def __init__(self, height=360, width=640, title='', caption='', grid=True):
5960
# List of joint sliders per robot
6061
self.__teachpanel = [] # 3D, robot -> joint -> options
6162
self.__teachpanel_sliders = []
62-
self.__idx_qlim_min, self.__idx_qlim_max, self.__idx_theta = 0, 1, 2
63+
self.__idx_qlim_min, self.__idx_qlim_max, self.__idx_theta, self.__idx_text = 0, 1, 2, 3
6364
# Checkbox states
6465
self.__grid_visibility = grid
6566
self.__camera_lock = False
@@ -151,13 +152,19 @@ def add_robot(self, robot):
151152
self.__robots.append(robot)
152153
self.__selected_robot = len(self.__robots) - 1
153154

154-
num_options = 3
155+
num_options = 4
155156
self.__teachpanel.append([[0] * num_options] * robot.num_joints) # Add spot for current robot settings
156157

157158
# Add robot joint sliders
158159
i = 0
159160
for joint in robot.joints:
160-
self.__teachpanel[self.__selected_robot][i] = [joint.qlim[0], joint.qlim[1], joint.theta]
161+
if joint.qlim[0] == joint.qlim[1]:
162+
self.__teachpanel[self.__selected_robot][i] = [joint.qlim[0], joint.qlim[1],
163+
joint.theta, None]
164+
else:
165+
string = "{:.2f} rad ({:.2f} deg)".format(joint.theta, degrees(joint.theta))
166+
self.__teachpanel[self.__selected_robot][i] = [joint.qlim[0], joint.qlim[1],
167+
joint.theta, wtext(text=string)]
161168
i += 1
162169

163170
# Refresh the caption
@@ -454,23 +461,27 @@ def __setup_joint_sliders(self):
454461
if len(self.__teachpanel) == 0:
455462
self.scene.append_to_caption("No robots available\n")
456463
return
457-
i = 1
464+
i = 0
458465
for joint in self.__teachpanel[self.__selected_robot]:
459466
if joint[self.__idx_qlim_min] == joint[self.__idx_qlim_max]:
460467
# If a slider with (effectively) no values, skip it
468+
i += 1
461469
continue
462470
# Add a title
463471
self.scene.append_to_caption('Joint {0}:\t'.format(i))
464-
i += 1
465472
# Add the slider, with the correct joint variables
466473
s = slider(
467474
bind=self.__joint_slider,
468475
min=joint[self.__idx_qlim_min],
469476
max=joint[self.__idx_qlim_max],
470-
value=joint[self.__idx_theta]
477+
value=joint[self.__idx_theta],
478+
id=i
471479
)
472480
self.__teachpanel_sliders.append(s)
481+
string = "{:.2f} rad ({:.2f} deg)".format(joint[self.__idx_theta], degrees(joint[self.__idx_theta]))
482+
joint[self.__idx_text] = wtext(text=string)
473483
self.scene.append_to_caption('\n\n')
484+
i += 1
474485

475486
#######################################
476487
# UI CALLBACKS
@@ -596,10 +607,8 @@ def __joint_slider(self, s):
596607
:param s: The slider object that has been modified
597608
:type s: class:`slider`
598609
"""
599-
# Save the values for updating later
600-
for slider_num in range(0, len(self.__teachpanel_sliders)):
601-
self.__teachpanel[self.__selected_robot][slider_num][self.__idx_theta] = \
602-
self.__teachpanel_sliders[slider_num].value
610+
# Save the value
611+
self.__teachpanel[self.__selected_robot][s.id][self.__idx_theta] = s.value
603612

604613
# Get all angles for the robot
605614
angles = []
@@ -612,6 +621,12 @@ def __joint_slider(self, s):
612621
# Update joints
613622
self.__robots[self.__selected_robot].set_joint_poses(poses)
614623

624+
for joint in self.__teachpanel[self.__selected_robot]:
625+
if joint[self.__idx_text] is None:
626+
continue
627+
string = "{:.2f} rad ({:.2f} deg)".format(joint[self.__idx_theta], degrees(joint[self.__idx_theta]))
628+
joint[self.__idx_text].text = string
629+
615630

616631
class GraphicsCanvas2D:
617632
"""
@@ -1026,15 +1041,15 @@ def __verify_plot_options(self, options_str):
10261041
return [default_line, default_marker, default_colour]
10271042

10281043
# If line_style given, join the first two options if applicable (some types have 2 characters)
1029-
for char in range(0, len(options_split)-1):
1044+
for char in range(0, len(options_split) - 1):
10301045
# If char is '-' (only leading character in double length option)
10311046
if options_split[char] == '-' and len(options_split) > 1:
10321047
# If one of the leading characters is valid
1033-
if options_split[char+1] == '-' or options_split[char+1] == '.':
1048+
if options_split[char + 1] == '-' or options_split[char + 1] == '.':
10341049
# Join the two into the first
1035-
options_split[char] = options_split[char] + options_split[char+1]
1050+
options_split[char] = options_split[char] + options_split[char + 1]
10361051
# Shuffle down the rest
1037-
for idx in range(char+2, len(options_split)):
1052+
for idx in range(char + 2, len(options_split)):
10381053
options_split[idx - 1] = options_split[idx]
10391054
# Remove duplicate extra
10401055
options_split.pop()

0 commit comments

Comments
 (0)