Skip to content

Commit bcde356

Browse files
committed
Modified wrap_to_pi() to take input of radians or degrees.
1 parent 4f9e222 commit bcde356

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

graphics/common_functions.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,28 @@
55
z_axis_vector = vector(0, 0, 1)
66

77

8-
# TODO handle radians (needed more than degrees)
9-
def wrap_to_pi(angle):
10-
angle = angle % radians(360)
11-
if angle > radians(180):
12-
angle -= radians(360)
8+
def wrap_to_pi(angle_type, angle):
9+
"""
10+
Wrap the given angle (deg or rad) to [-pi pi]
11+
12+
:param angle_type: String of whether the angle is deg or rad
13+
:type angle_type: `str`
14+
:param angle: The angle to wrap
15+
:type angle: `float`
16+
:return: The wrapped angle
17+
:rtype: `float`
18+
"""
19+
if angle_type == "deg":
20+
angle = angle % 360
21+
if angle > 180:
22+
angle -= 360
23+
24+
elif angle_type == "rad":
25+
angle = angle % radians(360)
26+
if angle > radians(180):
27+
angle -= radians(360)
28+
29+
else:
30+
raise ValueError('angle_type must be "deg" or "rad"')
31+
1332
return angle

graphics/graphics_robot.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ def rotate_around_joint_axis(self, angle_of_rotation, axis_of_rotation):
8484
# Then add the rotation amount to the axis counter
8585
if axis_of_rotation.equals(x_axis_vector):
8686
rotation_axis = self.__x_vector
87-
self.__x_rotation = wrap_to_pi(self.__x_rotation + angle_of_rotation)
87+
self.__x_rotation = wrap_to_pi("rad", self.__x_rotation + angle_of_rotation)
8888
elif axis_of_rotation.equals(y_axis_vector):
8989
rotation_axis = self.__y_vector
90-
self.__y_rotation = wrap_to_pi(self.__y_rotation + angle_of_rotation)
90+
self.__y_rotation = wrap_to_pi("rad", self.__y_rotation + angle_of_rotation)
9191
elif axis_of_rotation.equals(z_axis_vector):
9292
rotation_axis = self.__z_vector
93-
self.__z_rotation = wrap_to_pi(self.__z_rotation + angle_of_rotation)
93+
self.__z_rotation = wrap_to_pi("rad", self.__z_rotation + angle_of_rotation)
9494
else:
9595
error_str = "Bad input vector given ({0}). Must be either x_axis_vector ({1}), y_axis_vector ({2})," \
9696
"or z_axis_vector ({3}). Use rotate_around_vector for rotation about an arbitrary vector."
@@ -140,11 +140,11 @@ def rotate_around_vector(self, angle_of_rotation, axis_of_rotation):
140140
# axis of rotation will have the smallest (it's less affected by the rotation)
141141
min_angle_diff = min(angle_diff_x, angle_diff_y, angle_diff_z)
142142
if min_angle_diff == angle_diff_x:
143-
self.__x_rotation = wrap_to_pi(self.__x_rotation + angle_of_rotation)
143+
self.__x_rotation = wrap_to_pi("rad", self.__x_rotation + angle_of_rotation)
144144
elif min_angle_diff == angle_diff_y:
145-
self.__y_rotation = wrap_to_pi(self.__y_rotation + angle_of_rotation)
145+
self.__y_rotation = wrap_to_pi("rad", self.__y_rotation + angle_of_rotation)
146146
else:
147-
self.__z_rotation = wrap_to_pi(self.__z_rotation + angle_of_rotation)
147+
self.__z_rotation = wrap_to_pi("rad", self.__z_rotation + angle_of_rotation)
148148

149149
# Calculate the updated toolpoint location
150150
self.__connect_dir.rotate(angle=angle_of_rotation, axis=axis_of_rotation)
@@ -353,10 +353,10 @@ def rotate_joint(self, new_angle):
353353
:type new_angle: float (radians)
354354
"""
355355
# Wrap given angle to -pi to pi
356-
new_angle = wrap_to_pi(new_angle)
356+
new_angle = wrap_to_pi("rad", new_angle)
357357
current_angle = self.rotation_angle
358358
# Calculate amount to rotate the link
359-
angle_diff = wrap_to_pi(new_angle - current_angle)
359+
angle_diff = wrap_to_pi("rad", new_angle - current_angle)
360360
# Update the link
361361
self.rotate_around_joint_axis(angle_diff, self.rotation_axis)
362362
self.rotation_angle = new_angle

0 commit comments

Comments
 (0)