RadioComponentController.h 13.3 KB
Newer Older
1 2 3 4
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
Don Gagne's avatar
Don Gagne committed
5
 (c) 2009, 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL 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.
 
 QGROUNDCONTROL 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 QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/


/// @file
Don Gagne's avatar
Don Gagne committed
26
///     @brief Radio Config Qml Controller
27 28
///     @author Don Gagne <don@thegagnes.com

Don Gagne's avatar
Don Gagne committed
29 30
#ifndef RadioComponentController_H
#define RadioComponentController_H
31 32 33

#include <QTimer>

Don Gagne's avatar
Don Gagne committed
34
#include "FactPanelController.h"
35
#include "UASInterface.h"
36
#include "QGCLoggingCategory.h"
37
#include "AutoPilotPlugin.h"
38

Don Gagne's avatar
Don Gagne committed
39
Q_DECLARE_LOGGING_CATEGORY(RadioComponentControllerLog)
40

41
class RadioConfigest;
42 43

namespace Ui {
Don Gagne's avatar
Don Gagne committed
44
    class RadioComponentController;
45 46 47
}


Don Gagne's avatar
Don Gagne committed
48
class RadioComponentController : public FactPanelController
49 50 51
{
    Q_OBJECT
    
52
    friend class RadioConfigTest; ///< This allows our unit test to access internal information needed.
53 54
    
public:
Don Gagne's avatar
Don Gagne committed
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
    RadioComponentController(void);
    ~RadioComponentController();
    
    Q_PROPERTY(int minChannelCount MEMBER _chanMinimum CONSTANT)
    Q_PROPERTY(int channelCount READ channelCount NOTIFY channelCountChanged)
    
    Q_PROPERTY(QQuickItem* statusText MEMBER _statusText)
    Q_PROPERTY(QQuickItem* cancelButton MEMBER _cancelButton)
    Q_PROPERTY(QQuickItem* nextButton MEMBER _nextButton)
    Q_PROPERTY(QQuickItem* skipButton MEMBER _skipButton)
    
    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)
    
    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)
    
    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)
    
    Q_PROPERTY(int transmitterMode READ transmitterMode WRITE setTransmitterMode NOTIFY transmitterModeChanged)
    Q_PROPERTY(QString imageHelp MEMBER _imageHelp NOTIFY imageHelpChanged)
    
    Q_ENUMS(BindModes)
    enum BindModes {
        DSM2,
        DSMX7,
        DSMX8
    };
    
    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
96
    Q_INVOKABLE void copyTrims(void);
Don Gagne's avatar
Don Gagne committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    
    int rollChannelRCValue(void);
    int pitchChannelRCValue(void);
    int yawChannelRCValue(void);
    int throttleChannelRCValue(void);
    
    bool rollChannelMapped(void);
    bool pitchChannelMapped(void);
    bool yawChannelMapped(void);
    bool throttleChannelMapped(void);
    
    bool rollChannelReversed(void);
    bool pitchChannelReversed(void);
    bool yawChannelReversed(void);
    bool throttleChannelReversed(void);
    
    int channelCount(void);
    
    int transmitterMode(void) { return _transmitterMode; }
    void setTransmitterMode(int mode);
Don Gagne's avatar
Don Gagne committed
117 118
    
signals:
Don Gagne's avatar
Don Gagne committed
119 120
    void channelCountChanged(int channelCount);
    void channelRCValueChanged(int channel, int rcValue);
Don Gagne's avatar
Don Gagne committed
121
    
Don Gagne's avatar
Don Gagne committed
122 123 124 125
    void rollChannelMappedChanged(bool mapped);
    void pitchChannelMappedChanged(bool mapped);
    void yawChannelMappedChanged(bool mapped);
    void throttleChannelMappedChanged(bool mapped);
126
    
Don Gagne's avatar
Don Gagne committed
127 128 129 130
    void rollChannelRCValueChanged(int rcValue);
    void pitchChannelRCValueChanged(int rcValue);
    void yawChannelRCValueChanged(int rcValue);
    void throttleChannelRCValueChanged(int rcValue);
131
    
Don Gagne's avatar
Don Gagne committed
132 133 134 135
    void rollChannelReversedChanged(bool reversed);
    void pitchChannelReversedChanged(bool reversed);
    void yawChannelReversedChanged(bool reversed);
    void throttleChannelReversedChanged(bool reversed);
