PX4RCCalibration.h 9.85 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
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009, 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 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
///     @brief PX4 RC Calibration Widget
///     @author Don Gagne <don@thegagnes.com

#ifndef PX4RCCalibration_H
#define PX4RCCalibration_H

#include <QWidget>
#include <QTimer>

#include "UASInterface.h"
Don Gagne's avatar
Don Gagne committed
36
#include "RCValueWidget.h"
37
#include "QGCLoggingCategory.h"
38
#include "AutoPilotPlugin.h"
39 40 41

#include "ui_PX4RCCalibration.h"

42 43
Q_DECLARE_LOGGING_CATEGORY(PX4RCCalibrationLog)

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
class PX4RCCalibrationTest;

namespace Ui {
    class PX4RCCalibration;
}


class PX4RCCalibration : public QWidget
{
    Q_OBJECT
    
    friend class PX4RCCalibrationTest; ///< This allows our unit test to access internal information needed.
    
public:
    explicit PX4RCCalibration(QWidget *parent = 0);
Don Gagne's avatar
Don Gagne committed
59 60 61 62 63
    ~ PX4RCCalibration();
    
signals:
    // @brief Signalled when in unit test mode and a message box should be displayed by the next button
    void nextButtonMessageBoxDisplayed(void);
64 65

private slots:
Don Gagne's avatar
Don Gagne committed
66 67 68 69
    void _nextButton(void);
    void _skipButton(void);
    void _spektrumBind(void);
    
70 71 72
    void _mode1Toggled(bool checked);
    void _mode2Toggled(bool checked);
    
Don Gagne's avatar
Don Gagne committed
73
    void _trimNYI(void);
74 75 76 77
    
    void _updateView(void);
    
    void _remoteControlChannelRawChanged(int chan, float val);
Don Gagne's avatar
Don Gagne committed
78
    
79 80
private:
    /// @brief These identify the various controls functions. They are also used as indices into the _rgFunctioInfo
81
    /// array.
82 83 84 85 86 87 88 89 90
    enum rcCalFunctions {
        rcCalFunctionRoll,
        rcCalFunctionPitch,
        rcCalFunctionYaw,
        rcCalFunctionThrottle,
        rcCalFunctionModeSwitch,
        rcCalFunctionPosCtlSwitch,
        rcCalFunctionLoiterSwitch,
        rcCalFunctionReturnSwitch,
91
        rcCalFunctionAcroSwitch,
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
        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
118 119 120 121 122 123 124 125 126 127 128
    typedef void (PX4RCCalibration::*inputFn)(enum rcCalFunctions function, int chan, int value);
    typedef void (PX4RCCalibration::*buttonFn)(void);
    struct stateMachineEntry {
        enum rcCalFunctions function;
        const char*         instructions;
        const char*         image;
        inputFn             rcInputFn;
        buttonFn            nextFn;
        buttonFn            skipFn;
    };
    
129 130 131 132 133 134 135 136 137
    /// @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
138 139 140 141 142 143 144 145 146
        int                 rcMin;      ///< Minimum RC value
        int                 rcMax;      ///< Maximum RC value
        int                 rcTrim;     ///< Trim position
    };
    
    /// @brief Information to relate a function to it's value widget.
    struct AttitudeInfo {
        enum rcCalFunctions function;
        RCValueWidget*      valueWidget;
147 148 149 150
    };
    
    // Methods - see source code for documentation
    
Don Gagne's avatar
Don Gagne committed
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
    int _currentStep;  ///< Current step of state machine
    
    const struct stateMachineEntry* _getStateMachineEntry(int step);
    
    void _nextStep(void);
    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
176
    void _validateCalibration(void);
Don Gagne's avatar
Don Gagne committed
177
    void _writeCalibration(void);
178
    void _resetInternalCalibrationValues(void);
Don Gagne's avatar
Don Gagne committed
179
    void _setInternalCalibrationValuesFromParameters(void);
180
    
Don Gagne's avatar
Don Gagne committed
181 182
    void _startCalibration(void);
    void _stopCalibration(void);
183 184
    void _rcCalSave(void);

Don Gagne's avatar
Don Gagne committed
185 186
    void _writeParameters(void);
    
187 188 189
    void _rcCalSaveCurrentValues(void);
    
