8000 Fixed error raising, and added documentation for error raising. · lordkeks/robotics-toolbox-python@bd2771d · GitHub
[go: up one dir, main page]

Skip to content

Commit bd2771d

Browse files
committed
Fixed error raising, and added documentation for error raising.
1 parent 62b2c5f commit bd2771d

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

graphics/common_functions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from vpython import radians, vector
22

3+
"""
4+
global variables that can be used to easily reference X, Y, and Z axes directions.
5+
"""
36
x_axis_vector = vector(1, 0, 0)
47
y_axis_vector = vector(0, 1, 0)
58
z_axis_vector = vector(0, 0, 1)
@@ -13,6 +16,7 @@ def wrap_to_pi(angle_type, angle):
1316
:type angle_type: `str`
1417
:param angle: The angle to wrap
1518
:type angle: `float`
19+
:raises ValueError: Throws the error if the given string is not "deg" or "rad"
1620
:return: The wrapped angle
1721
:rtype: `float`
1822
"""
10000

graphics/graphics_grid.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ def __init__(self):
1010
self.camera_pos = scene.camera.pos
1111
self.camera_axes = scene.camera.axis
1212
# Initialise a grid object
13+
# grid_object[0] will always be the 3 plane graphics. [XY, XZ, YZ] (alphabetical in order and connection)
14+
# grid_object[1] will always be the labels. There is always a certain number of indices.
15+
# Order is [x-plane numbers, "X", y-plane numbers, "Y", z-plane numbers, "Z"]
1316
self.grid_object = [[], []]
1417
self.__init_grid()
1518

graphics/graphics_robot.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def rotate_around_joint_axis(self, angle_of_rotation, axis_of_rotation):
7979
:type angle_of_rotation: float (radians)
8080
:param axis_of_rotation: X, Y, or Z axis to apply around the objects specific X, Y, or Z axes
8181
:type axis_of_rotation: class:`vpython.vector`
82+
:raise ValueError: The given axis_of_rotation must be one of the default X, Y, Z vectors (e.g. x_axis_vector)
8283
"""
8384
# Determine the axis of rotation based on the given joint axis direction
8485
# Then add the rotation amount to the axis counter
@@ -274,6 +275,7 @@ def get_rotation_angle(self, axis):
274275
:type axis: class:`vpython.vector`
275276
:return: Current angle of rotation with respect to world (includes rotation from previous joints)
276277
:rtype: float (radians)
278+
:raise ValueError: The given axis_of_rotation must be one of the default X, Y, Z vectors (e.g. x_axis_vector)
277279
"""
278280
# Ensure copying value not reference
279281
ans = 0.0
@@ -284,7 +286,9 @@ def get_rotation_angle(self, axis):
284286
elif axis.equals(z_axis_vector):
285287
ans += self.__z_rotation
286288
else:
287-
ans += self.__y_rotation
289+
error_str = "Bad input vector given ({0}). Must be either x_axis_vector ({1}), y_axis_vector ({2})," \
290+
"or z_axis_vector ({3})."
291+
raise ValueError(error_str.format(axis), x_axis_vector, y_axis_vector, z_axis_vector)
288292

289293
return ans
290294

@@ -296,6 +300,7 @@ def get_axis_vector(self, axis):
296300
:type axis: class:`vpython.vector`
297301
:return: Current vector representation of the joints X, Y, or Z axis
298302
:rtype: class:`vpython.vector`
303+
:raise ValueError: The given axis_of_rotation must be one of the default X, Y, Z vectors (e.g. x_axis_vector)
299304
"""
300305
# Return new vectors to avoid pass by reference
301306
if axis.equals(x_axis_vector):
@@ -305,7 +310,9 @@ def get_axis_vector(self, axis):
305310
elif axis.equals(z_axis_vector):
306311
return vector(self.__z_vector)
307312
else:
308-
return vector(self.__y_vector)
313+
error_str = "Bad input vector given ({0}). Must be either x_axis_vector ({1}), y_axis_vector ({2})," \
314+
"or z_axis_vector ({3})."
315+
raise ValueError(error_str.format(axis), x_axis_vector, y_axis_vector, z_axis_vector)
309316

310317
def get_joint_type(self):
311318
"""
@@ -459,6 +466,7 @@ class GraphicalRobot:
459466
460467
:param joints: A list of the joints in order from base (0) to gripper (end), or other types.
461468
:type joints: list
469+
:raise ValueError: The given length of joints must not be 0
462470
"""
463471

464472
def __init__(self, joints):
@@ -514,6 +522,8 @@ def set_joint_angle(self, link_num, new_angle):
514522
:type link_num: int
515523
:param new_angle: The required angle to set the arm rotated towards
516524
:type new_angle: float (radians)
525+
:raise IndexError: Link index must be between 0 (inclusive) and number of joints (exclusive)
526+
:raise TypeError: The joint index chosen must be indexing a revolute joint
517527
"""
518528
if (link_num < 0) or (link_num >= self.num_joints):
519529
error_str = "link number given ({0}) is not between range of 0 (inclusive) and {1} (exclusive)"
@@ -538,7 +548,7 @@ def set_joint_angle(self, link_num, new_angle):
538548
self.__position_joints()
539549
else:
540550
error_str = "Given joint {0} is not a revolute joint. It is a {1} joint"
541-
raise ValueError(error_str.format(link_num, self.joints[link_num].get_joint_type()))
551+
raise TypeError(error_str.format(link_num, self.joints[link_num].get_joint_type()))
542552

543553
def set_all_joint_angles(self, new_angles):
544554
"""
@@ -547,6 +557,7 @@ def set_all_joint_angles(self, new_angles):
547557
:param new_angles: List of new angles (radians) to set each joint to.
548558
Must have the same length as number of joints in robot arm, even if the joints aren't revolute
549559
:type new_angles: float list (radians)
560+
:raise IndexError: The length of the given list must equal the number of joints.
550561
"""
551562
# Raise error if lengths don't match
552563
if len(new_angles) != len(self.joints):

tests/graphics_test_features.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
Next select which test you which to run, and call the function.
88
A canvas will be created and display the respective graphics.
99
Verify the output is as expected.
10-
Then close the browser window and run a different function. (Help clear graphics. Currently, no clearing implemented)
10+
Then close the browser window and run a different function. (Help clear graphics. Currently, no clearing implemented).
11+
12+
Alternatively, executing this file will run the test_puma560_angle_change() function.
1113
"""
1214

1315
from graphics.model_puma560 import *
@@ -141,15 +143,25 @@ def test_puma560_angle_change():
141143

142144

143145
def test_clear_scene():
146+
"""
147+
This test will import the Puma560 model, then after 2 seconds, clear the canvas of all models.
148+
"""
144149
the_grid = init_canvas()
150+
145151
puma560 = import_puma_560()
146152
puma560.move_base(vector(1, 1, 0))
153+
147154
sleep(2)
155+
148156
the_grid.clear_scene()
149157
del puma560
150158

151159

152160
def test_clear_scene_with_grid_updating():
161+
"""
162+
This test will import the Puma560 model, then after 2 seconds, clear the canvas of all models.
163+
Meanwhile, grid update calls have been placed in between. (Currently the only way to update the grid)
164+
"""
153165
the_grid = init_canvas()
154166
puma560 = import_puma_560()
155167
the_grid.update_grid()

0 commit comments

Comments
 (0)
0