PX4RCCalibrationTest.cc 27.6 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
/*=====================================================================
 
 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"
25
#include "RadioComponentController.h"
26
#include "MultiVehicleManager.h"
27 28

/// @file
29
///     @brief QRadioComponentController Widget unit test
30 31 32
///
///     @author Don Gagne <don@thegagnes.com>

33 34
UT_REGISTER_TEST(RadioConfigTest)
QGC_LOGGING_CATEGORY(RadioConfigTestLog, "RadioConfigTestLog")
Don Gagne's avatar
Don Gagne committed
35

36 37 38 39
// 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) \
{ \
40 41 42 43
    if (_controller->_nextButton->isEnabled() != !!((mask) & nextButtonMask) || \
        _controller->_skipButton->isEnabled() != !!((mask) & skipButtonMask) || \
        _controller->_cancelButton->isEnabled() != !!((mask) & cancelButtonMask) ) { \
        qCDebug(RadioConfigTestLog) << _controller->_statusText->property("text"); \
44
    } \
45 46 47
    QCOMPARE(_controller->_nextButton->isEnabled(), !!((mask) & nextButtonMask)); \
    QCOMPARE(_controller->_skipButton->isEnabled(), !!((mask) & skipButtonMask)); \
    QCOMPARE(_controller->_cancelButton->isEnabled(), !!((mask) & cancelButtonMask)); \
48 49 50 51 52 53
}

// 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
        QTest::mouseClick(_cancelButton, Qt::LeftButton); \
56
        QCOMPARE(_controller->_rcCalState, RadioComponentController::rcCalStateChannelWait); \
57 58 59 60 61 62 63
        tryCancel ## cancelNum = false; \
        goto StartOver; \
    } else { \
        QTest::mouseClick(_nextButton, Qt::LeftButton); \
    } \
}

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

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

70
const struct RadioConfigTest::ChannelSettings RadioConfigTest::_rgChannelSettings[RadioConfigTest::_availableChannels] = {
Don Gagne's avatar
Don Gagne committed
71
	// Function										Min                 Max                 #  Reversed
72 73
	
	// Channel 0 : Not mapped to function, Simulate invalid Min/Max
74
	{ RadioComponentController::rcCalFunctionMax,			_testCenterValue,	_testCenterValue,   0, false },
75
	
76
    // Channels 1-4: Mapped to attitude control function
77 78 79 80
    { RadioComponentController::rcCalFunctionRoll,			_testMinValue,      _testMaxValue,      0, true },
    { RadioComponentController::rcCalFunctionPitch,			_testMinValue,      _testMaxValue,      0, false },
    { RadioComponentController::rcCalFunctionYaw,			_testMinValue,      _testMaxValue,      0, true },
    { RadioComponentController::rcCalFunctionThrottle,		_testMinValue,      _testMaxValue,      0,  false },
81 82 83
    
    // 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
84 85 86 87
    { RadioComponentController::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
    { RadioComponentController::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
    { RadioComponentController::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
    { RadioComponentController::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
88 89
    
    // Channels 9-11: Remainder of non-flight mode switches
90 91 92
    { RadioComponentController::rcCalFunctionFlaps,			_testMinValue,      _testMaxValue,      0, false },
    { RadioComponentController::rcCalFunctionAux1,			_testMinValue,      _testMaxValue,      0, false },
    { RadioComponentController::rcCalFunctionAux2,			_testMinValue,      _testMaxValue,      0, false },
93 94
	
    // Channel 12 : Not mapped to function, Simulate invalid Min, valid Max
95
    { RadioComponentController::rcCalFunctionMax,			_testCenterValue,	_testMaxValue,      0,  false },
96 97
    
	// Channel 13 : Not mapped to function, Simulate valid Min, invalid Max
98
	{ RadioComponentController::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
101 102 103 104
    { RadioComponentController::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
    { RadioComponentController::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
	{ RadioComponentController::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
	{ RadioComponentController::rcCalFunctionMax,			_testCenterValue,   _testCenterValue,   0,	false },
Don Gagne's avatar
Don Gagne committed
105 106
};

107 108 109
// Note the: 1500/*RadioComponentController::_rcCalPWMCenterPoint*/ entires. For some reason I couldn't get the compiler to do the
// right thing with the constant. So I just hacked inthe real value instead of fighting with it any longer.
const struct RadioConfigTest::ChannelSettings RadioConfigTest::_rgChannelSettingsValidate[RadioComponentController::_chanMax] = {
Don Gagne's avatar
Don Gagne committed
110
    // Function										Min Value									Max Value									Trim Value										Reversed
111
	
112
    // Channels 0: not mapped and should be set to defaults
113
	{ RadioComponentController::rcCalFunctionMax,			RadioComponentController::_rcCalPWMDefaultMinValue,	RadioComponentController::_rcCalPWMDefaultMaxValue,	1500/*RadioComponentController::_rcCalPWMCenterPoint*/,         false },
114
	
115
    // Channels 1-4: Mapped to attitude control function
116 117 118 119
	{ RadioComponentController::rcCalFunctionRoll,			_testMinValue,                              _testMaxValue,                              _testCenterValue,                               true },
    { RadioComponentController::rcCalFunctionPitch,			_testMinValue,                              _testMaxValue,                              _testCenterValue,                               false },
    { RadioComponentController::rcCalFunctionYaw,			_testMinValue,                              _testMaxValue,                              _testCenterValue,                               true },
    { RadioComponentController::rcCalFunctionThrottle,		_testMinValue,                              _testMaxValue,                              _testMinValue,                                  false },
120 121
    
    // Channels 5-8: not mapped and should be set to defaults
122 123 124 125
    { RadioComponentController::rcCalFunctionMax,			RadioComponentController::_rcCalPWMDefaultMinValue,	RadioComponentController::_rcCalPWMDefaultMaxValue,	1500/*RadioComponentController::_rcCalPWMCenterPoint*/,         false },
    { RadioComponentController::rcCalFunctionMax,			RadioComponentController::_rcCalPWMDefaultMinValue,	RadioComponentController::_rcCalPWMDefaultMaxValue,	1500/*RadioComponentController::_rcCalPWMCenterPoint*/,         false },
    { RadioComponentController::rcCalFunctionMax,			RadioComponentController::_rcCalPWMDefaultMinValue,	RadioComponentController::_rcCalPWMDefaultMaxValue,	1500/*RadioComponentController::_rcCalPWMCenterPoint*/,         false },
    { RadioComponentController::rcCalFunctionMax,			RadioComponentController::_rcCalPWMDefaultMinValue,	RadioComponentController::_rcCalPWMDefaultMaxValue,	1500/*RadioComponentController::_rcCalPWMCenterPoint*/,         false },
126 127
    
    // Channels 9-11: Remainder of non-flight mode switches
128 129 130
    { RadioComponentController::rcCalFunctionFlaps,			_testMinValue,                              _testMaxValue,                              _testCenterValue,                               false },
    { RadioComponentController::rcCalFunctionAux1,			_testMinValue,                              _testMaxValue,                              _testCenterValue,                               false },
    { RadioComponentController::rcCalFunctionAux2,			_testMinValue,                              _testMaxValue,                              _testCenterValue,                               false },
131
	
Don Gagne's avatar
Don Gagne committed
132
	// Channels 12-17 are not mapped and should be set to defaults
133 134 135 136 137 138
	{ RadioComponentController::rcCalFunctionMax,			RadioComponentController::_rcCalPWMDefaultMinValue,	RadioComponentController::_rcCalPWMDefaultMaxValue,	1500/*RadioComponentController::_rcCalPWMCenterPoint*/,         false },
	{ RadioComponentController::rcCalFunctionMax,			RadioComponentController::_rcCalPWMDefaultMinValue,	RadioComponentController::_rcCalPWMDefaultMaxValue,	1500/*RadioComponentController::_rcCalPWMCenterPoint*/,         false },
	{ RadioComponentController::rcCalFunctionMax,			RadioComponentController::_rcCalPWMDefaultMinValue,	RadioComponentController::_rcCalPWMDefaultMaxValue,	1500/*RadioComponentController::_rcCalPWMCenterPoint*/,         false },
	{ RadioComponentController::rcCalFunctionMax,			RadioComponentController::_rcCalPWMDefaultMinValue,	RadioComponentController::_rcCalPWMDefaultMaxValue,	1500/*RadioComponentController::_rcCalPWMCenterPoint*/,         false },
    { RadioComponentController::rcCalFunctionMax,			RadioComponentController::_rcCalPWMDefaultMinValue,	RadioComponentController::_rcCalPWMDefaultMaxValue,	1500/*RadioComponentController::_rcCalPWMCenterPoint*/,         false },
    { RadioComponentController::rcCalFunctionMax,			RadioComponentController::_rcCalPWMDefaultMinValue,	RadioComponentController::_rcCalPWMDefaultMaxValue,	1500/*RadioComponentController::_rcCalPWMCenterPoint*/,         false },
Don Gagne's avatar
Don Gagne committed
139 140
};

