Skip to content

Commit a81b4e7

Browse files
Merge pull request #9 from ncsurobotics/skeleton
Add state machine skeleton
2 parents f644a87 + a8e69d6 commit a81b4e7

File tree

5 files changed

+198
-1
lines changed

5 files changed

+198
-1
lines changed

app/src/main/java/org/aquapackrobotics/sw8s/App.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@
33
*/
44
package org.aquapackrobotics.sw8s;
55

6+
import java.util.concurrent.ScheduledThreadPoolExecutor;
7+
8+
import org.aquapackrobotics.sw8s.missions.Mission;
9+
import org.aquapackrobotics.sw8s.missions.AutoMission;
10+
611
public class App {
12+
13+
static final int POOLSIZE = 1;
14+
715
public String getGreeting() {
816
return "Hello World!";
917
}
1018

1119
public static void main(String[] args) {
12-
System.out.println(new App().getGreeting());
20+
ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(POOLSIZE);
21+
Mission mission = (Mission) new AutoMission(pool);
22+
23+
mission.run();
1324
}
1425
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.aquapackrobotics.sw8s.missions;
2+
3+
import java.util.concurrent.ScheduledThreadPoolExecutor;
4+
5+
import org.aquapackrobotics.sw8s.states.State;
6+
7+
/**
8+
* Competition mission, fully autonomous.
9+
*/
10+
public class AutoMission extends Mission {
11+
12+
public AutoMission(ScheduledThreadPoolExecutor pool) {
13+
super(pool);
14+
}
15+
16+
// TODO: implement
17+
@Override
18+
protected State initialState() {
19+
return null;
20+
}
21+
22+
// TODO: implement
23+
@Override
24+
protected void executeState(State state) {
25+
}
26+
27+
// TODO: implement
28+
@Override
29+
protected State nextState(State state) {
30+
return null;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.aquapackrobotics.sw8s.states;
2+
3+
import java.util.concurrent.ScheduledThreadPoolExecutor;
4+
5+
public class InitState extends State {
6+
public InitState(ScheduledThreadPoolExecutor pool) {
7+
super(pool);
8+
}
9+
10+
// TODO: implement
11+
public void onEnter() {
12+
}
13+
14+
// TODO: implement
15+
public boolean onPeriodic() {
16+
return false;
17+
}
18+
19+
// TODO: implement
20+
public void onExit() {
21+
}
22+
23+
// TODO: implement
24+
public State nextState() {
25+
return null;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.aquapackrobotics.sw8s.states;
2+
3+
import java.util.concurrent.ScheduledThreadPoolExecutor;
4+
5+
/**
6+
* One step of machine execution.
7+
* <p>
8+
* Every separate task that is completed should be a different state.
9+
* Each task has start conditions, execution logic, and end conditions
10+
*/
11+
public abstract class State {
12+
protected ScheduledThreadPoolExecutor pool;
13+
14+
/**
15+
* Creates a state instance.
16+
* <p>
17+
* Do not preserve State instances, create new ones when a task repeats.
18+
*
19+
* @param pool the Missions' pool for task submission
20+
*/
21+
public State(ScheduledThreadPoolExecutor pool) {
22+
this.pool = pool;
23+
}
24+
25+
/**
26+
* Enforce starting conditions.
27+
*/
28+
abstract public void onEnter();
29+
30+
/**
31+
* Repeatedly called by state machine.
32+
* <p>
33+
* Should not loop.
34+
* Looping here can trap the state machine.
35+
* Condition checking should be if, not while.
36+
*
37+
* @return if exit conditions are met
38+
*/
39+
abstract public boolean onPeriodic();
40+
41+
/**
42+
* Cleans up state effects and threads.
43+
* <p>
44+
* After onExit, the robot and thread pool state should be identical to
45+
* before onEnter.
46+
*/
47+
abstract public void onExit();
48+
49+
/**
50+
* Recommends the next state.
51+
* <p>
52+
* May be ignored by a state machine override.
53+
* Otherwise should be the main decision on the next state.
54+
*
55+
* @return an instance for the next state
56+
*/
57+
abstract public State nextState();
58+
}

0 commit comments

Comments
 (0)