Skip to content

Commit 35a9fb3

Browse files
authored
Merge pull request #2596 from mavlink/pr-use-mission-current-extended
Improve handling of mission finished
2 parents 1ff26d1 + 84f1073 commit 35a9fb3

File tree

13 files changed

+1384
-109
lines changed

13 files changed

+1384
-109
lines changed

docs/en/cpp/api_reference/classmavsdk_1_1_mission_raw.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ void | [unsubscribe_mission_progress](#classmavsdk_1_1_mission_raw_1ac46f08b5270
6969
void | [unsubscribe_mission_changed](#classmavsdk_1_1_mission_raw_1ac6cd7602b2e5b46ad0ea1cf8bf602a0c) ([MissionChangedHandle](classmavsdk_1_1_mission_raw.md#classmavsdk_1_1_mission_raw_1a46da6d8a53822fd5fbd7b2a414624c5c) handle) | Unsubscribe from subscribe_mission_changed.
7070
std::pair< [Result](classmavsdk_1_1_mission_raw.md#classmavsdk_1_1_mission_raw_1a7ea2a624818ebb5a3e209cc275d58eaf), [MissionRaw::MissionImportData](structmavsdk_1_1_mission_raw_1_1_mission_import_data.md) > | [import_qgroundcontrol_mission](#classmavsdk_1_1_mission_raw_1a2a4ca261c37737e691c6954693d6d0a5) (std::string qgc_plan_path)const | Import a QGroundControl missions in JSON .plan format, from a file.
7171
std::pair< [Result](classmavsdk_1_1_mission_raw.md#classmavsdk_1_1_mission_raw_1a7ea2a624818ebb5a3e209cc275d58eaf), [MissionRaw::MissionImportData](structmavsdk_1_1_mission_raw_1_1_mission_import_data.md) > | [import_qgroundcontrol_mission_from_string](#classmavsdk_1_1_mission_raw_1a4a1b55650120d8af0ce7fa037f6b5ce9) (std::string qgc_plan)const | Import a QGroundControl missions in JSON .plan format, from a string.
72+
std::pair< [Result](classmavsdk_1_1_mission_raw.md#classmavsdk_1_1_mission_raw_1a7ea2a624818ebb5a3e209cc275d58eaf), bool > | [is_mission_finished](#classmavsdk_1_1_mission_raw_1a998db451c2718d9728276ca2d01ce315) () const | Check if the mission is finished.
7273
const [MissionRaw](classmavsdk_1_1_mission_raw.md) & | [operator=](#classmavsdk_1_1_mission_raw_1a2b8cdc1fbee72224a9ef6eb9266b2e2a) (const [MissionRaw](classmavsdk_1_1_mission_raw.md) &)=delete | Equality operator (object is not copyable).
7374

7475

@@ -734,6 +735,23 @@ This function is blocking.
734735

735736
&emsp;std::pair< [Result](classmavsdk_1_1_mission_raw.md#classmavsdk_1_1_mission_raw_1a7ea2a624818ebb5a3e209cc275d58eaf), [MissionRaw::MissionImportData](structmavsdk_1_1_mission_raw_1_1_mission_import_data.md) > - Result of request.
736737

738+
### is_mission_finished() {#classmavsdk_1_1_mission_raw_1a998db451c2718d9728276ca2d01ce315}
739+
```cpp
740+
std::pair< Result, bool > mavsdk::MissionRaw::is_mission_finished() const
741+
```
742+
743+
744+
Check if the mission is finished.
745+
746+
Returns true if the mission is finished, false otherwise.
747+
748+
749+
This function is blocking.
750+
751+
**Returns**
752+
753+
&emsp;std::pair< [Result](classmavsdk_1_1_mission_raw.md#classmavsdk_1_1_mission_raw_1a7ea2a624818ebb5a3e209cc275d58eaf), bool > - Result of request.
754+
737755
### operator=() {#classmavsdk_1_1_mission_raw_1a2b8cdc1fbee72224a9ef6eb9266b2e2a}
738756
```cpp
739757
const MissionRaw & mavsdk::MissionRaw::operator=(const MissionRaw &)=delete

proto

src/mavsdk/plugins/mission/mission_impl.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void MissionImpl::process_mission_current(const mavlink_message_t& message)
7070

7171
std::lock_guard<std::mutex> lock(_mission_data.mutex);
7272
_mission_data.last_current_mavlink_mission_item = mission_current.seq;
73+
_mission_data.mission_state = static_cast<MissionState>(mission_current.mission_state);
7374
report_progress_locked();
7475
}
7576

@@ -993,17 +994,30 @@ std::pair<Mission::Result, bool> MissionImpl::is_mission_finished_locked() const
993994
return std::make_pair<Mission::Result, bool>(Mission::Result::Success, false);
994995
}
995996

996-
// It is not straightforward to look at "current" because it jumps to 0
997-
// once the last item has been done. Therefore we have to lo decide using
998-
// "reached" here.
999-
// It seems that we never receive a reached when RTL is initiated after
1000-
// a mission, and we need to account for that.
1001-
const unsigned rtl_correction = _enable_return_to_launch_after_mission ? 2 : 1;
1002-
1003-
return std::make_pair<Mission::Result, bool>(
1004-
Mission::Result::Success,
1005-
unsigned(_mission_data.last_reached_mavlink_mission_item + rtl_correction) ==
1006-
_mission_data.mavlink_mission_item_to_mission_item_indices.size());
997+
// If mission_state is Unknown, fall back to the previous behavior
998+
if (_mission_data.mission_state == MissionState::Unknown) {
999+
// It is not straightforward to look at "current" because it jumps to 0
1000+
// once the last item has been done. Therefore we have to lo decide using
1001+
// "reached" here.
1002+
// With PX4, it seems that we didn't use to receive a reached when RTL is initiated
1003+
// after a mission, and we needed to account for that all the way to PX4 v1.16.
1004+
const unsigned rtl_correction = _enable_return_to_launch_after_mission ? 2 : 1;
1005+
1006+
// By accepting the current to be larger than the total, we are accepting the case
1007+
// where we no longer require the rtl_correction, as this is now getting fixed in PX4.
1008+
return std::make_pair<Mission::Result, bool>(
1009+
Mission::Result::Success,
1010+
unsigned(_mission_data.last_reached_mavlink_mission_item + rtl_correction) >=
1011+
_mission_data.mavlink_mission_item_to_mission_item_indices.size());
1012+
}
1013+
1014+
// If mission_state is Completed, the mission is finished
1015+
if (_mission_data.mission_state == MissionState::Completed) {
1016+
return std::make_pair<Mission::Result, bool>(Mission::Result::Success, true);
1017+
}
1018+
1019+
// If mission_state is NotCompleted, the mission is not finished
1020+
return std::make_pair<Mission::Result, bool>(Mission::Result::Success, false);
10071021
}
10081022

10091023
int MissionImpl::current_mission_item() const

src/mavsdk/plugins/mission/mission_impl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace mavsdk {
1616

1717
class MissionImpl : public PluginImplBase {
1818
public:
19+
enum class MissionState { Unknown = 0, NotCompleted = 1, Completed = 2 };
20+
1921
explicit MissionImpl(System& system);
2022
explicit MissionImpl(std::shared_ptr<System> system);
2123
~MissionImpl() override;
@@ -127,6 +129,7 @@ class MissionImpl : public PluginImplBase {
127129
std::weak_ptr<MavlinkMissionTransferClient::WorkItem> last_upload{};
128130
std::weak_ptr<MavlinkMissionTransferClient::WorkItem> last_download{};
129131
bool gimbal_v2_in_control{false};
132+
MissionState mission_state{MissionState::Unknown};
130133
} _mission_data{};
131134

132135
TimeoutHandler::Cookie _timeout_cookie{};

src/mavsdk/plugins/mission_raw/include/plugins/mission_raw/mission_raw.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,17 @@ class MissionRaw : public PluginBase {
507507
std::pair<Result, MissionRaw::MissionImportData>
508508
import_qgroundcontrol_mission_from_string(std::string qgc_plan) const;
509509

510+
/**
511+
* @brief Check if the mission is finished.
512+
*
513+
* Returns true if the mission is finished, false otherwise.
514+
*
515+
* This function is blocking.
516+
*
517+
* @return Result of request.
518+
*/
519+
std::pair<Result, bool> is_mission_finished() const;
520+
510521
/**
511522
* @brief Copy constructor.
512523
*/

src/mavsdk/plugins/mission_raw/mission_raw.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ MissionRaw::import_qgroundcontrol_mission_from_string(std::string qgc_plan) cons
180180
return _impl->import_qgroundcontrol_mission_from_string(qgc_plan);
181181
}
182182

183+
std::pair<MissionRaw::Result, bool> MissionRaw::is_mission_finished() const
184+
{
185+
return _impl->is_mission_finished();
186+
}
187+
183188
bool operator==(const MissionRaw::MissionProgress& lhs, const MissionRaw::MissionProgress& rhs)
184189
{
185190
return (rhs.current == lhs.current) && (rhs.total == lhs.total);

src/mavsdk/plugins/mission_raw/mission_raw_impl.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ void MissionRawImpl::process_mission_current(const mavlink_message_t& message)
105105
if (mission_current.seq != _mission_progress.last_reached) {
106106
_mission_progress.last.current = mission_current.seq;
107107
}
108+
_mission_progress.mission_state = static_cast<MissionState>(mission_current.mission_state);
108109
}
109110
{
110111
std::lock_guard<std::mutex> lock(_mission_changed.mutex);
@@ -693,4 +694,36 @@ MissionRaw::Result MissionRawImpl::convert_result(MavlinkMissionTransferClient::
693694
return MissionRaw::Result::Unknown;
694695
}
695696
}
697+
698+
std::pair<MissionRaw::Result, bool> MissionRawImpl::is_mission_finished() const
699+
{
700+
std::lock_guard<std::mutex> lock(_mission_progress.mutex);
701+
702+
if (_mission_progress.last.current < 0) {
703+
return std::make_pair<MissionRaw::Result, bool>(MissionRaw::Result::Success, false);
704+
}
705+
706+
if (_mission_progress.last_reached < 0) {
707+
return std::make_pair<MissionRaw::Result, bool>(MissionRaw::Result::Success, false);
708+
}
709+
710+
if (_mission_progress.last.total <= 0) {
711+
return std::make_pair<MissionRaw::Result, bool>(MissionRaw::Result::Success, false);
712+
}
713+
714+
// If mission_state is Unknown, fall back to the previous behavior
715+
if (_mission_progress.mission_state == MissionState::Unknown) {
716+
return std::make_pair<MissionRaw::Result, bool>(
717+
MissionRaw::Result::Success,
718+
_mission_progress.last_reached == _mission_progress.last.total - 1);
719+
}
720+
721+
// If mission_state is Completed, the mission is finished
722+
if (_mission_progress.mission_state == MissionState::Completed) {
723+
return std::make_pair<MissionRaw::Result, bool>(MissionRaw::Result::Success, true);
724+
}
725+
726+
// If mission_state is NotCompleted, the mission is not finished
727+
return std::make_pair<MissionRaw::Result, bool>(MissionRaw::Result::Success, false);
728+
}
696729
} // namespace mavsdk

src/mavsdk/plugins/mission_raw/mission_raw_impl.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace mavsdk {
1212

1313
class MissionRawImpl : public PluginImplBase {
1414
public:
15+
enum class MissionState { Unknown = 0, NotCompleted = 1, Completed = 2 };
16+
1517
explicit MissionRawImpl(System& system);
1618
explicit MissionRawImpl(std::shared_ptr<System> system);
1719
~MissionRawImpl() override;
@@ -66,6 +68,8 @@ class MissionRawImpl : public PluginImplBase {
6668
subscribe_mission_progress(const MissionRaw::MissionProgressCallback& callback);
6769
void unsubscribe_mission_progress(MissionRaw::MissionProgressHandle handle);
6870

71+
std::pair<MissionRaw::Result, bool> is_mission_finished() const;
72+
6973
std::pair<MissionRaw::Result, MissionRaw::MissionImportData>
7074
import_qgroundcontrol_mission(std::string qgc_plan_path);
7175

@@ -119,11 +123,12 @@ class MissionRawImpl : public PluginImplBase {
119123
std::weak_ptr<MavlinkMissionTransferClient::WorkItem> _last_download{};
120124

121125
struct {
122-
std::mutex mutex{};
126+
mutable std::mutex mutex{};
123127
MissionRaw::MissionProgress last{};
124128
MissionRaw::MissionProgress last_reported{};
125129
CallbackList<MissionRaw::MissionProgress> callbacks{};
126130
int32_t last_reached{};
131+
MissionState mission_state{MissionState::Unknown};
127132
} _mission_progress{};
128133

129134
struct {

src/mavsdk_server/src/generated/mission_raw/mission_raw.grpc.pb.cc

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)