• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mavlink / MAVSDK / 20604212380

30 Dec 2025 07:24PM UTC coverage: 48.017% (-0.06%) from 48.078%
20604212380

Pull #2746

github

web-flow
Merge 5269e5f1e into 5d2947b34
Pull Request #2746: Configuration and component cleanup

47 of 158 new or added lines in 19 files covered. (29.75%)

21 existing lines in 9 files now uncovered.

17769 of 37006 relevant lines covered (48.02%)

483.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.59
/src/mavsdk/plugins/action/action_impl.cpp
1
#include "action_impl.h"
2
#include "mavsdk_impl.h"
3
#include "math_utils.h"
4
#include "flight_mode.h"
5
#include "px4_custom_mode.h"
6
#include <cmath>
7
#include <future>
8

9
namespace mavsdk {
10

11
ActionImpl::ActionImpl(System& system) : PluginImplBase(system)
×
12
{
13
    _system_impl->register_plugin(this);
×
14
}
×
15

16
ActionImpl::ActionImpl(std::shared_ptr<System> system) : PluginImplBase(std::move(system))
1✔
17
{
18
    _system_impl->register_plugin(this);
1✔
19
}
1✔
20

21
ActionImpl::~ActionImpl()
2✔
22
{
23
    _system_impl->unregister_plugin(this);
1✔
24
}
2✔
25

26
void ActionImpl::init()
1✔
27
{
28
    // We need the system state.
29
    _system_impl->register_mavlink_message_handler(
1✔
30
        MAVLINK_MSG_ID_EXTENDED_SYS_STATE,
31
        [this](const mavlink_message_t& message) { process_extended_sys_state(message); },
×
32
        this);
33
}
1✔
34

35
void ActionImpl::deinit()
1✔
36
{
37
    _system_impl->unregister_all_mavlink_message_handlers_blocking(this);
1✔
38
}
1✔
39

40
void ActionImpl::enable()
2✔
41
{
42
    // And we need to make sure the system state is actually sent.
43
    // We use the async call here because we should not block in the init call because
44
    // we won't receive an answer anyway in init because the receive loop is not
45
    // called while we are being created here.
46
    _system_impl->set_msg_rate_async(
2✔
47
        MAVLINK_MSG_ID_EXTENDED_SYS_STATE,
48
        1.0,
49
        nullptr,
50
        MavlinkCommandSender::DEFAULT_COMPONENT_ID_AUTOPILOT);
51
}
2✔
52

53
void ActionImpl::disable() {}
1✔
54

55
Action::Result ActionImpl::arm() const
2✔
56
{
57
    auto prom = std::promise<Action::Result>();
2✔
58
    auto fut = prom.get_future();
2✔
59

60
    arm_async([&prom](Action::Result result) { prom.set_value(result); });
4✔
61

62
    return fut.get();
2✔
63
}
2✔
64

65
Action::Result ActionImpl::arm_force() const
×
66
{
67
    auto prom = std::promise<Action::Result>();
×
68
    auto fut = prom.get_future();
×
69

70
    arm_force_async([&prom](Action::Result result) { prom.set_value(result); });
×
71

72
    return fut.get();
×
73
}
×
74

75
Action::Result ActionImpl::disarm() const
2✔
76
{
77
    auto prom = std::promise<Action::Result>();
2✔
78
    auto fut = prom.get_future();
2✔
79

80
    disarm_async([&prom](Action::Result result) { prom.set_value(result); });
4✔
81

82
    return fut.get();
2✔
83
}
2✔
84

85
Action::Result ActionImpl::terminate() const
×
86
{
87
    auto prom = std::promise<Action::Result>();
×
88
    auto fut = prom.get_future();
×
89

90
    terminate_async([&prom](Action::Result result) { prom.set_value(result); });
×
91

92
    return fut.get();
×
93
}
×
94

95
Action::Result ActionImpl::kill() const
×
96
{
97
    auto prom = std::promise<Action::Result>();
×
98
    auto fut = prom.get_future();
×
99

100
    kill_async([&prom](Action::Result result) { prom.set_value(result); });
×
101

102
    return fut.get();
×
103
}
×
104

105
Action::Result ActionImpl::reboot() const
×
106
{
107
    auto prom = std::promise<Action::Result>();
×
108
    auto fut = prom.get_future();
×
109

110
    reboot_async([&prom](Action::Result result) { prom.set_value(result); });
×
111

112
    return fut.get();
×
113
}
×
114

115
Action::Result ActionImpl::shutdown() const
×
116
{
117
    auto prom = std::promise<Action::Result>();
×
118
    auto fut = prom.get_future();
×
119

120
    shutdown_async([&prom](Action::Result result) { prom.set_value(result); });
×
121

122
    return fut.get();
×
123
}
×
124

125
Action::Result ActionImpl::takeoff() const
×
126
{
127
    auto prom = std::promise<Action::Result>();
×
128
    auto fut = prom.get_future();
×
129

130
    takeoff_async([&prom](Action::Result result) { prom.set_value(result); });
×
131

132
    return fut.get();
×
133
}
×
134

135
Action::Result ActionImpl::land() const
×
136
{
137
    auto prom = std::promise<Action::Result>();
×
138
    auto fut = prom.get_future();
×
139

140
    land_async([&prom](Action::Result result) { prom.set_value(result); });
×
141

142
    return fut.get();
×
143
}
×
144

145
Action::Result ActionImpl::return_to_launch() const
×
146
{
147
    auto prom = std::promise<Action::Result>();
×
148
    auto fut = prom.get_future();
×
149

150
    return_to_launch_async([&prom](Action::Result result) { prom.set_value(result); });
×
151

152
    return fut.get();
×
153
}
×
154

155
Action::Result ActionImpl::goto_location(
×
156
    const double latitude_deg,
157
    const double longitude_deg,
158
    const float altitude_amsl_m,
159
    const float yaw_deg)
160
{
161
    auto prom = std::promise<Action::Result>();
×
162
    auto fut = prom.get_future();
×
163

164
    goto_location_async(
×
165
        latitude_deg, longitude_deg, altitude_amsl_m, yaw_deg, [&prom](Action::Result result) {
×
166
            prom.set_value(result);
×
167
        });
×
168

169
    return fut.get();
×
170
}
×
171

172
Action::Result ActionImpl::do_orbit(
×
173
    const float radius_m,
174
    const float velocity_ms,
175
    const Action::OrbitYawBehavior yaw_behavior,
176
    const double latitude_deg,
177
    const double longitude_deg,
178
    const double absolute_altitude_m)
179
{
180
    auto prom = std::promise<Action::Result>();
×
181
    auto fut = prom.get_future();
×
182

183
    do_orbit_async(
×
184
        radius_m,
185
        velocity_ms,
186
        yaw_behavior,
187
        latitude_deg,
188
        longitude_deg,
189
        absolute_altitude_m,
190
        [&prom](Action::Result result) { prom.set_value(result); });
×
191

192
    return fut.get();
×
193
}
×
194

195
Action::Result ActionImpl::hold() const
×
196
{
197
    auto prom = std::promise<Action::Result>();
×
198
    auto fut = prom.get_future();
×
199

200
    hold_async([&prom](Action::Result result) { prom.set_value(result); });
×
201

202
    return fut.get();
×
203
}
×
204

205
Action::Result ActionImpl::set_actuator(const int index, const float value)
×
206
{
207
    auto prom = std::promise<Action::Result>();
×
208
    auto fut = prom.get_future();
×
209

210
    set_actuator_async(index, value, [&prom](Action::Result result) { prom.set_value(result); });
×
211

212
    return fut.get();
×
213
}
×
214

215
Action::Result ActionImpl::transition_to_fixedwing() const
×
216
{
217
    auto prom = std::promise<Action::Result>();
×
218
    auto fut = prom.get_future();
×
219

220
    transition_to_fixedwing_async([&prom](Action::Result result) { prom.set_value(result); });
×
221

222
    return fut.get();
×
223
}
×
224

225
Action::Result ActionImpl::transition_to_multicopter() const
×
226
{
227
    auto prom = std::promise<Action::Result>();
×
228
    auto fut = prom.get_future();
×
229

230
    transition_to_multicopter_async([&prom](Action::Result result) { prom.set_value(result); });
×
231

232
    return fut.get();
×
233
}
×
234

235
void ActionImpl::arm_async(const Action::ResultCallback& callback) const
2✔
236
{
237
    auto send_arm_command = [this, callback]() {
8✔
238
        MavlinkCommandSender::CommandLong command{};
2✔
239

240
        command.command = MAV_CMD_COMPONENT_ARM_DISARM;
2✔
241
        command.params.maybe_param1 = 1.0f; // arm
2✔
242
        command.target_component_id = _system_impl->get_autopilot_id();
2✔
243

244
        _system_impl->send_command_async(
4✔
245
            command, [this, callback](MavlinkCommandSender::Result result, float) {
2✔
246
                command_result_callback(result, callback);
2✔
247
            });
2✔
248
    };
2✔
249

250
    if (need_hold_before_arm()) {
2✔
251
        _system_impl->set_flight_mode_async(
×
252
            FlightMode::Hold,
253
            [callback, send_arm_command](MavlinkCommandSender::Result result, float) {
×
254
                Action::Result action_result = action_result_from_command_result(result);
×
255
                if (action_result != Action::Result::Success) {
×
256
                    if (callback) {
×
257
                        callback(action_result);
×
258
                    }
259
                }
260
                send_arm_command();
×
261
            });
×
262
        return;
×
263
    } else {
264
        send_arm_command();
2✔
265
    }
266
}
2✔
267

268
bool ActionImpl::need_hold_before_arm() const
2✔
269
{
270
    if (_system_impl->effective_autopilot() == Autopilot::Px4) {
2✔
271
        return need_hold_before_arm_px4();
×
272
    } else {
273
        return need_hold_before_arm_apm();
2✔
274
    }
275
}
276

277
bool ActionImpl::need_hold_before_arm_px4() const
×
278
{
279
    if (_system_impl->get_flight_mode() == FlightMode::Mission ||
×
280
        _system_impl->get_flight_mode() == FlightMode::ReturnToLaunch) {
×
281
        return true;
×
282
    } else {
283
        return false;
×
284
    }
285
}
286

287
bool ActionImpl::need_hold_before_arm_apm() const
2✔
288
{
289
    if (_system_impl->get_flight_mode() == FlightMode::Mission ||
4✔
290
        _system_impl->get_flight_mode() == FlightMode::ReturnToLaunch ||
4✔
291
        _system_impl->get_flight_mode() == FlightMode::Land) {
2✔
292
        return true;
×
293
    } else {
294
        return false;
2✔
295
    }
296
}
297

298
void ActionImpl::arm_force_async(const Action::ResultCallback& callback) const
×
299
{
300
    MavlinkCommandSender::CommandLong command{};
×
301

302
    command.command = MAV_CMD_COMPONENT_ARM_DISARM;
×
303
    command.params.maybe_param1 = 0.0f; // arm
×
304
    command.params.maybe_param2 = 21196.f; // magic number to force
×
305
    command.target_component_id = _system_impl->get_autopilot_id();
×
306

307
    _system_impl->send_command_async(
×
308
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
309
            command_result_callback(result, callback);
×
310
        });
×
311
}
×
312

