ToledoCfTester.java

  1. package org.opentrafficsim.road.gtu.lane.tactical.toledo;

  2. import org.djunits.unit.DurationUnit;
  3. import org.djunits.unit.LengthUnit;
  4. import org.djunits.unit.SpeedUnit;
  5. import org.djunits.value.vdouble.scalar.Acceleration;
  6. import org.djunits.value.vdouble.scalar.Duration;
  7. import org.djunits.value.vdouble.scalar.Length;
  8. import org.djunits.value.vdouble.scalar.Speed;
  9. import org.djunits.value.vdouble.scalar.Time;
  10. import org.opentrafficsim.base.parameters.ParameterException;
  11. import org.opentrafficsim.base.parameters.ParameterSet;
  12. import org.opentrafficsim.core.gtu.GtuException;
  13. import org.opentrafficsim.road.gtu.lane.perception.PerceptionIterable;
  14. import org.opentrafficsim.road.gtu.lane.perception.PerceptionIterableSet;
  15. import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
  16. import org.opentrafficsim.road.gtu.lane.tactical.util.CarFollowingUtil.CarFollowingHeadway;

  17. /**
  18.  * <p>
  19.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  20.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  21.  * </p>
  22.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  23.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  24.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  25.  */

  26. public class ToledoCfTester
  27. {

  28.     /**
  29.      * @param args arguments for the run (should be empty at the moment)
  30.      * @throws ParameterException when Toledo parameters cannot be found
  31.      * @throws GtuException when CarFollowingHeadway cannot be calculated
  32.      */
  33.     public static void main(final String[] args) throws ParameterException, GtuException
  34.     {

  35.         ParameterSet params = new ParameterSet();
  36.         params.setDefaultParameters(ToledoCarFollowing.class);
  37.         ToledoCarFollowing cf = new ToledoCarFollowing();

  38.         Speed desiredSpeed = new Speed(120, SpeedUnit.KM_PER_HOUR);
  39.         Duration dt = new Duration(0.5, DurationUnit.SECOND);
  40.         Time t = Time.ZERO;

  41.         Speed speed = Speed.ZERO;
  42.         Length x = Length.ZERO;
  43.         Length leader = new Length(300, LengthUnit.METER);

  44.         while (x.eq0() || speed.gt0())
  45.         {
  46.             Length s = leader.minus(x);
  47.             PerceptionIterable<Headway> leaders = new PerceptionIterableSet<>(new CarFollowingHeadway(s, Speed.ZERO));
  48.             Length desiredHeadway = cf.desiredHeadway(params, speed);
  49.             Acceleration a = cf.followingAcceleration(params, speed, desiredSpeed, desiredHeadway, leaders);
  50.             System.out.println("t=" + t + ", v=" + speed + ", s=" + s + ", a=" + a);

  51.             a = Acceleration.max(a, speed.divide(dt).neg());
  52.             t = t.plus(dt);
  53.             x = new Length(x.si + speed.si * dt.si + .5 * a.si * dt.si * dt.si, LengthUnit.SI);
  54.             speed = new Speed(speed.si + a.si * dt.si, SpeedUnit.SI);

  55.         }

  56.     }

  57. }