QGCPX4AirframeConfig.cc 10.7 KB
Newer Older
1
#include <QMessageBox>
2
#include <QDebug>
3

Lorenz Meier's avatar
Lorenz Meier committed
4 5 6
#include "QGCPX4AirframeConfig.h"
#include "ui_QGCPX4AirframeConfig.h"

7
#include "UASManager.h"
8
#include "LinkManager.h"
9 10
#include "UAS.h"

Lorenz Meier's avatar
Lorenz Meier committed
11 12
QGCPX4AirframeConfig::QGCPX4AirframeConfig(QWidget *parent) :
    QWidget(parent),
13
    mav(NULL),
14
    selectedId(-1),
Lorenz Meier's avatar
Lorenz Meier committed
15 16 17
    ui(new Ui::QGCPX4AirframeConfig)
{
    ui->setupUi(this);
18 19 20 21

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

22 23
    ui->planeComboBox->addItem(tr("Multiplex Easystar 1/2"), 100);
    ui->planeComboBox->addItem(tr("Hobbyking Bixler 1/2"), 101);
24

25
    connect(ui->planePushButton, SIGNAL(clicked()), this, SLOT(planeSelected()));
26 27
    connect(ui->planeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(planeSelected(int)));

28 29
    ui->flyingWingComboBox->addItem(tr("Bormatec Camflyer Q"), 30);
    ui->flyingWingComboBox->addItem(tr("Phantom FPV"), 31);
30

31
    connect(ui->flyingWingPushButton, SIGNAL(clicked()), this, SLOT(flyingWingSelected()));
32 33 34 35
    connect(ui->flyingWingComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(flyingWingSelected(int)));

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

38
    connect(ui->quadXPushButton, SIGNAL(clicked()), this, SLOT(quadXSelected()));
39 40
    connect(ui->quadXComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(quadXSelected(int)));

41
    connect(ui->quadPlusPushButton, SIGNAL(clicked()), this, SLOT(quadPlusSelected()));
42 43 44 45 46 47 48 49 50 51 52 53
    connect(ui->quadPlusComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(quadPlusSelected(int)));

    connect(ui->hexaXComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(hexaXSelected(int)));

    connect(ui->hexaPlusComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(hexaPlusSelected(int)));

    connect(ui->octoXComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(octoXSelected(int)));

    connect(ui->octoPlusComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(octoPlusSelected(int)));

    connect(ui->hComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(hSelected(int)));

54 55 56
    ui->hComboBox->addItem(tr("TBS Discovery"), 15);
    ui->hComboBox->addItem(tr("H Custom"), 16);

57 58
    connect(ui->applyButton, SIGNAL(clicked()), this, SLOT(applyAndReboot()));

59
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
60 61

    setActiveUAS(UASManager::instance()->getActiveUAS());
62 63

    uncheckAll();
64 65
}

Lorenz Meier's avatar
Lorenz Meier committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
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 {
            ui->statusLabel->setText(tr("System not configured for autostart."));
        }
    }
}

83 84
void QGCPX4AirframeConfig::setActiveUAS(UASInterface* uas)
{
Lorenz Meier's avatar
Lorenz Meier committed
85 86 87
    if (mav)
    {
        disconnect(mav, SIGNAL(parameterChanged(int,int,QString,QVariant)), this, SLOT(parameterChanged(int,int,QString,QVariant)));
88
        mav = NULL;
Lorenz Meier's avatar
Lorenz Meier committed
89
    }
90 91 92 93 94

    if (!uas)
        return;

    mav = uas;
95
    paramMgr = mav->getParamManager();
96

Lorenz Meier's avatar
Lorenz Meier committed
97
    connect(mav, SIGNAL(parameterChanged(int,int,QString,QVariant)), this, SLOT(parameterChanged(int,int,QString,QVariant)));
98
}
Lorenz Meier's avatar
Lorenz Meier committed
99

100 101 102 103 104 105 106 107 108 109 110
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);
111 112 113 114
}

void QGCPX4AirframeConfig::setAirframeID(int id)
{
115 116 117 118 119 120 121

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

    if (selectedId == id)
        return;

122
    selectedId = id;
Lorenz Meier's avatar
Lorenz Meier committed
123

124 125 126 127 128 129
    // 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);
130
        ui->quadXComboBox->setCurrentIndex(ui->quadXComboBox->findData(id));
131
        ui->statusLabel->setText(tr("Selected quad X (ID: #%1)").arg(selectedId));
Lorenz Meier's avatar
Lorenz Meier committed
132
    }
133
    else if (id >= 15 && id < 20)