313
void ActionImpl::disarm_async(const Action::ResultCallback& callback) const
2✔
314
{
315
    MavlinkCommandSender::CommandLong command{};
2✔
316

317
    command.command = MAV_CMD_COMPONENT_ARM_DISARM;
2✔
318
    command.params.maybe_param1 = 0.0f; // disarm
2✔
319
    command.target_component_id = _system_impl->get_autopilot_id();
2✔
320

321
    _system_impl->send_command_async(
2✔
322
        command, [this, callback](MavlinkCommandSender::Result result, float) {
2✔
323
            command_result_callback(result, callback);
2✔
324
        });
2✔
325
}
2✔
326

327
void ActionImpl::terminate_async(const Action::ResultCallback& callback) const
×
328
{
329
    MavlinkCommandSender::CommandLong command{};
×
330

331
    command.command = MAV_CMD_DO_FLIGHTTERMINATION;
×
332
    command.params.maybe_param1 = 1.0f;
×
333
    command.target_component_id = _system_impl->get_autopilot_id();
×
334

335
    _system_impl->send_command_async(
×
336
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
337
            command_result_callback(result, callback);
×
338
        });
×
339
}
×
340

341
void ActionImpl::kill_async(const Action::ResultCallback& callback) const
×
342
{
343
    MavlinkCommandSender::CommandLong command{};
×
344

345
    command.command = MAV_CMD_COMPONENT_ARM_DISARM;
×
346
    command.params.maybe_param1 = 0.0f; // kill
×
347
    command.params.maybe_param2 = 21196.f; // magic number to enforce in-air
×
348
    command.target_component_id = _system_impl->get_autopilot_id();
×
349

350
    _system_impl->send_command_async(
×
351
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
352
            command_result_callback(result, callback);
×
353
        });
