initial routines added for navigateRoute
This commit is contained in:
parent
952599e916
commit
9fff530e77
1 changed files with 44 additions and 6 deletions
50
Vehicle.java
50
Vehicle.java
|
@ -1,10 +1,21 @@
|
|||
public class Vehicle {
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class Vehicle implements Runnable{
|
||||
private int currentSpeed;
|
||||
private int targetSpeed;
|
||||
private double fuelLevel;
|
||||
private int routeProgress;
|
||||
private int distanceRemaining;
|
||||
Route currentRoute;
|
||||
|
||||
public void run() {
|
||||
navigateRoute();
|
||||
}
|
||||
|
||||
public Vehicle() {
|
||||
fuelLevel = getFuelLevel();
|
||||
currentSpeed = 0;
|
||||
}
|
||||
|
||||
public void setTargetSpeed(int newSpeed) {targetSpeed = newSpeed;}
|
||||
public int getCurrentSpeed() {return currentSpeed;}
|
||||
public double getFuelLevel() {return fuelLevel;}
|
||||
|
@ -14,9 +25,27 @@ public class Vehicle {
|
|||
}
|
||||
|
||||
public void navigateRoute() {
|
||||
System.out.println("Start navigation");
|
||||
|
||||
for (String[] el : currentRoute.getSelectedRouteSteps()) {
|
||||
System.out.println("Start navigation");
|
||||
boolean turnCompleted = false; //flag to validate completion of moveset instruction
|
||||
targetSpeed = Integer.parseInt(el[2]);
|
||||
while (!turnCompleted) {
|
||||
if (currentSpeed < 15 || el[0] == "STRAIGHT") {
|
||||
changeDirection(el[0]);
|
||||
turnCompleted = true;
|
||||
} else {
|
||||
applyBrakes();
|
||||
}
|
||||
}
|
||||
|
||||
while (distanceRemaining > 0.2) {
|
||||
System.out.println(distanceRemaining + " to next instruction");
|
||||
distanceRemaining -= (currentSpeed/3600); //travel distance in one second
|
||||
controlThrottle();
|
||||
try {TimeUnit.MINUTES.sleep(1);} //sleep action for 1 second to simulate continuous movement
|
||||
catch (InterruptedException e) {System.err.println("Sleep interrupted");}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,9 +63,18 @@ public class Vehicle {
|
|||
}
|
||||
}
|
||||
|
||||
public void applyBrakes() {
|
||||
if (currentSpeed >= 5) {currentSpeed = currentSpeed - 5;}
|
||||
else if (currentSpeed < 5) {currentSpeed = 0;}
|
||||
public void controlThrottle() {
|
||||
if (currentSpeed < targetSpeed && (targetSpeed - currentSpeed) > 6) { //normal acceleration
|
||||
currentSpeed += 6;
|
||||
} else if (currentSpeed < targetSpeed && (targetSpeed - currentSpeed) < 6) { //partial acceleration to reach speed limit
|
||||
currentSpeed += (currentSpeed - targetSpeed);
|
||||
} else if (currentSpeed > targetSpeed) { //deceleration by coasting
|
||||
currentSpeed -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
public void applyBrakes() {
|
||||
if (currentSpeed >= 10) {currentSpeed -= 10;}
|
||||
else if (currentSpeed < 10) {currentSpeed = 0;}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue