Files
autopilot/drone_controller/az_drone_controller_plane.cpp
T
Tuomas Järvinen 37e8cfd3fe Use raw missions in AutoPilot
- uses MAVSDK::MissionRaw objects for missions
- added new state AZ_DRONE_STATE_MISSION_UPLOADED
- new state is used in AzDroneControllerPlane before waiting for AUTO switch

TODO!!
- move to AzMissionController
- use JSON file instead of hard coded mission items
2024-11-30 17:09:00 +01:00

66 lines
2.8 KiB
C++

#include <QCoreApplication>
#include <QDebug>
#include "az_drone_controller_plane.h"
AzDroneControllerPlane::AzDroneControllerPlane(AzMission &mission, QObject *parent) :
AzDroneController(mission, parent)
{}
bool AzDroneControllerPlane::stateWaitAutoSwitch(void)
{
Telemetry::FlightMode flight_mode = mTelemetry->flight_mode();
cout << "[CONTROLLER] Current flight mode: " << flight_mode << endl;
return flight_mode == Telemetry::FlightMode::Mission;
}
void AzDroneControllerPlane::droneStateMachineSlot(void)
{
using MethodPtr = bool (AzDroneControllerPlane::*)();
struct MethodInfo
{
AzDroneState currentState;
MethodPtr nextStateMethodPtr;
string description;
int nextStateDelay;
};
// Plane type will pass arming and taking off states and wait for auto mode from the RC controller instead.
static const MethodInfo stateMethods[]
= {{AZ_DRONE_STATE_DISCONNECTED, &AzDroneControllerPlane::stateConnect, "Connecting to autopilot", 1000},
{AZ_DRONE_STATE_CONNECTED, &AzDroneControllerPlane::stateGetSystem, "Getting MAVSDK system", 1000},
{AZ_DRONE_STATE_GOT_SYSTEM, &AzDroneControllerPlane::stateGetTelemetryModule, "Getting telemetry module", 1000},
{AZ_DRONE_STATE_GOT_TELEMETRY_MODULE, &AzDroneControllerPlane::stateGetActionModule, "Getting action module", 1000},
{AZ_DRONE_STATE_GOT_ACTION_MODULE, &AzDroneControllerPlane::stateHealthOk, "Drone health OK", 1000},
{AZ_DRONE_STATE_HEALTH_OK, &AzDroneControllerPlane::stateUploadMission, "Uploading the mission", 1000},
{AZ_DRONE_STATE_MISSION_UPLOADED, &AzDroneControllerPlane::stateWaitAutoSwitch, "Waiting user to switch to ArduPilot AUTO mode", 1000},
{AZ_DRONE_STATE_AUTO_MODE_ACTIVATED, &AzDroneControllerPlane::stateFlyMission, "Starting the mission", 1000},
{AZ_DRONE_STATE_FLY_MISSION, nullptr, "Mission started", 0}};
// Iterate through states. If the state function fails, it's repeated until it succeeds.
for (uint i = 0; i < sizeof(stateMethods) / sizeof(MethodInfo); i++) {
if (stateMethods[i].currentState == mDroneState) {
if (mDroneState == AZ_DRONE_STATE_FLY_MISSION) {
cout << "[CONTROLLER] Waiting signals from the mission controller." << endl;
return;
}
bool result = (this->*stateMethods[i].nextStateMethodPtr)();
if (result) {
cout << "[CONTROLLER] " << stateMethods[i].description << " succeeded." << endl;
mDroneState = stateMethods[i + 1].currentState;
}
else {
cout << "[CONTROLLER] " << stateMethods[i].description << " failed. Trying again." << endl;
}
delayedStateCallSlot(stateMethods[i].nextStateDelay);
break;
}
}
}