×
354
}
×
355

356
void ActionImpl::reboot_async(const Action::ResultCallback& callback) const
×
357
{
358
    MavlinkCommandSender::CommandLong command{};
×
359

360
    command.command = MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN;
×
361
    command.params.maybe_param1 = 1.0f; // reboot autopilot
×
362
    command.target_component_id = _system_impl->get_autopilot_id();
×
363

364
    _system_impl->send_command_async(
×
365
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
366
            command_result_callback(result, callback);
×
367
        });
×
368
}
×
369

370
void ActionImpl::shutdown_async(const Action::ResultCallback& callback) const
×
371
{
372
    MavlinkCommandSender::CommandLong command{};
×
373

374
    command.command = MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN;
×
375
    command.params.maybe_param1 = 2.0f; // shutdown autopilot
×
376
    command.target_component_id = _system_impl->get_autopilot_id();
×
377

378
    _system_impl->send_command_async(
×
379
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
380
            command_result_callback(result, callback);
×
381
        });
×
382
}
×
383

384
void ActionImpl::takeoff_async(const Action::ResultCallback& callback) const
×
385
{
NEW
386
    if (_system_impl->effective_autopilot() == Autopilot::Px4) {
×
387
        takeoff_async_px4(callback);
×
388
    } else {
389
        takeoff_async_apm(callback);
×
390
    }
391
}
×
392

