From e8cc93ec231284226460c8b014c45a3b47a6a027 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Mon, 15 Jan 2018 17:21:31 +0100 Subject: [PATCH] FlightTasks: create enumeration type for the task index while still offering integer index with checks --- src/lib/FlightTasks/FlightTasks.cpp | 75 ++++++++++++++++------------- src/lib/FlightTasks/FlightTasks.hpp | 27 ++++++++--- 2 files changed, 61 insertions(+), 41 deletions(-) diff --git a/src/lib/FlightTasks/FlightTasks.cpp b/src/lib/FlightTasks/FlightTasks.cpp index 891ee93447..049634e22f 100644 --- a/src/lib/FlightTasks/FlightTasks.cpp +++ b/src/lib/FlightTasks/FlightTasks.cpp @@ -25,10 +25,10 @@ const vehicle_local_position_setpoint_s &FlightTasks::getPositionSetpoint() } } -int FlightTasks::switchTask(int task_number) +int FlightTasks::switchTask(FlightTaskIndex new_task_index) { /* switch to the running task, nothing to do */ - if (task_number == _current_task_index) { + if (new_task_index == _current_task_index) { return 0; } @@ -36,45 +36,41 @@ int FlightTasks::switchTask(int task_number) if (_current_task) { _current_task->~FlightTask(); _current_task = nullptr; - _current_task_index = -1; + _current_task_index = FlightTaskIndex::None; } - switch (task_number) { - case 0: - /* This part will change with the next PR that uses enum */ - return -1; + switch (new_task_index) { + case FlightTaskIndex::None: + /* disable tasks is a success */ + return 0; - case 1: - _current_task = new (&_task_union.orbit) FlightTaskOrbit(this, "ORB"); - break; - - case 2: - _current_task = new (&_task_union.sport) FlightTaskSport(this, "SPO"); - break; - - case 3: - _current_task = new (&_task_union.altitude) FlightTaskManualAltitude(this, "MANALT"); - break; - - case 4: - _current_task = new (&_task_union.position) FlightTaskManualPosition(this, "MANPOS"); - break; - - case 5: + case FlightTaskIndex::Stabilized: _current_task = new (&_task_union.stabilized) FlightTaskManualStabilized(this, "MANSTAB"); break; - case 6: + case FlightTaskIndex::Altitude: + _current_task = new (&_task_union.altitude) FlightTaskManualAltitude(this, "MANALT"); + break; + + case FlightTaskIndex::AltitudeSmooth: _current_task = new (&_task_union.altitude_smooth) FlightTaskManualAltitudeSmooth(this, "MANALTSM"); break; - case 7: + case FlightTaskIndex::Position: + _current_task = new (&_task_union.position) FlightTaskManualPosition(this, "MANPOS"); + break; + + case FlightTaskIndex::PositionSmooth: _current_task = new (&_task_union.position_smooth) FlightTaskManualPositionSmooth(this, "MANPOSSM"); break; - case -1: - /* disable tasks is a success */ - return 0; + case FlightTaskIndex::Orbit: + _current_task = new (&_task_union.orbit) FlightTaskOrbit(this, "ORB"); + break; + + case FlightTaskIndex::Sport: + _current_task = new (&_task_union.sport) FlightTaskSport(this, "SPO"); + break; default: /* invalid task */ @@ -85,7 +81,7 @@ int FlightTasks::switchTask(int task_number) if (!_current_task->initializeSubscriptions(_subscription_array)) { _current_task->~FlightTask(); _current_task = nullptr; - _current_task_index = -1; + _current_task_index = FlightTaskIndex::None; return -2; } @@ -95,12 +91,24 @@ int FlightTasks::switchTask(int task_number) if (!_current_task->updateInitialize() || !_current_task->activate()) { _current_task->~FlightTask(); _current_task = nullptr; - _current_task_index = -1; + _current_task_index = FlightTaskIndex::None; return -3; } - _current_task_index = task_number; - return 1; + _current_task_index = new_task_index; + return 0; +} + +int FlightTasks::switchTask(int new_task_index) +{ + /* make sure we are in range of the enumeration before casting */ + if (static_cast(FlightTaskIndex::None) <= new_task_index && + static_cast(FlightTaskIndex::Count) > new_task_index) { + return switchTask(FlightTaskIndex(new_task_index)); + } + + switchTask(FlightTaskIndex::None); + return -1; } void FlightTasks::_updateCommand() @@ -127,6 +135,7 @@ void FlightTasks::_updateCommand() } /* evaluate command */ + // TODO: remapping of the Mavlink definition to the internal tasks necessary int switch_result = switchTask(int(command.param1 + 0.5f)); uint8_t cmd_result = vehicle_command_ack_s::VEHICLE_RESULT_FAILED; diff --git a/src/lib/FlightTasks/FlightTasks.hpp b/src/lib/FlightTasks/FlightTasks.hpp index 4425a8c947..c9e5ff5cf6 100644 --- a/src/lib/FlightTasks/FlightTasks.hpp +++ b/src/lib/FlightTasks/FlightTasks.hpp @@ -54,6 +54,19 @@ #include +enum class FlightTaskIndex : int { + None = -1, + Stabilized, + Altitude, + AltitudeSmooth, + Position, + PositionSmooth, + Orbit, + Sport, + + Count // number of tasks +}; + class FlightTasks : control::SuperBlock { public: @@ -89,23 +102,21 @@ public: * Switch to the next task in the available list (for testing) * @return true on success, false on error */ - int switchTask() - { - return switchTask(_current_task_index + 1); - } + int switchTask() { return switchTask(static_cast(_current_task_index) + 1); } /** * Switch to a specific task (for normal usage) - * @param task number to switch to + * @param task index to switch to * @return 1 on success, 0 on no change, <0 on error */ - int switchTask(int task_number); + int switchTask(FlightTaskIndex new_task_index); + int switchTask(int new_task_index); /** * Get the number of the active task * @return number of active task, -1 if there is none */ - int getActiveTask() const { return _current_task_index; } + int getActiveTask() const { return static_cast(_current_task_index); } /** * Check if any task is active @@ -133,7 +144,7 @@ private: } _task_union; /**< storage for the currently active task */ FlightTask *_current_task = nullptr; - int _current_task_index = -1; + FlightTaskIndex _current_task_index = FlightTaskIndex::None; SubscriptionArray _subscription_array;