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

mavlink / MAVSDK / 6568658631

19 Oct 2023 01:31AM UTC coverage: 31.23% (+0.02%) from 31.215%
6568658631

push

github

web-flow
Merge pull request #2155 from mavlink/pr-static-fixes

Threading fixes, MAVLink sequence number cleanup

1386 of 1386 new or added lines in 46 files covered. (100.0%)

7906 of 25315 relevant lines covered (31.23%)

23.54 hits per line

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

14.65
/src/mavsdk/plugins/action/action_impl.cpp
1
#include "action_impl.h"
2
#include "mavsdk_impl.h"
3
#include "mavsdk_math.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()
1✔
22
{
23
    _system_impl->unregister_plugin(this);
1✔
24
}
1✔
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(this);
1✔
38
}
1✔
39

40
void ActionImpl::enable()
1✔
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(
1✔
47
        MAVLINK_MSG_ID_EXTENDED_SYS_STATE,
48
        1.0,
49
        nullptr,
50
        MavlinkCommandSender::DEFAULT_COMPONENT_ID_AUTOPILOT);
51
}
1✔
52

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

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

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

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

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

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

72
    return fut.get();
2✔
73
}
74

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

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

82
    return fut.get();
×
83
}
84

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

145
Action::Result ActionImpl::goto_location(
×
146
    const double latitude_deg,
147
    const double longitude_deg,
148
    const float altitude_amsl_m,
149
    const float yaw_deg)
150
{
151
    auto prom = std::promise<Action::Result>();
×
152
    auto fut = prom.get_future();
×
153

154
    goto_location_async(
×
155
        latitude_deg, longitude_deg, altitude_amsl_m, yaw_deg, [&prom](Action::Result result) {
×
156
            prom.set_value(result);
×
157
        });
×
158

159
    return fut.get();
×
160
}
161

162
Action::Result ActionImpl::do_orbit(
×
163
    const float radius_m,
164
    const float velocity_ms,
165
    const Action::OrbitYawBehavior yaw_behavior,
166
    const double latitude_deg,
167
    const double longitude_deg,
168
    const double absolute_altitude_m)
169
{
170
    auto prom = std::promise<Action::Result>();
×
171
    auto fut = prom.get_future();
×
172

173
    do_orbit_async(
×
174
        radius_m,
175
        velocity_ms,
176
        yaw_behavior,
177
        latitude_deg,
178
        longitude_deg,
179
        absolute_altitude_m,
180
        [&prom](Action::Result result) { prom.set_value(result); });
×
181

182
    return fut.get();
×
183
}
184