393
void ActionImpl::takeoff_async_px4(const Action::ResultCallback& callback) const
×
394
{
395
    MavlinkCommandSender::CommandLong command{};
×
396

397
    command.command = MAV_CMD_NAV_TAKEOFF;
×
398
    command.target_component_id = _system_impl->get_autopilot_id();
×
399

400
    _system_impl->send_command_async(
×
401
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
402
            command_result_callback(result, callback);
×
403
        });
×
404
}
×
405

406
void ActionImpl::takeoff_async_apm(const Action::ResultCallback& callback) const
×
407
{
408
    auto send_takeoff_command = [this, callback]() {
×
409
        MavlinkCommandSender::CommandLong command{};
×
410

411
        command.command = MAV_CMD_NAV_TAKEOFF;
×
412
        command.target_component_id = _system_impl->get_autopilot_id();
×
413
        command.params.maybe_param7 = get_takeoff_altitude().second;
×
414

415
        _system_impl->send_command_async(
×
416
            command, [this, callback](MavlinkCommandSender::Result result, float) {
×
417
                command_result_callback(result, callback);
×
418
            });
×
419
    };
×
420
    if (_system_impl->get_flight_mode() != FlightMode::Offboard) {
×
421
        _system_impl->set_flight_mode_async(
×
422
            FlightMode::Offboard,
423
            [callback, send_takeoff_command](MavlinkCommandSender::Result result, float) {
×
424
                Action::Result action_result = action_result_from_command_result(result);
×
425
                if (action_result != Action::Result::Success) {
×
426
                    if (callback) {
×
427
                        callback(action_result);
×
428
                    }
429
                } else {
430
                    send_takeoff_command();
×
431
                }
432
            });
×
433
    } else {
434
        send_takeoff_command();
×
435
    }
436
}
×
437

438
void ActionImpl::land_async(const Action::ResultCallback& callback) const
×
439
{
440
    MavlinkCommandSender::CommandLong command{};
×
441

442
    command.command = MAV_CMD_NAV_LAND;
×
443
    command.params.maybe_param4 = NAN; // Don't change yaw.
×
444
    command.target_component_id = _system_impl->get_autopilot_id();
×
445

446
    _system_impl->send_command_async(
×
447
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
448
            command_result_callback(result, callback);
×
449
        });
×
450
}
×
451

452
void ActionImpl::return_to_launch_async(const Action::ResultCallback& callback) const
×
453
{
454
    _system_impl->set_flight_mode_async(
×
455
        FlightMode::ReturnToLaunch, [this, callback](MavlinkCommandSender::Result result, float) {
×
456
            command_result_callback(result, callback);
×
457
        });
×
458
}
×
459

460
void ActionImpl::goto_location_async(
×
461
    const double latitude_deg,
462
    const double longitude_deg,
463
    const float altitude_amsl_m,
464
    const float yaw_deg,
465
    const Action::ResultCallback& callback)
466
{
467
    auto send_do_reposition =
468
        [this, callback, yaw_deg, latitude_deg, longitude_deg, altitude_amsl_m]() {
×
469
            MavlinkCommandSender::CommandInt command{};
×
470

471
            command.command = MAV_CMD_DO_REPOSITION;
×
472
            command.target_component_id = _system_impl->get_autopilot_id();
×
473
            command.frame = MAV_FRAME_GLOBAL_INT;
×
474
            command.params.maybe_param4 = static_cast<float>(to_rad_from_deg(yaw_deg));
×
475
            command.params.x = int32_t(std::round(latitude_deg * 1e7));
×
476
            command.params.y = int32_t(std::round(longitude_deg * 1e7));
×
477
            command.params.maybe_z = altitude_amsl_m;
×
478
            command.params.maybe_param2 = static_cast<float>(MAV_DO_REPOSITION_FLAGS_CHANGE_MODE);
×
479

480
            _system_impl->send_command_async(
×
481
                command, [this, callback](MavlinkCommandSender::Result result, float) {
×
482
                    command_result_callback(result, callback);
×
483
                });
×
484
        };
×
485
    FlightMode goto_flight_mode;
NEW
486
    if (_system_impl->effective_autopilot() == Autopilot::Px4) {
×
487
        goto_flight_mode = FlightMode::Hold;
×
488
    } else {
489
        goto_flight_mode = FlightMode::Offboard;
×
490
    }
491
    if (_system_impl->get_flight_mode() != goto_flight_mode) {
×
492
        _system_impl->set_flight_mode_async(
×
493
            goto_flight_mode,
494
            [this, callback, send_do_reposition](MavlinkCommandSender::Result result, float) {
×
495
                Action::Result action_result = action_result_from_command_result(result);
×
496
                if (action_result != Action::Result::Success) {
×
497
                    command_result_callback(result, callback);
×
498
                    return;
×
499
                }
500
                send_do_reposition();
×
501
            });
502
        return;
×
503
    }
504

505
    send_do_reposition();
×
506
}
×
507