141 142 143
RadioConfigTest::RadioConfigTest(void) :
    _calWidget(NULL),
    _controller(NULL)
144
{
145
    
Don Gagne's avatar
Don Gagne committed
146 147
}

148
void RadioConfigTest::init(void)
149
{
Don Gagne's avatar
Don Gagne committed
150 151
    UnitTest::init();
    
152 153 154 155
    _mockLink = new MockLink();
    Q_CHECK_PTR(_mockLink);
    LinkManager::instance()->_addLink(_mockLink);
    LinkManager::instance()->connectLink(_mockLink);
156 157 158 159 160 161
    
    // Wait for the Vehicle to get created
    QSignalSpy spyVehicle(MultiVehicleManager::instance(), SIGNAL(parameterReadyVehicleAvailableChanged(bool)));
    QCOMPARE(spyVehicle.wait(5000), true);
    QVERIFY(MultiVehicleManager::instance()->parameterReadyVehicleAvailable());
    QVERIFY(MultiVehicleManager::instance()->activeVehicle());
162
    
163
    _autopilot = MultiVehicleManager::instance()->activeVehicle()->autopilotPlugin();
164
    Q_ASSERT(_autopilot);
165 166
    
    // This will instatiate the widget with an active uas with ready parameters
167
    _calWidget = new QGCQmlWidgetHolder(QString(), NULL);
168
    _calWidget->resize(600, 600);
169
    Q_CHECK_PTR(_calWidget);
170 171
    _calWidget->setAutoPilot(_autopilot);
    _calWidget->setSource(QUrl::fromUserInput("qrc:/qml/RadioComponent.qml"));
172
    
173 174 175
    // Nasty hack to get to controller
    _controller = RadioComponentController::_unitTestController;
    Q_ASSERT(_controller);
Don Gagne's avatar
Don Gagne committed
176

177
    _controller->_setUnitTestMode();
Don Gagne's avatar
Don Gagne committed
178 179 180 181
    
    _rgSignals[0] = SIGNAL(nextButtonMessageBoxDisplayed());
    _multiSpyNextButtonMessageBox = new MultiSignalSpy();
    Q_CHECK_PTR(_multiSpyNextButtonMessageBox);
182
    QCOMPARE(_multiSpyNextButtonMessageBox->init(_controller, _rgSignals, 1), true);
Don Gagne's avatar
Don Gagne committed
183
    
184
    QCOMPARE(_controller->_currentStep, -1);
185 186
}

