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

mavlink / MAVSDK / 5865961857

pending completion
5865961857

push

github

web-flow
Merge pull request #2113 from trolledbypro/pr-ardupilot-set-servo

Added new code for MAV_CMD_DO_SET_SERVO ArduPilot

25 of 25 new or added lines in 1 file covered. (100.0%)

7794 of 25037 relevant lines covered (31.13%)

22.84 hits per line

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

14.84
/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() == SystemImpl::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() == SystemImpl::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.params.maybe_param4 = static_cast<float>(to_rad_from_deg(yaw_deg));
×
454
            command.params.x = int32_t(std::round(latitude_deg * 1e7));
×
455
            command.params.y = int32_t(std::round(longitude_deg * 1e7));
×
456
            command.params.maybe_z = altitude_amsl_m;
×
457

458
            _system_impl->send_command_async(
×
459
                command, [this, callback](MavlinkCommandSender::Result result, float) {
×
460
                    command_result_callback(result, callback);
×
461
                });
×
462
        };
×
463

464
    // Change to Hold mode first
465
    if (_system_impl->get_flight_mode() != FlightMode::Hold) {
×
466
        _system_impl->set_flight_mode_async(
×
467
            FlightMode::Hold,
468
            [this, callback, send_do_reposition](MavlinkCommandSender::Result result, float) {
×
469
                Action::Result action_result = action_result_from_command_result(result);
×
470
                if (action_result != Action::Result::Success) {
×
471
                    command_result_callback(result, callback);
×
472
                    return;
×
473
                }
474
                send_do_reposition();
×
475
            });
476
        return;
×
477
    }
478

479
    send_do_reposition();
×
480
}
481

482
void ActionImpl::do_orbit_async(
×
483
    const float radius_m,
484
    const float velocity_ms,
485
    const Action::OrbitYawBehavior yaw_behavior,
486
    const double latitude_deg,
487
    const double longitude_deg,
488
    const double absolute_altitude_m,
489
    const Action::ResultCallback& callback)
490
{
491
    MavlinkCommandSender::CommandInt command{};
×
492

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

502
    _system_impl->send_command_async(
×
503
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
504
            command_result_callback(result, callback);
×
505
        });
×
506
}
×
507

508
void ActionImpl::hold_async(const Action::ResultCallback& callback) const
×
509
{
510
    _system_impl->set_flight_mode_async(
×
511
        FlightMode::Hold, [this, callback](MavlinkCommandSender::Result result, float) {
×
512
            command_result_callback(result, callback);
×
513
        });
×
514
}
×
515

516
void ActionImpl::set_actuator_async(
×
517
    const int index, const float value, const Action::ResultCallback& callback)
518
{
519
    MavlinkCommandSender::CommandLong command{};
×
520
    command.target_component_id = _system_impl->get_autopilot_id();
×
521

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

551
    _system_impl->send_command_async(
×
552
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
553
            command_result_callback(result, callback);
×
554
        });
×
555
}
×
556

557
void ActionImpl::transition_to_fixedwing_async(const Action::ResultCallback& callback) const
×
558
{
559
    if (!_vtol_transition_support_known) {
×
560
        if (callback) {
×
561
            callback(Action::Result::VtolTransitionSupportUnknown);
×
562
        }
563
        return;
×
564
    }
565

566
    if (!_vtol_transition_possible) {
×
567
        if (callback) {
×
568
            callback(Action::Result::NoVtolTransitionSupport);
×
569
        }
570
        return;
×
571
    }
572

573
    MavlinkCommandSender::CommandLong command{};
×
574

575
    command.command = MAV_CMD_DO_VTOL_TRANSITION;
×
576
    command.params.maybe_param1 = static_cast<float>(MAV_VTOL_STATE_FW);
×
577
    command.target_component_id = _system_impl->get_autopilot_id();
×
578

579
    _system_impl->send_command_async(
×
580
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
581
            command_result_callback(result, callback);
×
582
        });
×
583
}
584

