Package jmri.jmrix.bachrus.speedmatcher
Class SpeedMatcher
- java.lang.Object
-
- jmri.jmrix.bachrus.speedmatcher.SpeedMatcher
-
- All Implemented Interfaces:
java.util.EventListener
,ProgListener
,ThrottleListener
- Direct Known Subclasses:
BasicSpeedMatcher
,SpeedStepScaleSpeedMatcher
public abstract class SpeedMatcher extends java.lang.Object implements ThrottleListener, ProgListener
Abstract class defining the basic operations of a speed matcher. All speed matcher implementations must extend this class.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
SpeedMatcher.ProgrammerState
protected static class
SpeedMatcher.SpeedMatcherCV
static class
SpeedMatcher.SpeedTableStep
-
Nested classes/interfaces inherited from interface jmri.ThrottleListener
ThrottleListener.DecisionType
-
-
Field Summary
Fields Modifier and Type Field Description protected float
ALLOWED_SPEED_MATCH_ERROR
protected float
currentSpeedKPH
protected DccLocoAddress
dccLocoAddress
protected int
INITIAL_MOMENTUM
protected float
Kd
protected float
Ki
protected float
Kp
protected float
lastSpeedMatchError
protected AddressedProgrammer
opsModeProgrammer
protected PowerManager
powerManager
protected SpeedMatcher.ProgrammerState
programmerState
protected int
REVERSE_TRIM_MAX
protected int
REVERSE_TRIM_MIN
protected float
speedMatchDerivative
protected float
speedMatchError
protected float
speedMatchIntegral
protected javax.swing.JButton
startStopButton
protected javax.swing.JLabel
statusLabel
protected int
stepDuration
protected float
Td
protected float
Ti
protected boolean
trimReverseSpeed
protected int
warmUpForwardSeconds
protected int
warmUpReverseSeconds
-
Fields inherited from interface jmri.ProgListener
CommError, ConfirmFailed, FailedTimeout, NoAck, NoLocoDetected, NotImplemented, OK, ProgrammerBusy, ProgrammingShort, SequenceError, UnknownError, UserAborted
-
-
Constructor Summary
Constructors Constructor Description SpeedMatcher(SpeedMatcherConfig config)
Constructor for the abstract SpeedMatcher at the core of any Speed Matcher
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
cleanUpSpeedMatcher()
Cleans up the speed matcher when speed matching is stopped or is finishedprotected int
getNextSpeedMatchValue(int lastValue, int max, int min)
Gets the next value to try for speed matching using a PID controllerprotected boolean
initializeAndStartSpeedMatcher(java.awt.event.ActionListener timerActionListener)
Shared code to initialize the speed matcher's programmer and throttle and start the speed matching timer.abstract boolean
isSpeedMatcherIdle()
Indicates if the speed matcher is idle (not currently speed matching)void
notifyDecisionRequired(LocoAddress address, ThrottleListener.DecisionType question)
Called when we must decide whether to steal the throttle for the requested address.void
notifyFailedThrottleRequest(LocoAddress address, java.lang.String reason)
Called when a throttle could not be obtainedvoid
notifyThrottleFound(DccThrottle t)
Called when a throttle is found Implementers must override, call super, and start speed matcher in implementationvoid
programmingOpReply(int value, int status)
Called when the programmer has completed its operationprotected void
resetSpeedMatchError()
Resets the PID controller's speed match error, integral, and derivativeprotected void
setSpeedMatchError(float speedTarget)
Sets the PID controller's speed match error for speed matchingprotected void
setSpeedMatchStateTimerDuration(int timerDuration)
Sets the duration for the speed match timerprotected 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 CVabstract boolean
startSpeedMatcher()
Starts the speed matching processprotected void
startSpeedMatchStateTimer()
Starts the speed match state timerabstract void
stopSpeedMatcher()
Stops the speed matching processprotected void
stopSpeedMatchStateTimer()
Stops the speed match state timervoid
updateCurrentSpeed(float currentSpeedKPH)
Updates the locomotive's current speed in the speed matcherprotected abstract boolean
validate()
Validates the speed matcher's configurationprotected void
writeForwardTrim(int value)
Starts writing forward trim (CV 66) using the ops mode programmerprotected void
writeMomentumAccel(int value)
Starts writing acceleration momentum (CV 3) using the ops mode programmerprotected void
writeMomentumDecel(int value)
Starts writing deceleration momentum (CV 4) using the ops mode programmerprotected void
writeReverseTrim(int value)
Starts writing reverse trim (CV 95) using the ops mode programmerprotected void
writeSpeedTableStep(SpeedMatcher.SpeedTableStep step, int value)
Starts writing a Speed Table Step CV (CV 67-94) using the ops mode programmerprotected void
writeVHigh(int value)
Starts writing vHigh (CV 5) using the ops mode programmerprotected void
writeVMid(int value)
Starts writing vMid (CV 6) using the ops mode programmerprotected void
writeVStart(int value)
Starts writing vStart (CV 2) using the ops mode programmer
-
-
-
Field Detail
-
Kp
protected final float Kp
- See Also:
- Constant Field Values
-
Ti
protected final float Ti
- See Also:
- Constant Field Values
-
Td
protected final float Td
- See Also:
- Constant Field Values
-
Ki
protected final float Ki
- See Also:
- Constant Field Values
-
Kd
protected final float Kd
- See Also:
- Constant Field Values
-
INITIAL_MOMENTUM
protected final int INITIAL_MOMENTUM
- See Also:
- Constant Field Values
-
REVERSE_TRIM_MAX
protected final int REVERSE_TRIM_MAX
- See Also:
- Constant Field Values
-
REVERSE_TRIM_MIN
protected final int REVERSE_TRIM_MIN
- See Also:
- Constant Field Values
-
ALLOWED_SPEED_MATCH_ERROR
protected final float ALLOWED_SPEED_MATCH_ERROR
- See Also:
- Constant Field Values
-
speedMatchIntegral
protected float speedMatchIntegral
-
speedMatchDerivative
protected float speedMatchDerivative
-
lastSpeedMatchError
protected float lastSpeedMatchError
-
speedMatchError
protected float speedMatchError
-
trimReverseSpeed
protected boolean trimReverseSpeed
-
warmUpForwardSeconds
protected int warmUpForwardSeconds
-
warmUpReverseSeconds
protected int warmUpReverseSeconds
-
stepDuration
protected int stepDuration
-
currentSpeedKPH
protected float currentSpeedKPH
-
dccLocoAddress
protected DccLocoAddress dccLocoAddress
-
opsModeProgrammer
protected AddressedProgrammer opsModeProgrammer
-
powerManager
protected PowerManager powerManager
-
statusLabel
protected javax.swing.JLabel statusLabel
-
startStopButton
protected javax.swing.JButton startStopButton
-
programmerState
protected SpeedMatcher.ProgrammerState programmerState
-
-
Constructor Detail
-
SpeedMatcher
public SpeedMatcher(SpeedMatcherConfig config)
Constructor for the abstract SpeedMatcher at the core of any Speed Matcher- Parameters:
config
- SpeedMatcherConfig for initializing the SpeedMatcher
-
-
Method Detail
-
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(java.awt.event.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 reversspeedStep
- 0-28 or 0-128 depending on mode
-
setSpeedMatchError
protected void setSpeedMatchError(float speedTarget)
Sets the PID controller's speed match error for speed matching- 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 using a PID controller- Parameters:
lastValue
- the last speed match CV value triedmax
- the maximum valuemin
- the minimum value- Returns:
- the next value to try for speed matching [min:max]
-
resetSpeedMatchError
protected void resetSpeedMatchError()
Resets the PID controller's speed match error, integral, and derivative
-
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 setvalue
- 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 interfaceProgListener
- Parameters:
value
- value from a read operation, or value written on a writestatus
- 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
public void notifyThrottleFound(DccThrottle t)
Called when a throttle is found Implementers must override, call super, and start speed matcher in implementation- Specified by:
notifyThrottleFound
in interfaceThrottleListener
- Parameters:
t
- the requested DccThrottle
-
notifyDecisionRequired
public void notifyDecisionRequired(LocoAddress address, ThrottleListener.DecisionType question)
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 interfaceThrottleListener
- Parameters:
address
- the requested addressquestion
- the question being asked, steal / cancel, share / cancel, steal / share / cancel
-
notifyFailedThrottleRequest
public void notifyFailedThrottleRequest(LocoAddress address, java.lang.String reason)
Called when a throttle could not be obtained- Specified by:
notifyFailedThrottleRequest
in interfaceThrottleListener
- Parameters:
address
- the requested addressreason
- the reason the throttle could not be obtained
-
-