QGCPX4AirframeConfig.cc 15.4 KB
Newer Older
1
#include <QMessageBox>
2
#include <QProgressDialog>
3
#include <QDebug>
4
#include <QTimer>
5

Lorenz Meier's avatar
Lorenz Meier committed
6 7 8
#include "QGCPX4AirframeConfig.h"
#include "ui_QGCPX4AirframeConfig.h"

9
#include "UASManager.h"
10
#include "LinkManager.h"
11
#include "UAS.h"
12
#include "QGC.h"
13

Lorenz Meier's avatar
Lorenz Meier committed
14 15
QGCPX4AirframeConfig::QGCPX4AirframeConfig(QWidget *parent) :
    QWidget(parent),
16
    mav(NULL),
17 18 19
    progress(NULL),
    pendingParams(0),
    configState(CONFIG_STATE_ABORT),
20
    selectedId(-1),
Lorenz Meier's avatar
Lorenz Meier committed
21 22 23
    ui(new Ui::QGCPX4AirframeConfig)
{
    ui->setupUi(this);
24 25 26 27

    // Fill the lists here manually in accordance with the list from:
    // https://github.com/PX4/Firmware/blob/master/ROMFS/px4fmu_common/init.d/rcS

28 29
    ui->planeComboBox->addItem(tr("Multiplex Easystar 1/2"), 100);
    ui->planeComboBox->addItem(tr("Hobbyking Bixler 1/2"), 101);
30

31
    connect(ui->planePushButton, SIGNAL(clicked()), this, SLOT(planeSelected()));
Lorenz Meier's avatar
Lorenz Meier committed
32
    connect(ui->planeComboBox, SIGNAL(activated(int)), this, SLOT(planeSelected(int)));
33

34 35
    ui->flyingWingComboBox->addItem(tr("Bormatec Camflyer Q"), 30);
    ui->flyingWingComboBox->addItem(tr("Phantom FPV"), 31);
36

37
    connect(ui->flyingWingPushButton, SIGNAL(clicked()), this, SLOT(flyingWingSelected()));
Lorenz Meier's avatar
Lorenz Meier committed
38
    connect(ui->flyingWingComboBox, SIGNAL(activated(int)), this, SLOT(flyingWingSelected(int)));
39 40 41

    ui->quadXComboBox->addItem(tr("Standard 10\" Quad"), 1);
    ui->quadXComboBox->addItem(tr("DJI F330 8\" Quad"), 10);
42
    ui->quadXComboBox->addItem(tr("Turnigy Talon v2 X550 Quad"), 666);
43

44
    connect(ui->quadXPushButton, SIGNAL(clicked()), this, SLOT(quadXSelected()));
Lorenz Meier's avatar
Lorenz Meier committed
45
    connect(ui->quadXComboBox, SIGNAL(activated(int)), this, SLOT(quadXSelected(int)));
46

47
    connect(ui->quadPlusPushButton, SIGNAL(clicked()), this, SLOT(quadPlusSelected()));
Lorenz Meier's avatar
Lorenz Meier committed
48
    connect(ui->quadPlusComboBox, SIGNAL(activated(int)), this, SLOT(quadPlusSelected(int)));
Lorenz Meier's avatar
Lorenz Meier committed
49
    ui->quadPlusPushButton->setEnabled(ui->quadPlusComboBox->count() > 0);
50

Lorenz Meier's avatar
Lorenz Meier committed
51
    connect(ui->hexaXPushButton, SIGNAL(clicked()), this, SLOT(hexaXSelected()));
Lorenz Meier's avatar
Lorenz Meier committed
52
    connect(ui->hexaXComboBox, SIGNAL(activated(int)), this, SLOT(hexaXSelected(int)));
Lorenz Meier's avatar
Lorenz Meier committed
53
    ui->hexaXPushButton->setEnabled(ui->hexaXComboBox->count() > 0);
54

Lorenz Meier's avatar
Lorenz Meier committed
55
    connect(ui->hexaPlusPushButton, SIGNAL(clicked()), this, SLOT(hexaPlusSelected()));
Lorenz Meier's avatar
Lorenz Meier committed
56
    connect(ui->hexaPlusComboBox, SIGNAL(activated(int)), this, SLOT(hexaPlusSelected(int)));
Lorenz Meier's avatar
Lorenz Meier committed
57
    ui->hexaPlusPushButton->setEnabled(ui->hexaPlusComboBox->count() > 0);
58

Lorenz Meier's avatar
Lorenz Meier committed
59
    connect(ui->octoXPushButton, SIGNAL(clicked()), this, SLOT(octoXSelected()));
Lorenz Meier's avatar
Lorenz Meier committed
60
    connect(ui->octoXComboBox, SIGNAL(activated(int)), this, SLOT(octoXSelected(int)));
Lorenz Meier's avatar
Lorenz Meier committed
61
    ui->octoXPushButton->setEnabled(ui->octoXComboBox->count() > 0);
62

Lorenz Meier's avatar
Lorenz Meier committed
63
    connect(ui->octoPlusPushButton, SIGNAL(clicked()), this, SLOT(octoPlusSelected()));
Lorenz Meier's avatar
Lorenz Meier committed
64
    connect(ui->octoPlusComboBox, SIGNAL(activated(int)), this, SLOT(octoPlusSelected(int)));
Lorenz Meier's avatar
Lorenz Meier committed
65
    ui->octoPlusPushButton->setEnabled(ui->octoPlusComboBox->count() > 0);
66

67 68 69
    ui->hComboBox->addItem(tr("TBS Discovery"), 15);
    ui->hComboBox->addItem(tr("H Custom"), 16);

Lorenz Meier's avatar
Lorenz Meier committed
70 71 72 73
    connect(ui->hPushButton, SIGNAL(clicked()), this, SLOT(hSelected()));
    connect(ui->hComboBox, SIGNAL(activated(int)), this, SLOT(hSelected(int)));
    ui->hPushButton->setEnabled(ui->hComboBox->count() > 0);

74 75
    connect(ui->applyButton, SIGNAL(clicked()), this, SLOT(applyAndReboot()));

76
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
77 78

    setActiveUAS(UASManager::instance()->getActiveUAS());
79 80

    uncheckAll();
81 82
}