585
void ActionImpl::transition_to_multicopter_async(const Action::ResultCallback& callback) const
×
586
{
587
    if (!_vtol_transition_support_known) {
×
588
        if (callback) {
×
589
            callback(Action::Result::VtolTransitionSupportUnknown);
×
590
        }
591
        return;
×
592
    }
593

594
    if (!_vtol_transition_possible) {
×
595
        if (callback) {
×
596
            callback(Action::Result::NoVtolTransitionSupport);
×
597
        }
598
        return;
×
599
    }
600
    MavlinkCommandSender::CommandLong command{};
×
601

602
    command.command = MAV_CMD_DO_VTOL_TRANSITION;
×
603
    command.params.maybe_param1 = static_cast<float>(MAV_VTOL_STATE_MC);
×
604
    command.target_component_id = _system_impl->get_autopilot_id();
×
605

606
    _system_impl->send_command_async(
×
607
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
608
            command_result_callback(result, callback);
×
609
        });
×
610
}
611

612
void ActionImpl::process_extended_sys_state(const mavlink_message_t& message)
×
613
{
614
    mavlink_extended_sys_state_t extended_sys_state;
×
615
    mavlink_msg_extended_sys_state_decode(&message, &extended_sys_state);
×
616

617
    if (extended_sys_state.vtol_state != MAV_VTOL_STATE_UNDEFINED) {
×
618
        _vtol_transition_possible = true;
×
619
    } else {
620
        _vtol_transition_possible = false;
×
621
    }
622
    _vtol_transition_support_known = true;
×
623
}
×
624

625
void ActionImpl::set_takeoff_altitude_async(
×
626
    const float relative_altitude_m, const Action::ResultCallback& callback)
627
{
628
    callback(set_takeoff_altitude(relative_altitude_m));
×
629
}
×
630

631
Action::Result ActionImpl::set_takeoff_altitude(float relative_altitude_m)
×
632
{
633
    if (_system_impl->autopilot() == SystemImpl::Autopilot::Px4) {
×
634
        return set_takeoff_altitude_px4(relative_altitude_m);
×
635
    } else {
636
        return set_takeoff_altitude_apm(relative_altitude_m);
×
637
    }
638
}
639

