Package trclib.sensor

Class TrcTriggerThresholdRange

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

public class TrcTriggerThresholdRange extends Object implements TrcTrigger
This class implements a Threshold Range Trigger. It monitors the value source against the lower and upper threshold values. If the value stays within the lower and upper thresholds for at least the given settling period, the the trigger state is set to active and the trigger callback is called or an event is signaled. If the value exits the threshold range, the trigger state is set to inactive and the trigger callback is also called or an event signaled.
  • Field Details

  • Constructor Details

    • TrcTriggerThresholdRange

      public TrcTriggerThresholdRange(String instanceName, TrcValueSource<Double> valueSource)
      Constructor: Create an instance of the object.
      Parameters:
      instanceName - specifies the instance name.
      valueSource - specifies the interface that implements the value source.
  • 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 mode that will signal the event.
      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 mode that will trigger a callback.
      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 analog 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.
    • setTrigger

      public void setTrigger(double lowerThreshold, double upperThreshold, double settlingPeriod, int maxCachedSize)
      This method sets the lower/upper threshold values within which the sensor reading must stay for at least the settling period for it to trigger the notification.
      Parameters:
      lowerThreshold - specifies the lower threshold value for the trigger.
      upperThreshold - specifies the upper threshold value for the trigger.
      settlingPeriod - specifies the period in seconds the sensor value must stay within threshold range for it to trigger.
      maxCachedSize - specifies the max number of of cached values.
    • setTrigger

      public void setTrigger(double lowerThreshold, double upperThreshold, double settlingPeriod)
      This method sets the lower/upper threshold values within which the sensor reading must stay for at least the settling period for it to trigger the notification.
      Parameters:
      lowerThreshold - specifies the lower threshold value for the trigger.
      upperThreshold - specifies the upper threshold value for the trigger.
      settlingPeriod - specifies the period in seconds the sensor value must stay within threshold range for it to trigger.
    • getTriggerSettlingData

      public Double[] getTriggerSettlingData()
      This method returns an array of the data recorded during the trigger settling period.
      Returns:
      array of trigger settling data, null if no data recorded.
    • getMinimumValue

      public Double getMinimumValue()
      This method returns the minimum sensor value recorded in the cache. Cache only records values within thresholds.
      Returns:
      minimum value in the cache, null if cache was empty.
    • getMaximumValue

      public Double getMaximumValue()
      This method returns the maximum sensor value recorded in the cache. Cache only records values within thresholds.
      Returns:
      maximum value in the cache, null if cache was empty.
    • getAverageValue

      public Double getAverageValue()
      This method calculates the average sensor value recorded in the cache.
      Returns:
      average value calculated.
    • triggerTask

      private void triggerTask(TrcTaskMgr.TaskType taskType, TrcRobot.RunMode runMode, boolean slowPeriodicLoop)
      This method is called periodically to check if the sensor value is within the lower and upper threshold range.
      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.