Skip to content

Commit afc6cec

Browse files
authored
Merge pull request #2222 from mavlink/pr-fix-ardupilot-mission
ArduPilot mission_raw fixes
2 parents 07d2433 + dc919d0 commit afc6cec

11 files changed

+66
-24
lines changed

src/mavsdk/core/autopilot.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ enum class Autopilot {
88
ArduPilot,
99
};
1010

11-
} // namespace mavsdk
11+
} // namespace mavsdk

src/mavsdk/core/autopilot_callback.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include "autopilot.h"
4+
#include <functional>
5+
6+
namespace mavsdk {
7+
8+
using AutopilotCallback = std::function<Autopilot()>;
9+
10+
} // namespace mavsdk

src/mavsdk/core/mavlink_mission_transfer_client.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ MavlinkMissionTransferClient::MavlinkMissionTransferClient(
99
Sender& sender,
1010
MavlinkMessageHandler& message_handler,
1111
TimeoutHandler& timeout_handler,
12-
TimeoutSCallback timeout_s_callback) :
12+
TimeoutSCallback timeout_s_callback,
13+
AutopilotCallback autopilot_callback) :
1314
_sender(sender),
1415
_message_handler(message_handler),
1516
_timeout_handler(timeout_handler),
16-
_timeout_s_callback(std::move(timeout_s_callback))
17+
_timeout_s_callback(std::move(timeout_s_callback)),
18+
_autopilot_callback(std::move(autopilot_callback))
1719
{
1820
if (const char* env_p = std::getenv("MAVSDK_MISSION_TRANSFER_DEBUGGING")) {
1921
if (std::string(env_p) == "1") {
@@ -49,7 +51,8 @@ MavlinkMissionTransferClient::upload_items_async(
4951
callback,
5052
progress_callback,
5153
_debugging,
52-
target_system_id);
54+
target_system_id,
55+
_autopilot_callback());
5356

5457
_work_queue.push_back(ptr);
5558

@@ -181,12 +184,14 @@ MavlinkMissionTransferClient::UploadWorkItem::UploadWorkItem(
181184
ResultCallback callback,
182185
ProgressCallback progress_callback,
183186
bool debugging,
184-
uint8_t target_system_id) :
187+
uint8_t target_system_id,
188+
Autopilot autopilot) :
185189
WorkItem(sender, message_handler, timeout_handler, type, timeout_s, debugging),
186190
_items(items),
187191
_callback(callback),
188192
_progress_callback(progress_callback),
189-
_target_system_id(target_system_id)
193+
_target_system_id(target_system_id),
194+
_autopilot(autopilot)
190195
{
191196
_message_handler.register_one(
192197
MAVLINK_MSG_ID_MISSION_REQUEST,
@@ -322,7 +327,7 @@ void MavlinkMissionTransferClient::UploadWorkItem::process_mission_request(
322327
mavlink_mission_request_t request;
323328
mavlink_msg_mission_request_decode(&request_message, &request);
324329

325-
if (_sender.autopilot() == Autopilot::ArduPilot) {
330+
if (_autopilot == Autopilot::ArduPilot) {
326331
// ArduCopter 3.6 sends MISSION_REQUEST (not _INT) but actually accepts ITEM_INT in reply
327332

328333
// FIXME: this will mess with the sequence number.

src/mavsdk/core/mavlink_mission_transfer_client.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <memory>
77
#include <mutex>
88
#include <vector>
9+
#include "autopilot.h"
10+
#include "autopilot_callback.h"
911
#include "mavlink_address.h"
1012
#include "mavlink_include.h"
1113
#include "mavlink_message_handler.h"
@@ -110,7 +112,8 @@ class MavlinkMissionTransferClient {
110112
ResultCallback callback,
111113
ProgressCallback progress_callback,
112114
bool debugging,
113-
uint8_t target_system_id);
115+
uint8_t target_system_id,
116+
Autopilot autopilot);
114117

115118
~UploadWorkItem() override;
116119
void start() override;
@@ -147,6 +150,7 @@ class MavlinkMissionTransferClient {
147150
unsigned _retries_done{0};
148151

149152
uint8_t _target_system_id;
153+
Autopilot _autopilot;
150154
};
151155

152156
class DownloadWorkItem : public WorkItem {
@@ -272,7 +276,8 @@ class MavlinkMissionTransferClient {
272276
Sender& sender,
273277
MavlinkMessageHandler& message_handler,
274278
TimeoutHandler& timeout_handler,
275-
TimeoutSCallback get_timeout_s_callback);
279+
TimeoutSCallback get_timeout_s_callback,
280+
AutopilotCallback autopilot_callback);
276281

277282
~MavlinkMissionTransferClient() = default;
278283

@@ -307,6 +312,7 @@ class MavlinkMissionTransferClient {
307312
MavlinkMessageHandler& _message_handler;
308313
TimeoutHandler& _timeout_handler;
309314
TimeoutSCallback _timeout_s_callback;
315+
AutopilotCallback _autopilot_callback;
310316

311317
LockedQueue<WorkItem> _work_queue{};
312318

src/mavsdk/core/mavlink_mission_transfer_client_test.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ class MavlinkMissionTransferClientTest : public ::testing::Test {
3434
MavlinkMissionTransferClientTest() :
3535
::testing::Test(),
3636
timeout_handler(time),
37-
mmt(mock_sender, message_handler, timeout_handler, []() { return timeout_s; })
37+
mmt(
38+
mock_sender,
39+
message_handler,
40+
timeout_handler,
41+
[]() { return timeout_s; },
42+
[]() { return Autopilot::Px4; })
3843
{}
3944

4045
void SetUp() override

src/mavsdk/core/mavlink_parameter_client.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ MavlinkParameterClient::MavlinkParameterClient(
1515
MavlinkMessageHandler& message_handler,
1616
TimeoutHandler& timeout_handler,
1717
TimeoutSCallback timeout_s_callback,
18+
AutopilotCallback autopilot_callback,
1819
uint8_t target_system_id,
1920
uint8_t target_component_id,
2021
bool use_extended) :
2122
_sender(sender),
2223
_message_handler(message_handler),
2324
_timeout_handler(timeout_handler),
2425
_timeout_s_callback(std::move(timeout_s_callback)),
26+
_autopilot_callback(std::move(autopilot_callback)),
2527
_target_system_id(target_system_id),
2628
_target_component_id(target_component_id),
2729
_use_extended(use_extended)
@@ -115,7 +117,7 @@ void MavlinkParameterClient::set_param_int_async(
115117

116118
// PX4 only uses int32_t, so we can be sure and don't need to check the exact type first
117119
// by getting the param, or checking the cache.
118-
if (_sender.autopilot() == Autopilot::Px4) {
120+
if (_autopilot_callback() == Autopilot::Px4) {
119121
ParamValue value_to_set;
120122
value_to_set.set(static_cast<int32_t>(value));
121123
set_param_async(name, value_to_set, callback, cookie);
@@ -499,7 +501,7 @@ bool MavlinkParameterClient::send_set_param_message(WorkItemSet& work_item)
499501
return message;
500502
});
501503
} else {
502-
const float value_set = (_sender.autopilot() == Autopilot::ArduPilot) ?
504+
const float value_set = (_autopilot_callback() == Autopilot::ArduPilot) ?
503505
work_item.param_value.get_4_float_bytes_cast() :
504506
work_item.param_value.get_4_float_bytes_bytewise();
505507

@@ -632,8 +634,8 @@ void MavlinkParameterClient::process_param_value(const mavlink_message_t& messag
632634
ParamValue received_value;
633635
const bool set_value_success = received_value.set_from_mavlink_param_value(
634636
param_value,
635-
(_sender.autopilot() == Autopilot::ArduPilot) ? ParamValue::Conversion::Cast :
636-
ParamValue::Conversion::Bitwise);
637+
(_autopilot_callback() == Autopilot::ArduPilot) ? ParamValue::Conversion::Cast :
638+
ParamValue::Conversion::Bitwise);
637639
if (!set_value_success) {
638640
LogWarn() << "Got ill-formed param_ext_value message (param_type unknown)";
639641
return;

src/mavsdk/core/mavlink_parameter_client.h

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "autopilot_callback.h"
34
#include "log.h"
45
#include "mavlink_include.h"
56
#include "timeout_s_callback.h"
@@ -34,6 +35,7 @@ class MavlinkParameterClient : public MavlinkParameterSubscription {
3435
MavlinkMessageHandler& message_handler,
3536
TimeoutHandler& timeout_handler,
3637
TimeoutSCallback timeout_s_callback,
38+
AutopilotCallback autopilot_callback,
3739
uint8_t target_system_id,
3840
uint8_t target_component_id = MAV_COMP_ID_AUTOPILOT1,
3941
bool use_extended_protocol = false);
@@ -195,6 +197,7 @@ class MavlinkParameterClient : public MavlinkParameterSubscription {
195197
MavlinkMessageHandler& _message_handler;
196198
TimeoutHandler& _timeout_handler;
197199
TimeoutSCallback _timeout_s_callback;
200+
AutopilotCallback _autopilot_callback;
198201
uint8_t _target_system_id = 0;
199202
uint8_t _target_component_id = MAV_COMP_ID_AUTOPILOT1;
200203
bool _use_extended = false;

src/mavsdk/core/system_impl.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ SystemImpl::SystemImpl(MavsdkImpl& mavsdk_impl) :
3030
_mavsdk_impl.default_server_component_impl().sender(),
3131
_mavlink_message_handler,
3232
_mavsdk_impl.timeout_handler,
33-
[this]() { return timeout_s(); }),
33+
[this]() { return timeout_s(); },
34+
[this]() { return autopilot(); }),
3435
_request_message(
3536
*this, _command_sender, _mavlink_message_handler, _mavsdk_impl.timeout_handler),
3637
_mavlink_ftp_client(*this)
@@ -1348,6 +1349,7 @@ MavlinkParameterClient* SystemImpl::param_sender(uint8_t component_id, bool exte
13481349
_mavlink_message_handler,
13491350
_mavsdk_impl.timeout_handler,
13501351
[this]() { return timeout_s(); },
1352+
[this]() { return autopilot(); },
13511353
get_system_id(),
13521354
component_id,
13531355
extended),

src/mavsdk/core/timeout_s_callback.h

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
#pragma once
22

3+
#include <functional>
4+
5+
namespace mavsdk {
6+
37
using TimeoutSCallback = std::function<double()>;
8+
9+
} // namespace mavsdk

src/mavsdk/plugins/mission_raw/mission_import.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ MissionImport::import_mission(const Json::Value& root, Autopilot autopilot)
112112
mission_items[0].current = 1;
113113
}
114114

115-
// Don't forget sequence number
116-
unsigned sequence = 0;
117-
for (auto& mission_item : mission_items) {
118-
mission_item.seq = sequence++;
119-
}
120-
121115
// Add home position at 0 for ArduPilot
122116
if (autopilot == Autopilot::ArduPilot) {
123117
const auto home = mission["plannedHomePosition"];
@@ -146,6 +140,12 @@ MissionImport::import_mission(const Json::Value& root, Autopilot autopilot)
146140
}
147141
}
148142

143+
// Don't forget sequence number
144+
unsigned sequence = 0;
145+
for (auto& mission_item : mission_items) {
146+
mission_item.seq = sequence++;
147+
}
148+
149149
// Returning an empty vector is ok here if there were really no mission items.
150150
return {mission_items};
151151
}

src/mavsdk/plugins/mission_raw/mission_import_test.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <algorithm>
12
#include <atomic>
23
#include <cmath>
34
#include <fstream> // for `std::ifstream`
@@ -406,6 +407,11 @@ TEST(MissionRaw, ImportSamplePlanWithArduPilot)
406407
{
407408
auto reference_items = create_reference_items();
408409

410+
// We need to adjust the sequence plus one because we're about to add in home
411+
// at 0.
412+
413+
std::for_each(reference_items.begin(), reference_items.end(), [](auto& item) { ++item.seq; });
414+
409415
auto home_item = MissionRaw::MissionItem{
410416
0,
411417
MAV_FRAME_GLOBAL_INT,
@@ -423,9 +429,6 @@ TEST(MissionRaw, ImportSamplePlanWithArduPilot)
423429

424430
reference_items.insert(reference_items.begin(), home_item);
425431

426-
// There is no need to increment the sequence for all items, the sequence
427-
// still starts at 0 after home.
428-
429432
std::ifstream file{path_prefix("qgroundcontrol_sample.plan")};
430433
ASSERT_TRUE(file);
431434

0 commit comments

Comments
 (0)