RadioComponentController.h 12.1 KB
Newer Older
1 2
/****************************************************************************
 *
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10 11 12


/// @file
Don Gagne's avatar
Don Gagne committed
13
///     @brief Radio Config Qml Controller
14 15
///     @author Don Gagne <don@thegagnes.com

Don Gagne's avatar
Don Gagne committed
16 17
#ifndef RadioComponentController_H
#define RadioComponentController_H
18 19 20

#include <QTimer>

Don Gagne's avatar
Don Gagne committed
21
#include "FactPanelController.h"
22
#include "UASInterface.h"
23
#include "QGCLoggingCategory.h"
24
#include "AutoPilotPlugin.h"
25

Don Gagne's avatar
Don Gagne committed
26
Q_DECLARE_LOGGING_CATEGORY(RadioComponentControllerLog)
27
Q_DECLARE_LOGGING_CATEGORY(RadioComponentControllerVerboseLog)
28

29
class RadioConfigest;
30 31

namespace Ui {
Don Gagne's avatar
Don Gagne committed
32
    class RadioComponentController;
33 34 35
}


Don Gagne's avatar
Don Gagne committed
36
class RadioComponentController : public FactPanelController
37 38
{
    Q_OBJECT
39 40 41

    //friend class RadioConfigTest; ///< This allows our unit test to access internal information needed.

42
public:
Don Gagne's avatar
Don Gagne committed
43 44
    RadioComponentController(void);
    ~RadioComponentController();
45

Don Gagne's avatar
Don Gagne committed
46 47
    Q_PROPERTY(int minChannelCount MEMBER _chanMinimum CONSTANT)
    Q_PROPERTY(int channelCount READ channelCount NOTIFY channelCountChanged)
48

49 50 51 52
    Q_PROPERTY(QQuickItem* statusText   MEMBER _statusText      NOTIFY statusTextChanged)
    Q_PROPERTY(QQuickItem* cancelButton MEMBER _cancelButton    NOTIFY cancelButtonChanged)
    Q_PROPERTY(QQuickItem* nextButton   MEMBER _nextButton      NOTIFY nextButtonChanged)
    Q_PROPERTY(QQuickItem* skipButton   MEMBER _skipButton      NOTIFY skipButtonChanged)
53

Don Gagne's avatar
Don Gagne committed
54 55 56 57
    Q_PROPERTY(bool rollChannelMapped READ rollChannelMapped NOTIFY rollChannelMappedChanged)
    Q_PROPERTY(bool pitchChannelMapped READ pitchChannelMapped NOTIFY pitchChannelMappedChanged)
    Q_PROPERTY(bool yawChannelMapped READ yawChannelMapped NOTIFY yawChannelMappedChanged)
    Q_PROPERTY(bool throttleChannelMapped READ throttleChannelMapped NOTIFY throttleChannelMappedChanged)
58

Don Gagne's avatar
Don Gagne committed
59 60 61 62
    Q_PROPERTY(int rollChannelRCValue READ rollChannelRCValue NOTIFY rollChannelRCValueChanged)
    Q_PROPERTY(int pitchChannelRCValue READ pitchChannelRCValue NOTIFY pitchChannelRCValueChanged)
    Q_PROPERTY(int yawChannelRCValue READ yawChannelRCValue NOTIFY yawChannelRCValueChanged)
    Q_PROPERTY(int throttleChannelRCValue READ throttleChannelRCValue NOTIFY throttleChannelRCValueChanged)
63

Don Gagne's avatar
Don Gagne committed
64 65 66 67
    Q_PROPERTY(int rollChannelReversed READ rollChannelReversed NOTIFY rollChannelReversedChanged)
    Q_PROPERTY(int pitchChannelReversed READ pitchChannelReversed NOTIFY pitchChannelReversedChanged)
    Q_PROPERTY(int yawChannelReversed READ yawChannelReversed NOTIFY yawChannelReversedChanged)
    Q_PROPERTY(int throttleChannelReversed READ throttleChannelReversed NOTIFY throttleChannelReversedChanged)
68

Don Gagne's avatar
Don Gagne committed
69 70
    Q_PROPERTY(int transmitterMode READ transmitterMode WRITE setTransmitterMode NOTIFY transmitterModeChanged)
    Q_PROPERTY(QString imageHelp MEMBER _imageHelp NOTIFY imageHelpChanged)
71

Don Gagne's avatar
Don Gagne committed
72 73 74 75 76
    enum BindModes {
        DSM2,
        DSMX7,
        DSMX8
    };
77 78
    Q_ENUM(BindModes)

Don Gagne's avatar
Don Gagne committed
79 80 81 82 83
    Q_INVOKABLE void spektrumBindMode(int mode);
    Q_INVOKABLE void cancelButtonClicked(void);
    Q_INVOKABLE void skipButtonClicked(void);
    Q_INVOKABLE void nextButtonClicked(void);
    Q_INVOKABLE void start(void);
Don Gagne's avatar
Don Gagne committed
84
    Q_INVOKABLE void copyTrims(void);
85

Don Gagne's avatar
Don Gagne committed
86 87 88 89
    int rollChannelRCValue(void);
    int pitchChannelRCValue(void);
    int yawChannelRCValue(void);
    int throttleChannelRCValue(void);
90

Don Gagne's avatar
Don Gagne committed
91 92 93 94
    bool rollChannelMapped(void);
    bool pitchChannelMapped(void);
    bool yawChannelMapped(void);
    bool throttleChannelMapped(void);
95

Don Gagne's avatar
Don Gagne committed
96 97 98 99
    bool rollChannelReversed(void);
    bool pitchChannelReversed(void);
    bool yawChannelReversed(void);
    bool throttleChannelReversed(void);
100

Don Gagne's avatar
Don Gagne committed
101
    int channelCount(void);
102

Don Gagne's avatar
Don Gagne committed
103 104
    int transmitterMode(void) { return _transmitterMode; }
    void setTransmitterMode(int mode);
105

Don Gagne's avatar
Don Gagne committed
106
signals:
107 108 109 110 111
    void statusTextChanged(void);
    void cancelButtonChanged(void);
    void nextButtonChanged(void);
    void skipButtonChanged(void);

Don Gagne's avatar
Don Gagne committed
112 113
    void channelCountChanged(int channelCount);
    void channelRCValueChanged(int channel, int rcValue);
114

Don Gagne's avatar
Don Gagne committed
115 116 117 118
    void rollChannelMappedChanged(bool mapped);
    void pitchChannelMappedChanged(bool mapped);
    void yawChannelMappedChanged(bool mapped);
    void throttleChannelMappedChanged(bool mapped);
119

Don Gagne's avatar
Don Gagne committed
120 121 122 123
    void rollChannelRCValueChanged(int rcValue);
    void pitchChannelRCValueChanged(int rcValue);
    void yawChannelRCValueChanged(int rcValue);
    void throttleChannelRCValueChanged(int rcValue);
124

Don Gagne's avatar
Don Gagne committed
125 126 127 128
    void rollChannelReversedChanged(bool reversed);
    void pitchChannelReversedChanged(bool reversed);
    void yawChannelReversedChanged(bool reversed);
    void throttleChannelReversedChanged(bool reversed);
129

Don Gagne's avatar
Don Gagne committed
130 131
    void imageHelpChanged(QString source);
    void transmitterModeChanged(int mode);
132

133
    /// Signalled when in unit test mode and a message box should be displayed by the next button
Don Gagne's avatar
Don Gagne committed
134 135
    void nextButtonMessageBoxDisplayed(void);

136
    /// Signalled to QML to indicate reboot is required
Don Gagne's avatar
Don Gagne committed
137 138
    void functionMappingChangedAPMReboot(void);

139 140 141
    /// Signalled to Qml to indicate cal failure due to reversed throttle
    void throttleReversedCalFailure(void);

Don Gagne's avatar
Don Gagne committed
142
private slots:
Don Gagne's avatar
Don Gagne committed
143 144
    void _rcChannelsChanged(int channelCount, int pwmValues[Vehicle::cMaxRcChannels]);

145 146
private:
    /// @brief These identify the various controls functions. They are also used as indices into the _rgFunctioInfo
147
    /// array.
148 149 150 151 152 153 154
    enum rcCalFunctions {
        rcCalFunctionRoll,
        rcCalFunctionPitch,
        rcCalFunctionYaw,
        rcCalFunctionThrottle,
        rcCalFunctionMax,
    };
155

156 157 158 159 160 161 162 163 164 165 166
    /// @brief The states of the calibration state machine.
    enum rcCalStates {
        rcCalStateChannelWait,
        rcCalStateBegin,
        rcCalStateIdentify,
        rcCalStateMinMax,
        rcCalStateCenterThrottle,
        rcCalStateDetectInversion,
        rcCalStateTrims,
        rcCalStateSave
    };
167

Don Gagne's avatar
Don Gagne committed
168 169
    typedef void (RadioComponentController::*inputFn)(enum rcCalFunctions function, int chan, int value);
    typedef void (RadioComponentController::*buttonFn)(void);
Don Gagne's avatar
Don Gagne committed
170 171 172 173 174 175 176 177
    struct stateMachineEntry {
        enum rcCalFunctions function;
        const char*         instructions;
        const char*         image;
        inputFn             rcInputFn;
        buttonFn            nextFn;
        buttonFn            skipFn;
    };
178

179 180 181 182
    /// @brief A set of information associated with a function.
    struct FunctionInfo {
        const char* parameterName;  ///< Parameter name for function mapping
    };
183

184 185 186 187
    /// @brief A set of information associated with a radio channel.
    struct ChannelInfo {
        enum rcCalFunctions function;   ///< Function mapped to this channel, rcCalFunctionMax for none
        bool                reversed;   ///< true: channel is reverse, false: not reversed
Don Gagne's avatar
Don Gagne committed
188 189 190 191
        int                 rcMin;      ///< Minimum RC value
        int                 rcMax;      ///< Maximum RC value
        int                 rcTrim;     ///< Trim position
    };
192

Don Gagne's avatar
Don Gagne committed
193
    int _currentStep;  ///< Current step of state machine
194

Don Gagne's avatar
Don Gagne committed
195 196 197
    const struct stateMachineEntry* _getStateMachineEntry(int step) const;
    const struct FunctionInfo* _functionInfo(void) const;
    bool _px4Vehicle(void) const;
198

Don Gagne's avatar
Don Gagne committed
199
    void _advanceState(void);
Don Gagne's avatar
Don Gagne committed
200
    void _setupCurrentState(void);
201

Don Gagne's avatar
Don Gagne committed
202 203 204 205 206 207
    void _inputCenterWaitBegin(enum rcCalFunctions function, int channel, int value);
    void _inputStickDetect(enum rcCalFunctions function, int channel, int value);
    void _inputStickMin(enum rcCalFunctions function, int channel, int value);
    void _inputCenterWait(enum rcCalFunctions function, int channel, int value);
    void _inputSwitchMinMax(enum rcCalFunctions function, int channel, int value);
    void _inputSwitchDetect(enum rcCalFunctions function, int channel, int value);
208

Don Gagne's avatar
Don Gagne committed
209
    void _switchDetect(enum rcCalFunctions function, int channel, int value, bool moveToNextStep);
210

Don Gagne's avatar
Don Gagne committed
211
    void _saveAllTrims(void);
212

Don Gagne's avatar
Don Gagne committed
213
    bool _stickSettleComplete(int value);
214

Don Gagne's avatar
Don Gagne committed
215
    void _validateCalibration(void);
Don Gagne's avatar
Don Gagne committed
216
    void _writeCalibration(void);
217
    void _resetInternalCalibrationValues(void);
Don Gagne's avatar
Don Gagne committed
218
    void _setInternalCalibrationValuesFromParameters(void);
219

Don Gagne's avatar
Don Gagne committed
220 221
    void _startCalibration(void);
    void _stopCalibration(void);
222 223
    void _rcCalSave(void);

Don Gagne's avatar
Don Gagne committed
224
    void _writeParameters(void);
225

226
    void _rcCalSaveCurrentValues(void);
227

228
    void _setHelpImage(const char* imageFile);
229

230 231
    void _loadSettings(void);
    void _storeSettings(void);
Don Gagne's avatar
Don Gagne committed
232

233
    void _signalAllAttitudeValueChanges(void);
234 235 236 237

    bool _channelReversedParamValue(int channel);
    void _setChannelReversedParamValue(int channel, bool reversed);

Don Gagne's avatar
Don Gagne committed
238 239
    // @brief Called by unit test code to set the mode to unit testing
    void _setUnitTestMode(void){ _unitTestMode = true; }
240

241
    // Member variables
Don Gagne's avatar
Don Gagne committed
242

243 244
    static const char* _imageFileMode1Dir;
    static const char* _imageFileMode2Dir;
Don Gagne's avatar
Don Gagne committed
245
    static const char* _imageFilePrefix;
246
    static const char* _imageCenter;
Don Gagne's avatar
Don Gagne committed
247 248 249 250 251 252 253 254 255 256
    static const char* _imageHome;
    static const char* _imageThrottleUp;
    static const char* _imageThrottleDown;
    static const char* _imageYawLeft;
    static const char* _imageYawRight;
    static const char* _imageRollLeft;
    static const char* _imageRollRight;
    static const char* _imagePitchUp;
    static const char* _imagePitchDown;
    static const char* _imageSwitchMinMax;
257

258 259
    static const char* _settingsGroup;
    static const char* _settingsKeyTransmitterMode;
260

261
    int _transmitterMode;   ///< 1: transmitter is mode 1, 2: transmitted is mode 2
262

263
    static const int _updateInterval;   ///< Interval for ui update timer
264

Don Gagne's avatar
Don Gagne committed
265 266 267
    static const struct FunctionInfo _rgFunctionInfoAPM[rcCalFunctionMax]; ///< Information associated with each function, PX4 firmware
    static const struct FunctionInfo _rgFunctionInfoPX4[rcCalFunctionMax]; ///< Information associated with each function, APM firmware

268
    int _rgFunctionChannelMapping[rcCalFunctionMax];                    ///< Maps from rcCalFunctions to channel index. _chanMax indicates channel not set for this function.
Don Gagne's avatar
Don Gagne committed
269 270

    static const int _attitudeControls = 5;
271

272
    int _chanCount;                     ///< Number of actual rc channels available
273
    static const int _chanMax = 18;     ///< Maximum number of support rc channels by this implementation
Don Gagne's avatar
Don Gagne committed
274
    static const int _chanMinimum = 5;  ///< Minimum numner of channels required to run
275

276
    struct ChannelInfo _rgChannelInfo[_chanMax];    ///< Information associated with each rc channel
277

278 279 280 281 282
    enum rcCalStates _rcCalState;       ///< Current calibration state
    int _rcCalStateCurrentChannel;      ///< Current channel being worked on in rcCalStateIdentify and rcCalStateDetectInversion
    bool _rcCalStateChannelComplete;    ///< Work associated with current channel is complete
    int _rcCalStateIdentifyOldMapping;  ///< Previous mapping for channel being currently identified
    int _rcCalStateReverseOldMapping;   ///< Previous mapping for channel being currently used to detect inversion
283

Don Gagne's avatar
Don Gagne committed
284 285 286 287 288 289
    static const int _rcCalPWMCenterPoint;
    static const int _rcCalPWMValidMinValue;
    static const int _rcCalPWMValidMaxValue;
    static const int _rcCalPWMDefaultMinValue;
    static const int _rcCalPWMDefaultMaxValue;
    static const int _rcCalRoughCenterDelta;
Don Gagne's avatar
Don Gagne committed
290 291 292
    static const int _rcCalMoveDelta;
    static const int _rcCalSettleDelta;
    static const int _rcCalMinDelta;
293 294 295 296 297 298

    static const char*  _px4RevParamFormat;
    static const char*  _apmNewRevParamFormat;
    QString             _revParamFormat;
    bool                _revParamIsBool;

299 300 301 302
    int _rcValueSave[_chanMax];        ///< Saved values prior to detecting channel movement

    int _rcRawValue[_chanMax];         ///< Current set of raw channel values

Don Gagne's avatar
Don Gagne committed
303 304 305 306
    int     _stickDetectChannel;
    int     _stickDetectInitialValue;
    int     _stickDetectValue;
    bool    _stickDetectSettleStarted;
307
    QElapsedTimer   _stickDetectSettleElapsed;
Don Gagne's avatar
Don Gagne committed
308
    static const int _stickDetectSettleMSecs;
309 310 311 312 313 314 315 316

    bool        _unitTestMode   = false;

    QQuickItem* _statusText     = nullptr;
    QQuickItem* _cancelButton   = nullptr;
    QQuickItem* _nextButton     = nullptr;
    QQuickItem* _skipButton     = nullptr;

Don Gagne's avatar
Don Gagne committed
317
    QString _imageHelp;
318

319 320 321 322
#ifdef UNITTEST_BUILD
    // Nasty hack to expose controller to unit test code
    static RadioComponentController*    _unitTestController;
#endif
323 324
};

Don Gagne's avatar
Don Gagne committed
325
#endif // RadioComponentController_H