Class SpeedMatcher

java.lang.Object
jmri.jmrix.bachrus.speedmatcher.SpeedMatcher
All Implemented Interfaces:
EventListener, ProgListener, ThrottleListener
Direct Known Subclasses:
BasicSpeedMatcher, SpeedStepScaleSpeedMatcher

public abstract class SpeedMatcher extends Object implements ThrottleListener, ProgListener
Abstract class defining the basic operations of a speed matcher. All speed matcher implementations must extend this class.
  • Field Details

  • Constructor Details

    • SpeedMatcher

      Constructor for the abstract SpeedMatcher at the core of any Speed Matcher
      Parameters:
      config - SpeedMatcherConfig for initializing the SpeedMatcher
  • Method Details

    • startSpeedMatcher

      public abstract boolean startSpeedMatcher()
      Starts the speed matching process
      Returns:
      true if speed matching started successfully, false otherwise
    • stopSpeedMatcher

      public abstract void stopSpeedMatcher()
      Stops the speed matching process
    • isSpeedMatcherIdle

      public abstract boolean isSpeedMatcherIdle()
      Indicates if the speed matcher is idle (not currently speed matching)
      Returns:
      true if idle, false otherwise
    • updateCurrentSpeed

      public void updateCurrentSpeed(float currentSpeedKPH)
      Updates the locomotive's current speed in the speed matcher
      Parameters:
      currentSpeedKPH - the locomotive's current speed in KPH
    • validate

      protected abstract boolean validate()
      Validates the speed matcher's configuration
      Returns:
      true if the configuration is valid, false otherwise
    • cleanUpSpeedMatcher

      protected void cleanUpSpeedMatcher()
      Cleans up the speed matcher when speed matching is stopped or is finished
    • initializeAndStartSpeedMatcher

      protected boolean initializeAndStartSpeedMatcher(ActionListener timerActionListener)
      Shared code to initialize the speed matcher's programmer and throttle and start the speed matching timer. Expected to be called in an implementing speed matcher's Start function.
      Parameters:
      timerActionListener - callback to fire when the timer times out
      Returns:
      true if initialization and start is successful, false otherwise
    • startSpeedMatchStateTimer

      protected void startSpeedMatchStateTimer()
      Starts the speed match state timer
    • stopSpeedMatchStateTimer

      protected void stopSpeedMatchStateTimer()
      Stops the speed match state timer
    • setSpeedMatchStateTimerDuration

      protected void setSpeedMatchStateTimerDuration(int timerDuration)
      Sets the duration for the speed match timer
      Parameters:
      timerDuration - timer duration in milliseconds
    • setThrottle

      protected void setThrottle(boolean isForward, int speedStep)
      Sets the speed matcher's throttle direction and speed safely within timers to protect against executing a throttle change to close to setting a CV
      Parameters:
      isForward - true for forward, false for revers
      speedStep - 0-28 or 0-128 depending on mode
    • setSpeedMatchError

      protected void setSpeedMatchError(float speedTarget)
      Sets the current speed match error
      Parameters:
      speedTarget - - target speed in KPH
    • getNextSpeedMatchValue

      protected int getNextSpeedMatchValue(int lastValue, int max, int min)
      Gets the next value to try for speed matching
      Parameters:
      lastValue - the last speed match CV value tried
      max - the maximum value
      min - the minimum value
      Returns:
      the next value to try for speed matching [min:max]
    • resetSpeedMatcher

      protected void resetSpeedMatcher(int initialValueDelta)
      Resets the speed matcher with the given value delta
      Parameters:
      initialValueDelta - the value delta to use for the next use of the speed matcher
    • writeVStart

      protected void writeVStart(int value)
      Starts writing vStart (CV 2) using the ops mode programmer
      Parameters:
      value - vStart value (0-255 inclusive)
    • writeVMid

      protected void writeVMid(int value)
      Starts writing vMid (CV 6) using the ops mode programmer
      Parameters:
      value - vMid value (0-255 inclusive)
    • writeVHigh

      protected void writeVHigh(int value)
      Starts writing vHigh (CV 5) using the ops mode programmer
      Parameters:
      value - vHigh value (0-255 inclusive)
    • writeMomentumAccel

      protected void writeMomentumAccel(int value)
      Starts writing acceleration momentum (CV 3) using the ops mode programmer
      Parameters:
      value - acceleration value (0-255 inclusive)
    • writeMomentumDecel

      protected void writeMomentumDecel(int value)
      Starts writing deceleration momentum (CV 4) using the ops mode programmer
      Parameters:
      value - deceleration value (0-255 inclusive)
    • writeForwardTrim

      protected void writeForwardTrim(int value)
      Starts writing forward trim (CV 66) using the ops mode programmer
      Parameters:
      value - forward trim value (0-255 inclusive)
    • writeReverseTrim

      protected void writeReverseTrim(int value)
      Starts writing reverse trim (CV 95) using the ops mode programmer
      Parameters:
      value - reverse trim value (0-255 inclusive)
    • writeSpeedTableStep

      protected void writeSpeedTableStep(SpeedMatcher.SpeedTableStep step, int value)
      Starts writing a Speed Table Step CV (CV 67-94) using the ops mode programmer
      Parameters:
      step - the SpeedTableStep to set
      value - speed table step value (0-255 inclusive)
    • programmingOpReply

      public void programmingOpReply(int value, int status)
      Called when the programmer has completed its operation
      Specified by:
      programmingOpReply in interface ProgListener
      Parameters:
      value - value from a read operation, or value written on a write
      status - denotes the completion code. Note that this is a bitwise combination of the various states codes defined in this interface. (see ProgListener.java for possible values)
    • notifyThrottleFound

      Called when a throttle is found Implementers must override, call super, and start speed matcher in implementation
      Specified by:
      notifyThrottleFound in interface ThrottleListener
      Parameters:
      t - the requested DccThrottle
    • notifyDecisionRequired

      Called when we must decide whether to steal the throttle for the requested address. This is an automatically stealing implementation, so the throttle will be automatically stolen
      Specified by:
      notifyDecisionRequired in interface ThrottleListener
      Parameters:
      address - the requested address
      question - the question being asked, steal / cancel, share / cancel, steal / share / cancel
    • notifyFailedThrottleRequest

      public void notifyFailedThrottleRequest(LocoAddress address, String reason)
      Called when a throttle could not be obtained
      Specified by:
      notifyFailedThrottleRequest in interface ThrottleListener
      Parameters:
      address - the requested address
      reason - the reason the throttle could not be obtained