This commit is contained in:
Stephan Richter
2021-03-18 23:35:22 +01:00
parent 9172b60329
commit 483ebec39e
9 changed files with 101 additions and 53 deletions

View File

@@ -5,11 +5,14 @@ import org.slf4j.LoggerFactory;
import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.moving.Train;
public class BrakeProcess extends BaseClass implements Runnable{
private static final Logger LOG = LoggerFactory.getLogger(BrakeProcess.class);
private static final int SPEED_STEP = 10;
public static int defaultTimeStep = 500;
private Train train;
@@ -19,12 +22,31 @@ public class BrakeProcess extends BaseClass implements Runnable{
private int startSpeed;
private int lastSpeed;
private long lastTime;
private Integer timeStep;
private Route route;
private String brakeId;
public BrakeProcess(Train train) {
this.train = train;
this.train = train;
this.brakeId = train.brakeId();
this.route = train.route();
new Thread(this, Application.threadName(this)).start();
}
private long calcDistance(Integer ts) {
long dist = 0;
int s = startSpeed;
while (s > Train.defaultEndSpeed) {
s -= SPEED_STEP;
dist += s*ts;
}
LOG.debug("Estimated distamce with {} ms timestep: {}",ts,dist);
return dist;
}
public BrakeProcess end() {
LOG.debug("{}.end()",this);
ended = true;
@@ -33,14 +55,15 @@ public class BrakeProcess extends BaseClass implements Runnable{
@Override
public void run() {
Integer delay = train.route().brakeTime(train.brakeId());
timeStep = train.route().brakeTime(train.brakeId());
if (timeStep == null) timeStep = defaultTimeStep;
startSpeed = train.speed;
lastTime = timestamp();
while (!train.hasNextPreparedRoute()) {
sleep(delay);
sleep(timeStep);
lastSpeed = train.speed;
updateDistance();
if (lastSpeed > targetSpeed) lastSpeed -= 10;
if (lastSpeed > targetSpeed) lastSpeed -= SPEED_STEP;
if (ended) break;
if (lastSpeed <= targetSpeed && (ended = true)) lastSpeed = targetSpeed;
train.setSpeed(lastSpeed);
@@ -62,6 +85,23 @@ public class BrakeProcess extends BaseClass implements Runnable{
public void updateTime() {
updateDistance();
LOG.debug("updateTime(): start speed was {} {}.",startSpeed,BaseClass.speedUnit);
// TODO
Integer newTimeStep = timeStep;
long calculated;
int step = 32*newTimeStep;
for (int i=0; i<20; i++) {
step = step/2;
if (step<1) step = 1;
calculated = calcDistance(newTimeStep);
LOG.debug("Calculated distance for step = {} ms: {}",newTimeStep,calculated);
LOG.debug("Update step: {}",step);
newTimeStep = newTimeStep + (calculated > distance ? -step : step);
}
if (!newTimeStep.equals(timeStep)) {
route.brakeTime(brakeId,newTimeStep);
calculated = calcDistance(newTimeStep);
LOG.debug("Corrected brake timestep for {} @ {} from {} to {} ms.",train,route,timeStep,newTimeStep);
LOG.debug("Differemce from estimated distance: {} ({}%)",distance-calculated,100*(distance-calculated)/(float)distance);
}
}
}

View File

@@ -318,7 +318,7 @@ public class ControlUnit extends Thread implements Constants{
case FEEDBACK:
int addr = Integer.parseInt(parts[5]);
boolean active = !parts[6].equals("0");
new Thread(Application.threadName("CU.FeedBack("+addr+")")) {
new Thread(Application.threadName("CU.Feedback("+addr+")")) {
@Override
public void run() {
plan.sensor(addr,active);

View File

@@ -51,13 +51,13 @@ public class RoutePrepper extends BaseClass implements Runnable{
LOG.warn("{}→ {}.availableRoutes called without context.train!", inset, Train.class.getSimpleName());
if (error) return availableRoutes;
Block destination = train.destination();
if (isSet(startDirection)) {
LOG.debug("{}- Looking for {}-bound routes from {}", inset, startDirection, block);
} else {
LOG.debug("{}- Looking for all routes from {}", inset, block);
}
Block destination = train.destination();
if (isSet(destination) && visitedRoutes.isEmpty()) LOG.debug("{}- Destination: {}", inset, destination);
for (Route routeCandidate : block.leavingRoutes()) {
@@ -68,9 +68,9 @@ public class RoutePrepper extends BaseClass implements Runnable{
}
HashSet<Tile> stuckTrace = train.stuckTrace(); // if train has been stopped in between two blocks lastly:
// only allow routes that do not conflict with current train
// only allow starting routes that do not conflict with current train
// position
if (isSet(stuckTrace) && !routeCandidate.path().containsAll(stuckTrace)) {
if (isSet(stuckTrace) && visitedRoutes.isEmpty() && !routeCandidate.path().containsAll(stuckTrace)) {
LOG.debug("Stuck train occupies tiles ({}) outside of {} not allowed.", stuckTrace, routeCandidate);
continue;
}