77
88from urdfenvs .urdf_common .generic_robot import ControlMode , GenericRobot
99
10+
1011class 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