185
Action::Result ActionImpl::hold() const
×
186
{
187
    auto prom = std::promise<Action::Result>();
×
188
    auto fut = prom.get_future();
×
189

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

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

195
Action::Result ActionImpl::set_actuator(const int index, const float value)
×
196
{
197
    auto prom = std::promise<Action::Result>();
×
198
    auto fut = prom.get_future();
×
199

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

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

205
Action::Result ActionImpl::transition_to_fixedwing() const
×
206
{
207
    auto prom = std::promise<Action::Result>();
×
208
    auto fut = prom.get_future();
×
209

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

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

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

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

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

225
void ActionImpl::arm_async(const Action::ResultCallback& callback) const
2✔
226
{
227
    auto send_arm_command = [this, callback]() {
2✔
228
        MavlinkCommandSender::CommandLong command{};
2✔
229

230
        command.command = MAV_CMD_COMPONENT_ARM_DISARM;
2✔
231
        command.params.maybe_param1 = 1.0f; // arm
2✔
232
        command.target_component_id = _system_impl->get_autopilot_id();
2✔
233

234
        _system_impl->send_command_async(
4✔
235
            command, [this, callback](MavlinkCommandSender::Result result, float) {
2✔
236
                command_result_callback(result, callback);
2✔
237
            });
2✔
238
    };
2✔
239

240
    if (need_hold_before_arm()) {
2✔
241
        _system_impl->set_flight_mode_async(
×
242
            FlightMode::Hold,
243
            [callback, send_arm_command](MavlinkCommandSender::Result result, float) {
×
244
                Action::Result action_result = action_result_from_command_result(result);
×
245
                if (action_result != Action::Result::Success) {
×
246
                    if (callback) {
×
247
                        callback(action_result);
×
248
                    }
249
                }
250
                send_arm_command();
×
251
            });
×
252
        return;
×
253
    } else {
254
        send_arm_command();
2✔
255
    }
256
}
257

258
bool ActionImpl::need_hold_before_arm() const
2✔
259
{
260
    if (_system_impl->autopilot() == Autopilot::Px4) {
2✔
261
        return need_hold_before_arm_px4();
×
262
    } else {
263
        return need_hold_before_arm_apm();
2✔
264
    }
265
}
266

267
bool ActionImpl::need_hold_before_arm_px4() const
×
268
{
269
    if (_system_impl->get_flight_mode() == FlightMode::Mission ||
×
270
        _system_impl->get_flight_mode() == FlightMode::ReturnToLaunch) {
×
271
        return true;
×
272
    } else {
273
        return false;
×
274
    }
275
}
276

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

288
void ActionImpl::disarm_async(const Action::ResultCallback& callback) const
2✔
289
{
290
    MavlinkCommandSender::CommandLong command{};
2✔
291

292
    command.command = MAV_CMD_COMPONENT_ARM_DISARM;
2✔
293
    command.params.maybe_param1 = 0.0f; // disarm
2✔
294
    command.target_component_id = _system_impl->get_autopilot_id();
2✔
295

296
    _system_impl->send_command_async(
2✔
297
        command, [this, callback](MavlinkCommandSender::Result result, float) {
2✔
298
            command_result_callback(result, callback);
2✔
299
        });
2✔
300
}
2✔
301

302
void ActionImpl::terminate_async(const Action::ResultCallback& callback) const
×
303
{
304
    MavlinkCommandSender::CommandLong command{};
×
305

306
    command.command = MAV_CMD_DO_FLIGHTTERMINATION;
×
307
    command.params.maybe_param1 = 1.0f;
×
308
    command.target_component_id = _system_impl->get_autopilot_id();
×
309

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

316
void ActionImpl::kill_async(const Action::ResultCallback& callback) const
×
317
{
318
    MavlinkCommandSender::CommandLong command{};
×
319

320
    command.command = MAV_CMD_COMPONENT_ARM_DISARM;
×
321
    command.params.maybe_param1 = 0.0f; // kill
×
322
    command.params.maybe_param2 = 21196.f; // magic number to enforce in-air
×
323
    command.target_component_id = _system_impl->get_autopilot_id();
×
324

325
    _system_impl->send_command_async(
×
326
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
327
            command_result_callback(result, callback);
×
328
        });
×
329
}
×
330

331
void ActionImpl::reboot_async(const Action::ResultCallback& callback) const
×
332
{
333
    MavlinkCommandSender::CommandLong command{};
×
334

335
    command.command = MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN;
×
336
    command.params.maybe_param1 = 1.0f; // reboot autopilot
×
337
    command.params.maybe_param2 = 1.0f; // reboot onboard computer
×
338
    command.params.maybe_param3 = 1.0f; // reboot camera
×
339
    command.params.maybe_param4 = 1.0f; // reboot gimbal
×
340
    command.target_component_id = _system_impl->get_autopilot_id();
×
341

342
    _system_impl->send_command_async(
×
343
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
344
            command_result_callback(result, callback);
×
345
        });
×
346
}
×
347

348
void ActionImpl::shutdown_async(const Action::ResultCallback& callback) const
×
349
{
350
    MavlinkCommandSender::CommandLong command{};
×
351

352
    command.command = MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN;
×
353
    command.params.maybe_param1 = 2.0f; // shutdown autopilot
×
354
    command.params.maybe_param2 = 2.0f; // shutdown onboard computer
×
355
    command.params.maybe_param3 = 2.0f; // shutdown camera
×
356
    command.params.maybe_param4 = 2.0f; // shutdown gimbal
×
357
    command.target_component_id = _system_impl->get_autopilot_id();
×
358

359
    _system_impl->send_command_async(
×
360
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
361
            command_result_callback(result, callback);
×
362
        });
×
363
}
×
364

365
void ActionImpl::takeoff_async(const Action::ResultCallback& callback) const
×
366
{
367
    if (_system_impl->autopilot() == Autopilot::Px4) {
×
368
        takeoff_async_px4(callback);
×
369
    } else {
370
        takeoff_async_apm(callback);
×
371
    }
372
}
×
373

374
void ActionImpl::takeoff_async_px4(const Action::ResultCallback& callback) const
×
375
{
376
    MavlinkCommandSender::CommandLong command{};
×
377

378
    command.command = MAV_CMD_NAV_TAKEOFF;
×
379
    command.target_component_id = _system_impl->get_autopilot_id();
×
380

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

387
void ActionImpl::takeoff_async_apm(const Action::ResultCallback& callback) const
×
388
{
389
    auto send_takeoff_command = [this, callback]() {
×
390
        MavlinkCommandSender::CommandLong command{};
×
391

392
        command.command = MAV_CMD_NAV_TAKEOFF;
×
393
        command.target_component_id = _system_impl->get_autopilot_id();
×
394
        command.params.maybe_param7 = get_takeoff_altitude().second;
×
395

396
        _system_impl->send_command_async(
×
397
            command, [this, callback](MavlinkCommandSender::Result result, float) {
×
398
                command_result_callback(result, callback);
×
399
            });
×
400
    };
×
401
    if (_system_impl->get_flight_mode() != FlightMode::Offboard) {
×
402
        _system_impl->set_flight_mode_async(
×
403
            FlightMode::Offboard,
404
            [callback, send_takeoff_command](MavlinkCommandSender::Result result, float) {
×
405
                Action::Result action_result = action_result_from_command_result(result);
×
406
                if (action_result != Action::Result::Success) {
×
407
                    if (callback) {
×
408
                        callback(action_result);
×
409
                    }
410
                }
411
                send_takeoff_command();
×
412
            });
×
413
    } else {
414
        send_takeoff_command();
×
415
    }
416
}
×
417

418
void ActionImpl::land_async(const Action::ResultCallback& callback) const
×
419
{
420
    MavlinkCommandSender::CommandLong command{};
×
421

422
    command.command = MAV_CMD_NAV_LAND;
×
423
    command.params.maybe_param4 = NAN; // Don't change yaw.
×
424
    command.target_component_id = _system_impl->get_autopilot_id();
×
425

426
    _system_impl->send_command_async(
×
427
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
428
            command_result_callback(result, callback);
×
429
        });
×
430
}
×
431

432
void ActionImpl::return_to_launch_async(const Action::ResultCallback& callback) const
×
433
{
434
    _system_impl->set_flight_mode_async(
×
435
        FlightMode::ReturnToLaunch, [this, callback](MavlinkCommandSender::Result result, float) {
×
436
            command_result_callback(result, callback);
×
437
        });
×
438
}
×
439

440
void ActionImpl::goto_location_async(
×
441
    const double latitude_deg,
442
    const double longitude_deg,
443
    const float altitude_amsl_m,
444
    const float yaw_deg,
445
    const Action::ResultCallback& callback)
446
{
447
    auto send_do_reposition =
×
448
        [this, callback, yaw_deg, latitude_deg, longitude_deg, altitude_amsl_m]() {
×
449
            MavlinkCommandSender::CommandInt command{};
×
450

451
            command.command = MAV_CMD_DO_REPOSITION;
×
452
            command.target_component_id = _system_impl->get_autopilot_id();
×
453
            command.frame = MAV_FRAME_GLOBAL_INT;
×
454
            command.params.maybe_param4 = static_cast<float>(to_rad_from_deg(yaw_deg));
×
455
            command.params.x = int32_t(std::round(latitude_deg * 1e7));
×
456
            command.params.y = int32_t(std::round(longitude_deg * 1e7));
×
457
            command.params.maybe_z = altitude_amsl_m;
×
458

459
            _system_impl->send_command_async(
×
460
                command, [this, callback](MavlinkCommandSender::Result result, float) {
×
461
                    command_result_callback(result, callback);
×
462
                });
×
463
        };
×
464
    FlightMode goto_flight_mode;
465
    if (_system_impl->autopilot() == Autopilot::Px4) {
×
466
        goto_flight_mode = FlightMode::Hold;
×
467
    } else {
468
        goto_flight_mode = FlightMode::Offboard;
×
469
    }
470
    if (_system_impl->get_flight_mode() != goto_flight_mode) {
×
471
        _system_impl->set_flight_mode_async(
×
472
            goto_flight_mode,
473
            [this, callback, send_do_reposition](MavlinkCommandSender::Result result, float) {
×
474
                Action::Result action_result = action_result_from_command_result(result);
×
475
                if (action_result != Action::Result::Success) {
×
476
                    command_result_callback(result, callback);
×
477
                    return;
×
478
                }
479
                send_do_reposition();
×
480
            });
481
        return;
×
482
    }
483

484
    send_do_reposition();
×
485
}
486

487
void ActionImpl::do_orbit_async(
×
488
    const float radius_m,
489
    const float velocity_ms,
490
    const Action::OrbitYawBehavior yaw_behavior,
491
    const double latitude_deg,
492
    const double longitude_deg,
493
    const double absolute_altitude_m,
494
    const Action::ResultCallback& callback)
495
{
496
    MavlinkCommandSender::CommandInt command{};
×
497

498
    command.command = MAV_CMD_DO_ORBIT;
×
499
    command.target_component_id = _system_impl->get_autopilot_id();
×
500
    command.params.maybe_param1 = radius_m;
×
501
    command.params.maybe_param2 = velocity_ms;
×
502
    command.params.maybe_param3 = static_cast<float>(yaw_behavior);
×
503
    command.params.x = int32_t(std::round(latitude_deg * 1e7));
×
504
    command.params.y = int32_t(std::round(longitude_deg * 1e7));
×
505
    command.params.maybe_z = static_cast<float>(absolute_altitude_m);
×
506

507
    _system_impl->send_command_async(
×
508
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
509
            command_result_callback(result, callback);
×
510
        });
×
511
}
×
512

