Package trclib.robotcore
Class TrcStateMachine<T>
java.lang.Object
trclib.robotcore.TrcStateMachine<T>
- Type Parameters:
T
- specifies the State enum type that list all possible states.
This class implements an event driven state machine. The caller can add multiple events for the state machine
to monitor. If one or more events are signaled, the state machine will automatically advance to the specified
next state.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionTrcStateMachine
(String instanceName) Constructor: Creates an instance of the state machine with the given name. -
Method Summary
Modifier and TypeMethodDescriptionvoid
This method adds an event to the event list to be monitored.This method checks whether the state machine is ready.private void
This method clears the signaled state of all the events in the list.This method returns the next state of the state machine.getState()
This method returns the current state of the state machine.boolean
This method checks if the state machine is enabled.boolean
isReady()
This method checks if the state machine is in ready mode.boolean
This method checks if timeout has happened on waiting for event(s).void
This method sets the current state of the state machine.void
This method starts the state machine with the given starting state and puts it in ready mode.void
stop()
This method stops the state machine by disabling it.toString()
This method returns the instance name.void
waitForEvents
(T nextState) This method puts the state machine into not ready mode and starts monitoring the events in the list.void
waitForEvents
(T nextState, boolean waitForAllEvents) This method puts the state machine into not ready mode and starts monitoring the events in the list.void
waitForEvents
(T nextState, boolean clearEvents, boolean waitForAllEvents) This method puts the state machine into not ready mode and starts monitoring the events in the list.void
waitForEvents
(T nextState, boolean clearEvents, boolean waitForAllEvents, double timeout) This method puts the state machine into not ready mode and starts monitoring the events in the list.void
waitForEvents
(T nextState, boolean waitForAllEvents, double timeout) This method puts the state machine into not ready mode and starts monitoring the events in the list.void
waitForEvents
(T nextState, double timeout) This method puts the state machine into not ready mode and starts monitoring the events in the list.void
waitForSingleEvent
(TrcEvent event, T nextState) This method puts the state machine into not ready mode and starts monitoring a single event.void
waitForSingleEvent
(TrcEvent event, T nextState, boolean clearEvent, double timeout) This method puts the state machine into not ready mode and starts monitoring a single event.void
waitForSingleEvent
(TrcEvent event, T nextState, double timeout) This method puts the state machine into not ready mode and starts monitoring a single event.
-
Field Details
-
instanceName
-
eventList
-
currState
-
nextState
-
enabled
private boolean enabled -
ready
private boolean ready -
expired
private boolean expired -
expiredTime
private double expiredTime -
waitForAllEvents
private boolean waitForAllEvents
-
-
Constructor Details
-
TrcStateMachine
Constructor: Creates an instance of the state machine with the given name.- Parameters:
instanceName
- specifies the instance name of the state machine.
-
-
Method Details
-
toString
This method returns the instance name. -
start
This method starts the state machine with the given starting state and puts it in ready mode.- Parameters:
state
- specifies the starting state.
-
stop
public void stop()This method stops the state machine by disabling it. -
getState
This method returns the current state of the state machine.- Returns:
- current state of the state machine.
-
getNextState
This method returns the next state of the state machine.- Returns:
- next state of the state machine.
-
checkReadyAndGetState
This method checks whether the state machine is ready. If so, it returns the current state. If the state machine is not ready, it returns null.- Returns:
- current state of the state machine, null if state machine is not ready.
-
setState
This method sets the current state of the state machine.- Parameters:
state
- specifies the state to set the state machine to.
-
isEnabled
public boolean isEnabled()This method checks if the state machine is enabled.- Returns:
- true if state machine is enabled, false otherwise.
-
isReady
public boolean isReady()This method checks if the state machine is in ready mode. If not, it will enumerate all the events it is monitoring and make sure if any or all of them are signaled as the condition for putting the state machine back in ready mode.- Returns:
- true if the state machine is in ready mode, false otherwise.
-
isTimedout
public boolean isTimedout()This method checks if timeout has happened on waiting for event(s).- Returns:
- true if a timeout was set and expired, false otherwise.
-
addEvent
This method adds an event to the event list to be monitored.- Parameters:
event
- specifies the vent to be added to the list.
-
waitForEvents
public void waitForEvents(T nextState, boolean clearEvents, boolean waitForAllEvents, double timeout) This method puts the state machine into not ready mode and starts monitoring the events in the list. If waitForAllEvents is false and any event on the list is signaled or waitForAllEvents is true and all events are signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event(s) have not been signaled.- Parameters:
nextState
- specifies the next state when the state machine becomes ready.clearEvents
- specifies true to clear all events.waitForAllEvents
- specifies true if all events must be signaled for the state machine to go ready. If false, any signaled event will cause the state machine to go ready.timeout
- specifies a timeout value. A zero value means there is no timeout.
-
waitForEvents
This method puts the state machine into not ready mode and starts monitoring the events in the list. If waitForAllEvents is false and any event on the list is signaled or waitForAllEvents is true and all events are signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state.- Parameters:
nextState
- specifies the next state when the state machine becomes ready.clearEvents
- specifies true to clear all events.waitForAllEvents
- specifies true if all events must be signaled for the state machine to go ready. If false, any signaled event will cause the state machine to go ready.
-
waitForEvents
This method puts the state machine into not ready mode and starts monitoring the events in the list. If any event on the list is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event(s) have not been signaled.- Parameters:
nextState
- specifies the next state when the state machine becomes ready.waitForAllEvents
- specifies true if all events must be signaled for the state machine to go ready. If false, any signaled event will cause the state machine to go ready.timeout
- specifies a timeout value. A zero value means there is no timeout.
-
waitForEvents
This method puts the state machine into not ready mode and starts monitoring the events in the list. If any event on the list is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event(s) have not been signaled.- Parameters:
nextState
- specifies the next state when the state machine becomes ready.waitForAllEvents
- specifies true if all events must be signaled for the state machine to go ready. If false, any signaled event will cause the state machine to go ready.
-
waitForEvents
This method puts the state machine into not ready mode and starts monitoring the events in the list. If any event on the list is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event(s) have not been signaled.- Parameters:
nextState
- specifies the next state when the state machine becomes ready.timeout
- specifies a timeout value. A zero value means there is no timeout.
-
waitForEvents
This method puts the state machine into not ready mode and starts monitoring the events in the list. If any event on the list is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state.- Parameters:
nextState
- specifies the next state when the state machine becomes ready.
-
waitForSingleEvent
This method puts the state machine into not ready mode and starts monitoring a single event. If the event is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event have not been signaled.- Parameters:
event
- specifies the event to wait for.nextState
- specifies the next state when the state machine becomes ready.clearEvent
- specifies true to clear the event.timeout
- specifies a timeout value. A zero value means there is no timeout.
-
waitForSingleEvent
This method puts the state machine into not ready mode and starts monitoring a single event. If the event is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event have not been signaled.- Parameters:
event
- specifies the event to wait for.nextState
- specifies the next state when the state machine becomes ready.timeout
- specifies a timeout value. A zero value means there is no timeout.
-
waitForSingleEvent
This method puts the state machine into not ready mode and starts monitoring a single event. If the event is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event have not been signaled.- Parameters:
event
- specifies the event to wait for.nextState
- specifies the next state when the state machine becomes ready.
-
clearAllEvents
private void clearAllEvents()This method clears the signaled state of all the events in the list.
-