508
void ActionImpl::do_orbit_async(
×
509
    const float radius_m,
510
    const float velocity_ms,
511
    const Action::OrbitYawBehavior yaw_behavior,
512
    const double latitude_deg,
513
    const double longitude_deg,
514
    const double absolute_altitude_m,
515
    const Action::ResultCallback& callback)
516
{
517
    // MAV_CMD_DO_ORBIT is marked as WIP, only supported for PX4 for now.
NEW
518
    if (_system_impl->effective_autopilot() != Autopilot::Px4) {
×
NEW
519
        if (callback) {
×
NEW
520
            _system_impl->call_user_callback(
×
521
                [callback]() { callback(Action::Result::Unsupported); });
522
        }
NEW
523
        return;
×
524
    }
525

UNCOV
526
    MavlinkCommandSender::CommandInt command{};
×
527

528
    command.command = MAV_CMD_DO_ORBIT;
×
529
    command.target_component_id = _system_impl->get_autopilot_id();
×
530
    command.params.maybe_param1 = radius_m;
×
531
    command.params.maybe_param2 = velocity_ms;
×
532
    command.params.maybe_param3 = static_cast<float>(yaw_behavior);
×
533
    command.params.x = int32_t(std::round(latitude_deg * 1e7));
×
534
    command.params.y = int32_t(std::round(longitude_deg * 1e7));
×
535
    command.params.maybe_z = static_cast<float>(absolute_altitude_m);
×
536

537
    _system_impl->send_command_async(
×
538
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
539
            command_result_callback(result, callback);
×
540
        });
×
541
}
542

543
void ActionImpl::hold_async(const Action::ResultCallback& callback) const
×
544
{
545
    _system_impl->set_flight_mode_async(
×
546
        FlightMode::Hold, [this, callback](MavlinkCommandSender::Result result, float) {
×
547
            command_result_callback(result, callback);
×
548
        });
×
549
}
×
550

551
void ActionImpl::set_actuator_async(
×
552
    const int index, const float value, const Action::ResultCallback& callback)
553
{
554
    MavlinkCommandSender::CommandLong command{};
×
555
    command.target_component_id = _system_impl->get_autopilot_id();
×
556

NEW
557
    if (_system_impl->effective_autopilot() == Autopilot::ArduPilot) {
×
558
        command.command = MAV_CMD_DO_SET_SERVO;
×
559
        command.params.maybe_param1 = static_cast<float>(index);
×
560
        command.params.maybe_param2 = value;
×
561
    } else {
562
        auto zero_based_index = index - 1;
×
563
        if (zero_based_index >= 0) {
×
564
            command.command = MAV_CMD_DO_SET_ACTUATOR;
×
565
            switch (zero_based_index % 6) {
×
566
                case 0:
×
567
                    command.params.maybe_param1 = value;
×
568
                    break;
×
569
                case 1:
×
570
                    command.params.maybe_param2 = value;
×
571
                    break;
×
572
                case 2:
×
573
                    command.params.maybe_param3 = value;
×
574
                    break;
×
575
                case 3:
×
576
                    command.params.maybe_param4 = value;
×
577
                    break;
×
578
                case 4:
×
579
                    command.params.maybe_param5 = value;
×
580
                    break;
×
581
                case 5:
×
582
                    command.params.maybe_param6 = value;
×
583
                    break;
×
584
            }
585
            command.params.maybe_param7 = static_cast<float>(zero_based_index / 6);
×
586
        } else {
587
            if (callback) {
×
588
                _system_impl->call_user_callback([temp_callback = callback]() {
×
589
                    temp_callback(Action::Result::InvalidArgument);
590
                });
591
            }
592
            return;
×
593
        }
594
    }
595

596
    _system_impl->send_command_async(
×
597
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
598
            command_result_callback(result, callback);
×
599
        });