640
Action::Result ActionImpl::set_takeoff_altitude_px4(float relative_altitude_m)
×
641
{
642
    _takeoff_altitude = relative_altitude_m;
×
643

644
    const MavlinkParameterClient::Result result =
645
        _system_impl->set_param_float(TAKEOFF_ALT_PARAM, relative_altitude_m);
×
646
    return (result == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
647
                                                                 Action::Result::ParameterError;
×
648
}
649

650
Action::Result ActionImpl::set_takeoff_altitude_apm(float relative_altitude_m)
×
651
{
652
    _takeoff_altitude = relative_altitude_m;
×
653
    return Action::Result::Success;
×
654
}
655

656
void ActionImpl::get_takeoff_altitude_async(
×
657
    const Action::GetTakeoffAltitudeCallback& callback) const
658
{
659
    auto altitude_result = get_takeoff_altitude();
×
660
    callback(altitude_result.first, altitude_result.second);
×
661
}
×
662

663
std::pair<Action::Result, float> ActionImpl::get_takeoff_altitude() const
×
664
{
665
    if (_system_impl->autopilot() == SystemImpl::Autopilot::ArduPilot) {
×
666
        return std::make_pair<>(Action::Result::Success, _takeoff_altitude);
×
667
    } else {
668
        auto result = _system_impl->get_param_float(TAKEOFF_ALT_PARAM);
×
669
        return std::make_pair<>(
670
            (result.first == MavlinkParameterClient::Result::Success) ?
×
671
                Action::Result::Success :
672
                Action::Result::ParameterError,
673
            result.second);
×
674
    }
675
}
676

677
void ActionImpl::set_maximum_speed_async(
×
678
    const float speed_m_s, const Action::ResultCallback& callback) const
679
{
680
    callback(set_maximum_speed(speed_m_s));
×
681
}
×
682

683
Action::Result ActionImpl::set_maximum_speed(float speed_m_s) const
×
684
{
685
    const MavlinkParameterClient::Result result =
686
        _system_impl->set_param_float(MAX_SPEED_PARAM, speed_m_s);
×
687
    return (result == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
688
                                                                 Action::Result::ParameterError;
×
689
}
690

691
void ActionImpl::get_maximum_speed_async(const Action::GetMaximumSpeedCallback& callback) const
×
692
{
693
    auto speed_result = get_maximum_speed();
×
694
    callback(speed_result.first, speed_result.second);
×
695
}
×
696

697
std::pair<Action::Result, float> ActionImpl::get_maximum_speed() const
×
698
{
699
    auto result = _system_impl->get_param_float(MAX_SPEED_PARAM);
×
700
    return std::make_pair<>(
701
        (result.first == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
702
                                                                    Action::Result::ParameterError,
703
        result.second);
×
704
}
705

706
void ActionImpl::set_return_to_launch_altitude_async(
×
707
    const float relative_altitude_m, const Action::ResultCallback& callback) const
708
{
709
    callback(set_return_to_launch_altitude(relative_altitude_m));
×
710
}
×
711

712
Action::Result ActionImpl::set_return_to_launch_altitude(const float relative_altitude_m) const
×
713
{
714
    const MavlinkParameterClient::Result result =
715
        _system_impl->set_param_float(RTL_RETURN_ALTITUDE_PARAM, relative_altitude_m);
×
716
    return (result == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
717
                                                                 Action::Result::ParameterError;
×
718
}
719

720
void ActionImpl::get_return_to_launch_altitude_async(
×
721
    const Action::GetReturnToLaunchAltitudeCallback& callback) const
722
{
723
    const auto get_result = get_return_to_launch_altitude();
×
724
    callback(get_result.first, get_result.second);
×
725
}
×
726

727
std::pair<Action::Result, float> ActionImpl::get_return_to_launch_altitude() const
×
728
{
729
    auto result = _system_impl->get_param_float(RTL_RETURN_ALTITUDE_PARAM);
×
730
    return std::make_pair<>(
731
        (result.first == MavlinkParameterClient::Result::Success) ? Action::Result::Success :
×
732
                                                                    Action::Result::ParameterError,
733
        result.second);
×
734
}
735

736
void ActionImpl::set_current_speed_async(float speed_m_s, const Action::ResultCallback& callback)
×
737
{
738
    MavlinkCommandSender::CommandLong command{};
×
739

740
    command.command = MAV_CMD_DO_CHANGE_SPEED;
×
741
    command.params.maybe_param1 = 1.0f; // ground speed
×
742
    command.params.maybe_param2 = speed_m_s;
×
743
    command.params.maybe_param3 = -1.0f; // no throttle set
×
744
    command.params.maybe_param4 = 0.0f; // reserved
×
745
    command.target_component_id = _system_impl->get_autopilot_id();
×
746

747
    _system_impl->send_command_async(
×
748
        command, [this, callback](MavlinkCommandSender::Result result, float) {
×
749
            command_result_callback(result, callback);
×
750
        });
×
751
}
×
752

753
Action::Result ActionImpl::set_current_speed(float speed_m_s)
×
754
{
755
    auto prom = std::promise<Action::Result>();
×
756
    auto fut = prom.get_future();
×
757

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

760
    return fut.get();
×
761
}
762

763
Action::Result ActionImpl::action_result_from_command_result(MavlinkCommandSender::Result result)
4✔
764
{
765
    switch (result) {
4✔
766
        case MavlinkCommandSender::Result::Success:
2✔
767
            return Action::Result::Success;
2✔
768
        case MavlinkCommandSender::Result::NoSystem:
×
769
            return Action::Result::NoSystem;
×
770
        case MavlinkCommandSender::Result::ConnectionError:
×
771
            return Action::Result::ConnectionError;
×
772
        case MavlinkCommandSender::Result::Busy:
×
773
            return Action::Result::Busy;
×
774
        case MavlinkCommandSender::Result::Denied:
2✔
775
            // Fallthrough
776
        case MavlinkCommandSender::Result::TemporarilyRejected:
777
            return Action::Result::CommandDenied;
2✔
778
        case MavlinkCommandSender::Result::Failed:
×
779
            return Action::Result::Failed;
×
780
        case MavlinkCommandSender::Result::Timeout:
×
781
            return Action::Result::Timeout;
×
782
        case MavlinkCommandSender::Result::Unsupported:
×
783
            return Action::Result::Unsupported;
×
784
        default:
×
785
            return Action::Result::Unknown;
×
786
    }
787
}
788

789
void ActionImpl::command_result_callback(
4✔
790
    MavlinkCommandSender::Result command_result, const Action::ResultCallback& callback) const
791
{
792
    Action::Result action_result = action_result_from_command_result(command_result);
4✔
793

794
    if (callback) {
4✔
795
        auto temp_callback = callback;
8✔
796
        _system_impl->call_user_callback(
8✔
797
            [temp_callback, action_result]() { temp_callback(action_result); });
798
    }
799
}
4✔
800

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