Lorenz Meier's avatar
Lorenz Meier committed
83 84 85 86 87 88 89 90 91 92 93 94
void QGCPX4AirframeConfig::parameterChanged(int uas, int component, QString parameterName, QVariant value)
{
    Q_UNUSED(uas);
    Q_UNUSED(component);

    if (parameterName.contains("SYS_AUTOSTART"))
    {
        int index = value.toInt();
        if (index > 0) {
            setAirframeID(index);
            ui->statusLabel->setText(tr("Onboard start script ID: #%1").arg(index));
        } else {
95
            uncheckAll();
Lorenz Meier's avatar
Lorenz Meier committed
96 97 98 99 100
            ui->statusLabel->setText(tr("System not configured for autostart."));
        }
    }
}

101 102
void QGCPX4AirframeConfig::setActiveUAS(UASInterface* uas)
{
Lorenz Meier's avatar
Lorenz Meier committed
103 104 105
    if (mav)
    {
        disconnect(mav, SIGNAL(parameterChanged(int,int,QString,QVariant)), this, SLOT(parameterChanged(int,int,QString,QVariant)));
106
        mav = NULL;
Lorenz Meier's avatar
Lorenz Meier committed
107
    }
108 109 110 111 112

    if (!uas)
        return;

    mav = uas;
113
    paramMgr = mav->getParamManager();
114

Lorenz Meier's avatar
Lorenz Meier committed
115
    connect(mav, SIGNAL(parameterChanged(int,int,QString,QVariant)), this, SLOT(parameterChanged(int,int,QString,QVariant)));
116
}
Lorenz Meier's avatar
Lorenz Meier committed
117

118 119 120 121 122 123 124 125 126 127 128
void QGCPX4AirframeConfig::uncheckAll()
{
    ui->planePushButton->setChecked(false);
    ui->flyingWingPushButton->setChecked(false);
    ui->quadXPushButton->setChecked(false);
    ui->quadPlusPushButton->setChecked(false);
    ui->hexaXPushButton->setChecked(false);
    ui->hexaPlusPushButton->setChecked(false);
    ui->octoXPushButton->setChecked(false);
    ui->octoPlusPushButton->setChecked(false);
    ui->hPushButton->setChecked(false);
129 130 131 132
}

