-
Notifications
You must be signed in to change notification settings - Fork 17k
/
GCS_Common.cpp
5431 lines (4748 loc) · 171 KB
/
GCS_Common.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Common GCS MAVLink functions for all vehicle types
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "GCS.h"
#include <AC_Fence/AC_Fence.h>
#include <AP_AHRS/AP_AHRS.h>
#include <AP_HAL/AP_HAL.h>
#include <AP_Arming/AP_Arming.h>
#include <AP_InternalError/AP_InternalError.h>
#include <AP_Logger/AP_Logger.h>
#include <AP_OpticalFlow/AP_OpticalFlow.h>
#include <AP_Vehicle/AP_Vehicle.h>
#include <AP_RangeFinder/AP_RangeFinder.h>
#include <AP_RangeFinder/AP_RangeFinder_Backend.h>
#include <AP_Airspeed/AP_Airspeed.h>
#include <AP_Camera/AP_Camera.h>
#include <AP_Gripper/AP_Gripper.h>
#include <AC_Sprayer/AC_Sprayer.h>
#include <AP_BLHeli/AP_BLHeli.h>
#include <AP_RSSI/AP_RSSI.h>
#include <AP_RTC/AP_RTC.h>
#include <AP_Scheduler/AP_Scheduler.h>
#include <AP_SerialManager/AP_SerialManager.h>
#include <AP_RCTelemetry/AP_Spektrum_Telem.h>
#include <AP_Mount/AP_Mount.h>
#include <AP_Common/AP_FWVersion.h>
#include <AP_VisualOdom/AP_VisualOdom.h>
#include <AP_OpticalFlow/OpticalFlow.h>
#include <AP_Baro/AP_Baro.h>
#include <AP_EFI/AP_EFI.h>
#include <AP_Proximity/AP_Proximity.h>
#include <AP_Scripting/AP_Scripting.h>
#include <AP_Winch/AP_Winch.h>
#include <AP_OSD/AP_OSD.h>
#include <AP_RCTelemetry/AP_CRSF_Telem.h>
#include <stdio.h>
#if HAL_RCINPUT_WITH_AP_RADIO
#include <AP_Radio/AP_Radio.h>
#include <AP_BoardConfig/AP_BoardConfig.h>
#endif
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
#include <SITL/SITL.h>
#endif
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
#include <AP_CANManager/AP_CANManager.h>
#include <AP_CANManager/AP_CANTester.h>
#include <AP_Common/AP_Common.h>
// To be replaced with macro saying if KDECAN library is included
#if APM_BUILD_TYPE(APM_BUILD_ArduCopter) || APM_BUILD_TYPE(APM_BUILD_ArduPlane) || APM_BUILD_TYPE(APM_BUILD_ArduSub)
#include <AP_KDECAN/AP_KDECAN.h>
#endif
#include <AP_ToshibaCAN/AP_ToshibaCAN.h>
#include <AP_PiccoloCAN/AP_PiccoloCAN.h>
#include <AP_UAVCAN/AP_UAVCAN.h>
#endif
#include <AP_BattMonitor/AP_BattMonitor.h>
#include <AP_GPS/AP_GPS.h>
extern const AP_HAL::HAL& hal;
struct GCS_MAVLINK::LastRadioStatus GCS_MAVLINK::last_radio_status;
uint8_t GCS_MAVLINK::mavlink_active = 0;
uint8_t GCS_MAVLINK::chan_is_streaming = 0;
uint32_t GCS_MAVLINK::reserve_param_space_start_ms;
// private channels are ones used for point-to-point protocols, and
// don't get broadcasts or fwded packets
uint8_t GCS_MAVLINK::mavlink_private = 0;
GCS *GCS::_singleton = nullptr;
GCS_MAVLINK::GCS_MAVLINK(GCS_MAVLINK_Parameters ¶meters,
AP_HAL::UARTDriver &uart)
{
_port = &uart;
streamRates = parameters.streamRates;
}
bool GCS_MAVLINK::init(uint8_t instance)
{
// search for serial port
const AP_SerialManager& serial_manager = AP::serialmanager();
const AP_SerialManager::SerialProtocol protocol = AP_SerialManager::SerialProtocol_MAVLink;
// get associated mavlink channel
if (!serial_manager.get_mavlink_channel(protocol, instance, chan)) {
// return immediately in unlikely case mavlink channel cannot be found
return false;
}
// and init the gcs instance
if (!valid_channel(chan)) {
return false;
}
if (!serial_manager.should_forward_mavlink_telemetry(protocol, instance)) {
set_channel_private(chan);
}
/*
Now try to cope with SiK radios that may be stuck in bootloader
mode because CTS was held while powering on. This tells the
bootloader to wait for a firmware. It affects any SiK radio with
CTS connected that is externally powered. To cope we send 0x30
0x20 at 115200 on startup, which tells the bootloader to reset
and boot normally
*/
_port->begin(115200);
AP_HAL::UARTDriver::flow_control old_flow_control = _port->get_flow_control();
_port->set_flow_control(AP_HAL::UARTDriver::FLOW_CONTROL_DISABLE);
for (uint8_t i=0; i<3; i++) {
hal.scheduler->delay(1);
_port->write(0x30);
_port->write(0x20);
}
// since tcdrain() and TCSADRAIN may not be implemented...
hal.scheduler->delay(1);
_port->set_flow_control(old_flow_control);
// now change back to desired baudrate
_port->begin(serial_manager.find_baudrate(protocol, instance));
mavlink_comm_port[chan] = _port;
// create performance counters
snprintf(_perf_packet_name, sizeof(_perf_packet_name), "GCS_Packet_%u", chan);
_perf_packet = hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, _perf_packet_name);
snprintf(_perf_update_name, sizeof(_perf_update_name), "GCS_Update_%u", chan);
_perf_update = hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, _perf_update_name);
AP_SerialManager::SerialProtocol mavlink_protocol = serial_manager.get_mavlink_protocol(chan);
mavlink_status_t *status = mavlink_get_channel_status(chan);
if (status == nullptr) {
return false;
}
if (mavlink_protocol == AP_SerialManager::SerialProtocol_MAVLink2) {
// load signing key
load_signing_key();
if (status->signing == nullptr) {
// if signing is off start by sending MAVLink1.
status->flags |= MAVLINK_STATUS_FLAG_OUT_MAVLINK1;
}
} else if (status) {
// user has asked to only send MAVLink1
status->flags |= MAVLINK_STATUS_FLAG_OUT_MAVLINK1;
}
if (chan == MAVLINK_COMM_0) {
// Always start with MAVLink1 on first port for now, to allow for recovery
// after experiments with MAVLink2
status->flags |= MAVLINK_STATUS_FLAG_OUT_MAVLINK1;
}
return true;
}
void GCS_MAVLINK::send_meminfo(void)
{
unsigned __brkval = 0;
uint32_t memory = hal.util->available_memory();
mavlink_msg_meminfo_send(chan, __brkval, MIN(memory, 0xFFFFU), memory);
}
// report power supply status
void GCS_MAVLINK::send_power_status(void)
{
if (!gcs().vehicle_initialised()) {
// avoid unnecessary errors being reported to user
return;
}
mavlink_msg_power_status_send(chan,
hal.analogin->board_voltage() * 1000,
hal.analogin->servorail_voltage() * 1000,
hal.analogin->power_status_flags());
}
void GCS_MAVLINK::send_battery_status(const uint8_t instance) const
{
// catch the battery backend not supporting the required number of cells
static_assert(sizeof(AP_BattMonitor::cells) >= (sizeof(uint16_t) * MAVLINK_MSG_BATTERY_STATUS_FIELD_VOLTAGES_LEN),
"Not enough battery cells for the MAVLink message");
const AP_BattMonitor &battery = AP::battery();
float temp;
bool got_temperature = battery.get_temperature(temp, instance);
// prepare arrays of individual cell voltages
uint16_t cell_volts[MAVLINK_MSG_BATTERY_STATUS_FIELD_VOLTAGES_LEN];
uint16_t cell_volts_ext[MAVLINK_MSG_BATTERY_STATUS_FIELD_VOLTAGES_EXT_LEN];
if (battery.has_cell_voltages(instance)) {
const AP_BattMonitor::cells& batt_cells = battery.get_cell_voltages(instance);
// copy the first 10 cells
memcpy(cell_volts, batt_cells.cells, sizeof(cell_volts));
// 11 ... 14 use a second cell_volts_ext array
for (uint8_t i = 0; i < MAVLINK_MSG_BATTERY_STATUS_FIELD_VOLTAGES_EXT_LEN; i++) {
if (MAVLINK_MSG_BATTERY_STATUS_FIELD_VOLTAGES_LEN+i < uint8_t(ARRAY_SIZE(batt_cells.cells))) {
cell_volts_ext[i] = batt_cells.cells[MAVLINK_MSG_BATTERY_STATUS_FIELD_VOLTAGES_LEN+i];
} else {
cell_volts_ext[i] = 0;
}
}
} else {
// for battery monitors that cannot provide voltages for individual cells the battery's total voltage is put into the first cell
// if the total voltage cannot fit into a single field, the remainder into subsequent fields.
// the GCS can then recover the pack voltage by summing all non ignored cell values an we can report a pack up to 655.34 V
float voltage = battery.voltage(instance) * 1e3f;
for (uint8_t i = 0; i < MAVLINK_MSG_BATTERY_STATUS_FIELD_VOLTAGES_LEN; i++) {
if (voltage < 0.001f) {
// too small to send to the GCS, set it to the no cell value
cell_volts[i] = UINT16_MAX;
} else {
cell_volts[i] = MIN(voltage, 65534.0f); // Can't send more then UINT16_MAX - 1 in a cell
voltage -= 65534.0f;
}
}
for (uint8_t i = 0; i < MAVLINK_MSG_BATTERY_STATUS_FIELD_VOLTAGES_EXT_LEN; i++) {
cell_volts_ext[i] = 0;
}
}
float current, consumed_mah, consumed_wh;
if (battery.current_amps(current, instance)) {
current = constrain_float(current * 100,-INT16_MAX,INT16_MAX);
} else {
current = -1;
}
if (!battery.consumed_mah(consumed_mah, instance)) {
consumed_mah = -1;
}
if (battery.consumed_wh(consumed_wh, instance)) {
consumed_wh *= 36;
} else {
consumed_wh = -1;
}
mavlink_msg_battery_status_send(chan,
instance, // id
MAV_BATTERY_FUNCTION_UNKNOWN, // function
MAV_BATTERY_TYPE_UNKNOWN, // type
got_temperature ? ((int16_t) (temp * 100)) : INT16_MAX, // temperature. INT16_MAX if unknown
cell_volts, // cell voltages
current, // current in centiampere
consumed_mah, // total consumed current in milliampere.hour
consumed_wh, // consumed energy in hJ (hecto-Joules)
battery.capacity_remaining_pct(instance),
0, // time remaining, seconds (not provided)
MAV_BATTERY_CHARGE_STATE_UNDEFINED,
cell_volts_ext); // Cell 11..14 voltages
}
// returns true if all battery instances were reported
bool GCS_MAVLINK::send_battery_status()
{
const AP_BattMonitor &battery = AP::battery();
for(uint8_t i = 0; i < AP_BATT_MONITOR_MAX_INSTANCES; i++) {
const uint8_t battery_id = (last_battery_status_idx + 1) % AP_BATT_MONITOR_MAX_INSTANCES;
if (battery.get_type(battery_id) != AP_BattMonitor::Type::NONE) {
CHECK_PAYLOAD_SIZE(BATTERY_STATUS);
send_battery_status(battery_id);
last_battery_status_idx = battery_id;
return true;
} else {
last_battery_status_idx = battery_id;
}
}
return true;
}
void GCS_MAVLINK::send_distance_sensor(const AP_RangeFinder_Backend *sensor, const uint8_t instance) const
{
if (!sensor->has_data()) {
return;
}
mavlink_msg_distance_sensor_send(
chan,
AP_HAL::millis(), // time since system boot TODO: take time of measurement
sensor->min_distance_cm(), // minimum distance the sensor can measure in centimeters
sensor->max_distance_cm(), // maximum distance the sensor can measure in centimeters
sensor->distance_cm(), // current distance reading
sensor->get_mav_distance_sensor_type(), // type from MAV_DISTANCE_SENSOR enum
instance, // onboard ID of the sensor == instance
sensor->orientation(), // direction the sensor faces from MAV_SENSOR_ORIENTATION enum
0, // Measurement covariance in centimeters, 0 for unknown / invalid readings
0, // horizontal FOV
0, // vertical FOV
(const float *)nullptr, // quaternion of sensor orientation for MAV_SENSOR_ROTATION_CUSTOM
0); // Signal quality of the sensor. 0 = unknown/unset signal quality, 1 = invalid signal, 100 = perfect signal.
}
// send any and all distance_sensor messages. This starts by sending
// any distance sensors not used by a Proximity sensor, then sends the
// proximity sensor ones.
void GCS_MAVLINK::send_distance_sensor()
{
RangeFinder *rangefinder = RangeFinder::get_singleton();
if (rangefinder == nullptr) {
return;
}
// if we have a proximity backend that utilizes rangefinders cull
// sending them here, and allow the later proximity code to manage
// them
bool filter_possible_proximity_sensors = false;
#if HAL_PROXIMITY_ENABLED
AP_Proximity *proximity = AP_Proximity::get_singleton();
if (proximity != nullptr) {
for (uint8_t i = 0; i < proximity->num_sensors(); i++) {
if (proximity->get_type(i) == AP_Proximity::Type::RangeFinder) {
filter_possible_proximity_sensors = true;
}
}
}
#endif
for (uint8_t i = 0; i < RANGEFINDER_MAX_INSTANCES; i++) {
if (!HAVE_PAYLOAD_SPACE(chan, DISTANCE_SENSOR)) {
return;
}
AP_RangeFinder_Backend *sensor = rangefinder->get_backend(i);
if (sensor == nullptr) {
continue;
}
enum Rotation orient = sensor->orientation();
if (!filter_possible_proximity_sensors ||
(orient > ROTATION_YAW_315 && orient != ROTATION_PITCH_90)) {
send_distance_sensor(sensor, i);
}
}
send_proximity();
}
void GCS_MAVLINK::send_rangefinder() const
{
RangeFinder *rangefinder = RangeFinder::get_singleton();
if (rangefinder == nullptr) {
return;
}
AP_RangeFinder_Backend *s = rangefinder->find_instance(ROTATION_PITCH_270);
if (s == nullptr) {
return;
}
mavlink_msg_rangefinder_send(
chan,
s->distance_cm() * 0.01f,
s->voltage_mv() * 0.001f);
}
void GCS_MAVLINK::send_proximity()
{
#if HAL_PROXIMITY_ENABLED
AP_Proximity *proximity = AP_Proximity::get_singleton();
if (proximity == nullptr) {
return; // this is wrong, but pretend we sent data and don't requeue
}
// get min/max distances
const uint16_t dist_min = (uint16_t)(proximity->distance_min() * 100.0f); // minimum distance the sensor can measure in centimeters
const uint16_t dist_max = (uint16_t)(proximity->distance_max() * 100.0f); // maximum distance the sensor can measure in centimeters
// send horizontal distances
if (proximity->get_status() == AP_Proximity::Status::Good) {
AP_Proximity::Proximity_Distance_Array dist_array;
if (proximity->get_horizontal_distances(dist_array)) {
for (uint8_t i = 0; i < PROXIMITY_MAX_DIRECTION; i++) {
if (!HAVE_PAYLOAD_SPACE(chan, DISTANCE_SENSOR)) {
return;
}
if (dist_array.valid(i)) {
proximity_ever_valid_bitmask |= (1U << i);
} else if (!(proximity_ever_valid_bitmask & (1U << i))) {
// we've never sent this distance out, so we don't
// need to send an invalid one.
continue;
}
mavlink_msg_distance_sensor_send(
chan,
AP_HAL::millis(), // time since system boot
dist_min, // minimum distance the sensor can measure in centimeters
dist_max, // maximum distance the sensor can measure in centimeters
(uint16_t)(dist_array.distance[i] * 100.0f), // current distance reading
MAV_DISTANCE_SENSOR_LASER, // type from MAV_DISTANCE_SENSOR enum
PROXIMITY_SENSOR_ID_START + i, // onboard ID of the sensor
dist_array.orientation[i], // direction the sensor faces from MAV_SENSOR_ORIENTATION enum
0, // Measurement covariance in centimeters, 0 for unknown / invalid readings
0, 0, nullptr, 0);
}
}
}
// send upward distance
float dist_up;
if (proximity->get_upward_distance(dist_up)) {
if (!HAVE_PAYLOAD_SPACE(chan, DISTANCE_SENSOR)) {
return;
}
mavlink_msg_distance_sensor_send(
chan,
AP_HAL::millis(), // time since system boot
dist_min, // minimum distance the sensor can measure in centimeters
dist_max, // maximum distance the sensor can measure in centimeters
(uint16_t)(dist_up * 100.0f), // current distance reading
MAV_DISTANCE_SENSOR_LASER, // type from MAV_DISTANCE_SENSOR enum
PROXIMITY_SENSOR_ID_START + PROXIMITY_MAX_DIRECTION + 1, // onboard ID of the sensor
MAV_SENSOR_ROTATION_PITCH_90, // direction upwards
0, // Measurement covariance in centimeters, 0 for unknown / invalid readings
0, 0, nullptr, 0);
}
#endif // HAL_PROXIMITY_ENABLED
}
// report AHRS2 state
void GCS_MAVLINK::send_ahrs2()
{
#if AP_AHRS_NAVEKF_AVAILABLE
const AP_AHRS &ahrs = AP::ahrs();
Vector3f euler;
struct Location loc {};
// we want one or both of these, use | to avoid short-circuiting:
if (ahrs.get_secondary_attitude(euler) |
ahrs.get_secondary_position(loc)) {
mavlink_msg_ahrs2_send(chan,
euler.x,
euler.y,
euler.z,
loc.alt*1.0e-2f,
loc.lat,
loc.lng);
}
#endif
}
MissionItemProtocol *GCS::get_prot_for_mission_type(const MAV_MISSION_TYPE mission_type) const
{
switch (mission_type) {
case MAV_MISSION_TYPE_MISSION:
return _missionitemprotocol_waypoints;
case MAV_MISSION_TYPE_RALLY:
return _missionitemprotocol_rally;
case MAV_MISSION_TYPE_FENCE:
return _missionitemprotocol_fence;
default:
return nullptr;
}
}
// handle a request for the number of items we have stored for a mission type:
void GCS_MAVLINK::handle_mission_request_list(const mavlink_message_t &msg)
{
// decode
mavlink_mission_request_list_t packet;
mavlink_msg_mission_request_list_decode(&msg, &packet);
MissionItemProtocol *prot = gcs().get_prot_for_mission_type((MAV_MISSION_TYPE)packet.mission_type);
if (prot == nullptr) {
mavlink_msg_mission_ack_send(chan,
msg.sysid,
msg.compid,
MAV_MISSION_UNSUPPORTED,
packet.mission_type);
return;
}
prot->handle_mission_request_list(*this, packet, msg);
}
/*
handle a MISSION_REQUEST mavlink packet
*/
void GCS_MAVLINK::handle_mission_request_int(const mavlink_message_t &msg) const
{
// decode
mavlink_mission_request_int_t packet;
mavlink_msg_mission_request_int_decode(&msg, &packet);
MissionItemProtocol *prot = gcs().get_prot_for_mission_type((MAV_MISSION_TYPE)packet.mission_type);
if (prot == nullptr) {
return;
}
prot->handle_mission_request_int(*this, packet, msg);
}
void GCS_MAVLINK::handle_mission_request(const mavlink_message_t &msg) const
{
// decode
mavlink_mission_request_t packet;
mavlink_msg_mission_request_decode(&msg, &packet);
MissionItemProtocol *prot = gcs().get_prot_for_mission_type((MAV_MISSION_TYPE)packet.mission_type);
if (prot == nullptr) {
return;
}
prot->handle_mission_request(*this, packet, msg);
}
/*
handle a MISSION_SET_CURRENT mavlink packet
Note that there exists a relatively new mavlink DO command,
MAV_CMD_DO_SET_MISSION_CURRENT which provides an acknowledgement
that the command has been received, rather than the GCS having to
rely on getting back an identical sequence number as some currently
do.
*/
void GCS_MAVLINK::handle_mission_set_current(AP_Mission &mission, const mavlink_message_t &msg)
{
// decode
mavlink_mission_set_current_t packet;
mavlink_msg_mission_set_current_decode(&msg, &packet);
// set current command
if (mission.set_current_cmd(packet.seq)) {
// because MISSION_SET_CURRENT is a message not a command,
// there is not ACK associated with us successfully changing
// our waypoint. Some GCSs use the fact we return exactly the
// same mission sequence number in this packet as an ACK - so
// if they send a MISSION_SET_CURRENT with seq number of 4
// then they expect to receive a MISSION_CURRENT message with
// exactly that sequence number in it, even if ArduPilot never
// actually holds that as a sequence number (e.g. packet.seq==0).
if (HAVE_PAYLOAD_SPACE(chan, MISSION_CURRENT)) {
mavlink_msg_mission_current_send(chan, packet.seq);
} else {
// schedule it for later:
send_message(MSG_CURRENT_WAYPOINT);
}
}
}
/*
handle a MISSION_COUNT mavlink packet
*/
void GCS_MAVLINK::handle_mission_count(const mavlink_message_t &msg)
{
// decode
mavlink_mission_count_t packet;
mavlink_msg_mission_count_decode(&msg, &packet);
MissionItemProtocol *prot = gcs().get_prot_for_mission_type((MAV_MISSION_TYPE)packet.mission_type);
if (prot == nullptr) {
mavlink_msg_mission_ack_send(chan,
msg.sysid,
msg.compid,
MAV_MISSION_UNSUPPORTED,
packet.mission_type);
return;
}
prot->handle_mission_count(*this, packet, msg);
}
/*
handle a MISSION_CLEAR_ALL mavlink packet
*/
void GCS_MAVLINK::handle_mission_clear_all(const mavlink_message_t &msg) const
{
// decode
mavlink_mission_clear_all_t packet;
mavlink_msg_mission_clear_all_decode(&msg, &packet);
const MAV_MISSION_TYPE mission_type = (MAV_MISSION_TYPE)packet.mission_type;
MissionItemProtocol *prot = gcs().get_prot_for_mission_type(mission_type);
if (prot == nullptr) {
send_mission_ack(msg, mission_type, MAV_MISSION_UNSUPPORTED);
return;
}
prot->handle_mission_clear_all(*this, msg);
}
bool GCS_MAVLINK::requesting_mission_items() const
{
for (uint8_t i=0; i<ARRAY_SIZE(supported_mission_types); i++) {
MissionItemProtocol *prot = gcs().get_prot_for_mission_type(supported_mission_types[i]);
if (prot && prot->receiving && prot->active_link_is(this)) {
return true;
}
}
return false;
}
void GCS_MAVLINK::handle_mission_write_partial_list(const mavlink_message_t &msg)
{
// decode
mavlink_mission_write_partial_list_t packet;
mavlink_msg_mission_write_partial_list_decode(&msg, &packet);
MissionItemProtocol *use_prot = gcs().get_prot_for_mission_type((MAV_MISSION_TYPE)packet.mission_type);
if (use_prot == nullptr) {
send_mission_ack(msg, (MAV_MISSION_TYPE)packet.mission_type, MAV_MISSION_UNSUPPORTED);
return;
}
use_prot->handle_mission_write_partial_list(*this, msg, packet);
}
/*
pass mavlink messages to the AP_Mount singleton
*/
void GCS_MAVLINK::handle_mount_message(const mavlink_message_t &msg)
{
#if HAL_MOUNT_ENABLED
AP_Mount *mount = AP::mount();
if (mount == nullptr) {
return;
}
mount->handle_message(chan, msg);
#endif
}
/*
pass parameter value messages through to mount library
*/
void GCS_MAVLINK::handle_param_value(const mavlink_message_t &msg)
{
#if HAL_MOUNT_ENABLED
AP_Mount *mount = AP::mount();
if (mount == nullptr) {
return;
}
mount->handle_param_value(msg);
#endif
}
void GCS_MAVLINK::send_text(MAV_SEVERITY severity, const char *fmt, ...) const
{
va_list arg_list;
va_start(arg_list, fmt);
gcs().send_textv(severity, fmt, arg_list, (1<<chan));
va_end(arg_list);
}
float GCS_MAVLINK::telemetry_radio_rssi()
{
if (AP_HAL::millis() - last_radio_status.received_ms > 5000) {
// telemetry radio has disappeared?!
return 0;
}
if (last_radio_status.rssi == 255) {
// see RADIO_STATUS packet definition
return 0;
}
return last_radio_status.rssi/254.0f;
}
void GCS_MAVLINK::handle_radio_status(const mavlink_message_t &msg, bool log_radio)
{
mavlink_radio_t packet;
mavlink_msg_radio_decode(&msg, &packet);
const uint32_t now = AP_HAL::millis();
last_radio_status.received_ms = now;
last_radio_status.rssi = packet.rssi;
// record if the GCS has been receiving radio messages from
// the aircraft
if (packet.remrssi != 0) {
last_radio_status.remrssi_ms = now;
}
last_txbuf = packet.txbuf;
// use the state of the transmit buffer in the radio to
// control the stream rate, giving us adaptive software
// flow control
if (packet.txbuf < 20 && stream_slowdown_ms < 2000) {
// we are very low on space - slow down a lot
stream_slowdown_ms += 60;
} else if (packet.txbuf < 50 && stream_slowdown_ms < 2000) {
// we are a bit low on space, slow down slightly
stream_slowdown_ms += 20;
} else if (packet.txbuf > 95 && stream_slowdown_ms > 200) {
// the buffer has plenty of space, speed up a lot
stream_slowdown_ms -= 40;
} else if (packet.txbuf > 90 && stream_slowdown_ms != 0) {
// the buffer has enough space, speed up a bit
stream_slowdown_ms -= 20;
}
#if GCS_DEBUG_SEND_MESSAGE_TIMINGS
if (stream_slowdown_ms > max_slowdown_ms) {
max_slowdown_ms = stream_slowdown_ms;
}
#endif
//log rssi, noise, etc if logging Performance monitoring data
if (log_radio) {
AP::logger().Write_Radio(packet);
}
}
/*
handle an incoming mission item
return true if this is the last mission item, otherwise false
*/
void GCS_MAVLINK::handle_mission_item(const mavlink_message_t &msg)
{
// TODO: rename packet to mission_item_int
mavlink_mission_item_int_t packet;
if (msg.msgid == MAVLINK_MSG_ID_MISSION_ITEM) {
mavlink_mission_item_t mission_item;
mavlink_msg_mission_item_decode(&msg, &mission_item);
MAV_MISSION_RESULT ret = AP_Mission::convert_MISSION_ITEM_to_MISSION_ITEM_INT(mission_item, packet);
if (ret != MAV_MISSION_ACCEPTED) {
const MAV_MISSION_TYPE type = (MAV_MISSION_TYPE)packet.mission_type;
send_mission_ack(msg, type, ret);
return;
}
} else {
mavlink_msg_mission_item_int_decode(&msg, &packet);
}
const uint8_t current = packet.current;
const MAV_MISSION_TYPE type = (MAV_MISSION_TYPE)packet.mission_type;
if (type == MAV_MISSION_TYPE_MISSION && (current == 2 || current == 3)) {
struct AP_Mission::Mission_Command cmd = {};
MAV_MISSION_RESULT result = AP_Mission::mavlink_int_to_mission_cmd(packet, cmd);
if (result != MAV_MISSION_ACCEPTED) {
//decode failed
send_mission_ack(msg, MAV_MISSION_TYPE_MISSION, result);
return;
}
// guided or change-alt
if (current == 2) {
// current = 2 is a flag to tell us this is a "guided mode"
// waypoint and not for the mission
result = (handle_guided_request(cmd) ? MAV_MISSION_ACCEPTED
: MAV_MISSION_ERROR) ;
} else if (current == 3) {
//current = 3 is a flag to tell us this is a alt change only
// add home alt if needed
handle_change_alt_request(cmd);
// verify we recevied the command
result = MAV_MISSION_ACCEPTED;
}
send_mission_ack(msg, MAV_MISSION_TYPE_MISSION, result);
return;
}
// not a guided-mode reqest
MissionItemProtocol *prot = gcs().get_prot_for_mission_type(type);
if (prot == nullptr) {
send_mission_ack(msg, type, MAV_MISSION_UNSUPPORTED);
return;
}
if (!prot->receiving) {
send_mission_ack(msg, type, MAV_MISSION_ERROR);
return;
}
prot->handle_mission_item(msg, packet);
}
ap_message GCS_MAVLINK::mavlink_id_to_ap_message_id(const uint32_t mavlink_id) const
{
// MSG_NEXT_MISSION_REQUEST doesn't correspond to a mavlink message directly.
// It is used to request the next waypoint after receiving one.
// MSG_NEXT_PARAM doesn't correspond to a mavlink message directly.
// It is used to send the next parameter in a stream after sending one
// MSG_NAMED_FLOAT messages can't really be "streamed"...
static const struct {
uint32_t mavlink_id;
ap_message msg_id;
} map[] {
{ MAVLINK_MSG_ID_HEARTBEAT, MSG_HEARTBEAT},
{ MAVLINK_MSG_ID_ATTITUDE, MSG_ATTITUDE},
{ MAVLINK_MSG_ID_ATTITUDE_QUATERNION, MSG_ATTITUDE_QUATERNION},
{ MAVLINK_MSG_ID_GLOBAL_POSITION_INT, MSG_LOCATION},
{ MAVLINK_MSG_ID_HOME_POSITION, MSG_HOME},
{ MAVLINK_MSG_ID_GPS_GLOBAL_ORIGIN, MSG_ORIGIN},
{ MAVLINK_MSG_ID_SYS_STATUS, MSG_SYS_STATUS},
{ MAVLINK_MSG_ID_POWER_STATUS, MSG_POWER_STATUS},
{ MAVLINK_MSG_ID_MEMINFO, MSG_MEMINFO},
{ MAVLINK_MSG_ID_NAV_CONTROLLER_OUTPUT, MSG_NAV_CONTROLLER_OUTPUT},
{ MAVLINK_MSG_ID_MISSION_CURRENT, MSG_CURRENT_WAYPOINT},
{ MAVLINK_MSG_ID_VFR_HUD, MSG_VFR_HUD},
{ MAVLINK_MSG_ID_SERVO_OUTPUT_RAW, MSG_SERVO_OUTPUT_RAW},
{ MAVLINK_MSG_ID_RC_CHANNELS, MSG_RC_CHANNELS},
{ MAVLINK_MSG_ID_RC_CHANNELS_RAW, MSG_RC_CHANNELS_RAW},
{ MAVLINK_MSG_ID_RAW_IMU, MSG_RAW_IMU},
{ MAVLINK_MSG_ID_SCALED_IMU, MSG_SCALED_IMU},
{ MAVLINK_MSG_ID_SCALED_IMU2, MSG_SCALED_IMU2},
{ MAVLINK_MSG_ID_SCALED_IMU3, MSG_SCALED_IMU3},
{ MAVLINK_MSG_ID_SCALED_PRESSURE, MSG_SCALED_PRESSURE},
{ MAVLINK_MSG_ID_SCALED_PRESSURE2, MSG_SCALED_PRESSURE2},
{ MAVLINK_MSG_ID_SCALED_PRESSURE3, MSG_SCALED_PRESSURE3},
{ MAVLINK_MSG_ID_SENSOR_OFFSETS, MSG_SENSOR_OFFSETS},
{ MAVLINK_MSG_ID_GPS_RAW_INT, MSG_GPS_RAW},
{ MAVLINK_MSG_ID_GPS_RTK, MSG_GPS_RTK},
{ MAVLINK_MSG_ID_GPS2_RAW, MSG_GPS2_RAW},
{ MAVLINK_MSG_ID_GPS2_RTK, MSG_GPS2_RTK},
{ MAVLINK_MSG_ID_SYSTEM_TIME, MSG_SYSTEM_TIME},
{ MAVLINK_MSG_ID_RC_CHANNELS_SCALED, MSG_SERVO_OUT},
{ MAVLINK_MSG_ID_PARAM_VALUE, MSG_NEXT_PARAM},
{ MAVLINK_MSG_ID_FENCE_STATUS, MSG_FENCE_STATUS},
{ MAVLINK_MSG_ID_AHRS, MSG_AHRS},
{ MAVLINK_MSG_ID_SIMSTATE, MSG_SIMSTATE},
{ MAVLINK_MSG_ID_SIM_STATE, MSG_SIM_STATE},
{ MAVLINK_MSG_ID_AHRS2, MSG_AHRS2},
{ MAVLINK_MSG_ID_HWSTATUS, MSG_HWSTATUS},
{ MAVLINK_MSG_ID_WIND, MSG_WIND},
{ MAVLINK_MSG_ID_RANGEFINDER, MSG_RANGEFINDER},
{ MAVLINK_MSG_ID_DISTANCE_SENSOR, MSG_DISTANCE_SENSOR},
// request also does report:
{ MAVLINK_MSG_ID_TERRAIN_REQUEST, MSG_TERRAIN},
{ MAVLINK_MSG_ID_BATTERY2, MSG_BATTERY2},
{ MAVLINK_MSG_ID_CAMERA_FEEDBACK, MSG_CAMERA_FEEDBACK},
{ MAVLINK_MSG_ID_MOUNT_STATUS, MSG_MOUNT_STATUS},
{ MAVLINK_MSG_ID_OPTICAL_FLOW, MSG_OPTICAL_FLOW},
{ MAVLINK_MSG_ID_GIMBAL_REPORT, MSG_GIMBAL_REPORT},
{ MAVLINK_MSG_ID_MAG_CAL_PROGRESS, MSG_MAG_CAL_PROGRESS},
{ MAVLINK_MSG_ID_MAG_CAL_REPORT, MSG_MAG_CAL_REPORT},
{ MAVLINK_MSG_ID_EKF_STATUS_REPORT, MSG_EKF_STATUS_REPORT},
{ MAVLINK_MSG_ID_LOCAL_POSITION_NED, MSG_LOCAL_POSITION},
{ MAVLINK_MSG_ID_PID_TUNING, MSG_PID_TUNING},
{ MAVLINK_MSG_ID_VIBRATION, MSG_VIBRATION},
{ MAVLINK_MSG_ID_RPM, MSG_RPM},
{ MAVLINK_MSG_ID_MISSION_ITEM_REACHED, MSG_MISSION_ITEM_REACHED},
{ MAVLINK_MSG_ID_POSITION_TARGET_GLOBAL_INT, MSG_POSITION_TARGET_GLOBAL_INT},
{ MAVLINK_MSG_ID_POSITION_TARGET_LOCAL_NED, MSG_POSITION_TARGET_LOCAL_NED},
{ MAVLINK_MSG_ID_ADSB_VEHICLE, MSG_ADSB_VEHICLE},
{ MAVLINK_MSG_ID_BATTERY_STATUS, MSG_BATTERY_STATUS},
{ MAVLINK_MSG_ID_AOA_SSA, MSG_AOA_SSA},
{ MAVLINK_MSG_ID_DEEPSTALL, MSG_LANDING},
{ MAVLINK_MSG_ID_EXTENDED_SYS_STATE, MSG_EXTENDED_SYS_STATE},
{ MAVLINK_MSG_ID_AUTOPILOT_VERSION, MSG_AUTOPILOT_VERSION},
{ MAVLINK_MSG_ID_EFI_STATUS, MSG_EFI_STATUS},
{ MAVLINK_MSG_ID_GENERATOR_STATUS, MSG_GENERATOR_STATUS},
{ MAVLINK_MSG_ID_WINCH_STATUS, MSG_WINCH_STATUS},
};
for (uint8_t i=0; i<ARRAY_SIZE(map); i++) {
if (map[i].mavlink_id == mavlink_id) {
return map[i].msg_id;
}
}
return MSG_LAST;
}
bool GCS_MAVLINK::set_mavlink_message_id_interval(const uint32_t mavlink_id,
const uint16_t interval_ms)
{
const ap_message id = mavlink_id_to_ap_message_id(mavlink_id);
if (id == MSG_LAST) {
gcs().send_text(MAV_SEVERITY_INFO, "No ap_message for mavlink id (%u)", (unsigned int)mavlink_id);
return false;
}
return set_ap_message_interval(id, interval_ms);
}
bool GCS_MAVLINK::should_send_message_in_delay_callback(const ap_message id) const
{
// No ID we return true for may take more than a few hundred
// microseconds to return!
if (in_hil_mode()) {
// in HIL we need to keep sending servo values to ensure
// the simulator doesn't pause, otherwise our sensor
// calibration could stall
if (id == MSG_SERVO_OUT ||
id == MSG_SERVO_OUTPUT_RAW) {
return true;
}
}
switch (id) {
case MSG_HEARTBEAT:
case MSG_NEXT_PARAM:
case MSG_AUTOPILOT_VERSION:
return true;
default:
return false;
}
return false;
}
uint16_t GCS_MAVLINK::get_reschedule_interval_ms(const deferred_message_bucket_t &deferred) const
{
uint32_t interval_ms = deferred.interval_ms;
interval_ms += stream_slowdown_ms;
// slow most messages down if we're transfering parameters or
// waypoints:
if (_queued_parameter) {
// we are sending parameters, penalize streams:
interval_ms *= 4;
}
if (requesting_mission_items()) {
// we are sending requests for waypoints, penalize streams:
interval_ms *= 4;
}
if (ftp.replies && AP_HAL::millis() - ftp.last_send_ms < 500) {
// we are sending ftp replies
interval_ms *= 4;
}
if (interval_ms > 60000) {
return 60000;
}
return interval_ms;
}
// typical runtime on fmuv3: 5 microseconds for 3 buckets
void GCS_MAVLINK::find_next_bucket_to_send(uint16_t now16_ms)
{
#if GCS_DEBUG_SEND_MESSAGE_TIMINGS
void *data = hal.scheduler->disable_interrupts_save();
uint32_t start_us = AP_HAL::micros();
#endif
// all done sending this bucket... find another bucket...
sending_bucket_id = no_bucket_to_send;
uint16_t ms_before_send_next_bucket_to_send = UINT16_MAX;
for (uint8_t i=0; i<ARRAY_SIZE(deferred_message_bucket); i++) {
if (deferred_message_bucket[i].ap_message_ids.count() == 0) {
// no entries
continue;
}
const uint16_t interval = get_reschedule_interval_ms(deferred_message_bucket[i]);
const uint16_t ms_since_last_sent = now16_ms - deferred_message_bucket[i].last_sent_ms;
uint16_t ms_before_send_this_bucket;
if (ms_since_last_sent > interval) {
// should already have sent this bucket!
ms_before_send_this_bucket = 0;
} else {
ms_before_send_this_bucket = interval - ms_since_last_sent;
}
if (ms_before_send_this_bucket < ms_before_send_next_bucket_to_send) {
sending_bucket_id = i;
ms_before_send_next_bucket_to_send = ms_before_send_this_bucket;
}
}
if (sending_bucket_id != no_bucket_to_send) {
bucket_message_ids_to_send = deferred_message_bucket[sending_bucket_id].ap_message_ids;
} else {
bucket_message_ids_to_send.clearall();
}
#if GCS_DEBUG_SEND_MESSAGE_TIMINGS
uint32_t delta_us = AP_HAL::micros() - start_us;
hal.scheduler->restore_interrupts(data);
if (delta_us > try_send_message_stats.fnbts_maxtime) {
try_send_message_stats.fnbts_maxtime = delta_us;
}
#endif
}
ap_message GCS_MAVLINK::next_deferred_bucket_message_to_send(uint16_t now16_ms)
{
if (sending_bucket_id == no_bucket_to_send) {
// could happen if all streamrates are zero?
return no_message_to_send;
}
const uint16_t ms_since_last_sent = now16_ms - deferred_message_bucket[sending_bucket_id].last_sent_ms;
if (ms_since_last_sent < get_reschedule_interval_ms(deferred_message_bucket[sending_bucket_id])) {
// not time to send this bucket
return no_message_to_send;
}
const int16_t next = bucket_message_ids_to_send.first_set();
if (next == -1) {
// should not happen
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
AP_HAL::panic("next_deferred_bucket_message_to_send called on empty bucket");
#endif