QGCCameraControl.h 12.5 KB
Newer Older
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
/*!
 * @file
 *   @brief Camera Controller
 *   @author Gus Grubba <mavlink@grubba.com>
 *
 */

#pragma once

#include "QGCApplication.h"
#include <QLoggingCategory>

class QDomNode;
class QDomNodeList;
class QGCCameraParamIO;

Q_DECLARE_LOGGING_CATEGORY(CameraControlLog)
Q_DECLARE_LOGGING_CATEGORY(CameraControlLogVerbose)

//-----------------------------------------------------------------------------
class QGCCameraOptionExclusion : public QObject
{
public:
    QGCCameraOptionExclusion(QObject* parent, QString param_, QString value_, QStringList exclusions_)
        : QObject(parent)
        , param(param_)
        , value(value_)
        , exclusions(exclusions_)
    {
    }
    QString param;
    QString value;
    QStringList exclusions;
};

//-----------------------------------------------------------------------------
class QGCCameraOptionRange : public QObject
{
public:
    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_)
    {
    }
    QString param;
    QString value;
    QString targetParam;
    QString condition;
    QStringList  optNames;
    QStringList  optValues;
    QVariantList optVariants;
};

//-----------------------------------------------------------------------------
class QGCCameraControl : public FactGroup
{
    Q_OBJECT
63
    friend class QGCCameraParamIO;
64 65
public:
    QGCCameraControl(const mavlink_camera_information_t* info, Vehicle* vehicle, int compID, QObject* parent = NULL);
Gus Grubba's avatar
Gus Grubba committed
66
    virtual ~QGCCameraControl();
67 68 69

    //-- cam_mode
    enum CameraMode {
70
        CAM_MODE_UNDEFINED = -1,
71 72 73
        CAM_MODE_PHOTO  = 0,
        CAM_MODE_VIDEO  = 1,
        CAM_MODE_SURVEY = 2,
74 75
    };

Gus Grubba's avatar
Gus Grubba committed
76 77 78 79
    //-- Video Capture Status
    enum VideoStatus {
        VIDEO_CAPTURE_STATUS_STOPPED = 0,
        VIDEO_CAPTURE_STATUS_RUNNING,
80 81 82 83 84 85 86 87 88 89 90 91
        VIDEO_CAPTURE_STATUS_LAST,
        VIDEO_CAPTURE_STATUS_UNDEFINED = 255
    };

    //-- Photo Capture Status
    enum PhotoStatus {
        PHOTO_CAPTURE_IDLE = 0,
        PHOTO_CAPTURE_IN_PROGRESS,
        PHOTO_CAPTURE_INTERVAL_IDLE,
        PHOTO_CAPTURE_INTERVAL_IN_PROGRESS,
        PHOTO_CAPTURE_LAST,
        PHOTO_CAPTURE_STATUS_UNDEFINED = 255
Gus Grubba's avatar
Gus Grubba committed
92 93
    };

94 95 96 97 98 99
    //-- Photo Capture Modes
    enum PhotoMode {
        PHOTO_CAPTURE_SINGLE = 0,
        PHOTO_CAPTURE_TIMELAPSE,
    };

100
    Q_ENUMS(CameraMode)
Gus Grubba's avatar
Gus Grubba committed
101
    Q_ENUMS(VideoStatus)
102
    Q_ENUMS(PhotoStatus)
103
    Q_ENUMS(PhotoMode)
104 105 106 107 108 109 110 111 112 113 114 115 116 117