void QGCPX4AirframeConfig::setAirframeID(int id)
{
133 134 135 136

    qDebug() << "setAirframeID" << id;
    ui->statusLabel->setText(tr("Start script ID: #%1").arg(id));

137
    selectedId = id;
Lorenz Meier's avatar
Lorenz Meier committed
138

139 140 141 142 143 144
    // XXX too much boilerplate code here - this widget is really just
    // a quick hack to get started
    uncheckAll();

    if (id > 0 && id < 15) {
        ui->quadXPushButton->setChecked(true);
145
        ui->quadXComboBox->setCurrentIndex(ui->quadXComboBox->findData(id));
146
        ui->statusLabel->setText(tr("Selected quad X (ID: #%1)").arg(selectedId));
Lorenz Meier's avatar
Lorenz Meier committed
147
    }
148
    else if (id >= 15 && id < 20)
Lorenz Meier's avatar
Lorenz Meier committed
149
    {
150
        ui->hPushButton->setChecked(true);
151 152
        ui->hComboBox->setCurrentIndex(ui->hComboBox->findData(id));
        ui->statusLabel->setText(tr("Selected H Frame (ID: #%1)").arg(selectedId));
153 154 155 156
    }
    else if (id >= 30 && id < 50)
    {
        ui->flyingWingPushButton->setChecked(true);
157
        ui->flyingWingComboBox->setCurrentIndex(ui->flyingWingComboBox->findData(id));
158
        ui->statusLabel->setText(tr("Selected flying wing (ID: #%1)").arg(selectedId));
159 160 161 162
    }
    else if (id >= 100 && id < 150)
    {
        ui->planePushButton->setChecked(true);
163
        ui->planeComboBox->setCurrentIndex(ui->planeComboBox->findData(id));
164
        ui->statusLabel->setText(tr("Selected plane (ID: #%1)").arg(selectedId));
Lorenz Meier's avatar
Lorenz Meier committed
165
    }
166 167 168 169
}

void QGCPX4AirframeConfig::applyAndReboot()
{
170
    // Guard against the case of an edit where we didn't receive all params yet
Lorenz Meier's avatar
Lorenz Meier committed
171
    if (selectedId <= 0)
172 173 174 175 176 177 178 179 180 181 182 183 184 185
    {
        QMessageBox msgBox;
        msgBox.setText(tr("No airframe selected"));
        msgBox.setInformativeText(tr("Please select an airframe first."));
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        (void)msgBox.exec();

        return;
    }

    if (!mav)
        return;

186 187
    if (paramMgr->countOnboardParams() == 0 &&
            paramMgr->countPendingParams() == 0)
188
    {
189 190
        paramMgr->requestParameterList();
        QGC::SLEEP::msleep(300);
191 192
    }

193 194
    QList<int> components = paramMgr->getComponentForParam("SYS_AUTOSTART");

195
    // Guard against the case of an edit where we didn't receive all params yet
196
    if (paramMgr->countPendingParams() > 0 || components.count() == 0)
197 198 199 200 201 202 203
    {
        QMessageBox msgBox;
        msgBox.setText(tr("Parameter sync with UAS not yet complete"));
        msgBox.setInformativeText(tr("Please wait a few moments and retry"));
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        (void)msgBox.exec();
204 205

        return;
206 207 208 209 210 211 212 213 214 215 216 217 218 219
    }

    // Guard against multiple components responding - this will never show in practice
    if (components.count() != 1) {
        QMessageBox msgBox;
        msgBox.setText(tr("Invalid system setup detected"));
        msgBox.setInformativeText(tr("None or more than one component advertised to provide the main system configuration option. This is an invalid system setup - please check your autopilot."));
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        (void)msgBox.exec();

        return;
    }

220 221 222 223 224 225 226 227 228 229 230 231 232 233
    // This is really evil: 'fake' a thread by
    // periodic work queue calls and clock
    // through a small state machine
    // ugh.. if we just had time to do this properly.

    // To the reader who can't program and wants to whine:
    // this is not beautiful, but technically completely
    // sound. If you want to fix it, you'd be welcome
    // to rebase the link, param manager and UI classes
    // on a proper threading framework - which I'd love to do
    // if I just had more time..

    configState = CONFIG_STATE_SEND;
    QTimer::singleShot(200, this, SLOT(checkConfigState()));
Lorenz Meier's avatar
Lorenz Meier committed
234
    setEnabled(false);
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
}

