|
| 1 | +package org.aquapackrobotics.sw8s.missions; |
| 2 | + |
| 3 | +import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 4 | + |
| 5 | +import org.aquapackrobotics.sw8s.states.State; |
| 6 | + |
| 7 | +/** |
| 8 | + * Robot behavior interface. |
| 9 | + * <p> |
| 10 | + * Missions are state machines, using State objects to progress. |
| 11 | + */ |
| 12 | +public abstract class Mission { |
| 13 | + /** |
| 14 | + * Processing thread pool. |
| 15 | + * <p> |
| 16 | + * Repeated tasks are Runnables, submitted with FixedRate. |
| 17 | + * <p> |
| 18 | + * Single tasks with a return value are Callables, submitted with schedule |
| 19 | + */ |
| 20 | + protected ScheduledThreadPoolExecutor pool; |
| 21 | + |
| 22 | + /** |
| 23 | + * Generic Mission constructor. |
| 24 | + * <p> |
| 25 | + * Extension isn't expected. |
| 26 | + * Guarantees all Mission objects use a thread pool. |
| 27 | + * |
| 28 | + * @param pool A non-filled thread pool |
| 29 | + */ |
| 30 | + public Mission(ScheduledThreadPoolExecutor pool) { |
| 31 | + this.pool = pool; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Execute the state machine. |
| 36 | + * <p> |
| 37 | + * Proceeds through all states in graph. |
| 38 | + */ |
| 39 | + public void run() { |
| 40 | + State currentState = initialState(); |
| 41 | + while (currentState != null) { |
| 42 | + executeState(currentState); |
| 43 | + currentState = nextState(currentState); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns the machine's starting state. |
| 49 | + */ |
| 50 | + protected abstract State initialState(); |
| 51 | + |
| 52 | + /** |
| 53 | + * Wraps running the code in a state. |
| 54 | + * <p> |
| 55 | + * Wrapper is useful for non-state actions, i.e. checking operator input |
| 56 | + * |
| 57 | + * @param state current machine state |
| 58 | + */ |
| 59 | + protected abstract void executeState(State state); |
| 60 | + |
| 61 | + /** |
| 62 | + * Computes the next machine state. |
| 63 | + * <p> |
| 64 | + * Uses fields from the current state and Mission parameters. |
| 65 | + * |
| 66 | + * @param state current machine state |
| 67 | + */ |
| 68 | + protected abstract State nextState(State state); |
| 69 | +} |
0 commit comments