    Q_PROPERTY(int          version             READ version            NOTIFY infoChanged)
    Q_PROPERTY(QString      modelName           READ modelName          NOTIFY infoChanged)
    Q_PROPERTY(QString      vendor              READ vendor             NOTIFY infoChanged)
    Q_PROPERTY(QString      firmwareVersion     READ firmwareVersion    NOTIFY infoChanged)
    Q_PROPERTY(qreal        focalLength         READ focalLength        NOTIFY infoChanged)
    Q_PROPERTY(QSizeF       sensorSize          READ sensorSize         NOTIFY infoChanged)
    Q_PROPERTY(QSize        resolution          READ resolution         NOTIFY infoChanged)
    Q_PROPERTY(bool         capturesVideo       READ capturesVideo      NOTIFY infoChanged)
    Q_PROPERTY(bool         capturesPhotos      READ capturesPhotos     NOTIFY infoChanged)
    Q_PROPERTY(bool         hasModes            READ hasModes           NOTIFY infoChanged)
    Q_PROPERTY(bool         photosInVideoMode   READ photosInVideoMode  NOTIFY infoChanged)
    Q_PROPERTY(bool         videoInPhotoMode    READ videoInPhotoMode   NOTIFY infoChanged)
    Q_PROPERTY(bool         isBasic             READ isBasic            NOTIFY infoChanged)
Gus Grubba's avatar
Gus Grubba committed
118 119 120
    Q_PROPERTY(quint32      storageFree         READ storageFree        NOTIFY storageFreeChanged)
    Q_PROPERTY(QString      storageFreeStr      READ storageFreeStr     NOTIFY storageFreeChanged)
    Q_PROPERTY(quint32      storageTotal        READ storageTotal       NOTIFY storageTotalChanged)
121

122 123 124 125 126 127 128
    Q_PROPERTY(QStringList  activeSettings      READ activeSettings                                 NOTIFY activeSettingsChanged)
    Q_PROPERTY(VideoStatus  videoStatus         READ videoStatus                                    NOTIFY videoStatusChanged)
    Q_PROPERTY(PhotoStatus  photoStatus         READ photoStatus                                    NOTIFY photoStatusChanged)
    Q_PROPERTY(CameraMode   cameraMode          READ cameraMode         WRITE   setCameraMode       NOTIFY cameraModeChanged)
    Q_PROPERTY(qreal        photoLapse          READ photoLapse         WRITE   setPhotoLapse       NOTIFY photoLapseChanged)
    Q_PROPERTY(int          photoLapseCount     READ photoLapseCount    WRITE   setPhotoLapseCount  NOTIFY photoLapseCountChanged)
    Q_PROPERTY(PhotoMode    photoMode           READ photoMode          WRITE   setPhotoMode        NOTIFY photoModeChanged)
129

Gus Grubba's avatar
Gus Grubba committed
130 131 132 133
    Q_INVOKABLE virtual void setVideoMode   ();
    Q_INVOKABLE virtual void setPhotoMode   ();
    Q_INVOKABLE virtual void toggleMode     ();
    Q_INVOKABLE virtual bool takePhoto      ();
134
    Q_INVOKABLE virtual bool stopTakePhoto  ();
Gus Grubba's avatar
Gus Grubba committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    Q_INVOKABLE virtual bool startVideo     ();
    Q_INVOKABLE virtual bool stopVideo      ();
    Q_INVOKABLE virtual bool toggleVideo    ();
    Q_INVOKABLE virtual void resetSettings  ();
    Q_INVOKABLE virtual void formatCard     (int id = 1);

    virtual int         version             () { return _version; }
    virtual QString     modelName           () { return _modelName; }
    virtual QString     vendor              () { return _vendor; }
    virtual QString     firmwareVersion     ();
    virtual qreal       focalLength         () { return (qreal)_info.focal_length; }
    virtual QSizeF      sensorSize          () { return QSizeF(_info.sensor_size_h, _info.sensor_size_v); }
    virtual QSize       resolution          () { return QSize(_info.resolution_h, _info.resolution_v); }
    virtual bool        capturesVideo       () { return _info.flags & CAMERA_CAP_FLAGS_CAPTURE_VIDEO; }
    virtual bool        capturesPhotos      () { return _info.flags & CAMERA_CAP_FLAGS_CAPTURE_IMAGE; }
    virtual bool        hasModes            () { return _info.flags & CAMERA_CAP_FLAGS_HAS_MODES; }
    virtual bool        photosInVideoMode   () { return _info.flags & CAMERA_CAP_FLAGS_CAN_CAPTURE_IMAGE_IN_VIDEO_MODE; }
    virtual bool        videoInPhotoMode    () { return _info.flags & CAMERA_CAP_FLAGS_CAN_CAPTURE_VIDEO_IN_IMAGE_MODE; }

    virtual int         compID              () { return _compID; }
    virtual bool        isBasic             () { return _settings.size() == 0; }
    virtual VideoStatus videoStatus         ();
157
    virtual PhotoStatus photoStatus         ();
158 159 160
    virtual PhotoMode   photoMode           () { return _photoMode; }
    virtual qreal       photoLapse          () { return _photoLapse; }
    virtual int         photoLapseCount     () { return _photoLapseCount; }
Gus Grubba's avatar
Gus Grubba committed
161
    virtual CameraMode  cameraMode          () { return _cameraMode; }
Gus Grubba's avatar
Gus Grubba committed
162
    virtual QStringList activeSettings      ();
Gus Grubba's avatar
Gus Grubba committed
163 164 165 166 167
    virtual quint32     storageFree         () { return _storageFree;  }
    virtual QString     storageFreeStr      ();
    virtual quint32     storageTotal        () { return _storageTotal; }

    virtual void        setCameraMode       (CameraMode mode);
168 169 170
    virtual void        setPhotoMode        (PhotoMode mode);
    virtual void        setPhotoLapse       (qreal interval);
    virtual void        setPhotoLapseCount  (int count);
Gus Grubba's avatar
Gus Grubba committed
171 172 173 174 175 176

    virtual void        handleSettings      (const mavlink_camera_settings_t& settings);
    virtual void        handleCaptureStatus (const mavlink_camera_capture_status_t& capStatus);
    virtual void        handleParamAck      (const mavlink_param_ext_ack_t& ack);
    virtual void        handleParamValue    (const mavlink_param_ext_value_t& value);
    virtual void        handleStorageInfo   (const mavlink_storage_information_t& st);
177 178