void QGCPX4AirframeConfig::checkConfigState()
{

    if (configState == CONFIG_STATE_SEND)
    {
        QList<int> components = paramMgr->getComponentForParam("SYS_AUTOSTART");
        qDebug() << "Setting comp" << components.first() << "SYS_AUTOSTART" << (qint32)selectedId;

        paramMgr->setPendingParam(components.first(),"SYS_AUTOSTART", (qint32)selectedId);

        //need to set autoconfig in order for PX4 to pick up the selected airframe params
        if (ui->defaultGainsCheckBox->checkState() == Qt::Checked)
                setAutoConfig(true);

        // Send pending params and then write them to persistent storage when done
        paramMgr->sendPendingParameters(true);

        configState = CONFIG_STATE_WAIT_PENDING;
        pendingParams = 0;
        QTimer::singleShot(2000, this, SLOT(checkConfigState()));
        return;
    }

    if (configState == CONFIG_STATE_WAIT_PENDING) {
        // Guard against the case of an edit where we didn't receive all params yet
        if (paramMgr->countPendingParams() > 0)
        {
            if (pendingParams == 0) {

                pendingParams = paramMgr->countPendingParams();

                if (progress)
                    delete progress;

                progress = new QProgressDialog("Writing parameters", "Abort Send", 0, pendingParams, this);
                progress->setWindowModality(Qt::WindowModal);
                progress->setMinimumDuration(2000);
            }

            qDebug() << "PENDING" << paramMgr->countPendingParams() << "PROGRESS" << pendingParams - paramMgr->countPendingParams();
            progress->setValue(pendingParams - paramMgr->countPendingParams());

            if (progress->wasCanceled()) {
                configState = CONFIG_STATE_ABORT;
Lorenz Meier's avatar
Lorenz Meier committed
281
                setEnabled(true);
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
                pendingParams = 0;
                return;
            }
        } else {
            pendingParams = 0;
            configState = CONFIG_STATE_REBOOT;
        }

        qDebug() << "PENDING PARAMS WAIT PENDING: " << paramMgr->countPendingParams();
        QTimer::singleShot(1000, this, SLOT(checkConfigState()));
        return;
    }

    if (configState == CONFIG_STATE_REBOOT) {

        // Reboot
        //TODO right now this relies upon the above send & persist finishing before the reboot command is received...

        unsigned pendingMax = 20;

        qDebug() << "PENDING PARAMS REBOOT BEFORE" << pendingParams;

        if (pendingParams == 0) {
            pendingParams = 1;

            if (progress)
                delete progress;

            progress = new QProgressDialog("Waiting for autopilot reboot", "Abort", 0, pendingMax, this);
            progress->setWindowModality(Qt::WindowModal);
            qDebug() << "Waiting for reboot, pending" << pendingParams;
Lorenz Meier's avatar
Lorenz Meier committed
313 314 315 316 317 318 319
        } else {
            if (progress->wasCanceled()) {
                configState = CONFIG_STATE_ABORT;
                setEnabled(true);
                pendingParams = 0;
                return;
            }
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
        }

        if (pendingParams == 3) {
            qDebug() << "REQUESTING REBOOT";
            mav->executeCommand(MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN, 1, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0);
            mav->executeCommand(MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN, 1, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0);
        }

        if (pendingParams == 4) {
            qDebug() << "DISCONNECT AIRFRAME";
            LinkManager::instance()->disconnectAll();
        }

        if (pendingParams == 14) {
            qDebug() << "CONNECT AIRFRAME";
            LinkManager::instance()->connectAll();
        }
        if (pendingParams == 15) {
            qDebug() << "DISCONNECT AIRFRAME";
            LinkManager::instance()->disconnectAll();
        }
        if (pendingParams == 16) {
            qDebug() << "CONNECT AIRFRAME";
            LinkManager::instance()->connectAll();
        }

        if (pendingParams < pendingMax) {
            progress->setValue(pendingParams);
            QTimer::singleShot(1000, this, SLOT(checkConfigState()));
        } else {
            paramMgr->requestParameterList();
            progress->setValue(pendingMax);
            configState = CONFIG_STATE_ABORT;
            pendingParams = 0;
Lorenz Meier's avatar
Lorenz Meier committed
354
            setEnabled(true);
355 356 357 358 359 360
            return;
        }
        qDebug() << "PENDING PARAMS REBOOT AFTER:" << pendingParams;
        pendingParams++;
        return;
    }
361 362 363 364 365 366
}