187
void RadioConfigTest::cleanup(void)
188
{
Don Gagne's avatar
Don Gagne committed
189 190 191
    Q_ASSERT(_calWidget);
    delete _calWidget;
    
192 193
    // Disconnecting the link will prompt for log file save
    setExpectedFileDialog(getSaveFileName, QStringList());
194
    
195
    LinkManager::instance()->disconnectLink(_mockLink);
196
    
Don Gagne's avatar
Don Gagne committed
197
    UnitTest::cleanup();
198 199
}

Don Gagne's avatar
Don Gagne committed
200
/// @brief Test for correct behavior in determining minimum numbers of channels for flight.
201
void RadioConfigTest::_minRCChannels_test(void)
202 203
{
    // Next button won't be enabled until we see the minimum number of channels.
204 205
    for (int chan=0; chan<RadioComponentController::_chanMinimum; chan++) {
        _mockLink->emitRemoteControlChannelRawChanged(chan, (float)RadioComponentController::_rcCalPWMCenterPoint);
Don Gagne's avatar
Don Gagne committed
206 207
        
        // We use _chanCount internally so we should validate it
208
        QCOMPARE(_controller->_chanCount, chan+1);
Don Gagne's avatar
Don Gagne committed
209 210
        
        // Validate Next button state
211
        _controller->nextButtonClicked();
Don Gagne's avatar
Don Gagne committed
212
        bool signalFound = _multiSpyNextButtonMessageBox->waitForSignalByIndex(0, 200);
213
        if (chan == RadioComponentController::_chanMinimum - 1) {
Don Gagne's avatar
Don Gagne committed
214 215
            // Last channel should trigger Calibration available
            QCOMPARE(signalFound, false);
216
            QCOMPARE(_controller->_currentStep, 0);
217
        } else {
Don Gagne's avatar
Don Gagne committed
218 219
            // Still less than the minimum channels. Next button should show message box. Calibration should not start.
            QCOMPARE(signalFound, true);
220
            QCOMPARE(_controller->_currentStep, -1);
221
        }
Don Gagne's avatar
Don Gagne committed
222
        _multiSpyNextButtonMessageBox->clearAllSignals();
Don Gagne's avatar
Don Gagne committed
223

224 225 226
        // 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
227
        // Only available channels should have visible widget. A ui update cycle needs to have passed so we wait a little.
228 229 230
        QTest::qWait(RadioComponentController::_updateInterval * 2);
        for (int chanWidget=0; chanWidget<RadioComponentController::_chanMax; chanWidget++) {
            qCDebug(RadioConfigTestLog) << _rgValueWidget[chanWidget]->objectName() << chanWidget << chan;
Don Gagne's avatar
Don Gagne committed
231
            QCOMPARE(_rgValueWidget[chanWidget]->isVisible(), !!(chanWidget <= chan));
Don Gagne's avatar
Don Gagne committed
232
        }
233
#endif
234 235 236
    }
}

