Cruising State Module

This document concerns itself with cruisingState.hpp and cruisingState.cpp within the ./PathManager directory of ZeroPilot.

Responsibilities of Cruising State

Cruising State is responsible for two things: getting the plane’s desired direction and altitude during cruising flight; and updating the flight path if required. The module itself actually provides an API that the state machine (specifically cruisingState::execute()) can use to interact with the Waypoint Manager.

Sending Commands via Telemetry

In order to use the Cruising State module, data must be sent via telemetry. The following code snippet shows what needs to be sent in:

enum _ModifyFlightPathCommand { NO_FLIGHT_PATH_EDIT = 0, INITIALIZE_FLIGHT_PATH, APPEND, INSERT, UPDATE, DELETE, NUKE }; // Used by cruisingState enum _GetNextDirectionsCommand { REGULAR_PATH_FOLLOWING = 0, TOGGLE_HOLDING, TOGGLE_HEAD_HOME }; // Used by cruisingState struct Telemetry_Waypoint_Data_t { long double latitude; long double longitude; int altitude; float turnRadius; uint8_t waypointType; // 0 = Path follow, 1 = Orbit, 2 = Hold }; struct Telemetry_PIGO_t { ... other data sent by telemetry /* Parameters for the waypoint manager (crusingState) */ int numWaypoints; _ModifyFlightPathCommand waypointModifyFlightPathCommand; // 0 = nothing, 1 = initialize flight path, 2 = append, 3 = insert, 4 = update, 5 = delete, 6 = nuke bool initializingHomeBase; // 0 = no, 1 = yes _GetNextDirectionsCommand waypointNextDirectionsCommand; // 0 = nothing, 1 = start/end holding, 2 = head home int holdingAltitude; int holdingTurnRadius; uint8_t holdingTurnDirection; // 0 = CW, 1 = CCW // When modifying the flight path. int nextId; int prevId; int modifyId; Telemetry_Waypoint_Data_t waypoints[100]; Telemetry_Waypoint_Data_t homebase; }

The first struct is a template for a waypoint. It contains the essential information and will be used to initialize waypoints in the Cruising State module.

The second struct is the general struct that contains all data sent in by telemetry (variables that don’t concern Cruising State are excluded for obvious reasons).

  • numWaypoints stores the number of waypoints initialized in the waypoints[100] array

  • waypointNextDirectionsCommand is used to indicate if the plane should enter/exit holding patterns, start/stop heading home, or just get the next direction and altitude as normal.

  • Holding specific data

    • holdingAltitude is the altitude that the plane will fly at when holding (in m)

    • holdingTurnRadius is the radius of the plane’s holding pattern (in m)

    • holdingTurnDirection is the direction that the plane will turn (when looking down from above)

  • Modifying flight path

    • waypointModifyFlightPathCommand is used when editing the plane’s flight path to call the correct API for the Waypoint Manager.

    • initilaizingHomeBase is used when initializing the flight path. It indicates that in addition to initializing the flight path, we are also initializing the home base.

    • nextId is used when inserting a waypoint and represents the id of the waypoint that will come after the inserted waypoint

    • prevId is used when inserting a waypoint and represents the id of the waypoint that will come before the inserted waypoint

    • modifyId is used when updating or deleting a waypoint and represents the id of the affected waypoint.

  • Other

    • waypoints[100] stores the waypoints that will be used to modify or initialize the plane’s flight path

      • numWaypoints should be 1 when updating, appending, or inserting

      • numWaypoints should be greater than 1 when initializing

      • numWaypoints should be 0 when deleting, nuking, or doing nothing

    • homebase stores the home base when it is being initialized.

Functions Declared

Here is cruisingState.hpp:

