Skip to content

Commit 8f64142

Browse files
committed
Accomodate for pose being at CoG instead of rear axle for non-kinematic models
1 parent 1d1733b commit 8f64142

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

f1tenth_gym/envs/simulator.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ def _build_scan_cache(self, simulator: ScanSimulator2D, vehicle_params: VehicleP
297297
Computes side_distances as the distance from the LiDAR position to the
298298
vehicle body edge for each beam angle. This correctly accounts for the
299299
LiDAR offset (base_link_to_lidar_tf) and collision body center offset
300-
(collision_body_center_x/y) from the vehicle parameters.
300+
(collision_body_center_x/y) from the vehicle parameters, with an
301+
adjustment for collision checks when the pose is at the center of gravity.
301302
"""
302303
num_beams = simulator.num_beams
303304
angles = np.zeros(num_beams, dtype=np.float32)
@@ -317,9 +318,18 @@ def _build_scan_cache(self, simulator: ScanSimulator2D, vehicle_params: VehicleP
317318
body_dx = vehicle_params.collision_body_center_x
318319
body_dy = vehicle_params.collision_body_center_y
319320

321+
# If state is referenced at the center of gravity, base_link is behind it by lr.
322+
# Leave LiDAR in the state frame to avoid shifting the scan origin.
323+
base_dx = 0.0
324+
if self.model != DynamicModel.KS:
325+
base_dx = -float(vehicle_params.lr)
326+
if not math.isfinite(base_dx):
327+
base_dx = 0.0
328+
320329
# Compute LiDAR position relative to collision body center
321330
# (the collision body is centered at (body_dx, body_dy) from base_link)
322-
lidar_x_in_body = lidar_dx - body_dx
331+
body_x = base_dx + body_dx
332+
lidar_x_in_body = lidar_dx - body_x
323333
lidar_y_in_body = lidar_dy - body_dy
324334

325335
increment = simulator.get_increment()
@@ -359,16 +369,22 @@ def _lidar_pose_from_base(self, pose: np.ndarray) -> np.ndarray:
359369
return np.array([scan_x, scan_y, scan_theta], dtype=pose.dtype)
360370

361371
def _collision_pose_from_base(self, pose: np.ndarray) -> np.ndarray:
362-
"""Transform pose from base_link to collision body center.
372+
"""Transform pose to collision body center.
363373
364374
Args:
365-
pose: Base link pose (x, y, theta).
375+
pose: Pose of the model state (base_link for KS, CoG for others).
366376
367377
Returns:
368378
Collision body center pose in world frame.
369379
"""
370-
dx = self.vehicle_params.collision_body_center_x
371-
dy = self.vehicle_params.collision_body_center_y
380+
base_dx = 0.0
381+
base_dy = 0.0
382+
if self.model != DynamicModel.KS:
383+
base_dx = -float(self.vehicle_params.lr)
384+
if not math.isfinite(base_dx):
385+
base_dx = 0.0
386+
dx = base_dx + float(self.vehicle_params.collision_body_center_x)
387+
dy = base_dy + float(self.vehicle_params.collision_body_center_y)
372388
if dx == 0.0 and dy == 0.0:
373389
return pose
374390
cos_yaw = math.cos(pose[2])

0 commit comments

Comments
 (0)