DefaultCaccSensors.java

  1. package org.opentrafficsim.road.gtu.lane.perception.categories.neighbors;

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

  4. import org.djunits.value.vdouble.scalar.Acceleration;
  5. import org.djunits.value.vdouble.scalar.Length;
  6. import org.djunits.value.vdouble.scalar.Speed;
  7. import org.djunits.value.vdouble.scalar.Time;
  8. import org.opentrafficsim.base.parameters.ParameterException;
  9. import org.opentrafficsim.core.gtu.GtuException;
  10. import org.opentrafficsim.core.gtu.GtuType;
  11. import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
  12. import org.opentrafficsim.road.gtu.lane.control.ControlTacticalPlanner;
  13. import org.opentrafficsim.road.gtu.lane.perception.headway.GtuStatus;
  14. import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGtu;
  15. import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGtuSimple;

  16. /**
  17.  * Default CACC sensors. This returns all information except desired speed for the first leader and CACC leaders. Remaining
  18.  * leaders are provided null information.
  19.  * <p>
  20.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  21.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  22.  * </p>
  23.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  24.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  25.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  26.  */
  27. public class DefaultCaccSensors implements HeadwayGtuType
  28. {

  29.     /** {@inheritDoc} */
  30.     @Override
  31.     public HeadwayGtu createDownstreamGtu(final LaneBasedGtu perceivingGtu, final LaneBasedGtu perceivedGtu,
  32.             final Length distance) throws GtuException, ParameterException
  33.     {
  34.         Time t;
  35.         try
  36.         {
  37.             t = perceivingGtu.getSimulator().getSimulatorAbsTime()
  38.                     .minus(((ControlTacticalPlanner) perceivingGtu.getTacticalPlanner()).getSettings()
  39.                             .getParameter(LongitudinalControllerPerception.DELAY));
  40.         }
  41.         catch (ClassCastException exception)
  42.         {
  43.             if (!(perceivingGtu.getTacticalPlanner() instanceof ControlTacticalPlanner))
  44.             {
  45.                 throw new GtuException("DefaultCaccSensors relies on the tactical planner being a ControlTacticalPlanner",
  46.                         exception);
  47.             }
  48.             throw new GtuException(exception);
  49.         }
  50.         String id = perceivedGtu.getId();
  51.         GtuType gtuType = perceivedGtu.getType();
  52.         Length length = perceivedGtu.getLength();
  53.         Length width = perceivedGtu.getWidth();
  54.         Speed v = perceivedGtu.getSpeed(t);
  55.         Acceleration a = perceivedGtu.getAcceleration(t);
  56.         Speed desiredSpeed = null;
  57.         List<GtuStatus> status = new ArrayList<>();
  58.         if (perceivedGtu.isBrakingLightsOn(t))
  59.         {
  60.             status.add(GtuStatus.BRAKING_LIGHTS);
  61.         }
  62.         switch (perceivedGtu.getTurnIndicatorStatus(t))
  63.         {
  64.             case HAZARD:
  65.                 status.add(GtuStatus.EMERGENCY_LIGHTS);
  66.                 break;
  67.             case LEFT:
  68.                 status.add(GtuStatus.LEFT_TURNINDICATOR);
  69.                 break;
  70.             case RIGHT:
  71.                 status.add(GtuStatus.RIGHT_TURNINDICATOR);
  72.                 break;
  73.             default:
  74.                 break;
  75.         }
  76.         return new HeadwayGtuSimple(id, gtuType, distance, length, width, v, a, desiredSpeed,
  77.                 status.toArray(new GtuStatus[status.size()]));
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public HeadwayGtu createUpstreamGtu(final LaneBasedGtu perceivingGtu, final LaneBasedGtu perceivedGtu,
  82.             final Length distance) throws GtuException, ParameterException
  83.     {
  84.         throw new UnsupportedOperationException("Default CACC sensors can only determine leaders.");
  85.     }

  86.     /** {@inheritDoc} */
  87.     @Override
  88.     public HeadwayGtu createParallelGtu(final LaneBasedGtu perceivingGtu, final LaneBasedGtu perceivedGtu,
  89.             final Length overlapFront, final Length overlap, final Length overlapRear) throws GtuException
  90.     {
  91.         throw new UnsupportedOperationException("Default CACC sensors can only determine leaders.");
  92.     }

  93. }