diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..3f3893a --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fbb5fa9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/Climate$fanLevels.class +/Climate.class +/Destination.class +/Entertainment.class +/HelperFunctions.class +/RoadSensor.class +/Route.class +/SelfDrivingRoadtrip.class +/Vehicle.class diff --git a/.project b/.project new file mode 100644 index 0000000..e513f1d --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Code Implementation + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Destination.java b/Destination.java index 9c957e9..ee31b24 100644 --- a/Destination.java +++ b/Destination.java @@ -1,6 +1,10 @@ public class Destination { private String destinationAddress; - private double[] coordinates; //stores lat/long as terms in the array + private double[] coordinates = new double[2]; //stores lat/long as terms in the array + + public Destination() { + + } public Destination(String inputAddress) { //constructor takes address, validates, then retrieves GPS coordinates and stores them setDestinationAddress(inputAddress); @@ -24,7 +28,6 @@ public class Destination { public void setDestinationAddress(String newAddress) { if (validateAddress()) { destinationAddress = newAddress; - setCoordsForAddress(); } else { //prompt error message } diff --git a/DrivingRoute.txt b/DrivingRoute.txt index d9146b5..8eb57e3 100644 --- a/DrivingRoute.txt +++ b/DrivingRoute.txt @@ -5,7 +5,7 @@ LEFT;3.0;45;0.00 RIGHT;2.3;50;0.00 LEFT;0.3;50;0.00 STRAIGHT;9.6;70;0.00 -RIGHT;0.3;50;0.00 +STRAIGHT;0.3;50;0.00 RIGHT;0.2;35;0.00 LEFT;0.2;35;0.00 LEFT;0.4;35;0.00 diff --git a/HelperFunctions.java b/HelperFunctions.java index 365e585..40fef60 100644 --- a/HelperFunctions.java +++ b/HelperFunctions.java @@ -23,7 +23,7 @@ public class HelperFunctions { return returnArray; } - public Object[] routingAPI(double[] startCoords, double[] destinationCoords, Boolean tollsFlag, Boolean hwyFlag) { + public Vector routingAPI(double[] startCoords, String destinationAddress, Boolean tollsFlag, Boolean hwyFlag) { //method simulates a call to translate two pairs of coords into a driving route //for demo purposes returns a pregenerated set of move instructions //located in DrivingRoute.txt as semicolon delimited strings for direction, distance, and speedLimit @@ -41,7 +41,7 @@ public class HelperFunctions { } } - return routeArray.toArray(); + return routeArray; } public int[] internalTempSensors() { diff --git a/RoadSensor.java b/RoadSensor.java index e1367c8..63634d6 100644 --- a/RoadSensor.java +++ b/RoadSensor.java @@ -26,7 +26,16 @@ public class RoadSensor { //pedestrian on roadway //stationary object on roadway //for testing, this function will return 0 to signal no hazard detected - return 0; + //for testing purposes, this function will roll a 1/100 chance of detecting a hazard + double detection = Math.floor(Math.random()*100); + if (detection == 1) { return 1; } + else { return 0; } } + public int detectTrafficLight() { //rolls a 1 in 5 that a traffic light will be detected when approaching an intersection + double detection = Math.floor(Math.random()*5); + if (detection == 1) { return 1; } + else { return 0; } + } + } diff --git a/Route.java b/Route.java index 8b55e99..ed5f7f4 100644 --- a/Route.java +++ b/Route.java @@ -8,7 +8,21 @@ public class Route { private boolean hwyFlag; //stores user preference for whether to avoid major highways public Vector timeEstimate; //time estimate in seconds public Vector tollsAmount; //stores toll amount if toll roads are used - public Destination currentDestination; //uses user-created class Destination + private Destination currentDestination = new Destination(); + + public Route() { + this.routeSequences = new Vector>(); + this.timeEstimate = new Vector(); + this.tollsAmount = new Vector(); + } + + public Route(String destination) { + setRouteDestination(destination); + this.timeEstimate = new Vector(); + this.tollsAmount = new Vector(); + tollsFlag = false; + hwyFlag = false; + } private void updateCurrentLocation() { //execute api call out to GPS service to obtain current location @@ -21,18 +35,19 @@ public class Route { //execute api call out to routing service to get route info //ideally a query would go out using multiple pathfiding algorithms to get route options //here the same call is made for demo purposes + this.routeSequences = new Vector>(); updateCurrentLocation(); HelperFunctions apiCallToRouting = new HelperFunctions(); //route option 1 - routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag)); + routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationAddress(), tollsFlag, hwyFlag)); timeEstimate.add(estimateTime(routeSequences.get(0))); tollsAmount.add(estimateTolls(routeSequences.get(0))); //route option 2 - routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag)); + routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationAddress(), tollsFlag, hwyFlag)); timeEstimate.add(estimateTime(routeSequences.get(1))); tollsAmount.add(estimateTolls(routeSequences.get(1))); //route option 3 - routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag)); + routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationAddress(), tollsFlag, hwyFlag)); timeEstimate.add(estimateTime(routeSequences.get(2))); tollsAmount.add(estimateTolls(routeSequences.get(2))); } @@ -68,6 +83,11 @@ public class Route { return runningTime; } + public void setRouteDestination(String destination) { + currentDestination.setDestinationAddress(destination); + calculateRoutes(); + } + public void setHwyFlag(boolean flag) {hwyFlag = flag;} public void setTollsFlag(boolean flag) {tollsFlag = flag;} public boolean getHwyFlag() {return hwyFlag;} diff --git a/SelfDrivingRoadtrip.java b/SelfDrivingRoadtrip.java index 587c24f..5053863 100644 --- a/SelfDrivingRoadtrip.java +++ b/SelfDrivingRoadtrip.java @@ -1,7 +1,15 @@ import java.util.Scanner; +import java.util.concurrent.*; public class SelfDrivingRoadtrip { public static void main(String[] args) { + Vehicle myVehicle = new Vehicle(); + Scanner userInput = new Scanner(System.in); + myVehicle.currentRoute.setRouteDestination("250 Community College Pkwy SE, Palm Bay, FL 32909"); + System.out.println("Number of available routes: " + myVehicle.currentRoute.enumRoutes()); + int tmp = userInput.nextInt(); + myVehicle.currentRoute.selectRoute(tmp); + myVehicle.navigateRoute(); } } diff --git a/Vehicle.java b/Vehicle.java index b94f9e5..f99d27c 100644 --- a/Vehicle.java +++ b/Vehicle.java @@ -1,11 +1,11 @@ import java.util.concurrent.*; public class Vehicle implements Runnable{ - private int currentSpeed; - private int targetSpeed; + private Integer currentSpeed; + private Integer targetSpeed; private double fuelLevel; - private int distanceRemaining; - Route currentRoute; + private double distanceRemaining; + public Route currentRoute = new Route(); public void run() { navigateRoute(); @@ -14,6 +14,7 @@ public class Vehicle implements Runnable{ public Vehicle() { fuelLevel = getFuelLevel(); currentSpeed = 0; + currentRoute.setRouteDestination("250 Community College Pkwy SE, Palm Bay, FL 32909"); } public void setTargetSpeed(int newSpeed) {targetSpeed = newSpeed;} @@ -26,9 +27,24 @@ public class Vehicle implements Runnable{ public void navigateRoute() { System.out.println("Start navigation"); + boolean turnCompleted = false; for (String[] el : currentRoute.getSelectedRouteSteps()) { - boolean turnCompleted = false; //flag to validate completion of moveset instruction + RoadSensor trafficSensor = new RoadSensor(); + if (targetSpeed > 20 && trafficSensor.detectTrafficLight() == 1 && el[0] != "STRAIGHT") { + //simulates stopping at traffic lights on major roadways + //for demo purposes, the spherical cow in a vacuum is that this only happens at turns + long sleepTime = Double.valueOf(Math.floor(Math.random()*15)).longValue(); + while (sleepTime > 0) { + System.out.println("Traffic light detected, waiting..."); + try {TimeUnit.SECONDS.sleep(1);} //sleep for 1-15 second to simulate traffic light + catch (InterruptedException e) {System.err.println("Sleep interrupted");} + } + System.out.println("Resuming navigation"); + } + + turnCompleted = false; //flag to validate completion of moveset instruction + distanceRemaining = Double.parseDouble(el[1]); targetSpeed = Integer.parseInt(el[2]); while (!turnCompleted) { if (currentSpeed < 15 || el[0] == "STRAIGHT") { @@ -39,14 +55,16 @@ public class Vehicle implements Runnable{ } } - while (distanceRemaining > 0.2) { - System.out.println(distanceRemaining + " to next instruction"); - distanceRemaining -= (currentSpeed/3600); //travel distance in one second + while (distanceRemaining > 0) { + System.out.printf("%.2f miles to next instruction\n", distanceRemaining); + System.out.println("Current Speed: " + currentSpeed + " MPH"); controlThrottle(); - try {TimeUnit.MINUTES.sleep(1);} //sleep action for 1 second to simulate continuous movement + distanceRemaining = distanceRemaining - (Double.valueOf(currentSpeed)/3600); //travel distance in one second + try {TimeUnit.SECONDS.sleep(1);} //sleep action for 1 second to simulate continuous movement catch (InterruptedException e) {System.err.println("Sleep interrupted");} } } + System.out.println("Navigation complete, destination reached."); } public void changeDirection(String direction) { @@ -65,15 +83,18 @@ public class Vehicle implements Runnable{ 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); + currentSpeed = currentSpeed + 6; + } else if (currentSpeed < targetSpeed && (targetSpeed - currentSpeed) <= 6) { //partial acceleration to reach speed limit + currentSpeed = currentSpeed + (targetSpeed - currentSpeed); } else if (currentSpeed > targetSpeed) { //deceleration by coasting - currentSpeed -= 1; + currentSpeed--; + } else if (currentSpeed == targetSpeed) { + //do nothing } } public void applyBrakes() { + System.out.println("Applying brakes"); if (currentSpeed >= 10) {currentSpeed -= 10;} else if (currentSpeed < 10) {currentSpeed = 0;} }