136
    
Don Gagne's avatar
Don Gagne committed
137 138 139 140 141 142
    void imageHelpChanged(QString source);
    void transmitterModeChanged(int mode);
    
    // @brief Signalled when in unit test mode and a message box should be displayed by the next button
    void nextButtonMessageBoxDisplayed(void);

Don Gagne's avatar
Don Gagne committed
143 144 145
    // Signaled to QML to indicator reboot is required
    void functionMappingChangedAPMReboot(void);

Don Gagne's avatar
Don Gagne committed
146
private slots:
Don Gagne's avatar
Don Gagne committed
147 148
    void _rcChannelsChanged(int channelCount, int pwmValues[Vehicle::cMaxRcChannels]);

149 150
private:
    /// @brief These identify the various controls functions. They are also used as indices into the _rgFunctioInfo
151
    /// array.
152 153 154 155 156 157 158 159 160
    enum rcCalFunctions {
        rcCalFunctionRoll,
        rcCalFunctionPitch,
        rcCalFunctionYaw,
        rcCalFunctionThrottle,
        rcCalFunctionModeSwitch,
        rcCalFunctionPosCtlSwitch,
        rcCalFunctionLoiterSwitch,
        rcCalFunctionReturnSwitch,
161
        rcCalFunctionAcroSwitch,
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
        rcCalFunctionFlaps,
        rcCalFunctionAux1,
        rcCalFunctionAux2,
        rcCalFunctionMax,
        
        // Attitude functions are roll/pitch/yaw/throttle
        rcCalFunctionFirstAttitudeFunction = rcCalFunctionRoll,
        rcCalFunctionLastAttitudeFunction = rcCalFunctionThrottle,
        
        // Non-Attitude functions are everthing else
        rcCalFunctionFirstNonAttitudeFunction = rcCalFunctionModeSwitch,
        rcCalFunctionLastNonAttitudeFunction = rcCalFunctionAux2,
    };
    
    /// @brief The states of the calibration state machine.
    enum rcCalStates {
        rcCalStateChannelWait,
        rcCalStateBegin,
        rcCalStateIdentify,
        rcCalStateMinMax,
        rcCalStateCenterThrottle,
        rcCalStateDetectInversion,
        rcCalStateTrims,
        rcCalStateSave
    };
    
Don Gagne's avatar
Don Gagne committed
188 189
    typedef void (RadioComponentController::*inputFn)(enum rcCalFunctions function, int chan, int value);
    typedef void (RadioComponentController::*buttonFn)(void);
Don Gagne's avatar
Don Gagne committed
190 191 192 193 194 195 196 197 198
    struct stateMachineEntry {
        enum rcCalFunctions function;
        const char*         instructions;
        const char*         image;
        inputFn             rcInputFn;
        buttonFn            nextFn;
        buttonFn            skipFn;
    };
    
199 200 201 202 203 204 205 206 207
    /// @brief A set of information associated with a function.
    struct FunctionInfo {
        const char* parameterName;  ///< Parameter name for function mapping
    };
    
    /// @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
208 209 210 211 212 213 214
        int                 rcMin;      ///< Minimum RC value
        int                 rcMax;      ///< Maximum RC value
        int                 rcTrim;     ///< Trim position
    };
    
    int _currentStep;  ///< Current step of state machine
    
Don Gagne's avatar
Don Gagne committed
215 216 217
    const struct stateMachineEntry* _getStateMachineEntry(int step) const;
    const struct FunctionInfo* _functionInfo(void) const;
    bool _px4Vehicle(void) const;
Don Gagne's avatar
Don Gagne committed
218
    
Don Gagne's avatar
Don Gagne committed
219
    void _advanceState(void);
Don Gagne's avatar
Don Gagne committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    void _setupCurrentState(void);
    
    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 _inputFlapsDown(enum rcCalFunctions function, int channel, int value);
    void _inputFlapsUp(enum rcCalFunctions function, int channel, int value);
    void _inputSwitchDetect(enum rcCalFunctions function, int channel, int value);
    void _inputFlapsDetect(enum rcCalFunctions function, int channel, int value);
    
    void _switchDetect(enum rcCalFunctions function, int channel, int value, bool moveToNextStep);
    
    void _saveFlapsDown(void);
    void _skipFlaps(void);
    void _saveAllTrims(void);
    
    bool _stickSettleComplete(int value);
    
