// by default the target course is the current course
double course = courseOverGround.getValue();
if (navigationMode == DO_NAVIGATE && waypoints.size() > 0) {
// get the current target waypoint
Waypoint waypoint = waypoints.get(currentWaypointIndex);
// get the distnace to the target waypoint...
double distance = Navigation.getDistanceInMeters(latitude.getValue(), longitude.getValue(), waypoint.getLatitude(), waypoint.getLongitude());
// ...and check if the vehicle is within the target waypoint radius
if (distance <= targetRadius) {
// switch to the next waypoint
++currentWaypointIndex;
}
if (currentWaypointIndex < waypoints.size()) {
// calculate the new target course
waypoint = waypoints.get(currentWaypointIndex);
course = Navigation.getCourseInDegrees(latitude.getValue(), longitude.getValue(), waypoint.getLatitude(), waypoint.getLongitude());
}
else {
// all waypoints are reached -> set mission completed
navigationMode = missionCompletedAction;
}
currentWaypoint.setValue(currentWaypointIndex + 1);
}
else if (navigationMode == CIRCLE_AT_HOME) {
// calculate the new course (inspired by http://tom.pycke.be/mav/101/circle-navigation)
double tSeconds = 1; // timer for the ahead calculation (in seconds)
double vAngular = speedOverGround.getValue() / circlingRadius;
double alpha = Navigation.getCourseInRadians(latitude.getValue(), longitude.getValue(), homeLatitude, homeLongitude) + Math.PI;
Waypoint dest = Navigation.getDestinationPoint(new Waypoint(homeLatitude, homeLongitude), Math.toDegrees(alpha + (vAngular * tSeconds) * circlingDirection), circlingRadius);
course = Navigation.getCourseInDegrees(latitude.getValue(), longitude.getValue(), dest.getLatitude(), dest.getLongitude());
}
else if (navigationMode == RESTART_MISSION) {
currentWaypointIndex = 0;
navigationMode = DO_NAVIGATE;
}