237
void RadioConfigTest::_beginCalibration(void)
238 239 240
{
    CHK_BUTTONS(nextButtonMask | cancelButtonMask);

Don Gagne's avatar
Don Gagne committed
241 242
    // We should already have enough channels to proceed with calibration. Click next to start the process.
    
243 244
    _controller->nextButtonClicked();
    QCOMPARE(_controller->_currentStep, 1);
245 246 247
    CHK_BUTTONS(cancelButtonMask);
}

248
void RadioConfigTest::_stickMoveWaitForSettle(int channel, int value)
249
{
250
    qCDebug(RadioConfigTestLog) << "_stickMoveWaitForSettle channel:value" << channel << value;
Don Gagne's avatar
Don Gagne committed
251 252

    // Move the stick, this will initialized the settle checker
253
    _mockLink->emitRemoteControlChannelRawChanged(channel, value);
Don Gagne's avatar
Don Gagne committed
254 255
    
    // Emit the signal again to start the settle timer
256
    _mockLink->emitRemoteControlChannelRawChanged(channel, value);
Don Gagne's avatar
Don Gagne committed
257 258
    
    // Wait long enough for the settle timer to expire
259
    QTest::qWait(RadioComponentController::_stickDetectSettleMSecs * 1.5);
Don Gagne's avatar
Don Gagne committed
260 261
    
    // Emit the signal again so that we detect stick settle
262
    _mockLink->emitRemoteControlChannelRawChanged(channel, value);
263 264
}