Don Gagne's avatar
Don Gagne committed
240
    void _validateCalibration(void);
Don Gagne's avatar
Don Gagne committed
241
    void _writeCalibration(void);
242
    void _resetInternalCalibrationValues(void);
Don Gagne's avatar
Don Gagne committed
243
    void _setInternalCalibrationValuesFromParameters(void);
244
    
Don Gagne's avatar
Don Gagne committed
245 246
    void _startCalibration(void);
    void _stopCalibration(void);
247 248
    void _rcCalSave(void);

Don Gagne's avatar
Don Gagne committed
249 250
    void _writeParameters(void);
    
251 252
    void _rcCalSaveCurrentValues(void);
    
253 254 255 256 257
    void _setHelpImage(const char* imageFile);
    
    void _loadSettings(void);
    void _storeSettings(void);
    
Don Gagne's avatar
Don Gagne committed
258
    void _signalAllAttiudeValueChanges(void);
Don Gagne's avatar
Don Gagne committed
259 260

    int _chanMax(void) const;
Don Gagne's avatar
Don Gagne committed
261
    
Don Gagne's avatar
Don Gagne committed
262 263
    // @brief Called by unit test code to set the mode to unit testing
    void _setUnitTestMode(void){ _unitTestMode = true; }
264 265
    
    // Member variables
Don Gagne's avatar
Don Gagne committed
266

267 268
    static const char* _imageFileMode1Dir;
    static const char* _imageFileMode2Dir;
Don Gagne's avatar
Don Gagne committed
269
    static const char* _imageFilePrefix;
270
    static const char* _imageCenter;
Don Gagne's avatar
Don Gagne committed
271 272 273 274 275 276 277 278 279 280
    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;
281
    
282 283 284 285 286
    static const char* _settingsGroup;
    static const char* _settingsKeyTransmitterMode;
    
    int _transmitterMode;   ///< 1: transmitter is mode 1, 2: transmitted is mode 2
    
287 288
    static const int _updateInterval;   ///< Interval for ui update timer
    
Don Gagne's avatar
Don Gagne committed
289 290 291
    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

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

    static const int _attitudeControls = 5;
295 296
    
    int _chanCount;                     ///< Number of actual rc channels available
Don Gagne's avatar
Don Gagne committed
297 298 299 300
    static const int _chanMaxPX4 = 18;  ///< Maximum number of supported rc channels, PX4 Firmware
    static const int _chanMaxAPM = 14;  ///< Maximum number of supported rc channels, APM firmware
    static const int _chanMaxAny = 18;  ///< Maximum number of support rc channels by this implementation
    static const int _chanMinimum = 5;  ///< Minimum numner of channels required to run
301
    
Don Gagne's avatar
Don Gagne committed
302
    struct ChannelInfo _rgChannelInfo[_chanMaxAny];    ///< Information associated with each rc channel
303 304 305 306 307 308 309
    
    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
    
Don Gagne's avatar
Don Gagne committed
310 311 312 313 314 315
    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
316 317 318
    static const int _rcCalMoveDelta;
    static const int _rcCalSettleDelta;
    static const int _rcCalMinDelta;
319
    
Don Gagne's avatar
Don Gagne committed
320
    int _rcValueSave[_chanMaxAny];        ///< Saved values prior to detecting channel movement
321
    
Don Gagne's avatar
Don Gagne committed
322
    int _rcRawValue[_chanMaxAny];         ///< Current set of raw channel values
323
    
Don Gagne's avatar
Don Gagne committed
324 325 326 327 328 329 330 331
    int     _stickDetectChannel;
    int     _stickDetectInitialValue;
    int     _stickDetectValue;
    bool    _stickDetectSettleStarted;
    QTime   _stickDetectSettleElapsed;
    static const int _stickDetectSettleMSecs;
    
    bool _unitTestMode;
Don Gagne's avatar
Don Gagne committed
332 333 334 335 336 337 338
    
    QQuickItem* _statusText;
    QQuickItem* _cancelButton;
    QQuickItem* _nextButton;
    QQuickItem* _skipButton;
    
    QString _imageHelp;
339 340 341 342 343
    
#ifdef UNITTEST_BUILD
    // Nasty hack to expose controller to unit test code
    static RadioComponentController*    _unitTestController;
#endif
344 345
};

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