-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathVehicleCameraControl.cc
More file actions
2447 lines (2252 loc) · 94.2 KB
/
Copy pathVehicleCameraControl.cc
File metadata and controls
2447 lines (2252 loc) · 94.2 KB
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
#include "VehicleCameraControl.h"
#include "QGCCameraIO.h"
#include "QGCApplication.h"
#include "SettingsManager.h"
#include "AppSettings.h"
#include "VideoManager.h"
#include "QGCCameraManager.h"
#include "FTPManager.h"
#include "QGCCompression.h"
#include "QGCCorePlugin.h"
#include "QGCFileHelper.h"
#include "Vehicle.h"
#include "LinkInterface.h"
#include "MAVLinkProtocol.h"
#include "QGCVideoStreamInfo.h"
#include "MissionCommandTree.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtCore/QDir>
#include <algorithm>
#include <QtCore/QSettings>
#include <QtXml/QDomDocument>
#include <QtXml/QDomNodeList>
#include <QtQml/QQmlEngine>
#include <QtNetwork/QNetworkReply>
#include "QGCNetworkHelper.h"
QGCCameraOptionExclusion::QGCCameraOptionExclusion(QObject* parent, QString param_, QString value_, QStringList exclusions_)
: QObject(parent)
, param(param_)
, value(value_)
, exclusions(exclusions_)
{
}
QGCCameraOptionRange::QGCCameraOptionRange(QObject* parent, QString param_, QString value_, QString targetParam_, QString condition_, QStringList optNames_, QStringList optValues_)
: QObject(parent)
, param(param_)
, value(value_)
, targetParam(targetParam_)
, condition(condition_)
, optNames(optNames_)
, optValues(optValues_)
{
}
static bool read_attribute(QDomNode& node, const char* tagName, bool& target)
{
QDomNamedNodeMap attrs = node.attributes();
if(!attrs.count()) {
return false;
}
QDomNode subNode = attrs.namedItem(tagName);
if(subNode.isNull()) {
return false;
}
target = subNode.nodeValue() != "0";
return true;
}
static bool read_attribute(QDomNode& node, const char* tagName, int& target)
{
QDomNamedNodeMap attrs = node.attributes();
if(!attrs.count()) {
return false;
}
QDomNode subNode = attrs.namedItem(tagName);
if(subNode.isNull()) {
return false;
}
target = subNode.nodeValue().toInt();
return true;
}
static bool read_attribute(QDomNode& node, const char* tagName, QString& target)
{
QDomNamedNodeMap attrs = node.attributes();
if(!attrs.count()) {
return false;
}
QDomNode subNode = attrs.namedItem(tagName);
if(subNode.isNull()) {
return false;
}
target = subNode.nodeValue();
return true;
}
static bool read_value(QDomNode& element, const char* tagName, QString& target)
{
QDomElement de = element.firstChildElement(tagName);
if(de.isNull()) {
return false;
}
target = de.text();
return true;
}
VehicleCameraControl::VehicleCameraControl(const mavlink_camera_information_t *info, Vehicle* vehicle, int compID, QObject* parent)
: MavlinkCameraControlInterface(vehicle, parent)
, _compID(compID)
{
QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
memcpy(&_mavlinkCameraInfo, info, sizeof(mavlink_camera_information_t));
_vendor = QString(reinterpret_cast<const char*>(info->vendor_name));
_modelName = QString(reinterpret_cast<const char*>(info->model_name));
_cacheFile = QString::asprintf("%s/%s_%s_%03d.xml",
SettingsManager::instance()->appSettings()->parameterSavePath().toStdString().c_str(),
_vendor.toStdString().c_str(),
_modelName.toStdString().c_str(),
static_cast<int>(_mavlinkCameraInfo.cam_definition_version));
if(info->cam_definition_uri[0] != 0) {
//-- Process camera definition file
_handleDefinitionFile(info->cam_definition_uri);
} else {
_initWhenReady();
}
QSettings settings;
_photoCaptureMode = static_cast<PhotoCaptureMode>(settings.value(kPhotoMode, static_cast<int>(PHOTO_CAPTURE_SINGLE)).toInt());
_photoLapse = settings.value(kPhotoLapse, 1.0).toDouble();
_photoLapseCount = settings.value(kPhotoLapseCount, 0).toInt();
_thermalOpacity = settings.value(kThermalOpacity, 85.0).toDouble();
_thermalMode = static_cast<ThermalViewMode>(settings.value(kThermalMode, static_cast<uint32_t>(THERMAL_BLEND)).toUInt());
_videoRecordTimeUpdateTimer.setSingleShot(false);
_videoRecordTimeUpdateTimer.setInterval(333);
connect(&_videoRecordTimeUpdateTimer, &QTimer::timeout, this, &VehicleCameraControl::_recTimerHandler);
//-- Tracking capabilities
_hasTrackingRectCapability = _mavlinkCameraInfo.flags & CAMERA_CAP_FLAGS_HAS_TRACKING_RECTANGLE;
_hasTrackingPointCapability = _mavlinkCameraInfo.flags & CAMERA_CAP_FLAGS_HAS_TRACKING_POINT;
connect(this, &VehicleCameraControl::dataReady, this, &VehicleCameraControl::_dataReady);
qCDebug(CameraControlLog) << "Camera Info:";
qCDebug(CameraControlLog) << " vendor:" << vendor();
qCDebug(CameraControlLog) << " model:" << modelName();
qCDebug(CameraControlLog) << " version:" << version();
qCDebug(CameraControlLog) << " firmware:" << firmwareVersion();
qCDebug(CameraControlLog) << " focal length:" << focalLength();
qCDebug(CameraControlLog) << " sensor size:" << sensorSize();
qCDebug(CameraControlLog) << " resolution:" << resolution();
qCDebug(CameraControlLog) << " captures video:" << capturesVideo();
qCDebug(CameraControlLog) << " captures photos:" << capturesPhotos();
qCDebug(CameraControlLog) << " has modes:" << hasModes();
qCDebug(CameraControlLog) << " has zoom:" << hasZoom();
qCDebug(CameraControlLog) << " has focus:" << hasFocus();
qCDebug(CameraControlLog) << " has tracking:" << hasTracking();
qCDebug(CameraControlLog) << " has video stream:" << hasVideoStream();
qCDebug(CameraControlLog) << " photos in video mode:" << photosInVideoMode();
qCDebug(CameraControlLog) << " video in photo mode:" << videoInPhotoMode();
}
VehicleCameraControl::~VehicleCameraControl()
{
// Stop all timers to prevent them from firing during or after destruction
_captureStatusTimer.stop();
_videoRecordTimeUpdateTimer.stop();
_streamInfoTimer.stop();
_streamStatusTimer.stop();
_cameraSettingsTimer.stop();
_storageInfoTimer.stop();
delete _netManager;
_netManager = nullptr;
}
void VehicleCameraControl::_initWhenReady()
{
qCDebug(CameraControlLog) << "_initWhenReady()";
if(isBasic()) {
qCDebug(CameraControlLog) << "Basic, MAVLink only messages, no parameters.";
//-- Basic cameras have no parameters
_paramComplete = true;
emit parametersReady();
} else {
_requestAllParameters();
}
QTimer::singleShot(500, this, &VehicleCameraControl::_requestCameraSettings);
connect(&_cameraSettingsTimer, &QTimer::timeout, this, &VehicleCameraControl::_cameraSettingsTimeout);
QTimer::singleShot(1000, this, &VehicleCameraControl::_checkForVideoStreams);
connect(_vehicle, &Vehicle::mavCommandResult, this, &VehicleCameraControl::_mavCommandResult);
connect(&_captureStatusTimer, &QTimer::timeout, this, &VehicleCameraControl::_requestCaptureStatus);
_captureStatusTimer.setSingleShot(true);
_captureStatusTimer.start(1500);
connect(&_storageInfoTimer, &QTimer::timeout, this, &VehicleCameraControl::_storageInfoTimeout);
QTimer::singleShot(2000, this, &VehicleCameraControl::_requestStorageInfo);
connect(VideoManager::instance(), &VideoManager::recordingChanged, this, &VehicleCameraControl::captureVideoStateChanged);
connect(VideoManager::instance(), &VideoManager::recordingChanged, this, &VehicleCameraControl::_onVideoManagerRecordingChanged);
connect(this, &VehicleCameraControl::videoCaptureStatusChanged, this, &VehicleCameraControl::captureVideoStateChanged);
connect(this, &VehicleCameraControl::photoCaptureStatusChanged, this, &VehicleCameraControl::captureVideoStateChanged);
connect(this, &VehicleCameraControl::cameraModeChanged, this, &VehicleCameraControl::captureVideoStateChanged);
connect(this, &VehicleCameraControl::photoCaptureStatusChanged, this, &VehicleCameraControl::capturePhotosStateChanged);
connect(this, &VehicleCameraControl::cameraModeChanged, this, &VehicleCameraControl::capturePhotosStateChanged);
emit infoChanged();
delete _netManager;
_netManager = nullptr;
}
bool VehicleCameraControl::capturesVideo() const
{
// Even if the camera itself does not report video capture capability
// we can always save locally from a video stream, or use onboard recording
// if the camera reports video capture capability.
return _mavlinkCameraInfo.flags & (CAMERA_CAP_FLAGS_CAPTURE_VIDEO | CAMERA_CAP_FLAGS_HAS_VIDEO_STREAM);
}
bool VehicleCameraControl::capturesPhotos() const
{
// If we have a video stream we can always screen grab from it,
//even if the camera itself does not report still capture capability.
return _mavlinkCameraInfo.flags & (CAMERA_CAP_FLAGS_CAPTURE_IMAGE | CAMERA_CAP_FLAGS_HAS_VIDEO_STREAM);
}
MavlinkCameraControlInterface::CaptureVideoState VehicleCameraControl::captureVideoState() const
{
if (_videoCaptureStatus() == VIDEO_CAPTURE_STATUS_RUNNING || VideoManager::instance()->recording()) {
return CaptureVideoStateCapturing;
} else if (_photoCaptureStatus() != PHOTO_CAPTURE_IDLE) {
return CaptureVideoStateDisabled;
} else if (hasModes() && (_cameraMode == CAM_MODE_PHOTO || _cameraMode == CAM_MODE_SURVEY)) {
// The ui is not set up to support recording video while in photo/survey mode, even if the camera technically supports it.
return CaptureVideoStateDisabled;
} else if (_mavlinkCameraInfo.flags & CAMERA_CAP_FLAGS_HAS_VIDEO_STREAM || _mavlinkCameraInfo.flags & CAMERA_CAP_FLAGS_CAPTURE_VIDEO) {
return CaptureVideoStateIdle;
}
return CaptureVideoStateDisabled;
}
MavlinkCameraControlInterface::CapturePhotosState VehicleCameraControl::capturePhotosState() const
{
if (_photoCaptureStatus() == PHOTO_CAPTURE_IN_PROGRESS) {
return CapturePhotosStateCapturingSinglePhoto;
} else if (_photoCaptureStatus() == PHOTO_CAPTURE_INTERVAL_IN_PROGRESS || _photoCaptureStatus() == PHOTO_CAPTURE_INTERVAL_IDLE) {
return CapturePhotosStateCapturingMultiplePhotos;
} else if (_photoCaptureStatus() == PHOTO_CAPTURE_IDLE) {
// We can always do at least a screen grab from the video stream, even if camera doesn't report still capture capability
if (_mavlinkCameraInfo.flags & CAMERA_CAP_FLAGS_HAS_VIDEO_STREAM || _mavlinkCameraInfo.flags & CAMERA_CAP_FLAGS_CAPTURE_IMAGE) {
return CapturePhotosStateIdle;
}
}
return CapturePhotosStateDisabled;
}
QString VehicleCameraControl::firmwareVersion() const
{
int major = (_mavlinkCameraInfo.firmware_version >> 24) & 0xFF;
int minor = (_mavlinkCameraInfo.firmware_version >> 16) & 0xFF;
int build = _mavlinkCameraInfo.firmware_version & 0xFFFF;
return QString::asprintf("%d.%d.%d", major, minor, build);
}
QString VehicleCameraControl::recordTimeStr() const
{
return QTime(0, 0).addMSecs(static_cast<int>(recordTime())).toString("hh:mm:ss");
}
QString VehicleCameraControl::storageFreeStr() const
{
return qgcApp()->bigSizeMBToString(static_cast<quint64>(_storageFree));
}
QString VehicleCameraControl::batteryRemainingStr() const
{
if(_batteryRemaining >= 0) {
return qgcApp()->numberToString(static_cast<quint64>(_batteryRemaining)) + " %";
}
return "";
}
void VehicleCameraControl::setCameraModeVideo()
{
if (_resetting) {
return;
}
if (!hasModes()) {
qCWarning(CameraControlLog) << "Camera does not support modes";
return;
}
qCDebug(CameraControlLog) << "Camera set to video mode";
setCameraMode(CAM_MODE_VIDEO);
}
void VehicleCameraControl::setCameraModePhoto()
{
if (_resetting) {
return;
}
if (!hasModes()) {
qCWarning(CameraControlLog) << "Camera does not support modes";
return;
}
qCDebug(CameraControlLog) << "Camera set to photo mode";
setCameraMode(CAM_MODE_PHOTO);
}
void VehicleCameraControl::setCameraMode(CameraMode cameraMode)
{
if (_resetting) {
return;
}
if (!hasModes()) {
qCWarning(CameraControlLog) << "Camera does not support modes";
return;
}
if (cameraMode != CAM_MODE_PHOTO && cameraMode != CAM_MODE_VIDEO) {
qCWarning(CameraControlLog) << "Invalid camera mode" << cameraMode;
return;
}
if (_cameraMode == cameraMode) {
return;
}
qCDebug(CameraControlLog) << "Camera mode set to" << cameraModeToStr(cameraMode);
//-- Does it have a mode parameter?
Fact* pMode = mode();
if(pMode) {
pMode->setRawValue(cameraMode);
_setCameraMode(cameraMode);
} else {
//-- Use MAVLink Command
_vehicle->sendMavCommand(
_compID, // Target component
MAV_CMD_SET_CAMERA_MODE, // Command id
true, // ShowError
0, // Reserved (Set to 0)
cameraMode); // Camera mode (0: photo, 1: video)
_setCameraMode(cameraMode);
}
}
void VehicleCameraControl::setPhotoCaptureMode(PhotoCaptureMode mode)
{
if(!_resetting) {
_photoCaptureMode = mode;
QSettings settings;
settings.setValue(kPhotoMode, static_cast<int>(mode));
emit photoCaptureModeChanged();
}
}
void VehicleCameraControl::setPhotoLapse(qreal interval)
{
_photoLapse = interval;
QSettings settings;
settings.setValue(kPhotoLapse, interval);
emit photoLapseChanged();
}
void VehicleCameraControl::setPhotoLapseCount(int count)
{
_photoLapseCount = count;
QSettings settings;
settings.setValue(kPhotoLapseCount, count);
emit photoLapseCountChanged();
}
void VehicleCameraControl::_setCameraMode(CameraMode mode)
{
if(_cameraMode != mode) {
_cameraMode = mode;
emit cameraModeChanged();
//-- Update stream status
_streamStatusTimer.start(1000);
}
}
void VehicleCameraControl::toggleCameraMode()
{
if(!_resetting) {
if(_cameraMode == CAM_MODE_PHOTO || _cameraMode == CAM_MODE_SURVEY) {
setCameraModeVideo();
} else if(_cameraMode == CAM_MODE_VIDEO) {
setCameraModePhoto();
}
}
}
bool VehicleCameraControl::toggleVideoRecording()
{
if(_resetting) {
return false;
}
if (captureVideoState() == CaptureVideoStateCapturing) {
return stopVideoRecording();
} else {
return startVideoRecording();
}
return false;
}
bool VehicleCameraControl::takePhoto()
{
if (_resetting) {
return false;
}
if (capturePhotosState() == CapturePhotosStateDisabled) {
qCWarning(CameraControlLog) << "Take photo denied - photo capture is disabled";
return false;
}
if (capturePhotosState() != CapturePhotosStateIdle) {
qCWarning(CameraControlLog) << "Take photo denied - already capturing";
return false;
}
qCDebug(CameraControlLog) << "takePhoto()";
const bool canUseMavlinkImageCapture =
(_mavlinkCameraInfo.flags & CAMERA_CAP_FLAGS_CAPTURE_IMAGE) &&
(_cameraMode != CAM_MODE_VIDEO || photosInVideoMode());
if (canUseMavlinkImageCapture) {
_vehicle->sendMavCommand(
_compID,
MAV_CMD_IMAGE_START_CAPTURE,
true, // ShowError
0, // All cameras
static_cast<float>(_photoCaptureMode == PHOTO_CAPTURE_SINGLE ? 0 : _photoLapse), // Duration between two consecutive pictures (in seconds--ignored if single image)
_photoCaptureMode == PHOTO_CAPTURE_SINGLE ? 1 : _photoLapseCount); // Number of images to capture total - 0 for unlimited capture
_setPhotoCaptureStatus(PHOTO_CAPTURE_IN_PROGRESS);
_captureInfoRetries = 0;
return true;
} else {
if (_photoCaptureMode == PHOTO_CAPTURE_SINGLE) {
VideoManager::instance()->grabImage();
_setPhotoCaptureStatus(PHOTO_CAPTURE_IN_PROGRESS);
QTimer::singleShot(500, this, [this]() {
_setPhotoCaptureStatus(PHOTO_CAPTURE_IDLE);
});
return true;
} else {
qgcApp()->showAppMessage(tr("Timelapse photo capture is not supported on cameras without still capture capability"));
}
}
return false;
}
bool VehicleCameraControl::stopTakePhoto()
{
if (_resetting) {
return false;
}
if (capturePhotosState() != CapturePhotosStateCapturingMultiplePhotos) {
qCWarning(CameraControlLog) << "Stop taking photos requested - not currently capturing multiple photos";
return false;
}
qCDebug(CameraControlLog) << "Camera stop taking photos";
// Interval capture is only supported directly by cameras
_vehicle->sendMavCommand(
_compID, // Target component
MAV_CMD_IMAGE_STOP_CAPTURE,
true, // ShowError
0); // All cameras
_setPhotoCaptureStatus(PHOTO_CAPTURE_IDLE);
_captureInfoRetries = 0;
return true;
}
bool VehicleCameraControl::startVideoRecording()
{
if (_resetting) {
return false;
}
if (captureVideoState() == CaptureVideoStateCapturing) {
qCWarning(CameraControlLog) << "Start video denied - already recording";
return false;
}
if (captureVideoState() == CaptureVideoStateDisabled) {
qCWarning(CameraControlLog) << "Start video denied - video capture is disabled";
return false;
}
bool useMavlinkCommand = _mavlinkCameraInfo.flags & CAMERA_CAP_FLAGS_CAPTURE_VIDEO;
qCDebug(CameraControlLog) << "Start video recording:" << (useMavlinkCommand ? "MAVLink command" : "VideoManager");
if (useMavlinkCommand) {
_vehicle->sendMavCommand(
_compID, // Target component
MAV_CMD_VIDEO_START_CAPTURE,
true, // Show error on failure
0, // All streams
0, // CAMERA_CAPTURE_STATUS streaming frequency
0); // All cameras
} else {
VideoManager::instance()->startRecording();
}
return true;
}
bool VehicleCameraControl::stopVideoRecording()
{
if (_resetting) {
return false;
}
if (captureVideoState() == CaptureVideoStateIdle) {
qCWarning(CameraControlLog) << "Stop video recording requested - already idle";
return true;
}
bool useMavlinkCommand = _mavlinkCameraInfo.flags & CAMERA_CAP_FLAGS_CAPTURE_VIDEO;
qCDebug(CameraControlLog) << "Camera stop video recording" << (useMavlinkCommand ? "MAVLink command" : "VideoManager");
if (useMavlinkCommand) {
_vehicle->sendMavCommand(
_compID, // Target component
MAV_CMD_VIDEO_STOP_CAPTURE,
true, // Show error on failure
0, // All streams
0); // All cameras
} else {
VideoManager::instance()->stopRecording();
}
return true;
}
void VehicleCameraControl::setThermalMode(ThermalViewMode mode)
{
QSettings settings;
settings.setValue(kThermalMode, static_cast<uint32_t>(mode));
_thermalMode = mode;
emit thermalModeChanged();
}
void VehicleCameraControl::setThermalOpacity(double val)
{
if(val < 0.0) val = 0.0;
if(val > 100.0) val = 100.0;
if(fabs(_thermalOpacity - val) > 0.1) {
_thermalOpacity = val;
QSettings settings;
settings.setValue(kThermalOpacity, val);
emit thermalOpacityChanged();
}
}
void VehicleCameraControl::setZoomLevel(qreal level)
{
qCDebug(CameraControlLog) << "Camera set zoom level to" << level;
if(hasZoom()) {
//-- Limit
level = std::min(std::max(level, 0.0), 100.0);
if(_vehicle) {
_vehicle->sendMavCommand(
_compID, // Target component
MAV_CMD_SET_CAMERA_ZOOM, // Command id
false, // ShowError
ZOOM_TYPE_RANGE, // Zoom type
static_cast<float>(level)); // Level
}
}
}
void VehicleCameraControl::setFocusLevel(qreal level)
{
qCDebug(CameraControlLog) << "Camera set focus level to" << level;
if(hasFocus()) {
//-- Limit
level = std::min(std::max(level, 0.0), 100.0);
if(_vehicle) {
_vehicle->sendMavCommand(
_compID, // Target component
MAV_CMD_SET_CAMERA_FOCUS, // Command id
false, // ShowError
FOCUS_TYPE_RANGE, // Focus type
static_cast<float>(level)); // Level
}
}
}
void VehicleCameraControl::resetSettings()
{
if(!_resetting) {
qCDebug(CameraControlLog) << "resetSettings()";
_resetting = true;
_vehicle->sendMavCommand(
_compID, // Target component
MAV_CMD_RESET_CAMERA_SETTINGS, // Command id
true, // ShowError
1); // Do Reset
}
}
void VehicleCameraControl::formatCard(int id)
{
if(!_resetting) {
qCDebug(CameraControlLog) << "formatCard()";
if(_vehicle) {
_vehicle->sendMavCommand(
_compID, // Target component
MAV_CMD_STORAGE_FORMAT, // Command id
true, // ShowError
id, // Storage ID (1 for first, 2 for second, etc.)
1); // Do Format
}
}
}
void VehicleCameraControl::stepZoom(int direction)
{
qCDebug(CameraControlLog) << "Camera step zoom" << direction;
if(_vehicle && hasZoom()) {
_vehicle->sendMavCommand(
_compID, // Target component
MAV_CMD_SET_CAMERA_ZOOM, // Command id
false, // ShowError
ZOOM_TYPE_STEP, // Zoom type
direction); // Direction (-1 wide, 1 tele)
}
}
void VehicleCameraControl::startZoom(int direction)
{
qCDebug(CameraControlLog) << "Camera start zoom" << direction;
if(_vehicle && hasZoom()) {
_vehicle->sendMavCommand(
_compID, // Target component
MAV_CMD_SET_CAMERA_ZOOM, // Command id
false, // ShowError
ZOOM_TYPE_CONTINUOUS, // Zoom type
direction); // Direction (-1 wide, 1 tele)
}
}
void VehicleCameraControl::stopZoom()
{
qCDebug(CameraControlLog) << "Camera stop zoom";
if(_vehicle && hasZoom()) {
_vehicle->sendMavCommand(
_compID, // Target component
MAV_CMD_SET_CAMERA_ZOOM, // Command id
false, // ShowError
ZOOM_TYPE_CONTINUOUS, // Zoom type
0); // Direction (-1 wide, 1 tele)
}
}
void VehicleCameraControl::_requestCaptureStatus()
{
qCDebug(CameraControlLog) << "Camera request capture status - retries:" << _cameraCaptureStatusRetries;
if(_cameraCaptureStatusRetries++ % 2 == 0) {
qCDebug(CameraControlLog) << " Sending REQUEST_MESSAGE:MAVLINK_MSG_ID_CAMERA_CAPTURE_STATUS";
_vehicle->sendMavCommand(
_compID, // target component
MAV_CMD_REQUEST_MESSAGE, // command id
false, // showError
MAVLINK_MSG_ID_CAMERA_CAPTURE_STATUS); // msgid
} else {
qCDebug(CameraControlLog) << " Sending MAV_CMD_REQUEST_CAMERA_CAPTURE_STATUS (legacy)";
_vehicle->sendMavCommand(
_compID, // target component
MAV_CMD_REQUEST_CAMERA_CAPTURE_STATUS, // command id
false, // showError
1); // Do Request
}
}
void VehicleCameraControl::factChanged(Fact* pFact)
{
_updateActiveList();
_updateRanges(pFact);
}
void VehicleCameraControl::_mavCommandResult(int vehicleId, int component, int command, int result, int failureCode)
{
Q_UNUSED(failureCode);
//-- Is this ours?
if (_vehicle->id() != vehicleId || compID() != component) {
return;
}
if (result == MAV_RESULT_IN_PROGRESS) {
//-- Do Nothing
qCDebug(CameraControlLog) << "In progress response for" << command;
} else if(result == MAV_RESULT_ACCEPTED) {
switch(command) {
case MAV_CMD_RESET_CAMERA_SETTINGS:
_resetting = false;
if(isBasic()) {
_requestCameraSettings();
} else {
QTimer::singleShot(500, this, &VehicleCameraControl::_requestAllParameters);
QTimer::singleShot(2500, this, &VehicleCameraControl::_requestCameraSettings);
}
break;
case MAV_CMD_VIDEO_START_CAPTURE:
_setVideoCaptureStatus(VIDEO_CAPTURE_STATUS_RUNNING);
_captureStatusTimer.start(1000);
break;
case MAV_CMD_VIDEO_STOP_CAPTURE:
_setVideoCaptureStatus(VIDEO_CAPTURE_STATUS_STOPPED);
_captureStatusTimer.start(1000);
break;
case MAV_CMD_REQUEST_CAMERA_CAPTURE_STATUS:
_cameraCaptureStatusRetries = 0;
break;
case MAV_CMD_REQUEST_STORAGE_INFORMATION:
_storageInfoRetries = 0;
break;
case MAV_CMD_IMAGE_START_CAPTURE:
_captureStatusTimer.start(1000);
break;
}
} else {
QString commandStr = MissionCommandTree::instance()->rawName(static_cast<MAV_CMD>(command));
if ((result == MAV_RESULT_TEMPORARILY_REJECTED) || (result == MAV_RESULT_FAILED)) {
if (result == MAV_RESULT_TEMPORARILY_REJECTED) {
qCDebug(CameraControlLog) << "Command temporarily rejected (MAV_RESULT_TEMPORARILY_REJECTED) for" << commandStr;
} else {
qCDebug(CameraControlLog) << "Command failed (MAV_RESULT_FAILED) for" << commandStr;
}
switch(command) {
case MAV_CMD_RESET_CAMERA_SETTINGS:
_resetting = false;
qCDebug(CameraControlLog) << "Failed to reset camera settings";
break;
case MAV_CMD_IMAGE_START_CAPTURE:
case MAV_CMD_IMAGE_STOP_CAPTURE:
if(++_captureInfoRetries <= 5) {
_captureStatusTimer.start(1000);
} else {
qCDebug(CameraControlLog) << "Giving up start/stop image capture";
_setPhotoCaptureStatus(PHOTO_CAPTURE_IDLE);
}
break;
case MAV_CMD_REQUEST_CAMERA_CAPTURE_STATUS:
if(++_cameraCaptureStatusRetries <= 5) {
_captureStatusTimer.start(1000);
} else {
qCDebug(CameraControlLog) << "Giving up requesting capture status";
}
break;
case MAV_CMD_REQUEST_STORAGE_INFORMATION:
if(++_storageInfoRetries <= 5) {
QTimer::singleShot(1000, this, &VehicleCameraControl::_requestStorageInfo);
} else {
qCDebug(CameraControlLog) << "Giving up requesting storage status";
}
break;
}
} else {
qCDebug(CameraControlLog) << "Bad response for" << commandStr << QGCMAVLink::mavResultToString(result);
}
}
}
void VehicleCameraControl::_setVideoCaptureStatus(VideoCaptureStatus captureStatus)
{
if(_videoCaptureStatusValue != captureStatus) {
_videoCaptureStatusValue = captureStatus;
emit videoCaptureStatusChanged();
if(captureStatus == VIDEO_CAPTURE_STATUS_RUNNING) {
_recordTime = 0;
_recTime = QTime::currentTime();
_videoRecordTimeUpdateTimer.start();
} else {
_videoRecordTimeUpdateTimer.stop();
_recordTime = 0;
emit recordTimeChanged();
}
}
}
void VehicleCameraControl::_recTimerHandler()
{
_recordTime = static_cast<uint32_t>(_recTime.msecsTo(QTime::currentTime()));
emit recordTimeChanged();
}
void VehicleCameraControl::_onVideoManagerRecordingChanged(bool recording)
{
// Only track time here when not using MAVLink video capture (to avoid double-tracking)
if (_videoCaptureStatus() == VIDEO_CAPTURE_STATUS_RUNNING) {
return;
}
if (recording) {
_recordTime = 0;
_recTime = QTime::currentTime();
_videoRecordTimeUpdateTimer.start();
} else {
_videoRecordTimeUpdateTimer.stop();
_recordTime = 0;
emit recordTimeChanged();
}
}
void VehicleCameraControl::_setPhotoCaptureStatus(PhotoCaptureStatus captureStatus)
{
if(_photoCaptureStatusValue != captureStatus) {
qCDebug(CameraControlLog) << "Set Photo Status:" << captureStatus;
_photoCaptureStatusValue = captureStatus;
emit photoCaptureStatusChanged();
}
}
bool VehicleCameraControl::_loadCameraDefinitionFile(QByteArray& bytes)
{
QByteArray originalData(bytes);
//-- Handle localization
if(!_handleLocalization(bytes)) {
return false;
}
QDomDocument doc;
const QDomDocument::ParseResult result = doc.setContent(bytes, QDomDocument::ParseOption::Default);
if (!result) {
qCCritical(CameraControlLog) << "Unable to parse camera definition file on line:" << result.errorLine;
qCCritical(CameraControlLog) << result.errorMessage;
return false;
}
//-- Load camera constants
QDomNodeList defElements = doc.elementsByTagName(kDefnition);
if(!defElements.size() || !_loadConstants(defElements)) {
qCWarning(CameraControlLog) << "Unable to load camera constants from camera definition";
return false;
}
//-- Load camera parameters
QDomNodeList paramElements = doc.elementsByTagName(kParameters);
if(!paramElements.size()) {
qCDebug(CameraControlLog) << "No parameters to load from camera";
return false;
}
if(!_loadSettings(paramElements)) {
qCWarning(CameraControlLog) << "Unable to load camera parameters from camera definition";
return false;
}
//-- If this is new, cache it
if(!_cached) {
qCDebug(CameraControlLog) << "Saving camera definition file" << _cacheFile;
QFile file(_cacheFile);
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << QString("Could not save cache file %1. Error: %2").arg(_cacheFile).arg(file.errorString());
} else {
file.write(originalData);
}
}
return true;
}
bool VehicleCameraControl::_loadConstants(const QDomNodeList nodeList)
{
QDomNode node = nodeList.item(0);
if(!read_attribute(node, kVersion, _version)) {
return false;
}
if(!read_value(node, kModel, _modelName)) {
return false;
}
if(!read_value(node, kVendor, _vendor)) {
return false;
}
return true;
}
bool VehicleCameraControl::_loadSettings(const QDomNodeList nodeList)
{
QDomNode node = nodeList.item(0);
QDomElement elem = node.toElement();
QDomNodeList parameters = elem.elementsByTagName(kParameter);
//-- Pre-process settings (maintain order and skip non-controls)
for(int i = 0; i < parameters.size(); i++) {
QDomNode parameterNode = parameters.item(i);
QString name;
if(read_attribute(parameterNode, kName, name)) {
bool control = true;
read_attribute(parameterNode, kControl, control);
if(control) {
_settings << name;
}
} else {
qCritical() << "Parameter entry missing parameter name";
return false;
}
}
//-- Load parameters
for(int i = 0; i < parameters.size(); i++) {
QDomNode parameterNode = parameters.item(i);
QString factName;
read_attribute(parameterNode, kName, factName);
QString type;
if(!read_attribute(parameterNode, kType, type)) {
qCritical() << QString("Parameter %1 missing parameter type").arg(factName);
return false;
}
//-- Does it have a control?
bool control = true;
read_attribute(parameterNode, kControl, control);
//-- Is it read only?
bool readOnly = false;
read_attribute(parameterNode, kReadOnly, readOnly);
//-- Is it write only?
bool writeOnly = false;
read_attribute(parameterNode, kWriteOnly, writeOnly);
//-- It can't be both
if(readOnly && writeOnly) {
qCritical() << QString("Parameter %1 cannot be both read only and write only").arg(factName);
}
//-- Param type
bool unknownType;
FactMetaData::ValueType_t factType = FactMetaData::stringToType(type, unknownType);
if (unknownType) {
qCritical() << QString("Unknown type for parameter %1").arg(factName);
return false;
}
//-- By definition, custom types do not have control
if(factType == FactMetaData::valueTypeCustom) {
control = false;
}
//-- Description
QString description;
if(!read_value(parameterNode, kDescription, description)) {
qCritical() << QString("Parameter %1 missing parameter description").arg(factName);
return false;
}
//-- Check for updates
QStringList updates = _loadUpdates(parameterNode);
if(updates.size()) {
qCDebug(CameraControlVerboseLog) << "Parameter" << factName << "requires updates for:" << updates;
_requestUpdates[factName] = updates;
}
//-- Build metadata
FactMetaData* metaData = new FactMetaData(factType, factName, this);
QQmlEngine::setObjectOwnership(metaData, QQmlEngine::CppOwnership);
metaData->setShortDescription(description);
metaData->setLongDescription(description);
metaData->setHasControl(control);
metaData->setReadOnly(readOnly);
metaData->setWriteOnly(writeOnly);
//-- Options (enums)
QDomElement optionElem = parameterNode.toElement();
QDomNodeList optionsRoot = optionElem.elementsByTagName(kOptions);
if(optionsRoot.size()) {
//-- Iterate options
QDomNode optionsNode = optionsRoot.item(0);
QDomElement optionsElem = optionsNode.toElement();
QDomNodeList options = optionsElem.elementsByTagName(kOption);
for(int optionIndex = 0; optionIndex < options.size(); optionIndex++) {
QDomNode option = options.item(optionIndex);
QString optName;
QString optValue;
QVariant optVariant;
if(!_loadNameValue(option, factName, metaData, optName, optValue, optVariant)) {
delete metaData;
return false;
}
metaData->addEnumInfo(optName, optVariant);
_originalOptNames[factName] << optName;
_originalOptValues[factName] << optVariant;
//-- Check for exclusions
QStringList exclusions = _loadExclusions(option);
if(exclusions.size()) {
qCDebug(CameraControlVerboseLog) << "New exclusions:" << factName << optValue << exclusions;
QGCCameraOptionExclusion* pExc = new QGCCameraOptionExclusion(this, factName, optValue, exclusions);
QQmlEngine::setObjectOwnership(pExc, QQmlEngine::CppOwnership);
_valueExclusions.append(pExc);
}
//-- Check for range rules
if(!_loadRanges(option, factName, optValue)) {
delete metaData;
return false;
}
}
}
QString defaultValue;
if(read_attribute(parameterNode, kDefault, defaultValue)) {
QVariant defaultVariant;
QString errorString;
if (metaData->convertAndValidateRaw(defaultValue, false, defaultVariant, errorString)) {
metaData->setRawDefaultValue(defaultVariant);
} else {