×
600
}
601

602
void ActionImpl::transition_to_fixedwing_async(const Action::ResultCallback& callback) const
×
603
{
604
    if (!_vtol_transition_support_known) {
×
605
        if (callback) {
×
606
            callback(Action::Result::VtolTransitionSupportUnknown);
×
607
        }
608
        return;
×
609
    }
610

611
    if (!_vtol_transition_possible) {
×
612
        if (callback) {
×
613
            callback(Action::Result::NoVtolTransitionSupport);
×
614
        }
615
        return;
×
616
    }
617

618
    MavlinkCommandSender::CommandLong command{};
×
619

620
    command.command = MAV_CMD_DO_VTOL_TRANSITION;
×
621
    command.params.maybe_param1 = static_cast<float>(MAV_VTOL_STATE_FW);
×
622
    command.target_component_id = _system_impl->get_autopilot_id();
×
623

624
    _system_impl->send_command_async(
×
625
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
626
            command_result_callback(result, callback);
×
627
        });
×
628
}
629

630
void ActionImpl::transition_to_multicopter_async(const Action::ResultCallback& callback) const
×
631
{
632
    if (!_vtol_transition_support_known) {
×
633
        if (callback) {
×
634
            callback(Action::Result::VtolTransitionSupportUnknown);
×
635
        }
636
        return;
×
637
    }
638

639
    if (!_vtol_transition_possible) {
×
640
        if (callback) {
×
641
            callback(Action::Result::NoVtolTransitionSupport);
×
642
        }
643
        return;
×
644
    }
645
    MavlinkCommandSender::CommandLong command{};
×
646

647
    command.command = MAV_CMD_DO_VTOL_TRANSITION;
×
648
    command.params.maybe_param1 = static_cast<float>(MAV_VTOL_STATE_MC);
×
649
    command.target_component_id = _system_impl->get_autopilot_id();
×
650

651
    _system_impl->send_command_async(
×
652
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
653
            command_result_callback(result, callback);
×
654
        });
×
655
}
656

657
void ActionImpl::process_extended_sys_state(const mavlink_message_t& message)
×
658
{
659
    mavlink_extended_sys_state_t extended_sys_state;
660
    mavlink_msg_extended_sys_state_decode(&message, &extended_sys_state);
×
661

662
    if (extended_sys_state.vtol_state != MAV_VTOL_STATE_UNDEFINED) {
×
663
        _vtol_transition_possible = true;
×
664
    } else {
665
        _vtol_transition_possible = false;
×
666
    }
667
    _vtol_transition_support_known = true;
×
668
}
×
669

670
void ActionImpl::set_takeoff_altitude_async(
×
671
    const float relative_altitude_m, const Action::ResultCallback& callback)
672
{
673
    callback(set_takeoff_altitude(relative_altitude_m));
×
674
}
×
675

676
Action::Result ActionImpl::set_takeoff_altitude(float relative_altitude_m)
×
677
{
NEW
678
    if (_system_impl->effective_autopilot() == Autopilot::Px4) {
×
679
        return set_takeoff_altitude_px4(relative_altitude_m);
×
680
    } else {
681
        return set_takeoff_altitude_apm(relative_altitude_m);
×
682
    }
683
}
684