513
void ActionImpl::hold_async(const Action::ResultCallback& callback) const
×
514
{
515
    _system_impl->set_flight_mode_async(
×
516
        FlightMode::Hold, [this, callback](MavlinkCommandSender::Result result, float) {
×
517
            command_result_callback(result, callback);
×
518
        });
×
519
}
×
520

521
void ActionImpl::set_actuator_async(
×
522
    const int index, const float value, const Action::ResultCallback& callback)
523
{
524
    MavlinkCommandSender::CommandLong command{};
×
525
    command.target_component_id = _system_impl->get_autopilot_id();
×
526

527
    if (_system_impl->autopilot() == Autopilot::ArduPilot) {
×
528
        command.command = MAV_CMD_DO_SET_SERVO;
×
529
        command.params.maybe_param1 = static_cast<float>(index);
×
530
        command.params.maybe_param2 = value;
×
531
    } else {
532
        auto zero_based_index = index - 1;
×
533
        if (zero_based_index >= 0) {
×
534
            command.command = MAV_CMD_DO_SET_ACTUATOR;
×
535
            switch (zero_based_index % 6) {
×
536
                case 0:
×
537
                    command.params.maybe_param1 = value;
×
538
                    break;
×
539
                case 1:
×
540
                    command.params.maybe_param2 = value;
×
541
                    break;
×
542
                case 2:
×
543
                    command.params.maybe_param3 = value;
×
544
                    break;
×
545
                case 3:
×
546
                    command.params.maybe_param4 = value;
×
547
                    break;
×
548
                case 4:
×
549
                    command.params.maybe_param5 = value;
×
550
                    break;
×
551
                case 5:
×
552
                    command.params.maybe_param6 = value;
×
553
                    break;
×
554
            }
555
            command.params.maybe_param7 = static_cast<float>(zero_based_index) / 6.0f;
×
556
        }
557
    }
558

559
    _system_impl->send_command_async(
×
560
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
561
            command_result_callback(result, callback);
×
562
        });
