following logic wasn't considering length of lead vehicle properly?

adjusted code a bit, but still seeing a violation
This commit is contained in:
Dustin Carlino 2018-11-28 14:14:41 -08:00
parent 6bdb28cced
commit 17262063a1
2 changed files with 20 additions and 30 deletions

View File

@ -145,30 +145,32 @@ impl Car {
assert!(self.id != other.id.as_car()); assert!(self.id != other.id.as_car());
assert!(current_dist_along < other.dist_along); assert!(current_dist_along < other.dist_along);
let other_vehicle = other.vehicle.as_ref().unwrap(); let other_vehicle = other.vehicle.as_ref().unwrap();
let dist_behind_other = let dist_behind_others_back = dist_scanned_ahead
dist_scanned_ahead + (other.dist_along - current_dist_along); + (other.dist_along - current_dist_along)
- other_vehicle.length;
// If our lookahead doesn't even hit the lead vehicle (plus following distance!!!), then ignore them. // If our lookahead doesn't even hit the lead vehicle (plus following distance!!!), then ignore them.
// TODO Maybe always consider them. We might just speed up.
let total_scanning_dist = let total_scanning_dist =
dist_scanned_ahead + dist_to_lookahead + other_vehicle.following_dist(); dist_scanned_ahead + dist_to_lookahead + kinematics::FOLLOWING_DISTANCE;
if total_scanning_dist >= dist_behind_other { if total_scanning_dist >= dist_behind_others_back {
let accel = vehicle.accel_to_follow( let accel = vehicle.accel_to_follow(
self.speed, self.speed,
orig_speed_limit, orig_speed_limit,
other_vehicle, other_vehicle,
dist_behind_other, dist_behind_others_back,
other.speed, other.speed,
)?; )?;
if self.debug { if self.debug {
debug!( debug!(
" {} needs {} to not hit {}. Currently {} behind them", " {} needs {} to not hit {}. Currently {} behind their back",
self.id, accel, other.id, dist_behind_other self.id, accel, other.id, dist_behind_others_back
); );
} }
constraints.push(accel); constraints.push(accel);
} else if self.debug { } else if self.debug {
debug!(" {} is {} behind {}. Scanned ahead so far {} + lookahead dist {} + following dist {} = {} is less than that, so ignore them", self.id, dist_behind_other, other.id, dist_scanned_ahead, dist_to_lookahead, other_vehicle.following_dist(), total_scanning_dist); debug!(" {} is {} behind {}'s back. Scanned ahead so far {} + lookahead dist {} + following dist {} = {} is less than that, so ignore them", self.id, dist_behind_others_back, other.id, dist_scanned_ahead, dist_to_lookahead, kinematics::FOLLOWING_DISTANCE, total_scanning_dist);
} }
} }
@ -379,15 +381,15 @@ impl SimQueue {
// assert here we're not squished together too much // assert here we're not squished together too much
for slice in cars_queue.windows(2) { for slice in cars_queue.windows(2) {
let ((dist1, c1), (dist2, c2)) = (slice[0], slice[1]); let ((dist1, c1), (dist2, c2)) = (slice[0], slice[1]);
let following_dist = cars[&c1].vehicle.following_dist(); let dist_apart = dist1 - dist2 - cars[&c1].vehicle.length;
if dist1 - dist2 < following_dist { if dist_apart < kinematics::FOLLOWING_DISTANCE {
let mut err = format!( let mut err = format!(
"On {:?}, {} and {} are {} apart -- that's {} too close\n", "On {:?}, {} and {} are {} apart -- that's {} too close\n",
id, id,
c1, c1,
c2, c2,
dist1 - dist2, dist_apart,
following_dist - (dist1 - dist2), kinematics::FOLLOWING_DISTANCE - dist_apart,
); );
// TODO We used to have old_queue and could print more debug info. Meh. // TODO We used to have old_queue and could print more debug info. Meh.
err.push_str(&format!("Queue ({}):\n", cars_queue.len())); err.push_str(&format!("Queue ({}):\n", cars_queue.len()));
@ -715,7 +717,7 @@ impl DrivingSimState {
self.cars[&other].speed, self.cars[&other].speed,
other_vehicle.clamp_speed(map.get_parent(start_lane).get_speed_limit()), other_vehicle.clamp_speed(map.get_parent(start_lane).get_speed_limit()),
&params.vehicle, &params.vehicle,
start_dist - other_dist, start_dist - other_dist - params.vehicle.length,
0.0 * si::MPS, 0.0 * si::MPS,
).unwrap(); ).unwrap();
if accel_for_other_to_stop <= other_vehicle.max_deaccel { if accel_for_other_to_stop <= other_vehicle.max_deaccel {

View File

@ -36,7 +36,7 @@ const BUS_LENGTH: Distance = si::Meter {
// At all speeds (including at rest), cars must be at least this far apart, measured from front of // At all speeds (including at rest), cars must be at least this far apart, measured from front of
// one car to the back of the other. // one car to the back of the other.
const FOLLOWING_DISTANCE: Distance = si::Meter { pub const FOLLOWING_DISTANCE: Distance = si::Meter {
value_unsafe: 1.0, value_unsafe: 1.0,
_marker: std::marker::PhantomData, _marker: std::marker::PhantomData,
}; };
@ -243,35 +243,23 @@ impl Vehicle {
dist_at_constant_accel(self.max_deaccel, TIMESTEP, current_speed) dist_at_constant_accel(self.max_deaccel, TIMESTEP, current_speed)
} }
// Relative to the front of the car
pub fn following_dist(&self) -> Distance {
self.length + FOLLOWING_DISTANCE
}
pub fn accel_to_follow( pub fn accel_to_follow(
&self, &self,
our_speed: Speed, our_speed: Speed,
our_speed_limit: Speed, our_speed_limit: Speed,
other: &Vehicle, other: &Vehicle,
dist_behind_other: Distance, dist_behind_others_back: Distance,
other_speed: Speed, other_speed: Speed,
) -> Result<Acceleration, Error> { ) -> Result<Acceleration, Error> {
/* A seemingly failed attempt at a simpler version:
// What if they slam on their brakes right now?
let their_stopping_dist = other.stopping_distance(other.min_next_speed(other_speed));
let worst_case_dist_away = dist_behind_other + their_stopping_dist;
self.accel_to_stop_in_dist(our_speed, worst_case_dist_away)
*/
let us_worst_dist = self.max_lookahead_dist(our_speed, our_speed_limit)?; let us_worst_dist = self.max_lookahead_dist(our_speed, our_speed_limit)?;
let most_we_could_go = self.max_next_dist(our_speed, our_speed_limit)?; let most_we_could_go = self.max_next_dist(our_speed, our_speed_limit)?;
let least_they_could_go = other.min_next_dist(other_speed)?; let least_they_could_go = other.min_next_dist(other_speed)?;
// TODO this optimizes for next tick, so we're playing it really // TODO this optimizes for next tick, so we're playing it really
// conservative here... will that make us fluctuate more? // conservative here... will that make us fluctuate more?
let projected_dist_from_them = dist_behind_other - most_we_could_go + least_they_could_go; let projected_dist_from_them =
let desired_dist_btwn = us_worst_dist + other.following_dist(); dist_behind_others_back - most_we_could_go + least_they_could_go;
let desired_dist_btwn = us_worst_dist + FOLLOWING_DISTANCE;
// Positive = speed up, zero = go their speed, negative = slow down // Positive = speed up, zero = go their speed, negative = slow down
let delta_dist = projected_dist_from_them - desired_dist_btwn; let delta_dist = projected_dist_from_them - desired_dist_btwn;