PX4RCCalibrationTest.cc 26.8 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
/*=====================================================================
 
 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/>.
 
 ======================================================================*/

#include "PX4RCCalibrationTest.h"
#include "UASManager.h"
26
#include "AutoPilotPluginManager.h"
27 28

/// @file
Don Gagne's avatar
Don Gagne committed
29
///     @brief QPX4RCCalibration Widget unit test
30 31 32
///
///     @author Don Gagne <don@thegagnes.com>

Don Gagne's avatar
Don Gagne committed
33
UT_REGISTER_TEST(PX4RCCalibrationTest)
34
QGC_LOGGING_CATEGORY(PX4RCCalibrationTestLog, "PX4RCCalibrationTestLog")
Don Gagne's avatar
Don Gagne committed
35

36 37 38 39 40 41
// This will check for the wizard buttons being enabled of disabled according to the mask you pass in.
// We use a macro instead of a method so that we get better line number reporting on failure.
#define CHK_BUTTONS(mask) \
{ \
    if (_nextButton->isEnabled() != !!((mask) & nextButtonMask) || \
        _skipButton->isEnabled() != !!((mask) & skipButtonMask) || \
Don Gagne's avatar
Don Gagne committed
42
        _cancelButton->isEnabled() != !!((mask) & cancelButtonMask) ) { \
43
        qCDebug(PX4RCCalibrationTestLog) << _statusLabel->text(); \
44 45 46 47 48 49 50 51 52 53
    } \
    QCOMPARE(_nextButton->isEnabled(), !!((mask) & nextButtonMask)); \
    QCOMPARE(_skipButton->isEnabled(), !!((mask) & skipButtonMask)); \
    QCOMPARE(_cancelButton->isEnabled(), !!((mask) & cancelButtonMask)); \
}

// This allows you to write unit tests which will click the Cancel button the first time through, followed
// by the Next button on the second iteration.
#define NEXT_OR_CANCEL(cancelNum) \
{ \
Don Gagne's avatar
Don Gagne committed
54
    if (mode == testModeStandalone && tryCancel ## cancelNum) { \
55 56 57 58 59 60 61 62 63
        QTest::mouseClick(_cancelButton, Qt::LeftButton); \
        QCOMPARE(_calWidget->_rcCalState, PX4RCCalibration::rcCalStateChannelWait); \
        tryCancel ## cancelNum = false; \
        goto StartOver; \
    } else { \
        QTest::mouseClick(_nextButton, Qt::LeftButton); \
    } \
}

Don Gagne's avatar
Don Gagne committed
64 65
const int PX4RCCalibrationTest::_stickSettleWait = PX4RCCalibration::_stickDetectSettleMSecs * 1.5;

Don Gagne's avatar
Don Gagne committed
66 67
const int PX4RCCalibrationTest::_testMinValue = PX4RCCalibration::_rcCalPWMDefaultMinValue + 10;
const int PX4RCCalibrationTest::_testMaxValue = PX4RCCalibration::_rcCalPWMDefaultMaxValue - 10;
Don Gagne's avatar
Don Gagne committed
68
const int PX4RCCalibrationTest::_testCenterValue = PX4RCCalibrationTest::_testMinValue + ((PX4RCCalibrationTest::_testMaxValue - PX4RCCalibrationTest::_testMinValue) / 2);
Don Gagne's avatar
Don Gagne committed
69

