...
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:
Code Block | ||
---|---|---|
| ||
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; intuint8_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; char_ModifyFlightPathCommand waypointModifyFlightPathCommand; // 0 = nothing, 1 = initialize flight path, 2 = append, 3 = insert, 4 = update, 5 = delete, 6 = nuke bool initializingHomeBase; // 0 = no, 1 = yes char_GetNextDirectionsCommand waypointNextDirectionsCommand; // 0 = nothing, 1 = start/end holding, 2 = head home int holdingAltitude; int holdingTurnRadius; charuint8_t holdingTurnDirection; // 0 = CW, 1 = CCW // When modifying the flight path. int nextId; int prevId; int modifyId; Telemetry_Waypoint_Data_t * waypoints[100]; // Somehow need to get PATH_BUFFER_SIZE here... Telemetry_Waypoint_Data_t homebase; } |
...
numWaypoints
stores the number of waypoints initialized in thewaypoints[100]
arraywaypointNextDirectionsCommand
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 waypointprevId
is used when inserting a waypoint and represents the id of the waypoint that will come before the inserted waypointmodifyId
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 pathLength
numWaypoints
should be 1 when updating, appending, or insertingLength
numWaypoints
should be greater than 1 when initializingLength
numWaypoints
should be 0 when deleting, nuking, or doing nothing
homebase
stores the home base when it is being initialized.
...
Here is cruisingState.hpp
:
Code Block | ||
---|---|---|
| ||
#include "pathDatatypes.hpp" #include "waypointManager.hpp" #ifndef CRUISING_STATE #define CRUISING_STATE #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 { charuint8_t editingFlightPathErrorCode; // 0 = success, 1 = error, 2 = incorrect telemetry command charuint8_t pathFollowingErrorCode; // 0 = success, 1 = error, 2 = home base not initialized, 3 = incorrect telemetry command int currentWaypointId; int currentWaypointIndex; bool homeBaseInitialized; }; // The following functions are used to interact with the Waypoint Manager module int** * 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 intobject 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); void setReturnValues(/** * Function sets the _CruisingState_Telemetry_Return struct which will be sent to base via telemetry * * @param _returnToGround, WaypointManager& cruisingStateManager, int editErrorCode, int pathErrorCode); // The following functions are used to update the ID array that is a part of the CruisingState class void appendNewElement(int * idArray, int newId); int indexOfDesiredId(int * idArray, int id); void insertNewElement(int * idArray, int prevId, int newId); void updateElement(int * idArray, int oldid, int newId); void removeElement(int * idArray, int id); void clearArray(int * idArray --> 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.
...
The function setReturnValues()
populates an instance of the _CruisingState_Telemetry_Return
with relevant data.
The functions on lines 23 through 28 below are used to update an id array that is stored in the cruisingState class. The functions are called when needed by editFlightPath.
Code Block | ||
---|---|---|
| ||
// 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:
...