QGCPX4SensorCalibration.cc 7.64 KB
Newer Older
1 2 3 4 5
#include "QGCPX4SensorCalibration.h"
#include "ui_QGCPX4SensorCalibration.h"
#include <UASManager.h>
#include <QMenu>
#include <QScrollBar>
6
#include <QDebug>
7 8 9 10 11

QGCPX4SensorCalibration::QGCPX4SensorCalibration(QWidget *parent) :
    QWidget(parent),
    activeUAS(NULL),
    clearAction(new QAction(tr("Clear Text"), this)),
12 13 14
    accelStarted(false),
    gyroStarted(false),
    magStarted(false),
15 16
    ui(new Ui::QGCPX4SensorCalibration)
{
17 18 19 20 21 22 23
    accelAxes << "x+";
    accelAxes << "x-";
    accelAxes << "y+";
    accelAxes << "y-";
    accelAxes << "z+";
    accelAxes << "z-";

Lorenz Meier's avatar
Lorenz Meier committed
24

25 26 27
    ui->setupUi(this);
    connect(clearAction, SIGNAL(triggered()), ui->textView, SLOT(clear()));

28 29 30 31 32 33 34 35
    connect(ui->gyroButton, SIGNAL(clicked()), this, SLOT(gyroButtonClicked()));
    connect(ui->magButton, SIGNAL(clicked()), this, SLOT(magButtonClicked()));
    connect(ui->accelButton, SIGNAL(clicked()), this, SLOT(accelButtonClicked()));

    ui->gyroButton->setEnabled(false);
    ui->magButton->setEnabled(false);
    ui->accelButton->setEnabled(false);

Lorenz Meier's avatar
Lorenz Meier committed
36
    setInstructionImage(":/files/images/px4/calibration/accel_z-.png");
37 38 39 40 41 42 43

    setObjectName("PX4_SENSOR_CALIBRATION");

    setStyleSheet("QScrollArea { border: 0px; } QPlainTextEdit { border: 0px }");

    setActiveUAS(UASManager::instance()->getActiveUAS());
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
44
    ui->progressBar->setValue(0);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
}

QGCPX4SensorCalibration::~QGCPX4SensorCalibration()
{
    delete ui;
}

void QGCPX4SensorCalibration::setInstructionImage(const QString &path)
{
    instructionIcon.load(path);

    int w = ui->iconLabel->width();
    int h = ui->iconLabel->height();

    ui->iconLabel->setPixmap(instructionIcon.scaled(w, h, Qt::KeepAspectRatio));
}

void QGCPX4SensorCalibration::resizeEvent(QResizeEvent* event)
{

    int w = ui->iconLabel->width();
    int h = ui->iconLabel->height();

    ui->iconLabel->setPixmap(instructionIcon.scaled(w, h, Qt::KeepAspectRatio));

    QWidget::resizeEvent(event);
}

void QGCPX4SensorCalibration::setActiveUAS(UASInterface* uas)
{
    if (!uas)
        return;

    if (activeUAS) {
        disconnect(uas, SIGNAL(textMessageReceived(int,int,int,QString)), this, SLOT(handleTextMessage(int,int,int,QString)));
        ui->textView->clear();
    }

83 84 85 86
    ui->gyroButton->setEnabled(true);
    ui->magButton->setEnabled(true);
    ui->accelButton->setEnabled(true);

87 88 89 90 91 92
    connect(uas, SIGNAL(textMessageReceived(int,int,int,QString)), this, SLOT(handleTextMessage(int,int,int,QString)));
    activeUAS = uas;
}