    //-- Notify controller a parameter has changed
Gus Grubba's avatar
Gus Grubba committed
179
    virtual void        factChanged         (Fact* pFact);
180 181
    //-- Allow controller to modify or invalidate incoming parameter
    virtual bool        incomingParameter   (Fact* pFact, QVariant& newValue);
182 183
    //-- Allow controller to modify or invalidate parameter change
    virtual bool        validateParameter   (Fact* pFact, QVariant& newValue);
184 185 186

signals:
    void    infoChanged                     ();
Gus Grubba's avatar
Gus Grubba committed
187
    void    videoStatusChanged              ();
188
    void    photoStatusChanged              ();
189 190 191
    void    photoModeChanged                ();
    void    photoLapseChanged               ();
    void    photoLapseCountChanged          ();
192 193
    void    cameraModeChanged               ();
    void    activeSettingsChanged           ();
Gus Grubba's avatar
Gus Grubba committed
194 195 196
    void    storageFreeChanged              ();
    void    storageTotalChanged             ();
    void    dataReady                       (QByteArray data);
Gus Grubba's avatar
Gus Grubba committed
197
    void    parametersReady                 ();
Gus Grubba's avatar
Gus Grubba committed
198 199 200

protected:
    virtual void    _setVideoStatus         (VideoStatus status);
201
    virtual void    _setPhotoStatus         (PhotoStatus status);
Gus Grubba's avatar
Gus Grubba committed
202
    virtual void    _setCameraMode          (CameraMode mode);
203

204
protected slots:
Gus Grubba's avatar
Gus Grubba committed
205
    void    _initWhenReady                  ();
206 207
    void    _requestCameraSettings          ();
    void    _requestAllParameters           ();
208
    void    _requestParamUpdates            ();
Gus Grubba's avatar
Gus Grubba committed
209 210 211 212 213
    void    _requestCaptureStatus           ();
    void    _requestStorageInfo             ();
    void    _downloadFinished               ();
    void    _mavCommandResult               (int vehicleId, int component, int command, int result, bool noReponseFromVehicle);
    void    _dataReady                      (QByteArray data);
214
    void    _paramDone                      ();
215 216 217 218

private:
    bool    _handleLocalization             (QByteArray& bytes);
    bool    _replaceLocaleStrings           (const QDomNode node, QByteArray& bytes);
Gus Grubba's avatar
Gus Grubba committed
219
    bool    _loadCameraDefinitionFile       (QByteArray& bytes);
220 221 222 223 224 225 226 227 228
    bool    _loadConstants                  (const QDomNodeList nodeList);
    bool    _loadSettings                   (const QDomNodeList nodeList);
    void    _processRanges                  ();
    bool    _processCondition               (const QString condition);
    bool    _processConditionTest           (const QString conditionTest);
    bool    _loadNameValue                  (QDomNode option, const QString factName, FactMetaData* metaData, QString& optName, QString& optValue, QVariant& optVariant);
    bool    _loadRanges                     (QDomNode option, const QString factName, QString paramValue);
    void    _updateActiveList               ();
    void    _updateRanges                   (Fact* pFact);
Gus Grubba's avatar
Gus Grubba committed
229
    void    _httpRequest                    (const QString& url);
Gus Grubba's avatar
Gus Grubba committed
230
    void    _handleDefinitionFile           (const QString& url);
231 232

    QStringList     _loadExclusions         (QDomNode option);
233
    QStringList     _loadUpdates            (QDomNode option);
234 235
    QString         _getParamName           (const char* param_id);

Gus Grubba's avatar
Gus Grubba committed
236
protected:
237 238 239 240
    Vehicle*                            _vehicle;
    int                                 _compID;
    mavlink_camera_information_t        _info;
    int                                 _version;
Gus Grubba's avatar
Gus Grubba committed
241
    bool                                _cached;
Gus Grubba's avatar
Gus Grubba committed
242 243 244
    uint32_t                            _storageFree;
    uint32_t                            _storageTotal;
    QNetworkAccessManager*              _netManager;
245 246
    QString                             _modelName;
    QString                             _vendor;
247
    QString                             _cacheFile;
248
    CameraMode                          _cameraMode;
249 250 251
    PhotoMode                           _photoMode;
    qreal                               _photoLapse;
    int                                 _photoLapseCount;
Gus Grubba's avatar
Gus Grubba committed
252
    VideoStatus                         _video_status;
253
    PhotoStatus                         _photo_status;
254 255
    QStringList                         _activeSettings;
    QStringList                         _settings;
Gus Grubba's avatar
Gus Grubba committed
256
    QTimer                              _captureStatusTimer;
257 258 259 260 261
    QList<QGCCameraOptionExclusion*>    _valueExclusions;
    QList<QGCCameraOptionRange*>        _optionRanges;
    QMap<QString, QStringList>          _originalOptNames;
    QMap<QString, QVariantList>         _originalOptValues;
    QMap<QString, QGCCameraParamIO*>    _paramIO;
262 263
    int                                 _storageInfoRetries;
    int                                 _captureInfoRetries;
264
    //-- Parameters that require a full update
265
    QMap<QString, QStringList>          _requestUpdates;
266
    QStringList                         _updatesToRequest;
267
};