void QGCPX4AirframeConfig::setAutoConfig(bool enabled)
{
    if (!mav)
        return;
367
    paramMgr->setPendingParam(0, "SYS_AUTOCONFIG", (qint32) ((enabled) ? 1 : 0));
368 369 370 371
}

void QGCPX4AirframeConfig::flyingWingSelected()
{
372
    flyingWingSelected(ui->flyingWingComboBox->currentIndex());
Lorenz Meier's avatar
Lorenz Meier committed
373 374
}

375 376 377 378 379 380 381 382
void QGCPX4AirframeConfig::flyingWingSelected(int index)
{
    int system_index = ui->flyingWingComboBox->itemData(index).toInt();
    setAirframeID(system_index);
}

void QGCPX4AirframeConfig::planeSelected()
{
383
    planeSelected(ui->planeComboBox->currentIndex());
384 385 386 387 388 389 390 391 392 393 394
}

void QGCPX4AirframeConfig::planeSelected(int index)
{
    int system_index = ui->planeComboBox->itemData(index).toInt();
    setAirframeID(system_index);
}


void QGCPX4AirframeConfig::quadXSelected()
{
395
    quadXSelected(ui->quadXComboBox->currentIndex());
396 397 398 399 400 401 402 403 404 405
}

void QGCPX4AirframeConfig::quadXSelected(int index)
{
    int system_index = ui->quadXComboBox->itemData(index).toInt();
    setAirframeID(system_index);
}

void QGCPX4AirframeConfig::quadPlusSelected()
{
406
    quadPlusSelected(ui->quadPlusComboBox->currentIndex());
407 408 409 410 411 412 413 414 415 416
}

void QGCPX4AirframeConfig::quadPlusSelected(int index)
{
    int system_index = ui->quadPlusComboBox->itemData(index).toInt();
    setAirframeID(system_index);
}

void QGCPX4AirframeConfig::hexaXSelected()
{
417
    hexaXSelected(ui->hexaXComboBox->currentIndex());
418 419 420 421 422 423 424 425 426 427
}

void QGCPX4AirframeConfig::hexaXSelected(int index)
{
    int system_index = ui->hexaXComboBox->itemData(index).toInt();
    setAirframeID(system_index);
}

void QGCPX4AirframeConfig::hexaPlusSelected()
{
428
    hexaPlusSelected(ui->hexaPlusComboBox->currentIndex());
429 430 431 432 433 434 435 436 437 438
}

void QGCPX4AirframeConfig::hexaPlusSelected(int index)
{
    int system_index = ui->hexaPlusComboBox->itemData(index).toInt();
    setAirframeID(system_index);
}

void QGCPX4AirframeConfig::octoXSelected()
{
439
    octoXSelected(ui->octoXComboBox->currentIndex());
440 441 442 443 444 445 446 447 448 449
}

void QGCPX4AirframeConfig::octoXSelected(int index)
{
    int system_index = ui->octoXComboBox->itemData(index).toInt();
    setAirframeID(system_index);
}

void QGCPX4AirframeConfig::octoPlusSelected()
{
450
    octoPlusSelected(ui->octoPlusComboBox->currentIndex());
451 452 453 454 455 456 457 458 459 460
}

void QGCPX4AirframeConfig::octoPlusSelected(int index)
{
    int system_index = ui->octoPlusComboBox->itemData(index).toInt();
    setAirframeID(system_index);
}

void QGCPX4AirframeConfig::hSelected()
{
461
    hSelected(ui->hComboBox->currentIndex());
462 463 464 465 466 467 468 469 470
}

void QGCPX4AirframeConfig::hSelected(int index)
{
    int system_index = ui->hComboBox->itemData(index).toInt();
    setAirframeID(system_index);
}


Lorenz Meier's avatar
Lorenz Meier committed
471 472 473 474
QGCPX4AirframeConfig::~QGCPX4AirframeConfig()
{
    delete ui;
}