265
void RadioConfigTest::_stickMoveAutoStep(const char* functionStr, enum RadioComponentController::rcCalFunctions function, enum RadioConfigTest::MoveToDirection direction, bool identifyStep)
266
{
Don Gagne's avatar
Don Gagne committed
267
    Q_UNUSED(functionStr);
268
    qCDebug(RadioConfigTestLog) << "_stickMoveAutoStep function:direction:reversed:identifyStep" << functionStr << function << direction << identifyStep;
269
    
Don Gagne's avatar
Don Gagne committed
270 271 272
    CHK_BUTTONS(cancelButtonMask);
    
    int channel = _rgFunctionChannelMap[function];
273
    int saveStep = _controller->_currentStep;
Don Gagne's avatar
Don Gagne committed
274 275
    
    bool reversed = _rgChannelSettings[channel].reversed;
276
    
Don Gagne's avatar
Don Gagne committed
277 278
    if (!identifyStep && direction != moveToCenter) {
        // We have already identified the function channel mapping. Move other channels around to make sure there is no impact.
279
        
Don Gagne's avatar
Don Gagne committed
280
        int otherChannel = channel + 1;
281
        if (otherChannel >= RadioComponentController::_chanMax) {
Don Gagne's avatar
Don Gagne committed
282
            otherChannel = 0;
Don Gagne's avatar
Don Gagne committed
283 284
        }
        
Don Gagne's avatar
Don Gagne committed
285
        _stickMoveWaitForSettle(otherChannel, _testMinValue);
286
        QCOMPARE(_controller->_currentStep, saveStep);
Don Gagne's avatar
Don Gagne committed
287
        CHK_BUTTONS(cancelButtonMask);
288
        
289 290
        _stickMoveWaitForSettle(otherChannel, RadioComponentController::_rcCalPWMCenterPoint);
        QCOMPARE(_controller->_currentStep, saveStep);
Don Gagne's avatar
Don Gagne committed
291
        CHK_BUTTONS(cancelButtonMask);
292 293
    }
    
Don Gagne's avatar
Don Gagne committed
294 295 296 297 298 299 300 301
    // 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) {
302
        value = RadioComponentController::_rcCalPWMCenterPoint;
Don Gagne's avatar
Don Gagne committed
303 304
    } else {
        Q_ASSERT(false);
305
    }
Don Gagne's avatar
Don Gagne committed
306 307
    
    _stickMoveWaitForSettle(channel, value);
308
    QCOMPARE(_controller->_currentStep, saveStep + 1);
309 310
}

311
void RadioConfigTest::_switchMinMaxStep(void)
312
{
Don Gagne's avatar
Don Gagne committed
313
    CHK_BUTTONS(nextButtonMask | cancelButtonMask);
Don Gagne's avatar
Don Gagne committed
314 315
    
    // Try setting a min/max value that is below the threshold to make sure min/max doesn't go valid
316 317
    _mockLink->emitRemoteControlChannelRawChanged(0, (float)(RadioComponentController::_rcCalPWMValidMinValue + 1));
    _mockLink->emitRemoteControlChannelRawChanged(0, (float)(RadioComponentController::_rcCalPWMValidMaxValue - 1));
Don Gagne's avatar
Don Gagne committed
318 319
    
    // Send min/max values switch channels
320
    for (int chan=0; chan<_availableChannels; chan++) {
321 322
        _mockLink->emitRemoteControlChannelRawChanged(chan, _rgChannelSettings[chan].rcMin);
        _mockLink->emitRemoteControlChannelRawChanged(chan, _rgChannelSettings[chan].rcMax);
323
    }
Don Gagne's avatar
Don Gagne committed
324
    
Don Gagne's avatar
Don Gagne committed
325
    _channelHomePosition();
326

327 328 329
    int saveStep = _controller->_currentStep;
    _controller->nextButtonClicked();
    QCOMPARE(_controller->_currentStep, saveStep + 1);
330 331
}

