Skip to content

Commit cdfef89

Browse files
committed
ft[clean]: Removes dangling specifications of physics engine realizations. Alligns quotation marks style.
1 parent f48d63e commit cdfef89

File tree

5 files changed

+230
-292
lines changed

5 files changed

+230
-292
lines changed

urdfenvs/sensors/physics_engine_interface.py

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import numpy as np
66
from scipy.spatial.transform import Rotation
7-
#import pybullet
87

98
class LinkIdNotFoundError(Exception):
109
pass
@@ -65,118 +64,3 @@ def add_visualization_line(self, *args) -> None:
6564
)
6665

6766

68-
class PybulletInterface(PhysicsEngineInterface):
69-
"""
70-
Physics engine interface for bullet.
71-
"""
72-
def extract_link_id(self, robot, link_name: str) -> int:
73-
number_links = pybullet.getNumJoints(robot)
74-
joint_names = []
75-
for i in range(number_links):
76-
joint_name = pybullet.getJointInfo(robot, i)[1].decode("UTF-8")
77-
joint_names.append(joint_name)
78-
if joint_name == link_name:
79-
return i
80-
raise LinkIdNotFoundError(
81-
f"Link with name {link_name} not found. "
82-
f"Possible links are {joint_names}"
83-
)
84-
85-
86-
def get_obstacle_pose(self, obst_id: int, obst_name: str, movable: bool = False) -> None:
87-
position, orientation = pybullet.getBasePositionAndOrientation(obst_id)
88-
return position, orientation
89-
90-
def get_obstacle_velocity(self, obst_id: int, obst_name: str, movable: bool = False) -> None:
91-
linear, angular = pybullet.getBaseVelocity(obst_id)
92-
return linear, angular
93-
94-
def get_goal_pose(self, goal_id: int) -> Tuple[List[float]]:
95-
position, orientation = pybullet.getBasePositionAndOrientation(goal_id)
96-
return position, orientation
97-
98-
def get_goal_velocity(self, goal_id: int) -> Tuple[List[float]]:
99-
linear, angular = pybullet.getBaseVelocity(goal_id)
100-
return linear, angular
101-
102-
def get_link_position(self, robot, link_id) -> np.ndarray:
103-
link_position = np.array(pybullet.getLinkState(robot, link_id)[0])
104-
return link_position
105-
106-
def get_link_orientation(self, robot, link_id) -> np.ndarray:
107-
link_orientation = np.array(pybullet.getLinkState(robot, link_id)[1])
108-
return link_orientation
109-
110-
def ray_cast(
111-
self,
112-
ray_start: np.ndarray,
113-
ray_end: np.ndarray,
114-
ray_index: int,
115-
ray_length: float
116-
) -> np.ndarray:
117-
lidar = pybullet.rayTest(ray_start, ray_end)
118-
return lidar[0][2] * ray_length
119-
120-
def clear_visualizations(self) -> None:
121-
pybullet.removeAllUserDebugItems()
122-
123-
def add_visualization_line(
124-
self, start_point: Tuple[float], end_point: Tuple[float]
125-
) -> None:
126-
pybullet.addUserDebugLine(start_point, end_point)
127-
128-
class MujocoInterface(PhysicsEngineInterface):
129-
"""
130-
Physics engine interface for mujoco.
131-
"""
132-
def extract_link_id(self, robot, link_name: str) -> int:
133-
return self._data.body(link_name).id
134-
135-
def set_data(self, data) -> None:
136-
"""Set pointer to mujoco data to sensor."""
137-
self._data = data
138-
139-
def get_link_position(self, robot, link_id) -> np.ndarray:
140-
link_position = self._data.xpos[link_id]
141-
return link_position
142-
143-
def get_link_orientation(self, robot, link_id) -> np.ndarray:
144-
link_orientation_matrix = np.reshape(self._data.xmat[link_id], (3, 3))
145-
return Rotation.from_matrix(link_orientation_matrix).as_quat()
146-
147-
def get_obstacle_pose(self, obst_id: int, obst_name: str, movable: bool = False) -> Tuple[List[float], List[float]]:
148-
if movable:
149-
150-
free_joint_data = self._data.jnt(f"freejoint_{obst_name}").qpos
151-
print(free_joint_data)
152-
return free_joint_data[0:3].tolist(), free_joint_data[3:].tolist()
153-
pos = self._data.body(obst_name).xpos
154-
ori = self._data.body(obst_name).xquat
155-
return pos.tolist(), ori.tolist()
156-
157-
def get_goal_pose(self, goal_id: int) -> Tuple[List[float]]:
158-
pos = self._data.site(goal_id).xpos
159-
goal_rotation = np.reshape(self._data.site(goal_id).xmat, (3, 3))
160-
ori = Rotation.from_matrix(goal_rotation).as_quat()
161-
return pos.tolist(), ori.tolist()
162-
163-
def get_goal_velocity(self, goal_id: int) -> Tuple[List[float]]:
164-
return [0, 0, 0], [0, 0, 0]
165-
166-
def ray_cast(
167-
self,
168-
ray_start: np.ndarray,
169-
ray_end: np.ndarray,
170-
ray_index: int,
171-
ray_length: float
172-
) -> np.ndarray:
173-
if self._data.sensordata[ray_index] < 0:
174-
ray_value = ray_length - (0.01 / 2)
175-
else:
176-
ray_value = self._data.sensordata[ray_index] - (0.01 / 2)
177-
return ray_value
178-
179-
def get_obstacle_velocity(self, obst_id: int, obst_name: str, movable: bool = False) -> None:
180-
raise NotImplementedError(
181-
"Obstacle velocity not implemented for mujoco."
182-
)

