From 9fff530e77c97aa5708720de4ffc7f344a39fcf4 Mon Sep 17 00:00:00 2001 From: Fennel Kora Date: Sun, 14 Apr 2024 02:55:54 -0400 Subject: [PATCH] initial routines added for navigateRoute --- Vehicle.java | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/Vehicle.java b/Vehicle.java index 9f32435..b94f9e5 100644 --- a/Vehicle.java +++ b/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;} + } }