332
void RadioConfigTest::_flapsDetectStep(void)
333
{
334
    int channel = _rgFunctionChannelMap[RadioComponentController::rcCalFunctionFlaps];
335
    
336
    qCDebug(RadioConfigTestLog) << "_flapsDetectStep channel" << channel;
337
    
Don Gagne's avatar
Don Gagne committed
338 339
    // Test code can't handle reversed flaps channel
    Q_ASSERT(!_rgChannelSettings[channel].reversed);
340
    
Don Gagne's avatar
Don Gagne committed
341 342
    CHK_BUTTONS(nextButtonMask | cancelButtonMask | skipButtonMask);
    
343
    int saveStep = _controller->_currentStep;
344

Don Gagne's avatar
Don Gagne committed
345 346 347
    // Wiggle channel to identify
    _stickMoveWaitForSettle(channel, _testMaxValue);
    _stickMoveWaitForSettle(channel, _testMinValue);
348
    
Don Gagne's avatar
Don Gagne committed
349 350
    // Leave channel on full flaps down
    _stickMoveWaitForSettle(channel, _testMaxValue);
351
    
Don Gagne's avatar
Don Gagne committed
352
    // User has to hit next at this step
353
    QCOMPARE(_controller->_currentStep, saveStep);
Don Gagne's avatar
Don Gagne committed
354
    CHK_BUTTONS(nextButtonMask | cancelButtonMask | skipButtonMask);
355 356
    _controller->nextButtonClicked();
    QCOMPARE(_controller->_currentStep, saveStep + 1);
357 358
}

359
void RadioConfigTest::_switchSelectAutoStep(const char* functionStr, RadioComponentController::rcCalFunctions function)
360
{
Don Gagne's avatar
Don Gagne committed
361
    Q_UNUSED(functionStr);
362
    ////qCDebug(RadioConfigTestLog)() << "_switchSelectAutoStep" << functionStr << "function:" << function;
363
    
Don Gagne's avatar
Don Gagne committed
364
    int buttonMask = cancelButtonMask;
365
    if (function != RadioComponentController::rcCalFunctionModeSwitch) {
Don Gagne's avatar
Don Gagne committed
366
        buttonMask |= skipButtonMask;
Don Gagne's avatar
Don Gagne committed
367 368
    }
    
Don Gagne's avatar
Don Gagne committed
369
    CHK_BUTTONS(buttonMask);
Don Gagne's avatar
Don Gagne committed
370
    
371
    int saveStep = _controller->_currentStep;
372
    
Don Gagne's avatar
Don Gagne committed
373 374
    // Wiggle stick for channel
    int channel = _rgFunctionChannelMap[function];
375 376
    _mockLink->emitRemoteControlChannelRawChanged(channel, _testMinValue);
    _mockLink->emitRemoteControlChannelRawChanged(channel, _testMaxValue);
Don Gagne's avatar
Don Gagne committed
377
    
378
    QCOMPARE(_controller->_currentStep, saveStep + 1);
379 380
}

