Package trclib.sensor

Class TrcTriggerThresholdZones

java.lang.Object
trclib.sensor.TrcTriggerThresholdZones
All Implemented Interfaces:
TrcTrigger

public class TrcTriggerThresholdZones extends Object implements TrcTrigger
This class implements a Threshold Zones Trigger. It monitors the value source against an array of threshold values. If the sensor reading crosses any of the thresholds in the array, it signals an event or notifies the callback handler so that an action could be performed.
  • Field Details

  • Constructor Details

    • TrcTriggerThresholdZones

      public TrcTriggerThresholdZones(String instanceName, TrcValueSource<Double> valueSource, double[] dataPoints, boolean dataIsTrigger)
      Constructor: Create an instance of the object.
      Parameters:
      instanceName - specifies the instance name.
      valueSource - specifies the interface that implements the value source.
      dataPoints - specifies an array of trigger points or an array of thresholds if dataIsTrigger is true.
      dataIsTrigger - specifies true if dataPoints specifies an array of trigger points, false if it is an array of thresholds. Trigger points will be converted to threshold points.
  • Method Details

    • toString

      public String toString()
      This method returns the instance name and its state.
      Overrides:
      toString in class Object
      Returns:
      instance name and state.
    • setEnabled

      private void setEnabled(boolean enabled, TrcEvent event)
      This method arms/disarms the trigger. It enables/disables the task that monitors the sensor value.
      Parameters:
      enabled - specifies true to enable, false to disable.
      event - specifies the event to signal when the trigger state changed, ignored if enabled is false.
    • enableTrigger

      public void enableTrigger(TrcTrigger.TriggerMode triggerMode, TrcEvent event)
      This method arms the trigger. It enables the task that monitors the sensor value.
      Specified by:
      enableTrigger in interface TrcTrigger
      Parameters:
      triggerMode - specifies the trigger direction that will signal the event. TriggerMode.OnActive will trigger only when crossing a lower zone to a higher zone. TriggerMode.OnInactive will trigger only when crossing from a higher zone to a lower zone. TriggerMode.OnBoth will trigger on both directions.
      event - specifies the event to signal when the trigger state changed.
    • enableTrigger

      public void enableTrigger(TrcTrigger.TriggerMode triggerMode, TrcEvent.Callback callback)
      This method arms the trigger. It enables the task that monitors the sensor value.
      Specified by:
      enableTrigger in interface TrcTrigger
      Parameters:
      triggerMode - specifies the trigger direction that will trigger a callback. TriggerMode.OnActive will trigger only when crossing a lower zone to a higher zone. TriggerMode.OnInactive will trigger only when crossing from a higher zone to a lower zone. TriggerMode.OnBoth will trigger on both directions.
      callback - specifies the callback handler to notify when the trigger state changed.
    • disableTrigger

      public void disableTrigger()
      This method disarms the trigger. It disables the task that monitors the sensor value.
      Specified by:
      disableTrigger in interface TrcTrigger
    • isEnabled

      public boolean isEnabled()
      This method checks if the trigger task is enabled.
      Specified by:
      isEnabled in interface TrcTrigger
      Returns:
      true if enabled, false otherwise.
    • getSensorValue

      public double getSensorValue()
      This method reads the current sensor value. It may return null if it failed to read the sensor.
      Specified by:
      getSensorValue in interface TrcTrigger
      Returns:
      current sensor value.
    • getSensorState

      public boolean getSensorState()
      This method reads the current digital sensor state (not supported).
      Specified by:
      getSensorState in interface TrcTrigger
      Returns:
      current sensor state.
    • getCurrentZone

      public int getCurrentZone()
      This method returns the current zone it is in.
      Returns:
      current zone index.
    • getPreviousZone

      public int getPreviousZone()
      This method returns the previous zone it was in.
      Returns:
      previous zone index.
    • getValueZone

      private int getValueZone(double value)
      This method determines the sensor zone with the given sensor value.
      Parameters:
      value - specifies the sensor value.
      Returns:
      sensor zone the value is in.
    • setTriggerPoints

      public void setTriggerPoints(double[] triggerPoints)
      This method creates and threshold array and calculates all the threshold values. A threshold value is the average of two adjacent trigger points.
      Parameters:
      triggerPoints - specifies the array of trigger points.
    • setThresholds

      public void setThresholds(double[] thresholds)
      This method stores the threshold array.
      Parameters:
      thresholds - specifies the array of thresholds.
    • triggerTask

      private void triggerTask(TrcTaskMgr.TaskType taskType, TrcRobot.RunMode runMode, boolean slowPeriodicLoop)
      This method is called periodically to check the current sensor value against the threshold array to see it crosses any thresholds and the triggerHandler will be notified.
      Parameters:
      taskType - specifies the type of task being run.
      runMode - specifies the competition mode that is running. (e.g. Autonomous, TeleOp, Test).
      slowPeriodicLoop - specifies true if it is running the slow periodic loop on the main robot thread, false otherwise.