urdfenvs/urdf_common/differential_drive_robot.py

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from urdfenvs.urdf_common.generic_robot import ControlMode, GenericRobot
99

10+
1011
class FacingDirectionUndefinedError(Exception):
1112
pass
1213

@@ -25,22 +26,23 @@ class DifferentialDriveRobot(GenericRobot):
2526
The offset by which the initial position must be shifted to align
2627
observation with that position.
2728
"""
29+
2830
def __init__(
29-
self,
30-
n: int,
31-
urdf_file: str,
32-
mode: ControlMode,
33-
actuated_wheels: List[str],
34-
castor_wheels: List[str],
35-
wheel_radius: float,
36-
wheel_distance: float,
37-
spawn_offset: np.ndarray = np.array([0.0, 0.0, 0.15]),
38-
spawn_rotation: float = 0.0,
39-
facing_direction: str = 'x',
40-
not_actuated_joints: List[str] = []
31+
self,
32+
n: int,
33+
urdf_file: str,
34+
mode: ControlMode,
35+
actuated_wheels: List[str],
36+
castor_wheels: List[str],
37+
wheel_radius: float,
38+
wheel_distance: float,
39+
spawn_offset: np.ndarray = np.array([0.0, 0.0, 0.15]),
40+
spawn_rotation: float = 0.0,
41+
facing_direction: str = "x",
42+
not_actuated_joints: List[str] = [],
4143
):
4244
"""Constructor for differential drive robots."""
43-
self._number_actuated_axes = int(len(actuated_wheels)/2)
45+
self._number_actuated_axes = int(len(actuated_wheels) / 2)
4446
self._spawn_rotation = deepcopy(spawn_rotation)
4547
self._spawn_offset = deepcopy(spawn_offset)
4648
self._castor_wheels = castor_wheels
@@ -55,7 +57,14 @@ def set_degrees_of_freedom(self, n):
5557
if n > 0:
5658
self._n = n
5759
else:
58-
self._n: int = self._urdf_robot.num_actuated_joints - len(self._actuated_wheels) - len(self._castor_wheels) - len(self._not_actuated_joints) + 2
60+
self._n: int = (
61+
self._urdf_robot.num_actuated_joints
62+
- len(self._actuated_wheels)
63+
- len(self._castor_wheels)
64+
- len(self._not_actuated_joints)
65+
+ 2
66+
)
67+
5968
def ns(self) -> int:
6069
"""Returns the number of degrees of freedom.
6170
@@ -69,7 +78,8 @@ def get_velocity_spaces(self) -> tuple:
6978
control.
7079
7180
Overrides velocity spaces from default, because a differential drive has limits in x,y and
72-
theta direction, while the action space should be limited to the forward and angular velocity."""
81+
theta direction, while the action space should be limited to the forward and angular velocity.
82+
"""
7383
ospace = self.get_observation_space()
7484
uu = np.concatenate(
7585
(self._limit_vel_forward_j[1, :], self._limit_vel_j[1, 3:]), axis=0
@@ -81,19 +91,22 @@ def get_velocity_spaces(self) -> tuple:
8191
return (ospace, aspace)
8292

8393
def reset(
84-
self,
85-
pos: np.ndarray,
86-
vel: np.ndarray,
87-
mount_position: np.ndarray,
88-
mount_orientation: np.ndarray,) -> None:
89-
""" Reset simulation and add robot """
94+
self,
95+
pos: np.ndarray,
96+
vel: np.ndarray,
97+
mount_position: np.ndarray,
98+
mount_orientation: np.ndarray,
99+
) -> None:
100+
"""Reset simulation and add robot"""
90101
logging.warning(
91102
"The argument 'mount_position' and 'mount_orientation' are \
92103
ignored for differential drive robots."
93104
)
94105
if hasattr(self, "_robot"):
95106
p.removeBody(self._robot)
96-
base_orientation = p.getQuaternionFromEuler([0, 0, self._spawn_rotation + pos[2]])
107+
base_orientation = p.getQuaternionFromEuler(
108+
[0, 0, self._spawn_rotation + pos[2]]
109+
)
97110
spawn_position = self._spawn_offset
98111
spawn_position[0:2] += pos[0:2]
99112
self._robot = p.loadURDF(
@@ -137,8 +150,8 @@ def check_state(self, pos: np.ndarray, vel: np.ndarray) -> tuple:
137150
return pos, vel
138151

139152
def read_limits(self) -> None:
140-
""" Set position, velocity, acceleration
141-
and motor torque lower en upper limits """
153+
"""Set position, velocity, acceleration
154+
and motor torque lower en upper limits"""
142155
self._limit_pos_j = np.zeros((2, self.ns()))
143156
self._limit_vel_j = np.zeros((2, self.ns()))
144157
self._limit_tor_j = np.zeros((2, self.n()))
@@ -260,7 +273,7 @@ def apply_base_velocity(self, vels: np.ndarray) -> None:
260273

261274
def apply_velocity_action(self, vels: np.ndarray) -> None:
262275
"""Applies angular velocities to the arm joints."""
263-
self.apply_base_velocity(vels)
276+
self.apply_base_velocity(vels)
264277
for i in range(2, self._n):
265278
p.setJointMotorControl2(
266279
self._robot,
@@ -278,7 +291,7 @@ def correct_base_orientation(self, pos_base: np.ndarray) -> np.ndarray:
278291
between -pi and pi.
279292
"""
280293
pos_base[2] -= self._spawn_rotation
281-
#TODO: The orientation is with respect to the spawn position
294+
# TODO: The orientation is with respect to the spawn position
282295
# If this is changed as suggested below, it breaks the velocities.
283296
"""
284297
if self._facing_direction == '-y':
@@ -335,24 +348,26 @@ def update_state(self) -> None:
335348
(v_right - v_left) * self._wheel_radius / self._wheel_distance
336349
)
337350

338-
if self._facing_direction == 'x':
351+
if self._facing_direction == "x":
339352
jacobi_nonholonomic = np.array(
340353
[[np.cos(pos_base[2]), 0], [np.sin(pos_base[2]), 0], [0, 1]]
341354
)
342-
elif self._facing_direction == '-x':
355+
elif self._facing_direction == "-x":
343356
jacobi_nonholonomic = np.array(
344357
[[-np.cos(pos_base[2]), 0], [-np.sin(pos_base[2]), 0], [0, 1]]
345358
)
346-
elif self._facing_direction == 'y':
359+
elif self._facing_direction == "y":
347360
jacobi_nonholonomic = np.array(
348361
[[-np.sin(pos_base[2]), 0], [np.cos(pos_base[2]), 0], [0, 1]]
349362
)
350-
elif self._facing_direction == '-y':
363+
elif self._facing_direction == "-y":
351364
jacobi_nonholonomic = np.array(
352365
[[np.sin(pos_base[2]), 0], [-np.cos(pos_base[2]), 0], [0, 1]]
353366
)
354367
else:
355-
raise FacingDirectionUndefinedError(f"Facing direction {self._facing_direction} undefined. Use 'x', '-x', 'y' or '-y'")
368+
raise FacingDirectionUndefinedError(
369+
f"Facing direction {self._facing_direction} undefined. Use 'x', '-x', 'y' or '-y'"
370+
)
356371
velocity_base = np.dot(
357372
jacobi_nonholonomic, np.array([forward_velocity, angular_velocity])
358373
)
@@ -374,4 +389,3 @@ def update_state(self) -> None:
374389
"forward_velocity": np.array([forward_velocity]),
375390
}
376391
}
377-

0 commit comments

Comments
 (0)