381
void RadioConfigTest::_fullCalibration_test(void)
382
{
383
    // IMPORTANT NOTE: We used channels 1-5 for attitude mapping in the test below.
384
    // MockLink.params file cannot have flight mode switches mapped to those channels.
385 386 387 388 389
    // 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.
    
390
    for (int function=0; function<RadioComponentController::rcCalFunctionMax; function++) {
391 392 393
        bool found = false;
        
        // If we are mapping this function during cal set it into _rgFunctionChannelMap
394
        for (int channel=0; channel<RadioConfigTest::_availableChannels; channel++) {
395 396 397 398 399
            if (_rgChannelSettings[channel].function == function) {
                
                // First make sure this function isn't being use for a switch
                
                QStringList switchList;
400
                switchList << "RC_MAP_MODE_SW" << "RC_MAP_LOITER_SW" << "RC_MAP_RETURN_SW" << "RC_MAP_POSCTL_SW" << "RC_MAP_ACRO_SW";
401 402
                
                foreach (QString switchParam, switchList) {
Don Gagne's avatar
Don Gagne committed
403
                    Q_ASSERT(_autopilot->getParameterFact(FactSystem::defaultComponentId, switchParam)->value().toInt() != channel + 1);
404 405 406 407 408 409 410 411 412 413 414
                }
                
                _rgFunctionChannelMap[function] = channel;
                found = true;
                
                break;
            }
        }
        
        // If we aren't mapping this function during calibration, set it to the previous setting
        if (!found) {
415 416
            _rgFunctionChannelMap[function] = _autopilot->getParameterFact(FactSystem::defaultComponentId, RadioComponentController::_rgFunctionInfo[function].parameterName)->value().toInt();
            qCDebug(RadioConfigTestLog) << "Assigning switch" << function << _rgFunctionChannelMap[function];
417 418 419 420 421 422 423
            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
424 425
    
    _channelHomePosition();
426
    _controller->nextButtonClicked();
Don Gagne's avatar
Don Gagne committed
427
    _beginCalibration();
428 429 430 431 432 433 434 435 436
    _stickMoveAutoStep("Throttle", RadioComponentController::rcCalFunctionThrottle, moveToMax, true /* identify step */);
    _stickMoveAutoStep("Throttle", RadioComponentController::rcCalFunctionThrottle, moveToMin, false /* not identify step */);
    _stickMoveAutoStep("Yaw", RadioComponentController::rcCalFunctionYaw, moveToMax, true /* identify step */);
    _stickMoveAutoStep("Yaw", RadioComponentController::rcCalFunctionYaw, moveToMin, false /* not identify step */);
    _stickMoveAutoStep("Roll", RadioComponentController::rcCalFunctionRoll, moveToMax, true /* identify step */);
    _stickMoveAutoStep("Roll", RadioComponentController::rcCalFunctionRoll, moveToMin, false /* not identify step */);
    _stickMoveAutoStep("Pitch", RadioComponentController::rcCalFunctionPitch, moveToMax, true /* identify step */);
    _stickMoveAutoStep("Pitch", RadioComponentController::rcCalFunctionPitch, moveToMin, false /* not identify step */);
    _stickMoveAutoStep("Pitch", RadioComponentController::rcCalFunctionPitch, moveToCenter, false /* not identify step */);
Don Gagne's avatar
Don Gagne committed
437 438
    _switchMinMaxStep();
    _flapsDetectStep();
439 440 441
    _stickMoveAutoStep("Flaps", RadioComponentController::rcCalFunctionFlaps, moveToMin, false /* not identify step */);
    _switchSelectAutoStep("Aux1", RadioComponentController::rcCalFunctionAux1);
    _switchSelectAutoStep("Aux2", RadioComponentController::rcCalFunctionAux2);
442 443

    // One more click and the parameters should get saved
444
    _controller->nextButtonClicked();
Don Gagne's avatar
Don Gagne committed
445
    _validateParameters();
446 447
}

Don Gagne's avatar
Don Gagne committed
448
/// @brief Sets rc input to Throttle down home position. Centers all other channels.
449
void RadioConfigTest::_channelHomePosition(void)
450
{
Don Gagne's avatar
Don Gagne committed
451
    // Initialize available channels to center point.
Don Gagne's avatar
Don Gagne committed
452
    for (int i=0; i<_availableChannels; i++) {
453
        _mockLink->emitRemoteControlChannelRawChanged(i, (float)RadioComponentController::_rcCalPWMCenterPoint);
454
    }
Don Gagne's avatar
Don Gagne committed
455
    
456 457
    // 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.
458
    _mockLink->emitRemoteControlChannelRawChanged(_rgFunctionChannelMap[RadioComponentController::rcCalFunctionThrottle], _testMinValue - 1);
459 460
}

461
void RadioConfigTest::_validateParameters(void)
462 463 464 465 466 467
{
    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
468
    // Check mapping for all fuctions
469
    for (int chanFunction=0; chanFunction<RadioComponentController::rcCalFunctionMax; chanFunction++) {
Don Gagne's avatar
Don Gagne committed
470 471 472 473 474 475 476 477 478
        int chanIndex = _rgFunctionChannelMap[chanFunction];
        
        int expectedParameterValue;
        if (chanIndex == -1) {
            expectedParameterValue = 0;  // 0 signals no mapping
        } else {
            expectedParameterValue = chanIndex + 1; // 1-based parameter value
        }
        
479 480
        qCDebug(RadioConfigTestLog) << "Validate" << chanFunction << _autopilot->getParameterFact(FactSystem::defaultComponentId, RadioComponentController::_rgFunctionInfo[chanFunction].parameterName)->value().toInt();
        QCOMPARE(_autopilot->getParameterFact(FactSystem::defaultComponentId, RadioComponentController::_rgFunctionInfo[chanFunction].parameterName)->value().toInt(), expectedParameterValue);
Don Gagne's avatar
Don Gagne committed
481
    }
Don Gagne's avatar
Don Gagne committed
482 483

    // Validate the channel settings. Note the channels are 1-based in parameter names.
484
    for (int chan = 0; chan<RadioComponentController::_chanMax; chan++) {
Don Gagne's avatar
Don Gagne committed
485 486 487 488
        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
489 490 491 492 493
		int rcMinExpected = _rgChannelSettingsValidate[chan].rcMin;
		int rcMaxExpected = _rgChannelSettingsValidate[chan].rcMax;
		int rcTrimExpected = _rgChannelSettingsValidate[chan].rcTrim;
        bool rcReversedExpected = _rgChannelSettingsValidate[chan].reversed;

Don Gagne's avatar
Don Gagne committed
494
        int rcMinActual = _autopilot->getParameterFact(FactSystem::defaultComponentId, minTpl.arg(oneBasedChannel))->value().toInt(&convertOk);
Don Gagne's avatar
Don Gagne committed
495
        QCOMPARE(convertOk, true);
Don Gagne's avatar
Don Gagne committed
496
        int rcMaxActual = _autopilot->getParameterFact(FactSystem::defaultComponentId, maxTpl.arg(oneBasedChannel))->value().toInt(&convertOk);
Don Gagne's avatar
Don Gagne committed
497
        QCOMPARE(convertOk, true);
Don Gagne's avatar
Don Gagne committed
498
        int rcTrimActual = _autopilot->getParameterFact(FactSystem::defaultComponentId, trimTpl.arg(oneBasedChannel))->value().toInt(&convertOk);
Don Gagne's avatar
Don Gagne committed
499
        QCOMPARE(convertOk, true);
Don Gagne's avatar
Don Gagne committed
500
        float rcReversedFloat = _autopilot->getParameterFact(FactSystem::defaultComponentId, revTpl.arg(oneBasedChannel))->value().toFloat(&convertOk);
Don Gagne's avatar
Don Gagne committed
501 502 503
        QCOMPARE(convertOk, true);
        bool rcReversedActual = (rcReversedFloat == -1.0f);
        
Lorenz Meier's avatar
Lorenz Meier committed
504 505
        qCDebug(RadioConfigTestLog) << "_validateParameters expected channel:min:max:trim:rev" << chan << rcMinExpected << rcMaxExpected << rcTrimExpected << rcReversedExpected;
        qCDebug(RadioConfigTestLog) << "_validateParameters actual channel:min:max:trim:rev" << chan << rcMinActual << rcMaxActual << rcTrimActual << rcReversedActual;
506

Don Gagne's avatar
Don Gagne committed
507 508 509 510
        QCOMPARE(rcMinExpected, rcMinActual);
        QCOMPARE(rcMaxExpected, rcMaxActual);
        QCOMPARE(rcTrimExpected, rcTrimActual);
        QCOMPARE(rcReversedExpected, rcReversedActual);
511 512
    }
    
Don Gagne's avatar
Don Gagne committed
513
    // Check mapping for all fuctions
514
    for (int chanFunction=0; chanFunction<RadioComponentController::rcCalFunctionMax; chanFunction++) {
Don Gagne's avatar
Don Gagne committed
515 516 517 518 519
        int expectedValue;
        if (_rgFunctionChannelMap[chanFunction] == -1) {
            expectedValue = 0;  // 0 signals no mapping
        } else {
            expectedValue = _rgFunctionChannelMap[chanFunction] + 1; // 1-based
520
        }
521 522
        // qCDebug(RadioConfigTestLog) << chanFunction << expectedValue << mapParamsSet[RadioComponentController::_rgFunctionInfo[chanFunction].parameterName].toInt();
        QCOMPARE(_autopilot->getParameterFact(FactSystem::defaultComponentId, RadioComponentController::_rgFunctionInfo[chanFunction].parameterName)->value().toInt(), expectedValue);
523
    }
Don Gagne's avatar
Don Gagne committed
524
}