Browse Source

re-implemented basic autopilot

lookup-tables
Stephan Richter 4 years ago
parent
commit
9effb8d337
  1. 2
      pom.xml
  2. 6
      src/main/java/de/srsoftware/web4rail/Route.java
  3. 2
      src/main/java/de/srsoftware/web4rail/actions/StartStopAuto.java
  4. 80
      src/main/java/de/srsoftware/web4rail/moving/Train.java
  5. 11
      src/main/java/de/srsoftware/web4rail/threads/BrakeProcessor.java
  6. 11
      src/main/java/de/srsoftware/web4rail/threads/PathFinder.java

2
pom.xml

@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.srsoftware</groupId>
<artifactId>web4rail</artifactId>
<version>1.3.57</version>
<version>1.3.58</version>
<name>Web4Rail</name>
<packaging>jar</packaging>
<description>Java Model Railway Control</description>

6
src/main/java/de/srsoftware/web4rail/Route.java

@ -378,11 +378,9 @@ public class Route extends BaseClass { @@ -378,11 +378,9 @@ public class Route extends BaseClass {
public void finish(Train train) {
LOG.debug("{}.finish()",this);
train.endRoute();
free();
train.set(endBlock);
train.heading(endDirection);
train.endRoute(endBlock,endDirection);
train = null;
free();
}
private void free() {

2
src/main/java/de/srsoftware/web4rail/actions/StartStopAuto.java

@ -24,7 +24,7 @@ public class StartStopAuto extends Action { @@ -24,7 +24,7 @@ public class StartStopAuto extends Action {
public boolean fire(Context context,Object cause) {
if (isNull(context.train())) return false;
if (inverted) {
context.train().automatic();
context.train().start(true);
} else context.train().quitAutopilot();
return true;
}

80
src/main/java/de/srsoftware/web4rail/moving/Train.java

@ -96,6 +96,8 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -96,6 +96,8 @@ public class Train extends BaseClass implements Comparable<Train> {
private PathFinder pathFinder;
private boolean autopilot = false;
public static Object action(HashMap<String, String> params, Plan plan) throws IOException {
String action = params.get(ACTION);
if (isNull(action)) return t("No action passed to Train.action!");
@ -115,7 +117,7 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -115,7 +117,7 @@ public class Train extends BaseClass implements Comparable<Train> {
case ACTION_ADD:
return train.addCar(params);
case ACTION_AUTO:
return train.automatic();
return train.start(true);
case ACTION_CONNECT:
return train.connect(params);
case ACTION_DROP:
@ -135,14 +137,13 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -135,14 +137,13 @@ public class Train extends BaseClass implements Comparable<Train> {
case ACTION_PROPS:
return train.properties();
case ACTION_QUIT:
return train.quitAutopilot();
return train.properties(train.quitAutopilot());
case ACTION_REVERSE:
return train.reverse().properties();
case ACTION_SLOWER10:
return train.slower(10);
case ACTION_START:
String error = train.start();
return train.properties(error);
return train.properties(train.start(false));
case ACTION_STOP:
return train.stopNow();
case ACTION_TIMES:
@ -177,10 +178,6 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -177,10 +178,6 @@ public class Train extends BaseClass implements Comparable<Train> {
return properties();
}
public boolean automatic() {
return false;
}
private Fieldset blockHistory() {
Fieldset fieldset = new Fieldset(t("Last blocks")).id("props-history");
Tag list = new Tag("ol");
@ -355,7 +352,7 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -355,7 +352,7 @@ public class Train extends BaseClass implements Comparable<Train> {
}
public Block destination(){
return null; // TODO
return destination;
}
public String destinationTag() {
@ -404,14 +401,20 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -404,14 +401,20 @@ public class Train extends BaseClass implements Comparable<Train> {
while (!trace.isEmpty()) trace.removeFirst().free();
}
public void endRoute() {
public void endRoute(Block newBlock, Direction newDirection) {
setSpeed(0);
if (isSet(brakeProcessor)) brakeProcessor.end();
brakeProcessor = null;
set(newBlock);
if (newBlock == destination) {
destination = null;
quitAutopilot();
}
heading(newDirection);
route = null;
brakeProcessor = null;
if (autopilot) start(false);
}
private Tag faster(int steps) {
setSpeed(speed+steps);
return properties();
@ -665,9 +668,12 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -665,9 +668,12 @@ public class Train extends BaseClass implements Comparable<Train> {
return super.properties(preForm, formInputs, postForm,errors);
}
public Object quitAutopilot() {
// TODO Auto-generated method stub
return "not implemented";
public String quitAutopilot() {
if (autopilot) {
autopilot = false;
return null;
}
return t("Autopilot already was disabled!");
}
@Override
@ -764,21 +770,31 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -764,21 +770,31 @@ public class Train extends BaseClass implements Comparable<Train> {
}
}
private void set(BrakeProcessor bp) {
LOG.debug("{}.set({})",this,bp);
brakeProcessor = bp;
}
private void set(PathFinder pf) {
LOG.debug("{}.set({})",this,pf);
pathFinder = pf;
}
private Object setDestination(HashMap<String, String> params) {
String dest = params.get(DESTINATION);
if (isNull(dest)) return t("No destination supplied!");
if (isNull(dest)) return properties(t("No destination supplied!"));
if (dest.isEmpty()) {
destination = null;
return properties();
}
Tile tile = plan.get(new Id(dest), true);
if (isNull(tile)) return t("Tile {} not known!",dest);
if (isNull(tile)) return properties(t("Tile {} not known!",dest));
if (tile instanceof Block) {
destination = (Block) tile;
start();
start(false);
return t("{} now heading for {}",this,destination);
}
return t("{} is not a block!",tile);
return properties(t("{} is not a block!",tile));
}
public Object setFunction(int num, boolean active) {
@ -865,9 +881,10 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -865,9 +881,10 @@ public class Train extends BaseClass implements Comparable<Train> {
}
public String start() {
public String start(boolean autopilot) {
this.autopilot |= autopilot;
if (isSet(pathFinder)) return t("Pathfinder already active for {}!",this);
pathFinder = new PathFinder(this,currentBlock,direction) {
PathFinder pathFinder = new PathFinder(this,currentBlock,direction) {
@Override
public void aborted() {
@ -889,23 +906,17 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -889,23 +906,17 @@ public class Train extends BaseClass implements Comparable<Train> {
public void prepared(Route newRoute) {
LOG.debug("Prepared route {} for {}",newRoute,Train.this);
route = newRoute;
pathFinder = null;
set((PathFinder)null);
route.start(Train.this);
}
}.start();
};
set(pathFinder);
return null;
}
public static void startAll() {
LOG.debug("Train.startAll()");
for (Train train : BaseClass.listElements(Train.class)) LOG.info(train.startAutopilot());
}
private String startAutopilot() {
// TODO Auto-generated method stub
return null;
for (Train train : BaseClass.listElements(Train.class)) LOG.info(train.start(true));
}
public void startBrake() {
@ -917,19 +928,20 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -917,19 +928,20 @@ public class Train extends BaseClass implements Comparable<Train> {
LOG.debug("{} already is braking.");
return;
}
brakeProcessor = new BrakeProcessor(this).start();
set(new BrakeProcessor(this));
}
public Window stopNow() {
setSpeed(0);
if (isSet(pathFinder)) {
pathFinder.abort();
pathFinder = null;
set((PathFinder)null);
}
if (isSet(route)) {
route.reset();
route = null;
}
quitAutopilot();
return properties();
}
@ -1021,7 +1033,7 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -1021,7 +1033,7 @@ public class Train extends BaseClass implements Comparable<Train> {
}
public boolean usesAutopilot() {
return false; // TODO
return autopilot ;
}
public boolean isStoppable() {

11
src/main/java/de/srsoftware/web4rail/threads/BrakeProcessor.java

@ -28,6 +28,9 @@ public class BrakeProcessor extends BaseClass implements Runnable { @@ -28,6 +28,9 @@ public class BrakeProcessor extends BaseClass implements Runnable {
public BrakeProcessor(Train train) {
this.train = train;
Thread thread = new Thread(this);
thread.setName(getClass().getSimpleName());
thread.start();
}
public void end() {
@ -88,12 +91,4 @@ public class BrakeProcessor extends BaseClass implements Runnable { @@ -88,12 +91,4 @@ public class BrakeProcessor extends BaseClass implements Runnable {
}
LOG.debug("{} reached final speed.", train);
}
public BrakeProcessor start() {
Thread thread = new Thread(this);
thread.setName(getClass().getSimpleName());
thread.start();
return this;
}
}

11
src/main/java/de/srsoftware/web4rail/threads/PathFinder.java

@ -30,6 +30,10 @@ public abstract class PathFinder extends BaseClass implements Runnable{ @@ -30,6 +30,10 @@ public abstract class PathFinder extends BaseClass implements Runnable{
this.train = train;
this.startBlock = start;
this.direction = direction;
Thread thread = new Thread(this);
thread.setName("Pathfinder("+train+")");
thread.start();
}
public void abort() {
@ -178,11 +182,4 @@ public abstract class PathFinder extends BaseClass implements Runnable{ @@ -178,11 +182,4 @@ public abstract class PathFinder extends BaseClass implements Runnable{
public abstract void locked(Route r);
public abstract void found(Route r);
public abstract void prepared(Route r);
public PathFinder start() {
Thread thread = new Thread(this);
thread.setName("Pathfinder("+train+")");
thread.start();
return this;
}
}

Loading…
Cancel
Save