AbstractHeadwayGTU.java

  1. package org.opentrafficsim.road.gtu.lane.perception.headway;

  2. import java.util.ArrayList;
  3. import java.util.EnumSet;
  4. import java.util.List;

  5. import org.djunits.value.vdouble.scalar.Acceleration;
  6. import org.djunits.value.vdouble.scalar.Length;
  7. import org.djunits.value.vdouble.scalar.Speed;
  8. import org.djunits.value.vdouble.scalar.Time;
  9. import org.djutils.exceptions.Throw;
  10. import org.opentrafficsim.core.gtu.GTUException;
  11. import org.opentrafficsim.core.gtu.GTUType;
  12. import org.opentrafficsim.core.network.NetworkException;
  13. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  14. import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
  15. import org.opentrafficsim.road.network.speed.SpeedLimitTypes;

  16. /**
  17.  * Container for a reference to information about a (lane based) GTU and a headway. The Headway can store information about GTUs
  18.  * or objects ahead of the reference GTU, behind the reference GTU, or (partially) parallel to the reference GTU. In addition to
  19.  * the (perceived) headway, several other pieces of information can be stored, such as (perceived) speed, (perceived)
  20.  * acceleration, (perceived) turn indicators, and (perceived) braking lights. <br>
  21.  * Special care must be taken in curves when perceiving headway of a GTU or object on an adjacent lane.The question is whether
  22.  * we perceive the parallel or ahead/behind based on a line perpendicular to the front/back of the GTU (rectangular), or
  23.  * perpendicular to the center line of the lane (wedge-shaped in case of a curve). The difficulty of a wedge-shaped situation is
  24.  * that reciprocity might be violated: in case of a clothoid, for instance, it is not sure that the point on the center line
  25.  * when projected from lane 1 to lane 2 is the same as the projection from lane 2 to lane 1. The same holds for shapes with
  26.  * sharp bends. Therefore, algorithms implementing headway should only project the <i>reference point</i> of the reference GTU
  27.  * on the center line of the adjacent lane, and then calculate the forward position and backward position on the adjacent lane
  28.  * based on the reference point. Still, our human perception of what is parallel and what not, is not reflected by fractional
  29.  * positions. See examples in
  30.  * <a href= "http://simulation.tudelft.nl:8085/browse/OTS-113">http://simulation.tudelft.nl:8085/browse/OTS-113</a>.
  31.  * <p>
  32.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  33.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  34.  * <p>
  35.  * @version $Revision: 1368 $, $LastChangedDate: 2015-09-02 00:20:20 +0200 (Wed, 02 Sep 2015) $, by $Author: averbraeck $,
  36.  *          initial version 11 feb. 2015 <br>
  37.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  38.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  39.  */
  40. public abstract class AbstractHeadwayGTU extends AbstractHeadwayCopy implements HeadwayGTU
  41. {
  42.     /** */
  43.     private static final long serialVersionUID = 20160410L;

  44.     /** The perceived GTU Type, or null if unknown. */
  45.     private final GTUType gtuType;

  46.     /** Whether the GTU is facing the same direction. */
  47.     private final boolean facingSameDirection;

  48.     /** The observable characteristics of the GTU. */
  49.     private final EnumSet<GTUStatus> gtuStatus = EnumSet.noneOf(GTUStatus.class);

  50.     /** Perceived desired speed. */
  51.     private final Speed desiredSpeed;

  52.     /** Perceived width. */
  53.     private final Length width;

  54.     /**
  55.      * Construct a new Headway information object, for a moving GTU ahead of us or behind us.
  56.      * @param id String; the id of the GTU for comparison purposes, can not be null.
  57.      * @param gtuType GTUType; the perceived GTU Type, or null if unknown.
  58.      * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
  59.      * @param facingSameDirection boolean; whether the GTU is facing the same direction.
  60.      * @param length the (perceived) length of the other object; can not be null.
  61.      * @param width the (perceived) width of the other object; can not be null.
  62.      * @param speed the (perceived) speed of the other object; can be null if unknown.
  63.      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
  64.      * @param desiredSpeed Speed; desired speed
  65.      * @param gtuStatus GTUStatus...; the observable characteristics of the GTU.
  66.      * @throws GTUException when id is null, objectType is null, or parameters are inconsistent
  67.      */
  68.     @SuppressWarnings("checkstyle:parameternumber")
  69.     public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length distance, final boolean facingSameDirection,
  70.             final Length length, final Length width, final Speed speed, final Acceleration acceleration,
  71.             final Speed desiredSpeed, final GTUStatus... gtuStatus) throws GTUException
  72.     {
  73.         super(ObjectType.GTU, id, distance, length, speed, acceleration);
  74.         Throw.whenNull(width, "Width may not be null.");
  75.         this.width = width;
  76.         this.facingSameDirection = facingSameDirection;
  77.         this.gtuType = gtuType;
  78.         this.desiredSpeed = desiredSpeed;
  79.         for (GTUStatus status : gtuStatus)
  80.         {
  81.             this.gtuStatus.add(status);
  82.         }
  83.     }

  84.     /**
  85.      * Construct a new Headway information object, for a non-moving GTU ahead of us or behind us.
  86.      * @param id String; the id of the GTU for comparison purposes, can not be null.
  87.      * @param gtuType GTUType; the perceived GTU Type, or null if unknown.
  88.      * @param distance Length; the distance to the other GTU; if this constructor is used, distance cannot be null.
  89.      * @param facingSameDirection boolean; whether the GTU is facing the same direction.
  90.      * @param length the (perceived) length of the other object; can not be null.
  91.      * @param width the (perceived) width of the other object; can not be null.
  92.      * @param desiredSpeed Speed; desired speed
  93.      * @param gtuStatus GTUStatus...; the observable characteristics of the GTU.
  94.      * @throws GTUException when id is null, or parameters are inconsistent
  95.      */
  96.     public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length distance, final boolean facingSameDirection,
  97.             final Length length, final Length width, final Speed desiredSpeed, final GTUStatus... gtuStatus) throws GTUException
  98.     {
  99.         super(ObjectType.GTU, id, distance, length);
  100.         Throw.whenNull(width, "Width may not be null.");
  101.         this.width = width;
  102.         this.facingSameDirection = facingSameDirection;
  103.         this.gtuType = gtuType;
  104.         this.desiredSpeed = desiredSpeed;
  105.         for (GTUStatus status : gtuStatus)
  106.         {
  107.             this.gtuStatus.add(status);
  108.         }
  109.     }

  110.     /**
  111.      * Construct a new Headway information object, for a moving GTU parallel with us.
  112.      * @param id String; the id of the GTU for comparison purposes, can not be null.
  113.      * @param gtuType GTUType; the perceived GTU Type, or null if unknown.
  114.      * @param overlapFront the front-front distance to the other GTU; if this constructor is used, this value cannot be null.
  115.      * @param overlap the 'center' overlap with the other GTU; if this constructor is used, this value cannot be null.
  116.      * @param overlapRear the rear-rear distance to the other GTU; if this constructor is used, this value cannot be null.
  117.      * @param facingSameDirection boolean; whether the GTU is facing the same direction.
  118.      * @param length the (perceived) length of the other object; can not be null.
  119.      * @param width the (perceived) width of the other object; can not be null.
  120.      * @param speed the (perceived) speed of the other GTU; can be null if unknown.
  121.      * @param acceleration the (perceived) acceleration of the other GTU; can be null if unknown.
  122.      * @param desiredSpeed Speed; desired speed
  123.      * @param gtuStatus GTUStatus...; the observable characteristics of the GTU.
  124.      * @throws GTUException when id is null, or parameters are inconsistent
  125.      */
  126.     @SuppressWarnings("checkstyle:parameternumber")
  127.     public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length overlapFront, final Length overlap,
  128.             final Length overlapRear, final boolean facingSameDirection, final Length length, final Length width,
  129.             final Speed speed, final Acceleration acceleration, final Speed desiredSpeed, final GTUStatus... gtuStatus)
  130.             throws GTUException
  131.     {
  132.         super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length, speed, acceleration);
  133.         Throw.whenNull(width, "Width may not be null.");
  134.         this.width = width;
  135.         this.facingSameDirection = facingSameDirection;
  136.         this.gtuType = gtuType;
  137.         this.desiredSpeed = desiredSpeed;
  138.         for (GTUStatus status : gtuStatus)
  139.         {
  140.             this.gtuStatus.add(status);
  141.         }
  142.     }

  143.     /**
  144.      * Construct a new Headway information object, for a non-moving GTU parallel with us.
  145.      * @param id String; the id of the GTU for comparison purposes, can not be null.
  146.      * @param gtuType GTUType; the perceived GTU Type, or null if unknown.
  147.      * @param overlapFront the front-front distance to the other GTU; if this constructor is used, this value cannot be null.
  148.      * @param overlap the 'center' overlap with the other GTU; if this constructor is used, this value cannot be null.
  149.      * @param overlapRear the rear-rear distance to the other GTU; if this constructor is used, this value cannot be null.
  150.      * @param facingSameDirection boolean; whether the GTU is facing the same direction.
  151.      * @param length the (perceived) length of the other object; can not be null.
  152.      * @param width the (perceived) width of the other object; can not be null.
  153.      * @param desiredSpeed Speed; desired speed
  154.      * @param gtuStatus GTUStatus...; the observable characteristics of the GTU.
  155.      * @throws GTUException when id is null, or parameters are inconsistent
  156.      */
  157.     @SuppressWarnings("checkstyle:parameternumber")
  158.     public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length overlapFront, final Length overlap,
  159.             final Length overlapRear, final boolean facingSameDirection, final Length length, final Length width,
  160.             final Speed desiredSpeed, final GTUStatus... gtuStatus) throws GTUException
  161.     {
  162.         super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length);
  163.         Throw.whenNull(width, "Width may not be null.");
  164.         this.width = width;
  165.         this.facingSameDirection = facingSameDirection;
  166.         this.gtuType = gtuType;
  167.         this.desiredSpeed = desiredSpeed;
  168.         for (GTUStatus status : gtuStatus)
  169.         {
  170.             this.gtuStatus.add(status);
  171.         }
  172.     }

  173.     /**
  174.      * @return gtuType
  175.      */
  176.     @Override
  177.     public final GTUType getGtuType()
  178.     {
  179.         return this.gtuType;
  180.     }

  181.     /** {@inheritDoc} */
  182.     @Override
  183.     public final Speed getDesiredSpeed()
  184.     {
  185.         return this.desiredSpeed;
  186.     }

  187.     /**
  188.      * @return facingSameDirection
  189.      */
  190.     @Override
  191.     public final boolean isFacingSameDirection()
  192.     {
  193.         return this.facingSameDirection;
  194.     }

  195.     /** @return were the braking lights on? */
  196.     @Override
  197.     public final boolean isBrakingLightsOn()
  198.     {
  199.         return this.gtuStatus.contains(GTUStatus.BRAKING_LIGHTS);
  200.     }

  201.     /** @return was the left turn indicator on? */
  202.     @Override
  203.     public final boolean isLeftTurnIndicatorOn()
  204.     {
  205.         return this.gtuStatus.contains(GTUStatus.LEFT_TURNINDICATOR);
  206.     }

  207.     /** @return was the right turn indicator on? */
  208.     @Override
  209.     public final boolean isRightTurnIndicatorOn()
  210.     {
  211.         return this.gtuStatus.contains(GTUStatus.RIGHT_TURNINDICATOR);
  212.     }

  213.     /** @return were the emergency lights on? */
  214.     @Override
  215.     public final boolean isEmergencyLightsOn()
  216.     {
  217.         return this.gtuStatus.contains(GTUStatus.EMERGENCY_LIGHTS);
  218.     }

  219.     /** @return was the vehicle honking or ringing its bell when being observed for the headway? */
  220.     @Override
  221.     public final boolean isHonking()
  222.     {
  223.         return this.gtuStatus.contains(GTUStatus.HONK);
  224.     }

  225.     /**
  226.      * For subclasses that create a copy of themselves.
  227.      * @return set of gtu status
  228.      */
  229.     protected final GTUStatus[] getGtuStatus()
  230.     {
  231.         return this.gtuStatus.toArray(new GTUStatus[this.gtuStatus.size()]);
  232.     }

  233.     /**
  234.      * Collects GTU statuses from a gtu.
  235.      * @param gtu LaneBasedGTU; gtu
  236.      * @param when Time; time
  237.      * @return GTU statuses
  238.      */
  239.     public static final GTUStatus[] getGTUStatuses(final LaneBasedGTU gtu, final Time when)
  240.     {
  241.         List<GTUStatus> statuses = new ArrayList<>();
  242.         if (gtu.isBrakingLightsOn(when))
  243.         {
  244.             statuses.add(GTUStatus.BRAKING_LIGHTS);
  245.         }
  246.         if (gtu.getTurnIndicatorStatus(when).isHazard())
  247.         {
  248.             statuses.add(GTUStatus.EMERGENCY_LIGHTS);
  249.         }
  250.         else if (gtu.getTurnIndicatorStatus(when).isLeft())
  251.         {
  252.             statuses.add(GTUStatus.LEFT_TURNINDICATOR);
  253.         }
  254.         else if (gtu.getTurnIndicatorStatus(when).isRight())
  255.         {
  256.             statuses.add(GTUStatus.RIGHT_TURNINDICATOR);
  257.         }
  258.         return statuses.toArray(new GTUStatus[statuses.size()]);
  259.     }

  260.     /**
  261.      * Creates speed limit info for given GTU.
  262.      * @param gtu LaneBasedGTU; gtu to the the speed limit info for
  263.      * @return speed limit info for given GTU
  264.      */
  265.     public static SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGTU gtu)
  266.     {
  267.         SpeedLimitInfo sli = new SpeedLimitInfo();
  268.         sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, gtu.getMaximumSpeed());
  269.         try
  270.         {
  271.             sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN, gtu.getReferencePosition().getLane().getSpeedLimit(gtu.getGTUType()));
  272.         }
  273.         catch (NetworkException | GTUException exception)
  274.         {
  275.             throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
  276.         }
  277.         return sli;
  278.     }

  279.     /** {@inheritDoc} */
  280.     @Override
  281.     public Length getWidth()
  282.     {
  283.         return this.width;
  284.     }

  285.     /** {@inheritDoc} */
  286.     @Override
  287.     @SuppressWarnings("checkstyle:designforextension")
  288.     public String toString()
  289.     {
  290.         return "AbstractHeadwayGTU [gtuType=" + this.gtuType + ", gtuStatus=" + this.gtuStatus + ", getSpeed()="
  291.                 + this.getSpeed() + ", getDistance()=" + this.getDistance() + ", getAcceleration()=" + this.getAcceleration()
  292.                 + "]";
  293.     }

  294. }