in CARLA_0.9.8/PythonAPI/carla/agents/navigation/carla_env.py [0:0]
def dist_from_center_lane(self, vehicle):
# assume on highway
vehicle_location = vehicle.get_location()
vehicle_waypoint = self.map.get_waypoint(vehicle_location)
vehicle_xy = np.array([vehicle_location.x, vehicle_location.y])
vehicle_s = vehicle_waypoint.s
vehicle_velocity = vehicle.get_velocity() # Vecor3D
vehicle_velocity_xy = np.array([vehicle_velocity.x, vehicle_velocity.y])
speed = np.linalg.norm(vehicle_velocity_xy)
vehicle_waypoint_closest_to_road = \
self.map.get_waypoint(vehicle_location, project_to_road=True, lane_type=carla.LaneType.Driving)
road_id = vehicle_waypoint_closest_to_road.road_id
assert road_id is not None
lane_id = int(vehicle_waypoint_closest_to_road.lane_id)
goal_lane_id = lane_id
current_waypoint = self.map.get_waypoint(vehicle_location, project_to_road=False)
goal_waypoint = self.map.get_waypoint_xodr(road_id, goal_lane_id, vehicle_s)
if goal_waypoint is None:
# try to fix, bit of a hack, with CARLA waypoint discretizations
carla_waypoint_discretization = 0.02 # meters
goal_waypoint = self.map.get_waypoint_xodr(road_id, goal_lane_id, vehicle_s - carla_waypoint_discretization)
if goal_waypoint is None:
goal_waypoint = self.map.get_waypoint_xodr(road_id, goal_lane_id, vehicle_s + carla_waypoint_discretization)
if goal_waypoint is None:
print("Episode fail: goal waypoint is off the road! (frame %d)" % self.count)
done, dist, vel_s = True, 100., 0.
else:
goal_location = goal_waypoint.transform.location
goal_xy = np.array([goal_location.x, goal_location.y])
dist = np.linalg.norm(vehicle_xy - goal_xy)
next_goal_waypoint = goal_waypoint.next(0.1) # waypoints are ever 0.02 meters
if len(next_goal_waypoint) != 1:
print('warning: {} waypoints (not 1)'.format(len(next_goal_waypoint)))
if len(next_goal_waypoint) == 0:
print("Episode done: no more waypoints left. (frame %d)" % self.count)
done, vel_s = True, 0.
else:
location_ahead = next_goal_waypoint[0].transform.location
highway_vector = np.array([location_ahead.x, location_ahead.y]) - goal_xy
highway_unit_vector = np.array(highway_vector) / np.linalg.norm(highway_vector)
vel_s = np.dot(vehicle_velocity_xy, highway_unit_vector)
done = False
# not algorithm's fault, but the simulator sometimes throws the car in the air wierdly
if vehicle_velocity.z > 1. and self.count < 20:
print("Episode done: vertical velocity too high ({}), usually a simulator glitch (frame {})".format(vehicle_velocity.z, self.count))
done = True
if vehicle_location.z > 0.5 and self.count < 20:
print("Episode done: vertical velocity too high ({}), usually a simulator glitch (frame {})".format(vehicle_location.z, self.count))
done = True
return dist, vel_s, speed, done