×
563
}
×
564

565
void ActionImpl::transition_to_fixedwing_async(const Action::ResultCallback& callback) const
×
566
{
567
    if (!_vtol_transition_support_known) {
×
568
        if (callback) {
×
569
            callback(Action::Result::VtolTransitionSupportUnknown);
×
570
        }
571
        return;
×
572
    }
573

574
    if (!_vtol_transition_possible) {
×
575
        if (callback) {
×
576
            callback(Action::Result::NoVtolTransitionSupport);
×
577
        }
578
        return;
×
579
    }
580

581
    MavlinkCommandSender::CommandLong command{};
×
582

583
    command.command = MAV_CMD_DO_VTOL_TRANSITION;
×
584
    command.params.maybe_param1 = static_cast<float>(MAV_VTOL_STATE_FW);
×
585
    command.target_component_id = _system_impl->get_autopilot_id();
×
586

587
    _system_impl->send_command_async(
×
588
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
589
            command_result_callback(result, callback);
×
590
        });
×
591
}
592

593
void ActionImpl::transition_to_multicopter_async(const Action::ResultCallback& callback) const
×
594
{
595
    if (!_vtol_transition_support_known) {
×
596
        if (callback) {
×
597
            callback(Action::Result::VtolTransitionSupportUnknown);
×
598
        }
599
        return;
×
600
    }
601

602
    if (!_vtol_transition_possible) {
×
603
        if (callback) {
×
604
            callback(Action::Result::NoVtolTransitionSupport);
×
605
        }
606
        return;
×
607
    }
608
    MavlinkCommandSender::CommandLong command{};
×
609

610
    command.command = MAV_CMD_DO_VTOL_TRANSITION;
×
611
    command.params.maybe_param1 = static_cast<float>(MAV_VTOL_STATE_MC);
×
612
    command.target_component_id = _system_impl->get_autopilot_id();
×
613

614
    _system_impl->send_command_async(
×
615
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
616
            command_result_callback(result, callback);
×
617
        });