685
Action::Result ActionImpl::set_takeoff_altitude_px4(float relative_altitude_m)
×
686
{
687
    _takeoff_altitude = relative_altitude_m;
×
688

689
    const MavlinkParameterClient::Result result =
690
        _system_impl->set_param_float(TAKEOFF_ALT_PARAM, relative_altitude_m);
×
691
    return (result == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
692
                                                                 Action::Result::ParameterError;
×
693
}
694

695
Action::Result ActionImpl::set_takeoff_altitude_apm(float relative_altitude_m)
×
696
{
697
    _takeoff_altitude = relative_altitude_m;
×
698
    return Action::Result::Success;
×
699
}
700

701
void ActionImpl::get_takeoff_altitude_async(
×
702
    const Action::GetTakeoffAltitudeCallback& callback) const
703
{
704
    auto altitude_result = get_takeoff_altitude();
×
705
    callback(altitude_result.first, altitude_result.second);
×
706
}
×
707

708
std::pair<Action::Result, float> ActionImpl::get_takeoff_altitude() const
×
709
{
NEW
710
    if (_system_impl->effective_autopilot() == Autopilot::ArduPilot) {
×
711
        return std::make_pair<>(Action::Result::Success, _takeoff_altitude);
×
712
    } else {
713
        auto result = _system_impl->get_param_float(TAKEOFF_ALT_PARAM);
×
714
        return std::make_pair<>(
×
715
            (result.first == MavlinkParameterClient::Result::Success) ?
×
716
                Action::Result::Success :
717
                Action::Result::ParameterError,
718
            result.second);
×
719
    }
720
}
721

722
void ActionImpl::set_return_to_launch_altitude_async(
×
723
    const float relative_altitude_m, const Action::ResultCallback& callback) const
724
{
725
    // RTL_RETURN_ALT param is PX4-specific.
NEW
726
    if (_system_impl->effective_autopilot() != Autopilot::Px4) {
×
NEW
727
        if (callback) {
×
NEW
728
            _system_impl->call_user_callback(
×
729
                [callback]() { callback(Action::Result::Unsupported); });
730
        }
NEW
731
        return;
×
732
    }
733
    callback(set_return_to_launch_altitude(relative_altitude_m));
×
734
}
735

736
Action::Result ActionImpl::set_return_to_launch_altitude(const float relative_altitude_m) const
×
737
{
738
    // RTL_RETURN_ALT param is PX4-specific.
NEW
739
    if (_system_impl->effective_autopilot() != Autopilot::Px4) {
×
NEW
740
        return Action::Result::Unsupported;
×
741
    }
742

743
    const MavlinkParameterClient::Result result =
744
        _system_impl->set_param_float(RTL_RETURN_ALTITUDE_PARAM, relative_altitude_m);
×
745
    return (result == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
746
                                                                 Action::Result::ParameterError;
×
747
}
748

749
void ActionImpl::get_return_to_launch_altitude_async(
×
750
    const Action::GetReturnToLaunchAltitudeCallback& callback) const
751
{
752
    // RTL_RETURN_ALT param is PX4-specific.
NEW
753
    if (_system_impl->effective_autopilot() != Autopilot::Px4) {
×
NEW
754
        if (callback) {
×
NEW
755
            _system_impl->call_user_callback(
×
756
                [callback]() { callback(Action::Result::Unsupported, NAN); });
757
        }
NEW
758
        return;
×
759
    }
760
    const auto get_result = get_return_to_launch_altitude();
×
761
    callback(get_result.first, get_result.second);
×
762
}
763

764
std::pair<Action::Result, float> ActionImpl::get_return_to_launch_altitude() const
×
765
{
766
    // RTL_RETURN_ALT param is PX4-specific.
NEW
767
    if (_system_impl->effective_autopilot() != Autopilot::Px4) {
×
NEW
768
        return std::make_pair(Action::Result::Unsupported, NAN);
×
769
    }
770

771
    auto result = _system_impl->get_param_float(RTL_RETURN_ALTITUDE_PARAM);
×
772
    return std::make_pair<>(
×
773
        (result.first == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
774
                                                                    Action::Result::ParameterError,
775
        result.second);
×
776
}
777

778
void ActionImpl::set_current_speed_async(float speed_m_s, const Action::ResultCallback& callback)
×
779
{
780
    MavlinkCommandSender::CommandLong command{};
×
781

782
    command.command = MAV_CMD_DO_CHANGE_SPEED;
×
783
    command.params.maybe_param1 = 1.0f; // ground speed
×
784
    command.params.maybe_param2 = speed_m_s;
×
785
    command.params.maybe_param3 = -1.0f; // no throttle set
×
786
    command.params.maybe_param4 = 0.0f; // reserved
×
787
    command.target_component_id = _system_impl->get_autopilot_id();
×
788

789
    _system_impl->send_command_async(
×
790
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
791
            command_result_callback(result, callback);
×
792
        });
×
793
}
×
794

795
Action::Result ActionImpl::set_current_speed(float speed_m_s)
×
796
{
797
    auto prom = std::promise<Action::Result>();
×
798
    auto fut = prom.get_future();
×
799

800
    set_current_speed_async(speed_m_s, [&prom](Action::Result result) { prom.set_value(result); });
×
801

802
    return fut.get();
×
803
}
×
804

805
Action::Result ActionImpl::set_gps_global_origin(
×
806
    double latitude_deg, double longitude_deg, float absolute_altitude_m) const
807
{
808
    const int32_t latitude_e7 = static_cast<int32_t>(std::round(latitude_deg * 1e7));
×
809
    const int32_t longitude_e7 = static_cast<int32_t>(std::round(longitude_deg * 1e7));
×
810
    const int32_t altitude_mm = static_cast<int32_t>(std::round(absolute_altitude_m * 1000.0f));
×
811

812
    auto prom = std::promise<Action::Result>();
×
813
    auto fut = prom.get_future();
×
814
    std::atomic<bool> prom_already_set{false};
×
815

816
    // Use a unique cookie for this handler
817
    const void* cookie = this;
×
818

819
    // Register handler for GPS_GLOBAL_ORIGIN response.
820
    // Note: Older PX4 versions (pre-v1.17) had a race condition where they would
821
    // broadcast GPS_GLOBAL_ORIGIN with stale values immediately after receiving
822
    // SET_GPS_GLOBAL_ORIGIN, before EKF2 had processed the command. To handle
823
    // this, we wait for a response with matching values rather than accepting
824
    // the first response.
825
    _system_impl->register_mavlink_message_handler(
×
826
        MAVLINK_MSG_ID_GPS_GLOBAL_ORIGIN,
827
        [&prom, &prom_already_set, latitude_e7, longitude_e7, altitude_mm](
×
828
            const mavlink_message_t& message) {
×
829
            mavlink_gps_global_origin_t origin;
830
            mavlink_msg_gps_global_origin_decode(&message, &origin);
×
831

832
            // Only signal success when we receive the values we set
833
            if (origin.latitude == latitude_e7 && origin.longitude == longitude_e7 &&
×
834
                origin.altitude == altitude_mm) {
×
835
                if (!prom_already_set.exchange(true)) {
×
836
                    prom.set_value(Action::Result::Success);
×
837
                }
838
            }
839
            // Otherwise, keep waiting for the correct values (or timeout)
840
        },
×
841
        cookie);
842

843
    // Send the SET_GPS_GLOBAL_ORIGIN message
844
    if (!_system_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
×
845
            mavlink_message_t message;
846
            mavlink_msg_set_gps_global_origin_pack_chan(
×
847
                mavlink_address.system_id,
×
848
                mavlink_address.component_id,
×
849
                channel,
850
                &message,
851
                _system_impl->get_system_id(),
×
852
                latitude_e7,
×
853
                longitude_e7,
×
854
                altitude_mm,
×
855
                0);
856
            return message;
×
857
        })) {
858
        _system_impl->unregister_mavlink_message_handler(MAVLINK_MSG_ID_GPS_GLOBAL_ORIGIN, cookie);
×
859
        return Action::Result::ConnectionError;
×
860
    }
861

862
    // Wait for response with timeout
863
    auto status = fut.wait_for(std::chrono::duration<double>(_system_impl->timeout_s()));
×
864

865
    _system_impl->unregister_mavlink_message_handler(MAVLINK_MSG_ID_GPS_GLOBAL_ORIGIN, cookie);
×
866

867
    if (status == std::future_status::timeout) {
×
868
        return Action::Result::Timeout;
×
869
    }
870

871
    return fut.get();
×
872
}
×
873