Don Gagne's avatar
Don Gagne committed
70 71
const struct PX4RCCalibrationTest::ChannelSettings PX4RCCalibrationTest::_rgChannelSettings[PX4RCCalibrationTest::_availableChannels] = {
	// Function										Min                 Max                 #  Reversed
72 73
	
	// Channel 0 : Not mapped to function, Simulate invalid Min/Max
Don Gagne's avatar
Don Gagne committed
74
	{ PX4RCCalibration::rcCalFunctionMax,			_testCenterValue,	_testCenterValue,   0, false },
75
	
76
    // Channels 1-4: Mapped to attitude control function
Don Gagne's avatar
Don Gagne committed
77 78 79 80
    { PX4RCCalibration::rcCalFunctionRoll,			_testMinValue,      _testMaxValue,      0, true },
    { PX4RCCalibration::rcCalFunctionPitch,			_testMinValue,      _testMaxValue,      0, false },
    { PX4RCCalibration::rcCalFunctionYaw,			_testMinValue,      _testMaxValue,      0, true },
    { PX4RCCalibration::rcCalFunctionThrottle,		_testMinValue,      _testMaxValue,      0,  false },
81 82 83 84 85 86 87 88 89
    
    // Channels 5-8: Not mapped to function, Simulate invalid Min/Max, since available channel Min/Max is still shown.
    // These are here to skip over the flight mode functions
    { PX4RCCalibration::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
    { PX4RCCalibration::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
    { PX4RCCalibration::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
    { PX4RCCalibration::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
    
    // Channels 9-11: Remainder of non-flight mode switches
Don Gagne's avatar
Don Gagne committed
90 91 92
    { PX4RCCalibration::rcCalFunctionFlaps,			_testMinValue,      _testMaxValue,      0, false },
    { PX4RCCalibration::rcCalFunctionAux1,			_testMinValue,      _testMaxValue,      0, false },
    { PX4RCCalibration::rcCalFunctionAux2,			_testMinValue,      _testMaxValue,      0, false },
93 94
	
    // Channel 12 : Not mapped to function, Simulate invalid Min, valid Max
Don Gagne's avatar
Don Gagne committed
95
    { PX4RCCalibration::rcCalFunctionMax,			_testCenterValue,	_testMaxValue,      0,  false },
96 97
    
	// Channel 13 : Not mapped to function, Simulate valid Min, invalid Max
Don Gagne's avatar
Don Gagne committed
98
	{ PX4RCCalibration::rcCalFunctionMax,           _testMinValue,      _testCenterValue,   0,	false },
99 100
	
    // Channels 14-17: Not mapped to function, Simulate invalid Min/Max, since available channel Min/Max is still shown
Don Gagne's avatar
Don Gagne committed
101 102 103 104
    { PX4RCCalibration::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
    { PX4RCCalibration::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
	{ PX4RCCalibration::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
	{ PX4RCCalibration::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
Don Gagne's avatar
Don Gagne committed
105 106
};

Don Gagne's avatar
Don Gagne committed
107 108
const struct PX4RCCalibrationTest::ChannelSettings PX4RCCalibrationTest::_rgChannelSettingsValidate[PX4RCCalibration::_chanMax] = {
    // Function										Min Value									Max Value									Trim Value										Reversed
109
	
110
    // Channels 0: not mapped and should be set to defaults
Don Gagne's avatar
Don Gagne committed
111
	{ PX4RCCalibration::rcCalFunctionMax,			PX4RCCalibration::_rcCalPWMDefaultMinValue,	PX4RCCalibration::_rcCalPWMDefaultMaxValue,	PX4RCCalibration::_rcCalPWMCenterPoint,         false },
112
	
113
    // Channels 1-4: Mapped to attitude control function
114 115 116 117
	{ PX4RCCalibration::rcCalFunctionRoll,			_testMinValue,                              _testMaxValue,                              _testCenterValue,                               true },
    { PX4RCCalibration::rcCalFunctionPitch,			_testMinValue,                              _testMaxValue,                              _testCenterValue,                               false },
    { PX4RCCalibration::rcCalFunctionYaw,			_testMinValue,                              _testMaxValue,                              _testCenterValue,                               true },
    { PX4RCCalibration::rcCalFunctionThrottle,		_testMinValue,                              _testMaxValue,                              _testMinValue,                                  false },
118 119 120 121 122 123 124 125
    
    // Channels 5-8: not mapped and should be set to defaults
    { PX4RCCalibration::rcCalFunctionMax,			PX4RCCalibration::_rcCalPWMDefaultMinValue,	PX4RCCalibration::_rcCalPWMDefaultMaxValue,	PX4RCCalibration::_rcCalPWMCenterPoint,         false },
    { PX4RCCalibration::rcCalFunctionMax,			PX4RCCalibration::_rcCalPWMDefaultMinValue,	PX4RCCalibration::_rcCalPWMDefaultMaxValue,	PX4RCCalibration::_rcCalPWMCenterPoint,         false },
    { PX4RCCalibration::rcCalFunctionMax,			PX4RCCalibration::_rcCalPWMDefaultMinValue,	PX4RCCalibration::_rcCalPWMDefaultMaxValue,	PX4RCCalibration::_rcCalPWMCenterPoint,         false },
    { PX4RCCalibration::rcCalFunctionMax,			PX4RCCalibration::_rcCalPWMDefaultMinValue,	PX4RCCalibration::_rcCalPWMDefaultMaxValue,	PX4RCCalibration::_rcCalPWMCenterPoint,         false },
    
    // Channels 9-11: Remainder of non-flight mode switches
126 127 128
    { PX4RCCalibration::rcCalFunctionFlaps,			_testMinValue,                              _testMaxValue,                              _testCenterValue,                               false },
    { PX4RCCalibration::rcCalFunctionAux1,			_testMinValue,                              _testMaxValue,                              _testCenterValue,                               false },
    { PX4RCCalibration::rcCalFunctionAux2,			_testMinValue,                              _testMaxValue,                              _testCenterValue,                               false },
129
	
Don Gagne's avatar
Don Gagne committed
130 131 132 133 134
	// Channels 12-17 are not mapped and should be set to defaults
	{ PX4RCCalibration::rcCalFunctionMax,			PX4RCCalibration::_rcCalPWMDefaultMinValue,	PX4RCCalibration::_rcCalPWMDefaultMaxValue,	PX4RCCalibration::_rcCalPWMCenterPoint,         false },
	{ PX4RCCalibration::rcCalFunctionMax,			PX4RCCalibration::_rcCalPWMDefaultMinValue,	PX4RCCalibration::_rcCalPWMDefaultMaxValue,	PX4RCCalibration::_rcCalPWMCenterPoint,         false },
	{ PX4RCCalibration::rcCalFunctionMax,			PX4RCCalibration::_rcCalPWMDefaultMinValue,	PX4RCCalibration::_rcCalPWMDefaultMaxValue,	PX4RCCalibration::_rcCalPWMCenterPoint,         false },
	{ PX4RCCalibration::rcCalFunctionMax,			PX4RCCalibration::_rcCalPWMDefaultMinValue,	PX4RCCalibration::_rcCalPWMDefaultMaxValue,	PX4RCCalibration::_rcCalPWMCenterPoint,         false },
135 136
    { PX4RCCalibration::rcCalFunctionMax,			PX4RCCalibration::_rcCalPWMDefaultMinValue,	PX4RCCalibration::_rcCalPWMDefaultMaxValue,	PX4RCCalibration::_rcCalPWMCenterPoint,         false },
    { PX4RCCalibration::rcCalFunctionMax,			PX4RCCalibration::_rcCalPWMDefaultMinValue,	PX4RCCalibration::_rcCalPWMDefaultMaxValue,	PX4RCCalibration::_rcCalPWMCenterPoint,         false },
Don Gagne's avatar
Don Gagne committed
137 138
};

139 140 141
PX4RCCalibrationTest::PX4RCCalibrationTest(void) :
    _calWidget(NULL)
{
Don Gagne's avatar
Don Gagne committed
142 143
}

144 145
void PX4RCCalibrationTest::init(void)
{
Don Gagne's avatar
Don Gagne committed
146 147
    UnitTest::init();
    
148 149 150 151 152
    _mockLink = new MockLink();
    Q_CHECK_PTR(_mockLink);
    LinkManager::instance()->_addLink(_mockLink);
    LinkManager::instance()->connectLink(_mockLink);
    QTest::qWait(5000); // Give enough time for UI to settle and heartbeats to go through
153
    
154 155
    _autopilot = AutoPilotPluginManager::instance()->getInstanceForAutoPilotPlugin(UASManager::instance()->getActiveUAS());
    Q_ASSERT(_autopilot);
156
    
157 158 159 160 161 162
    QSignalSpy spyPlugin(_autopilot, SIGNAL(pluginReadyChanged(bool)));
    if (!_autopilot->pluginReady()) {
        QCOMPARE(spyPlugin.wait(10000), true);
    }
    Q_ASSERT(_autopilot->pluginReady());
    
163
    // This will instatiate the widget with an active uas with ready parameters
164 165
    _calWidget = new PX4RCCalibration();
    Q_CHECK_PTR(_calWidget);
Don Gagne's avatar
Don Gagne committed
166
    _calWidget->_setUnitTestMode();    
167 168 169 170 171 172 173 174 175 176 177 178 179 180

    // Get pointers to the push buttons
    _cancelButton = _calWidget->findChild<QPushButton*>("rcCalCancel");
    _nextButton = _calWidget->findChild<QPushButton*>("rcCalNext");
    _skipButton = _calWidget->findChild<QPushButton*>("rcCalSkip");
    
    Q_ASSERT(_cancelButton);
    Q_ASSERT(_nextButton);
    Q_ASSERT(_skipButton);
    
    _statusLabel = _calWidget->findChild<QLabel*>("rcCalStatus");
    Q_ASSERT(_statusLabel);
 
    for (size_t i=0; i<PX4RCCalibration::_chanMax; i++) {
Don Gagne's avatar
Don Gagne committed
181 182 183
        QString radioWidgetName("channel%1Value");

        RCValueWidget* radioWidget = _calWidget->findChild<RCValueWidget*>(radioWidgetName.arg(i+1));
184 185
        Q_ASSERT(radioWidget);
        
Don Gagne's avatar
Don Gagne committed
186
        _rgValueWidget[i] = radioWidget;
187
    }
Don Gagne's avatar
Don Gagne committed
188 189 190 191 192 193 194
    
    _rgSignals[0] = SIGNAL(nextButtonMessageBoxDisplayed());
    _multiSpyNextButtonMessageBox = new MultiSignalSpy();
    Q_CHECK_PTR(_multiSpyNextButtonMessageBox);
    QCOMPARE(_multiSpyNextButtonMessageBox->init(_calWidget, _rgSignals, 1), true);
    
    QCOMPARE(_calWidget->_currentStep, -1);
195 196 197 198
}

void PX4RCCalibrationTest::cleanup(void)
{
Don Gagne's avatar
Don Gagne committed
199 200 201
    Q_ASSERT(_calWidget);
    delete _calWidget;
    
202 203
    // Disconnecting the link will prompt for log file save
    setExpectedFileDialog(getSaveFileName, QStringList());
204
    
205
    LinkManager::instance()->disconnectLink(_mockLink);
206
    
Don Gagne's avatar
Don Gagne committed
207
    UnitTest::cleanup();
208 209
}

Don Gagne's avatar
Don Gagne committed
210
/// @brief Test for correct behavior in determining minimum numbers of channels for flight.
211 212 213
void PX4RCCalibrationTest::_minRCChannels_test(void)
{
    // Next button won't be enabled until we see the minimum number of channels.
214
    for (int chan=0; chan<PX4RCCalibration::_chanMinimum; chan++) {
215
        _mockLink->emitRemoteControlChannelRawChanged(chan, (float)PX4RCCalibration::_rcCalPWMCenterPoint);
Don Gagne's avatar
Don Gagne committed
216 217
        
        // We use _chanCount internally so we should validate it
218
        QCOMPARE(_calWidget->_chanCount, chan+1);
Don Gagne's avatar
Don Gagne committed
219 220
        
        // Validate Next button state
Don Gagne's avatar
Don Gagne committed
221 222
        QTest::mouseClick(_nextButton, Qt::LeftButton);
        bool signalFound = _multiSpyNextButtonMessageBox->waitForSignalByIndex(0, 200);
223
        if (chan == PX4RCCalibration::_chanMinimum - 1) {
Don Gagne's avatar
Don Gagne committed
224 225 226
            // Last channel should trigger Calibration available
            QCOMPARE(signalFound, false);
            QCOMPARE(_calWidget->_currentStep, 0);
227
        } else {
Don Gagne's avatar
Don Gagne committed
228 229 230
            // Still less than the minimum channels. Next button should show message box. Calibration should not start.
            QCOMPARE(signalFound, true);
            QCOMPARE(_calWidget->_currentStep, -1);
231
        }
Don Gagne's avatar
Don Gagne committed
232
        _multiSpyNextButtonMessageBox->clearAllSignals();
Don Gagne's avatar
Don Gagne committed
233

234 235 236
        // The following test code no longer works since view update doesn't happens until parameters are received.
        // Leaving code here because RC Cal could be restructured to handle this case at some point.
#if 0
Don Gagne's avatar
Don Gagne committed
237
        // Only available channels should have visible widget. A ui update cycle needs to have passed so we wait a little.
Don Gagne's avatar
Don Gagne committed
238 239
        QTest::qWait(PX4RCCalibration::_updateInterval * 2);
        for (int chanWidget=0; chanWidget<PX4RCCalibration::_chanMax; chanWidget++) {
240
            qCDebug(PX4RCCalibrationTestLog) << _rgValueWidget[chanWidget]->objectName() << chanWidget << chan;
Don Gagne's avatar
Don Gagne committed
241
            QCOMPARE(_rgValueWidget[chanWidget]->isVisible(), !!(chanWidget <= chan));
Don Gagne's avatar
Don Gagne committed
242
        }
243
#endif
244 245 246
    }
}

Don Gagne's avatar
Don Gagne committed
247
void PX4RCCalibrationTest::_beginCalibration(void)
248 249 250
{
    CHK_BUTTONS(nextButtonMask | cancelButtonMask);

Don Gagne's avatar
Don Gagne committed
251 252 253 254
    // We should already have enough channels to proceed with calibration. Click next to start the process.
    
    QTest::mouseClick(_nextButton, Qt::LeftButton);
    QCOMPARE(_calWidget->_currentStep, 1);
255 256 257
    CHK_BUTTONS(cancelButtonMask);
}

Don Gagne's avatar
Don Gagne committed
258
void PX4RCCalibrationTest::_stickMoveWaitForSettle(int channel, int value)
259
{
260
    qCDebug(PX4RCCalibrationTestLog) << "_stickMoveWaitForSettle channel:value" << channel << value;
Don Gagne's avatar
Don Gagne committed
261 262

    // Move the stick, this will initialized the settle checker
263
    _mockLink->emitRemoteControlChannelRawChanged(channel, value);
Don Gagne's avatar
Don Gagne committed
264 265
    
    // Emit the signal again to start the settle timer
266
    _mockLink->emitRemoteControlChannelRawChanged(channel, value);
Don Gagne's avatar
Don Gagne committed
267 268 269 270 271
    
    // Wait long enough for the settle timer to expire
    QTest::qWait(PX4RCCalibration::_stickDetectSettleMSecs * 1.5);
    
    // Emit the signal again so that we detect stick settle
272
    _mockLink->emitRemoteControlChannelRawChanged(channel, value);
273 274
}

Don Gagne's avatar
Don Gagne committed
275
void PX4RCCalibrationTest::_stickMoveAutoStep(const char* functionStr, enum PX4RCCalibration::rcCalFunctions function, enum PX4RCCalibrationTest::MoveToDirection direction, bool identifyStep)
276
{
Don Gagne's avatar
Don Gagne committed
277
    Q_UNUSED(functionStr);
278
    qCDebug(PX4RCCalibrationTestLog) << "_stickMoveAutoStep function:direction:reversed:identifyStep" << functionStr << function << direction << identifyStep;
279
    
Don Gagne's avatar
Don Gagne committed
280 281 282 283 284 285
    CHK_BUTTONS(cancelButtonMask);
    
    int channel = _rgFunctionChannelMap[function];
    int saveStep = _calWidget->_currentStep;
    
    bool reversed = _rgChannelSettings[channel].reversed;
286
    
Don Gagne's avatar
Don Gagne committed
287 288
    if (!identifyStep && direction != moveToCenter) {
        // We have already identified the function channel mapping. Move other channels around to make sure there is no impact.
289
        
Don Gagne's avatar
Don Gagne committed
290 291 292
        int otherChannel = channel + 1;
        if (otherChannel >= PX4RCCalibration::_chanMax) {
            otherChannel = 0;
Don Gagne's avatar
Don Gagne committed
293 294
        }
        
Don Gagne's avatar
Don Gagne committed
295 296 297
        _stickMoveWaitForSettle(otherChannel, _testMinValue);
        QCOMPARE(_calWidget->_currentStep, saveStep);
        CHK_BUTTONS(cancelButtonMask);
298
        
Don Gagne's avatar
Don Gagne committed
299 300 301
        _stickMoveWaitForSettle(otherChannel, PX4RCCalibration::_rcCalPWMCenterPoint);
        QCOMPARE(_calWidget->_currentStep, saveStep);
        CHK_BUTTONS(cancelButtonMask);
302 303
    }
    
Don Gagne's avatar
Don Gagne committed
304 305 306 307 308 309 310 311 312 313 314
    // Move channel to specified position to trigger next step
    
    int value;
    if (direction == moveToMin) {
        value = reversed ? _testMaxValue : _testMinValue;
    } else if (direction == moveToMax) {
        value = reversed ? _testMinValue : _testMaxValue;
    } else if (direction == moveToCenter) {
        value = PX4RCCalibration::_rcCalPWMCenterPoint;
    } else {
        Q_ASSERT(false);
315
    }
Don Gagne's avatar
Don Gagne committed
316 317 318
    
    _stickMoveWaitForSettle(channel, value);
    QCOMPARE(_calWidget->_currentStep, saveStep + 1);
319 320
}

Don Gagne's avatar
Don Gagne committed
321
void PX4RCCalibrationTest::_switchMinMaxStep(void)
322
{
Don Gagne's avatar
Don Gagne committed
323
    CHK_BUTTONS(nextButtonMask | cancelButtonMask);
Don Gagne's avatar
Don Gagne committed
324 325
    
    // Try setting a min/max value that is below the threshold to make sure min/max doesn't go valid
326 327
    _mockLink->emitRemoteControlChannelRawChanged(0, (float)(PX4RCCalibration::_rcCalPWMValidMinValue + 1));
    _mockLink->emitRemoteControlChannelRawChanged(0, (float)(PX4RCCalibration::_rcCalPWMValidMaxValue - 1));
Don Gagne's avatar
Don Gagne committed
328 329 330 331
    QCOMPARE(_rgValueWidget[0]->isMinValid(), false);
    QCOMPARE(_rgValueWidget[0]->isMaxValid(), false);
    
    // Send min/max values switch channels
332
    for (int chan=0; chan<_availableChannels; chan++) {
333 334
        _mockLink->emitRemoteControlChannelRawChanged(chan, _rgChannelSettings[chan].rcMin);
        _mockLink->emitRemoteControlChannelRawChanged(chan, _rgChannelSettings[chan].rcMax);
335
    }
Don Gagne's avatar
Don Gagne committed
336
    
Don Gagne's avatar
Don Gagne committed
337
    _channelHomePosition();
338

Don Gagne's avatar
Don Gagne committed
339 340 341
    int saveStep = _calWidget->_currentStep;
    QTest::mouseClick(_nextButton, Qt::LeftButton);
    QCOMPARE(_calWidget->_currentStep, saveStep + 1);
342 343
}

Don Gagne's avatar
Don Gagne committed
344
void PX4RCCalibrationTest::_flapsDetectStep(void)
345
{
Don Gagne's avatar
Don Gagne committed
346
    int channel = _rgFunctionChannelMap[PX4RCCalibration::rcCalFunctionFlaps];
347
    
348
    qCDebug(PX4RCCalibrationTestLog) << "_flapsDetectStep channel" << channel;
349
    
Don Gagne's avatar
Don Gagne committed
350 351
    // Test code can't handle reversed flaps channel
    Q_ASSERT(!_rgChannelSettings[channel].reversed);
352
    
Don Gagne's avatar
Don Gagne committed
353 354 355
    CHK_BUTTONS(nextButtonMask | cancelButtonMask | skipButtonMask);
    
    int saveStep = _calWidget->_currentStep;
356

Don Gagne's avatar
Don Gagne committed
357 358 359
    // Wiggle channel to identify
    _stickMoveWaitForSettle(channel, _testMaxValue);
    _stickMoveWaitForSettle(channel, _testMinValue);
360
    
Don Gagne's avatar
Don Gagne committed
361 362
    // Leave channel on full flaps down
    _stickMoveWaitForSettle(channel, _testMaxValue);
363
    
Don Gagne's avatar
Don Gagne committed
364 365 366 367 368
    // User has to hit next at this step
    QCOMPARE(_calWidget->_currentStep, saveStep);
    CHK_BUTTONS(nextButtonMask | cancelButtonMask | skipButtonMask);
    QTest::mouseClick(_nextButton, Qt::LeftButton);
    QCOMPARE(_calWidget->_currentStep, saveStep + 1);
369 370
}

Don Gagne's avatar
Don Gagne committed
371
void PX4RCCalibrationTest::_switchSelectAutoStep(const char* functionStr, PX4RCCalibration::rcCalFunctions function)
372
{
Don Gagne's avatar
Don Gagne committed
373
    Q_UNUSED(functionStr);
374
    ////qCDebug(PX4RCCalibrationTestLog)() << "_switchSelectAutoStep" << functionStr << "function:" << function;
375
    
Don Gagne's avatar
Don Gagne committed
376 377 378
    int buttonMask = cancelButtonMask;
    if (function != PX4RCCalibration::rcCalFunctionModeSwitch) {
        buttonMask |= skipButtonMask;
Don Gagne's avatar
Don Gagne committed
379 380
    }
    
Don Gagne's avatar
Don Gagne committed
381
    CHK_BUTTONS(buttonMask);
Don Gagne's avatar
Don Gagne committed
382
    
Don Gagne's avatar
Don Gagne committed
383
    int saveStep = _calWidget->_currentStep;
384
    
Don Gagne's avatar
Don Gagne committed
385 386
    // Wiggle stick for channel
    int channel = _rgFunctionChannelMap[function];
387 388
    _mockLink->emitRemoteControlChannelRawChanged(channel, _testMinValue);
    _mockLink->emitRemoteControlChannelRawChanged(channel, _testMaxValue);
Don Gagne's avatar
Don Gagne committed
389 390
    
    QCOMPARE(_calWidget->_currentStep, saveStep + 1);
391 392
}

Don Gagne's avatar
Don Gagne committed
393
void PX4RCCalibrationTest::_fullCalibration_test(void)
394
{
395
    // IMPORTANT NOTE: We used channels 1-5 for attitude mapping in the test below.
396
    // MockLink.params file cannot have flight mode switches mapped to those channels.
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    // If it does it will cause errors since the stick will not be detetected where

    /// _rgFunctionChannelMap maps from function index to channel index. For channels which are not part of
    /// rc cal set the mapping the the previous mapping.
    
    for (int function=0; function<PX4RCCalibration::rcCalFunctionMax; function++) {
        bool found = false;
        
        // If we are mapping this function during cal set it into _rgFunctionChannelMap
        for (int channel=0; channel<PX4RCCalibrationTest::_availableChannels; channel++) {
            if (_rgChannelSettings[channel].function == function) {
                
                // First make sure this function isn't being use for a switch
                
                QStringList switchList;
                switchList << "RC_MAP_MODE_SW" << "RC_MAP_LOITER_SW" << "RC_MAP_RETURN_SW" << "RC_MAP_POSCTL_SW";
                
                foreach (QString switchParam, switchList) {
415
                    Q_ASSERT(_autopilot->getParameterFact(switchParam)->value().toInt() != channel + 1);
416 417 418 419 420 421 422 423 424 425 426
                }
                
                _rgFunctionChannelMap[function] = channel;
                found = true;
                
                break;
            }
        }
        
        // If we aren't mapping this function during calibration, set it to the previous setting
        if (!found) {
427 428
            _rgFunctionChannelMap[function] = _autopilot->getParameterFact(PX4RCCalibration::_rgFunctionInfo[function].parameterName)->value().toInt();
            qCDebug(PX4RCCalibrationTestLog) << "Assigning switch" << function << _rgFunctionChannelMap[function];
429 430 431 432 433 434 435
            if (_rgFunctionChannelMap[function] == 0) {
                _rgFunctionChannelMap[function] = -1;   // -1 signals no mapping
            } else {
                _rgFunctionChannelMap[function]--;   // parameter is 1-based, _rgFunctionChannelMap is not
            }
        }
    }
Don Gagne's avatar
Don Gagne committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    
    _channelHomePosition();
    QTest::mouseClick(_nextButton, Qt::LeftButton);    
    _beginCalibration();
    _stickMoveAutoStep("Throttle", PX4RCCalibration::rcCalFunctionThrottle, moveToMax, true /* identify step */);
    _stickMoveAutoStep("Throttle", PX4RCCalibration::rcCalFunctionThrottle, moveToMin, false /* not identify step */);
    _stickMoveAutoStep("Yaw", PX4RCCalibration::rcCalFunctionYaw, moveToMax, true /* identify step */);
    _stickMoveAutoStep("Yaw", PX4RCCalibration::rcCalFunctionYaw, moveToMin, false /* not identify step */);
    _stickMoveAutoStep("Roll", PX4RCCalibration::rcCalFunctionRoll, moveToMax, true /* identify step */);
    _stickMoveAutoStep("Roll", PX4RCCalibration::rcCalFunctionRoll, moveToMin, false /* not identify step */);
    _stickMoveAutoStep("Pitch", PX4RCCalibration::rcCalFunctionPitch, moveToMax, true /* identify step */);
    _stickMoveAutoStep("Pitch", PX4RCCalibration::rcCalFunctionPitch, moveToMin, false /* not identify step */);
    _stickMoveAutoStep("Pitch", PX4RCCalibration::rcCalFunctionPitch, moveToCenter, false /* not identify step */);
    _switchMinMaxStep();
    _flapsDetectStep();
    _stickMoveAutoStep("Flaps", PX4RCCalibration::rcCalFunctionFlaps, moveToMin, false /* not identify step */);
    _switchSelectAutoStep("Aux1", PX4RCCalibration::rcCalFunctionAux1);
    _switchSelectAutoStep("Aux2", PX4RCCalibration::rcCalFunctionAux2);
454 455 456

    // One more click and the parameters should get saved
    QTest::mouseClick(_nextButton, Qt::LeftButton);
Don Gagne's avatar
Don Gagne committed
457
    _validateParameters();
458 459
}

Don Gagne's avatar
Don Gagne committed
460 461
/// @brief Sets rc input to Throttle down home position. Centers all other channels.
void PX4RCCalibrationTest::_channelHomePosition(void)
462
{
Don Gagne's avatar
Don Gagne committed
463
    // Initialize available channels to center point.
Don Gagne's avatar
Don Gagne committed
464
    for (int i=0; i<_availableChannels; i++) {
465
        _mockLink->emitRemoteControlChannelRawChanged(i, (float)PX4RCCalibration::_rcCalPWMCenterPoint);
466
    }
Don Gagne's avatar
Don Gagne committed
467
    
468 469
    // Throttle to min - 1 (throttle is not reversed). We do this so that the trim value is below the min value. This should end up
    // being validated and raised to min value. If not, something is wrong with RC Cal code.
470
    _mockLink->emitRemoteControlChannelRawChanged(_rgFunctionChannelMap[PX4RCCalibration::rcCalFunctionThrottle], _testMinValue - 1);
471 472
}

Don Gagne's avatar
Don Gagne committed
473
void PX4RCCalibrationTest::_validateParameters(void)
474 475 476 477 478 479
{
    QString minTpl("RC%1_MIN");
    QString maxTpl("RC%1_MAX");
    QString trimTpl("RC%1_TRIM");
    QString revTpl("RC%1_REV");
    
Don Gagne's avatar
Don Gagne committed
480 481 482 483 484 485 486 487 488 489 490
    // Check mapping for all fuctions
    for (int chanFunction=0; chanFunction<PX4RCCalibration::rcCalFunctionMax; chanFunction++) {
        int chanIndex = _rgFunctionChannelMap[chanFunction];
        
        int expectedParameterValue;
        if (chanIndex == -1) {
            expectedParameterValue = 0;  // 0 signals no mapping
        } else {
            expectedParameterValue = chanIndex + 1; // 1-based parameter value
        }
        
491 492
        qCDebug(PX4RCCalibrationTestLog) << "Validate" << chanFunction << _autopilot->getParameterFact(PX4RCCalibration::_rgFunctionInfo[chanFunction].parameterName)->value().toInt();
        QCOMPARE(_autopilot->getParameterFact(PX4RCCalibration::_rgFunctionInfo[chanFunction].parameterName)->value().toInt(), expectedParameterValue);
Don Gagne's avatar
Don Gagne committed
493
    }
Don Gagne's avatar
Don Gagne committed
494 495 496 497 498 499 500

    // Validate the channel settings. Note the channels are 1-based in parameter names.
    for (int chan = 0; chan<PX4RCCalibration::_chanMax; chan++) {
        int oneBasedChannel = chan + 1;
        bool convertOk;
        
        // Required channels have min/max set on them. Remaining channels are left to default.
Don Gagne's avatar
Don Gagne committed
501 502 503 504 505
		int rcMinExpected = _rgChannelSettingsValidate[chan].rcMin;
		int rcMaxExpected = _rgChannelSettingsValidate[chan].rcMax;
		int rcTrimExpected = _rgChannelSettingsValidate[chan].rcTrim;
        bool rcReversedExpected = _rgChannelSettingsValidate[chan].reversed;

506
        int rcMinActual = _autopilot->getParameterFact(minTpl.arg(oneBasedChannel))->value().toInt(&convertOk);
Don Gagne's avatar
Don Gagne committed
507
        QCOMPARE(convertOk, true);
508
        int rcMaxActual = _autopilot->getParameterFact(maxTpl.arg(oneBasedChannel))->value().toInt(&convertOk);
Don Gagne's avatar
Don Gagne committed
509
        QCOMPARE(convertOk, true);
510
        int rcTrimActual = _autopilot->getParameterFact(trimTpl.arg(oneBasedChannel))->value().toInt(&convertOk);
Don Gagne's avatar
Don Gagne committed
511
        QCOMPARE(convertOk, true);
512
        float rcReversedFloat = _autopilot->getParameterFact(revTpl.arg(oneBasedChannel))->value().toFloat(&convertOk);
Don Gagne's avatar
Don Gagne committed
513 514 515
        QCOMPARE(convertOk, true);
        bool rcReversedActual = (rcReversedFloat == -1.0f);
        
516 517
        qCDebug(PX4RCCalibrationTestLog) << "_validateParemeters expected channel:min:max:trim:rev" << chan << rcMinExpected << rcMaxExpected << rcTrimExpected << rcReversedExpected;
        qCDebug(PX4RCCalibrationTestLog) << "_validateParemeters actual channel:min:max:trim:rev" << chan << rcMinActual << rcMaxActual << rcTrimActual << rcReversedActual;
518

Don Gagne's avatar
Don Gagne committed
519 520 521 522
        QCOMPARE(rcMinExpected, rcMinActual);
        QCOMPARE(rcMaxExpected, rcMaxActual);
        QCOMPARE(rcTrimExpected, rcTrimActual);
        QCOMPARE(rcReversedExpected, rcReversedActual);
523 524
    }
    
Don Gagne's avatar
Don Gagne committed
525 526 527 528 529 530 531
    // Check mapping for all fuctions
    for (int chanFunction=0; chanFunction<PX4RCCalibration::rcCalFunctionMax; chanFunction++) {
        int expectedValue;
        if (_rgFunctionChannelMap[chanFunction] == -1) {
            expectedValue = 0;  // 0 signals no mapping
        } else {
            expectedValue = _rgFunctionChannelMap[chanFunction] + 1; // 1-based
532
        }
533
        // qCDebug(PX4RCCalibrationTestLog) << chanFunction << expectedValue << mapParamsSet[PX4RCCalibration::_rgFunctionInfo[chanFunction].parameterName].toInt();
534
        QCOMPARE(_autopilot->getParameterFact(PX4RCCalibration::_rgFunctionInfo[chanFunction].parameterName)->value().toInt(), expectedValue);
535
    }
Don Gagne's avatar
Don Gagne committed
536
}