Lorenz Meier's avatar
Lorenz Meier committed
134
    {
135
        ui->hPushButton->setChecked(true);
136 137
        ui->hComboBox->setCurrentIndex(ui->hComboBox->findData(id));
        ui->statusLabel->setText(tr("Selected H Frame (ID: #%1)").arg(selectedId));
138 139 140 141
    }
    else if (id >= 30 && id < 50)
    {
        ui->flyingWingPushButton->setChecked(true);
142
        ui->flyingWingComboBox->setCurrentIndex(ui->flyingWingComboBox->findData(id));
143
        ui->statusLabel->setText(tr("Selected flying wing (ID: #%1)").arg(selectedId));
144 145 146 147
    }
    else if (id >= 100 && id < 150)
    {
        ui->planePushButton->setChecked(true);
148
        ui->planeComboBox->setCurrentIndex(ui->planeComboBox->findData(id));
149
        ui->statusLabel->setText(tr("Selected plane (ID: #%1)").arg(selectedId));
Lorenz Meier's avatar
Lorenz Meier committed
150
    }
151 152 153 154
}

void QGCPX4AirframeConfig::applyAndReboot()
{
155
    // Guard against the case of an edit where we didn't receive all params yet
Lorenz Meier's avatar
Lorenz Meier committed
156
    if (selectedId <= 0)
157 158 159 160 161 162 163 164 165 166 167 168 169 170
    {
        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;

171 172
    if (paramMgr->countOnboardParams() == 0 &&
            paramMgr->countPendingParams() == 0)
173
    {
174
        paramMgr->requestParameterListIfEmpty();
175 176 177 178
        QGC::SLEEP::msleep(100);
    }

    // Guard against the case of an edit where we didn't receive all params yet
179
    if (paramMgr->countPendingParams() > 0)
180 181 182 183 184 185 186
    {
        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();
187 188

        return;
189 190
    }

191
    QList<int> components = paramMgr->getComponentForParam("SYS_AUTOSTART");
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

    // 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;
    }

    qDebug() << "Setting comp" << components.first() << "SYS_AUTOSTART" << (qint32)selectedId;

207
    paramMgr->setPendingParam(components.first(),"SYS_AUTOSTART", (qint32)selectedId);
Lorenz Meier's avatar
Lorenz Meier committed
208

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

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

216
    // Reboot
217
    //TODO right now this relies upon the above send & persist finishing before the reboot command is received...
218 219 220 221 222 223 224
    QMessageBox msgBox(this);
    msgBox.setText(tr("Storing Parameters and Rebooting Autopilot"));
    msgBox.setInformativeText(tr("Please wait a few seconds for the reboot to complete."));
    msgBox.setStandardButtons(QMessageBox::NoButton);
    msgBox.setModal(false);
    msgBox.show();
    QGC::SLEEP::sleep(2);
225
    mav->executeCommand(MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN, 1, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0);
226 227 228 229 230
    QGC::SLEEP::msleep(200);
    LinkManager::instance()->disconnectAll();
    QGC::SLEEP::sleep(8);
    LinkManager::instance()->connectAll();
    msgBox.close();
231 232 233 234 235 236
}

void QGCPX4AirframeConfig::setAutoConfig(bool enabled)
{
    if (!mav)
        return;
237
    paramMgr->setPendingParam(0, "SYS_AUTOCONFIG", (qint32) ((enabled) ? 1 : 0));
238 239 240 241
}

void QGCPX4AirframeConfig::flyingWingSelected()
{
242
    flyingWingSelected(ui->flyingWingComboBox->currentIndex());
Lorenz Meier's avatar
Lorenz Meier committed
243 244
}

245 246 247 248 249 250 251 252
void QGCPX4AirframeConfig::flyingWingSelected(int index)
{
    int system_index = ui->flyingWingComboBox->itemData(index).toInt();
    setAirframeID(system_index);
}

void QGCPX4AirframeConfig::planeSelected()
{
253
    planeSelected(ui->planeComboBox->currentIndex());
254 255 256 257 258 259 260 261 262 263 264
}

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


void QGCPX4AirframeConfig::quadXSelected()
{
265
    quadXSelected(ui->quadXComboBox->currentIndex());
266 267 268 269 270 271 272 273 274 275
}

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

void QGCPX4AirframeConfig::quadPlusSelected()
{
276
    quadPlusSelected(ui->quadPlusComboBox->currentIndex());
277 278 279 280 281 282 283 284 285 286
}

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

void QGCPX4AirframeConfig::hexaXSelected()
{
287
    hexaXSelected(ui->hexaXComboBox->currentIndex());
288 289 290 291 292 293 294 295 296 297
}

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

void QGCPX4AirframeConfig::hexaPlusSelected()
{
298
    hexaPlusSelected(ui->hexaPlusComboBox->currentIndex());
299 300 301 302 303 304 305 306 307 308
}

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

void QGCPX4AirframeConfig::octoXSelected()
{
309
    octoXSelected(ui->octoXComboBox->currentIndex());
310 311 312 313 314 315 316 317 318 319
}

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

void QGCPX4AirframeConfig::octoPlusSelected()
{
320
    octoPlusSelected(ui->octoPlusComboBox->currentIndex());
321 322 323 324 325 326 327 328 329 330
}

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

void QGCPX4AirframeConfig::hSelected()
{
331
    hSelected(ui->hComboBox->currentIndex());
332 333 334 335 336 337 338 339 340
}

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


Lorenz Meier's avatar
Lorenz Meier committed
341 342 343 344
QGCPX4AirframeConfig::~QGCPX4AirframeConfig()
{
    delete ui;
}