×
618
}
619

620
void ActionImpl::process_extended_sys_state(const mavlink_message_t& message)
×
621
{
622
    mavlink_extended_sys_state_t extended_sys_state;
×
623
    mavlink_msg_extended_sys_state_decode(&message, &extended_sys_state);
×
624

625
    if (extended_sys_state.vtol_state != MAV_VTOL_STATE_UNDEFINED) {
×
626
        _vtol_transition_possible = true;
×
627
    } else {
628
        _vtol_transition_possible = false;
×
629
    }
630
    _vtol_transition_support_known = true;
×
631
}
×
632

633
void ActionImpl::set_takeoff_altitude_async(
×
634
    const float relative_altitude_m, const Action::ResultCallback& callback)
635
{
636
    callback(set_takeoff_altitude(relative_altitude_m));
×
637
}
×
638

639
Action::Result ActionImpl::set_takeoff_altitude(float relative_altitude_m)
×
640
{
641
    if (_system_impl->autopilot() == Autopilot::Px4) {
×
642
        return set_takeoff_altitude_px4(relative_altitude_m);
×
643
    } else {
644
        return set_takeoff_altitude_apm(relative_altitude_m);
×
645
    }
646
}
647

648
Action::Result ActionImpl::set_takeoff_altitude_px4(float relative_altitude_m)
×
649
{
650
    _takeoff_altitude = relative_altitude_m;
×
651

652
    const MavlinkParameterClient::Result result =
653
        _system_impl->set_param_float(TAKEOFF_ALT_PARAM, relative_altitude_m);
×
654
    return (result == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
655
                                                                 Action::Result::ParameterError;
×
656
}
657

658
Action::Result ActionImpl::set_takeoff_altitude_apm(float relative_altitude_m)
×
659
{
660
    _takeoff_altitude = relative_altitude_m;
×
661
    return Action::Result::Success;
×
662
}
663

664
void ActionImpl::get_takeoff_altitude_async(
×
665
    const Action::GetTakeoffAltitudeCallback& callback) const
666
{
667
    auto altitude_result = get_takeoff_altitude();
×
668
    callback(altitude_result.first, altitude_result.second);
×
669
}
×
670

671
std::pair<Action::Result, float> ActionImpl::get_takeoff_altitude() const
×
672
{
673
    if (_system_impl->autopilot() == Autopilot::ArduPilot) {
×
674
        return std::make_pair<>(Action::Result::Success, _takeoff_altitude);
×
675
    } else {
676
        auto result = _system_impl->get_param_float(TAKEOFF_ALT_PARAM);
×
677
        return std::make_pair<>(
678
            (result.first == MavlinkParameterClient::Result::Success) ?
×
679
                Action::Result::Success :
680
                Action::Result::ParameterError,
681
            result.second);
×
682
    }
683
}
684

685
void ActionImpl::set_maximum_speed_async(
×
686
    const float speed_m_s, const Action::ResultCallback& callback) const
687
{
688
    callback(set_maximum_speed(speed_m_s));
×
689
}
×
690

691
Action::Result ActionImpl::set_maximum_speed(float speed_m_s) const
×
692
{
693
    const MavlinkParameterClient::Result result =
694
        _system_impl->set_param_float(MAX_SPEED_PARAM, speed_m_s);
×
695
    return (result == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
696
                                                                 Action::Result::ParameterError;
×
697
}
698

699
void ActionImpl::get_maximum_speed_async(const Action::GetMaximumSpeedCallback& callback) const
×
700
{
701
    auto speed_result = get_maximum_speed();
×
702
    callback(speed_result.first, speed_result.second);
×
703
}
×
704

705
std::pair<Action::Result, float> ActionImpl::get_maximum_speed() const
×
706
{
707
    auto result = _system_impl->get_param_float(MAX_SPEED_PARAM);
×
708
    return std::make_pair<>(
709
        (result.first == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
710
                                                                    Action::Result::ParameterError,
711
        result.second);
×
712
}
713

714
void ActionImpl::set_return_to_launch_altitude_async(
×
715
    const float relative_altitude_m, const Action::ResultCallback& callback) const
716
{
717
    callback(set_return_to_launch_altitude(relative_altitude_m));
×
718
}
×
719

720
Action::Result ActionImpl::set_return_to_launch_altitude(const float relative_altitude_m) const
×
721
{
722
    const MavlinkParameterClient::Result result =
723
        _system_impl->set_param_float(RTL_RETURN_ALTITUDE_PARAM, relative_altitude_m);
×
724
    return (result == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
725
                                                                 Action::Result::ParameterError;
×
726
}
727

728
void ActionImpl::get_return_to_launch_altitude_async(
×
729
    const Action::GetReturnToLaunchAltitudeCallback& callback) const
730
{
731
    const auto get_result = get_return_to_launch_altitude();
×
732
    callback(get_result.first, get_result.second);
×
733
}
×
734

735
std::pair<Action::Result, float> ActionImpl::get_return_to_launch_altitude() const
×
736
{
737
    auto result = _system_impl->get_param_float(RTL_RETURN_ALTITUDE_PARAM);
×
738
    return std::make_pair<>(
739
        (result.first == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
740
                                                                    Action::Result::ParameterError,
741
        result.second);
×
742
}
743

744
void ActionImpl::set_current_speed_async(float speed_m_s, const Action::ResultCallback& callback)
×
745
{
746
    MavlinkCommandSender::CommandLong command{};
×
747

748
    command.command = MAV_CMD_DO_CHANGE_SPEED;
×
749
    command.params.maybe_param1 = 1.0f; // ground speed
×
750
    command.params.maybe_param2 = speed_m_s;
×
751
    command.params.maybe_param3 = -1.0f; // no throttle set
×
752
    command.params.maybe_param4 = 0.0f; // reserved
×
753
    command.target_component_id = _system_impl->get_autopilot_id();
×
754

755
    _system_impl->send_command_async(
×
756
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
757
            command_result_callback(result, callback);
×
758
        });
×
759
}
×
760

761
Action::Result ActionImpl::set_current_speed(float speed_m_s)
×
762
{
763
    auto prom = std::promise<Action::Result>();
×
764
    auto fut = prom.get_future();
×
765

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

768
    return fut.get();
×
769
}
770

771
Action::Result ActionImpl::action_result_from_command_result(MavlinkCommandSender::Result result)
4✔
772
{
773
    switch (result) {
4✔
774
        case MavlinkCommandSender::Result::Success:
2✔
775
            return Action::Result::Success;
2✔
776
        case MavlinkCommandSender::Result::NoSystem:
×
777
            return Action::Result::NoSystem;
×
778
        case MavlinkCommandSender::Result::ConnectionError:
×
779
            return Action::Result::ConnectionError;
×
780
        case MavlinkCommandSender::Result::Busy:
×
781
            return Action::Result::Busy;
×
782
        case MavlinkCommandSender::Result::Denied:
2✔
783
            // Fallthrough
784
        case MavlinkCommandSender::Result::TemporarilyRejected:
785
            return Action::Result::CommandDenied;
2✔
786
        case MavlinkCommandSender::Result::Failed:
×
787
            return Action::Result::Failed;
×
788
        case MavlinkCommandSender::Result::Timeout:
×
789
            return Action::Result::Timeout;
×
790
        case MavlinkCommandSender::Result::Unsupported:
×
791
            return Action::Result::Unsupported;
×
792
        default:
×
793
            return Action::Result::Unknown;
×
794
    }
795
}
796

797
void ActionImpl::command_result_callback(
4✔
798
    MavlinkCommandSender::Result command_result, const Action::ResultCallback& callback) const
799
{
800
    Action::Result action_result = action_result_from_command_result(command_result);
4✔
801

802
    if (callback) {
4✔
803
        auto temp_callback = callback;
8✔
804
        _system_impl->call_user_callback(
8✔
805
            [temp_callback, action_result]() { temp_callback(action_result); });
806
    }
807
}
4✔
808

809
} // 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

© 2025 Coveralls, Inc