    void _showMinMaxOnRadioWidgets(bool show);
Don Gagne's avatar
Don Gagne committed
190
    void _showTrimOnRadioWidgets(bool show);
191
    
192 193 194 195 196
    void _setHelpImage(const char* imageFile);
    
    void _loadSettings(void);
    void _storeSettings(void);
    
Don Gagne's avatar
Don Gagne committed
197 198
    // @brief Called by unit test code to set the mode to unit testing
    void _setUnitTestMode(void){ _unitTestMode = true; }
199 200
    
    // Member variables
Don Gagne's avatar
Don Gagne committed
201

202 203
    static const char* _imageFileMode1Dir;
    static const char* _imageFileMode2Dir;
Don Gagne's avatar
Don Gagne committed
204
    static const char* _imageFilePrefix;
205
    static const char* _imageCenter;
Don Gagne's avatar
Don Gagne committed
206 207 208 209 210 211 212 213 214 215
    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;
216
    
217 218 219 220 221
    static const char* _settingsGroup;
    static const char* _settingsKeyTransmitterMode;
    
    int _transmitterMode;   ///< 1: transmitter is mode 1, 2: transmitted is mode 2
    
222 223 224 225
    static const int _updateInterval;   ///< Interval for ui update timer
    
    static const struct FunctionInfo _rgFunctionInfo[rcCalFunctionMax]; ///< Information associated with each function.
    int _rgFunctionChannelMapping[rcCalFunctionMax];                    ///< Maps from rcCalFunctions to channel index. _chanMax indicates channel not set for this function.
Don Gagne's avatar
Don Gagne committed
226 227 228

    static const int _attitudeControls = 5;
    struct AttitudeInfo _rgAttitudeControl[_attitudeControls];
229 230 231 232 233 234 235 236 237 238 239 240 241
    
    int _chanCount;                     ///< Number of actual rc channels available
    static const int _chanMax = 18;     ///< Maximum number of supported rc channels
    static const int _chanMinimum = 5;  ///< Minimum numner of channels required to run PX4
    
    struct ChannelInfo _rgChannelInfo[_chanMax];    ///< Information associated with each rc channel
    
    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
242 243 244 245 246 247
    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
248 249 250
    static const int _rcCalMoveDelta;
    static const int _rcCalSettleDelta;
    static const int _rcCalMinDelta;
251
    
Don Gagne's avatar
Don Gagne committed
252
    int _rcValueSave[_chanMax];        ///< Saved values prior to detecting channel movement
253
    
Don Gagne's avatar
Don Gagne committed
254
    int _rcRawValue[_chanMax];         ///< Current set of raw channel values
255
    
Don Gagne's avatar
Don Gagne committed
256 257
    RCValueWidget* _rgRCValueMonitorWidget[_chanMax];   ///< Array of radio channel value widgets
    QLabel* _rgRCValueMonitorLabel[_chanMax];           ///< Array of radio channel value labels
258

259 260
    UASInterface*                   _uas;
    QSharedPointer<AutoPilotPlugin> _autopilot;
Don Gagne's avatar
Don Gagne committed
261
    
262 263 264
    Ui::PX4RCCalibration* _ui;
    
    QTimer _updateTimer;    ///< Timer used to update widgete ui
Don Gagne's avatar
Don Gagne committed
265 266 267 268 269 270 271 272 273
    
    int     _stickDetectChannel;
    int     _stickDetectInitialValue;
    int     _stickDetectValue;
    bool    _stickDetectSettleStarted;
    QTime   _stickDetectSettleElapsed;
    static const int _stickDetectSettleMSecs;
    
    bool _unitTestMode;
274 275 276
};

#endif // PX4RCCalibration_H