void QGCPX4SensorCalibration::handleTextMessage(int uasid, int compId, int severity, QString text)
{
Lorenz Meier's avatar
Lorenz Meier committed
93 94 95 96
    Q_UNUSED(uasid);
    Q_UNUSED(compId);
    Q_UNUSED(severity);

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    if (text.startsWith("[cmd]") ||
        text.startsWith("[mavlink pm]"))
        return;

    if (text.contains("progress")) {
        qDebug() << "PROGRESS:" << text;
        QString percent = text.split("<").last().split(">").first();
        qDebug() << "PERCENT CANDIDATE" << percent;
        bool ok;
        int p = percent.toInt(&ok);
        if (ok)
            ui->progressBar->setValue(p);
        return;
    }

Lorenz Meier's avatar
Lorenz Meier committed
112 113
    ui->instructionLabel->setText(QString("%1").arg(text));

114 115 116 117

    if (text.contains("accel")) {
        qDebug() << "ACCEL" << text;

Lorenz Meier's avatar
Lorenz Meier committed
118 119
        if (text.startsWith("accel measurement started: ")) {
            QString axis = text.split("measurement started: ").last().left(2);
120
            qDebug() << "AXIS" << axis << "STR" << text;
Lorenz Meier's avatar
Lorenz Meier committed
121
            setInstructionImage(QString(":/files/images/px4/calibration/accel_%1.png").arg(axis));
122 123 124 125

        }
    }

Lorenz Meier's avatar
Lorenz Meier committed
126 127 128 129 130 131 132 133 134 135 136
    if (text.startsWith("directions left")) {
        for (int i = 0; i < 6; i++)
        {
            if (!text.contains(accelAxes[i])) {
                qDebug() << "FINISHED" << accelAxes[i];
                accelDone[i] = true;
            }
        }
    }

    if (text.startsWith("result for")) {
137

Lorenz Meier's avatar
Lorenz Meier committed
138
        QString axis = text.split("result for ").last().left(2);
139 140 141 142 143

        qDebug() << "ACCELDONE AXIS" << axis << "STR" << text;

        for (int i = 0; i < 6; i++)
        {
Lorenz Meier's avatar
Lorenz Meier committed
144
            if (axis == accelAxes[i])
145 146 147
                accelDone[i] = true;

            if (!accelDone[i]) {
Lorenz Meier's avatar
Lorenz Meier committed
148
                qDebug() << "NEW AXIS: " << accelAxes[i];
Lorenz Meier's avatar
Lorenz Meier committed
149
                setInstructionImage(QString(":/files/images/px4/calibration/accel_%1.png").arg(accelAxes[i]));
Lorenz Meier's avatar
Lorenz Meier committed
150 151
                ui->instructionLabel->setText(tr("Axis %1 completed. Please rotate like shown here.").arg(axis));
                break;
152 153 154 155 156
            }
        }
    }

    if (text.contains("please rotate in a figure 8")) {
Lorenz Meier's avatar
Lorenz Meier committed
157
        setInstructionImage(":/files/images/px4/calibration/mag_calibration_figure8.png");
158 159 160 161 162
    }

    if (text.contains("accel calibration done")) {
        accelStarted = false;
        // XXX use a confirmation image or something
Lorenz Meier's avatar
Lorenz Meier committed
163
        setInstructionImage(":/files/images/px4/calibration/accel_z-.png");
164 165 166 167 168
    }

    if (text.contains("gyro calibration done")) {
        gyroStarted = false;
        // XXX use a confirmation image or something
Lorenz Meier's avatar
Lorenz Meier committed
169
        setInstructionImage(":/files/images/px4/calibration/accel_z-.png");
170 171
    }

Lorenz Meier's avatar
Lorenz Meier committed
172 173
    if (text.contains("mag calibration done")
            || text.contains("magnetometer calibration completed")) {
174 175
        magStarted = false;
        // XXX use a confirmation image or something
Lorenz Meier's avatar
Lorenz Meier committed
176
        setInstructionImage(":/files/images/px4/calibration/accel_z-.png");
177 178
    }

Lorenz Meier's avatar
Lorenz Meier committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192
    if (text.contains("accel calibration started")) {
        accelStarted = true;
        setInstructionImage(":/files/images/px4/calibration/accel_z-.png");
    }

    if (text.contains("gyro calibration started")) {
        gyroStarted = true;
        setInstructionImage(":/files/images/px4/calibration/accel_z-.png");
    }

    if (text.contains("mag calibration started")) {
        magStarted = false;
        setInstructionImage(":/files/images/px4/calibration/accel_z-.png");
    }
193 194


195 196 197 198 199 200 201 202
    // XXX color messages according to severity

    QPlainTextEdit *msgWidget = ui->textView;

    //turn off updates while we're appending content to avoid breaking the autoscroll behavior
    msgWidget->setUpdatesEnabled(false);
    QScrollBar *scroller = msgWidget->verticalScrollBar();

203
    msgWidget->appendHtml(QString("%4").arg(text));
204 205 206 207

    // Ensure text area scrolls correctly
    scroller->setValue(scroller->maximum());
    msgWidget->setUpdatesEnabled(true);
208
}
209

210 211
void QGCPX4SensorCalibration::gyroButtonClicked()
{
Lorenz Meier's avatar
Lorenz Meier committed
212
    setInstructionImage(":/files/images/px4/calibration/accel_z-.png");
213 214
    activeUAS->executeCommand(MAV_CMD_PREFLIGHT_CALIBRATION, 1, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0);
    ui->progressBar->setValue(0);
Lorenz Meier's avatar
Lorenz Meier committed
215
    ui->instructionLabel->setText(tr("Please do not move the system at all."));
216 217 218 219
}

void QGCPX4SensorCalibration::magButtonClicked()
{
Lorenz Meier's avatar
Lorenz Meier committed
220
    setInstructionImage(":/files/images/px4/calibration/accel_z-.png");
221 222
    activeUAS->executeCommand(MAV_CMD_PREFLIGHT_CALIBRATION, 1, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0);
    ui->progressBar->setValue(0);
Lorenz Meier's avatar
Lorenz Meier committed
223
    ui->instructionLabel->setText(tr("Please put the system in a rest position."));
224 225 226 227
}

void QGCPX4SensorCalibration::accelButtonClicked()
{
Lorenz Meier's avatar
Lorenz Meier committed
228
    setInstructionImage(":/files/images/px4/calibration/accel_z-.png");
229 230 231 232 233 234 235 236 237 238
    activeUAS->executeCommand(MAV_CMD_PREFLIGHT_CALIBRATION, 1, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0);
    ui->progressBar->setValue(0);
    accelStarted = true;
    accelDone[0] = false;
    accelDone[1] = false;
    accelDone[2] = false;
    accelDone[3] = false;
    accelDone[4] = false;
    accelDone[5] = false;

Lorenz Meier's avatar
Lorenz Meier committed
239
    ui->instructionLabel->setText(tr("Please hold the system very still in the shown orientations."));
240 241 242 243 244 245 246 247
}

void QGCPX4SensorCalibration::contextMenuEvent(QContextMenuEvent* event)
{
    QMenu menu(this);
    menu.addAction(clearAction);
    menu.exec(event->globalPos());
}