#ifndef CRUISING_STATE #define CRUISING_STATE enum _ModifyFlightPathErrorCode { MODIFY_CRUISING_SUCCESS = 0, MODIFY_CRUISING_ERROR, MODIFY_CRUISING_INCORRECT_TELEMETRY_COMMAND }; enum _GetNextDirectionsErrorCode { PATH_CRUISING_SUCCESS = 0, PATH_CRUISING_ERROR, PATH_CRUISING_INCORRECT_TELEMETRY_COMMAND, PATH_CRUISING_UNINITIALIZED_HOMEBASE }; // Struct contains the data that will be returned via telemetry to ground station struct _CruisingState_Telemetry_Return { uint8_t editingFlightPathErrorCode; // 0 = success, 1 = error, 2 = incorrect telemetry command uint8_t pathFollowingErrorCode; // 0 = success, 1 = error, 2 = home base not initialized, 3 = incorrect telemetry command int currentWaypointId; int currentWaypointIndex; bool homeBaseInitialized; }; /** * Function performs any requested modifications on the flight path including appending, inserting, deleting, updating, initializing, and nuking * * @param telemetryData --> contains the most recent commands from telemetry * @param cruisingStateManager --> this is the waypointManager class object for the cruisingState * @param idArray --> an array of integers that is used to keep track of the ids of the waypoints in the flight path * * @return error code indicating success of operation */ _ModifyFlightPathErrorCode editFlightPath(Telemetry_PIGO_t * telemetryData, WaypointManager& cruisingStateManager, int * idArray); /** * Function retrieves the next desired path for the aircraft * * @param telemetryData --> contains the most recent commands from telemetry * @param cruisingStateManager --> this is the waypointManager class object for the cruisingState * @param input --> struct contains inputs required to calculate the desired path for the aircraft * @param output --> struct collects the output data from the Waypoint Manager for use in other states * @param goingHome --> boolean flag that is used to indicate if we want to start/stop holding * @param inHold --> boolean flag that is used to indicate if we want to start/stop heading back to our home base * * @return error code indicating success of operation */ _GetNextDirectionsErrorCode pathFollow(Telemetry_PIGO_t * telemetryData, WaypointManager& cruisingStateManager, _WaypointManager_Data_In input, _WaypointManager_Data_Out * output, bool& goingHome, bool& inHold); /** * Function sets the _CruisingState_Telemetry_Return struct which will be sent to base via telemetry * * @param _returnToGround --> contains the info we are sending back to telemetry * @param cruisingStateManager --> this is the waypointManager class object for the cruisingState * @param editErrorCode --> error code that was returned by editFlightPath() on previous call * @param pathErrorCode --> error code that was returned by pathFollow() on previous call */ void setReturnValues(_CruisingState_Telemetry_Return * _returnToGround, WaypointManager& cruisingStateManager, _ModifyFlightPathErrorCode editErrorCode, _GetNextDirectionsErrorCode pathErrorCode); #endif

The struct, _CruisingState_Telemetry_Return will be used by the commsWithTelemetry state when preparing to send data to the ground. It contains data regarding the current state of the waypoint manager and whether any commands executed properly. Error codes, which are included below, are also sent through this struct.

  • Error Codes:

    • editingFlightPathErrorCode:

      • 0 = success

      • 1 = error

      • 2 = incorrect telemetry command

    • pathFollowingErrorCode:

      • 0 = success

      • 1 = error

      • 2 = incorrect telemetry command

      • 3 = going home failed because the home base is not initialized

The function editFlightPath() is called every time cruisingState::execute() is run. Using commands sent from telemetry, it will update the plane’s flight path by calling the appropriate Waypoint Manager API.

The function pathFollow() is called every time cruisingState::execute() is run. Using commands sent from telemetry, it will either get the plane’s next directions and altitude, make the plane enter/exit a holding pattern, or make the plane start/stop heading home.

The function setReturnValues() populates an instance of the _CruisingState_Telemetry_Return with relevant data.

The functions below are used to update an id array that is stored in the cruisingState class. The functions are called when needed by editFlightPath.

// The following functions are used to update the ID array that is a part of the CruisingState class static void appendNewElement(int * idArray, int newId); // Adds newId to the first free element in idArray static int indexOfDesiredId(int * idArray, int id); // Returns the index of id in idArray static void insertNewElement(int * idArray, int prevId, int newId); // Inserts newId after prevId in idArray static void updateElement(int * idArray, int oldId, int newId); // Replaces oldId with newId in idArray static void removeElement(int * idArray, int id); // Removes id from idArray static void clearArray(int * idArray); // Resets all elements in idArray to 0

Testing

Unit tests were written in Test_CruisingState.cpp to test the functions declared in cruisingState.hpp. The unit tests tested the following:

  1. Incorrect telemetry commands returns error codes

  2. Initializing the flight path works

  3. Nuking the flight path works

  4. Appending a waypoint

    1. Case where it works

    2. Case where it fails

  5. Inserting a waypoint

    1. Case where it works

    2. Case where it fails

  6. Updating a waypoint

    1. Case where it works

    2. Case where it fails

  7. Deleting a waypoint

    1. Case where it works

    2. Case where it fails

  8. Successfully getting the next flight direction and altitude

  9. Entering a holding pattern successfully, and returning correct next flight direction and altitude

  10. Successfully start and stop heading to home base

  11. Fail to start heading to home base