874
Action::Result ActionImpl::action_result_from_command_result(MavlinkCommandSender::Result result)
4✔
875
{
876
    switch (result) {
4✔
877
        case MavlinkCommandSender::Result::Success:
2✔
878
            return Action::Result::Success;
2✔
879
        case MavlinkCommandSender::Result::NoSystem:
×
880
            return Action::Result::NoSystem;
×
881
        case MavlinkCommandSender::Result::ConnectionError:
×
882
            return Action::Result::ConnectionError;
×
883
        case MavlinkCommandSender::Result::Busy:
×
884
            return Action::Result::Busy;
×
885
        case MavlinkCommandSender::Result::Denied:
2✔
886
            // Fallthrough
887
        case MavlinkCommandSender::Result::TemporarilyRejected:
888
            return Action::Result::CommandDenied;
2✔
889
        case MavlinkCommandSender::Result::Failed:
×
890
            return Action::Result::Failed;
×
891
        case MavlinkCommandSender::Result::Timeout:
×
892
            return Action::Result::Timeout;
×
893
        case MavlinkCommandSender::Result::Unsupported:
×
894
            return Action::Result::Unsupported;
×
895
        default:
×
896
            return Action::Result::Unknown;
×
897
    }
898
}
899

900
void ActionImpl::command_result_callback(
4✔
901
    MavlinkCommandSender::Result command_result, const Action::ResultCallback& callback) const
902
{
903
    if (command_result == MavlinkCommandSender::Result::InProgress) {
4✔
904
        // We only want to return once, so we can't call the callback on progress updates.
905
        return;
×
906
    }
907

908
    Action::Result action_result = action_result_from_command_result(command_result);
4✔
909

910
    if (callback) {
4✔
911
        auto temp_callback = callback;
4✔
912
        _system_impl->call_user_callback(
8✔
913
            [temp_callback, action_result]() { temp_callback(action_result); });
914
    }
4✔
915
}
916

917
} // namespace mavsdk
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc