UASInfoWidget.cc 5.72 KB
Newer Older
pixhawk's avatar
pixhawk committed
1
/*=====================================================================
lm's avatar
lm committed
2

pixhawk's avatar
pixhawk committed
3
PIXHAWK Micro Air Vehicle Flying Robotics Toolkit
lm's avatar
lm committed
4

pixhawk's avatar
pixhawk committed
5
(c) 2009 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>
lm's avatar
lm committed
6

pixhawk's avatar
pixhawk committed
7
This file is part of the PIXHAWK project
lm's avatar
lm committed
8

pixhawk's avatar
pixhawk committed
9 10 11 12
    PIXHAWK 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.
lm's avatar
lm committed
13

pixhawk's avatar
pixhawk committed
14 15 16 17
    PIXHAWK 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.
lm's avatar
lm committed
18

pixhawk's avatar
pixhawk committed
19 20
    You should have received a copy of the GNU General Public License
    along with PIXHAWK. If not, see <http://www.gnu.org/licenses/>.
lm's avatar
lm committed
21

pixhawk's avatar
pixhawk committed
22
======================================================================*/
lm's avatar
lm committed
23

pixhawk's avatar
pixhawk committed
24 25
/**
 * @file
26
 *   @brief Implementation of class UASInfoWidget
pixhawk's avatar
pixhawk committed
27 28 29 30 31
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

32 33
#include <QtGlobal>

pixhawk's avatar
pixhawk committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <float.h>
#include <UASInfoWidget.h>
#include <UASManager.h>
#include <MG.h>
#include <QTimer>
#include <QDir>
#include <cstdlib>
#include <cmath>

#include <QDebug>

UASInfoWidget::UASInfoWidget(QWidget *parent, QString name) : QWidget(parent)
{
    ui.setupUi(this);
    this->name = name;

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

    activeUAS = NULL;

    //instruments = new QMap<QString, QProgressBar*>();

    // Set default battery type
    //    setBattery(0, LIPOLY, 3);
    startTime = MG::TIME::getGroundTimeNow();
    //    startVoltage = 0.0f;

    //    lastChargeLevel = 0.5f;
    //    lastRemainingTime = 1;

    // Set default values
    /** Set two voltage decimals and zero charge level decimals **/
66
    this->voltageDecimals = 2;
pixhawk's avatar
pixhawk committed
67 68 69 70 71
    this->loadDecimals = 2;

    this->voltage = 0;
    this->chargeLevel = 0;
    this->load = 0;
lm's avatar
lm committed
72 73
    receiveLoss = 0;
    sendLoss = 0;
74
    changed = true;
75
    errors = QMap<QString, int>();
pixhawk's avatar
pixhawk committed
76 77 78

    updateTimer = new QTimer(this);
    connect(updateTimer, SIGNAL(timeout()), this, SLOT(refresh()));
79
    updateTimer->start(updateInterval);
pixhawk's avatar
pixhawk committed
80

81
    this->setVisible(false);
pixhawk's avatar
pixhawk committed
82 83 84 85 86 87 88
}

UASInfoWidget::~UASInfoWidget()
{

}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
void UASInfoWidget::showEvent(QShowEvent* event)
{
    // React only to internal (pre-display)
    // events
    Q_UNUSED(event);
    updateTimer->start(updateInterval);
}

void UASInfoWidget::hideEvent(QHideEvent* event)
{
    // React only to internal (pre-display)
    // events
    Q_UNUSED(event);
    updateTimer->stop();
}

pixhawk's avatar
pixhawk committed
105 106 107 108
void UASInfoWidget::addUAS(UASInterface* uas)
{
    if (uas != NULL)
    {
lm's avatar
lm committed
109
        connect(uas, SIGNAL(batteryChanged(UASInterface*,double,double,int)), this, SLOT(updateBattery(UASInterface*,double,double,int)));
110
        connect(uas, SIGNAL(dropRateChanged(int,float)), this, SLOT(updateReceiveLoss(int,float)));
lm's avatar
lm committed
111
        connect(uas, SIGNAL(loadChanged(UASInterface*, double)), this, SLOT(updateCPULoad(UASInterface*,double)));
112
        connect(uas, SIGNAL(errCountChanged(int,QString,QString,int)), this, SLOT(updateErrorCount(int,QString,QString,int)));
lm's avatar
lm committed
113 114 115

        // Set this UAS as active if it is the first one
        if (activeUAS == 0) activeUAS = uas;
pixhawk's avatar
pixhawk committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }
}

void UASInfoWidget::setActiveUAS(UASInterface* uas)
{
    activeUAS = uas;
}

void UASInfoWidget::updateBattery(UASInterface* uas, double voltage, double percent, int seconds)
{
    setVoltage(uas, voltage);
    setChargeLevel(uas, percent);
    setTimeRemaining(uas, seconds);
}

131 132
void UASInfoWidget::updateErrorCount(int uasid, QString component, QString device, int count)
{
133
    //qDebug() << __FILE__ << __LINE__ << activeUAS->getUASID() << "=" << uasid;
134 135 136 137 138 139 140
    if (activeUAS->getUASID() == uasid)
    {
        errors.remove(component + ":" + device);
        errors.insert(component + ":" + device, count);
    }
}

lm's avatar
lm committed
141 142 143
/**
 *
 */
pixhawk's avatar
pixhawk committed
144 145 146 147
void UASInfoWidget::updateCPULoad(UASInterface* uas, double load)
{
    if (activeUAS == uas)
    {
148
        this->load = load;
pixhawk's avatar
pixhawk committed
149 150 151
    }
}

pixhawk's avatar
pixhawk committed
152
void UASInfoWidget::updateReceiveLoss(int uasId, float receiveLoss)
lm's avatar
lm committed
153
{
pixhawk's avatar
pixhawk committed
154
    Q_UNUSED(uasId);
lm's avatar
lm committed
155
    this->receiveLoss = this->receiveLoss * 0.8f + receiveLoss * 0.2f;
lm's avatar
lm committed
156 157
}

pixhawk's avatar
pixhawk committed
158 159 160 161 162
/**
  The send loss is typically calculated on the GCS based on packets
  that were received scrambled from the MAV
 */
void UASInfoWidget::updateSendLoss(int uasId, float sendLoss)
lm's avatar
lm committed
163
{
pixhawk's avatar
pixhawk committed
164
    Q_UNUSED(uasId);
lm's avatar
lm committed
165
    this->sendLoss = this->sendLoss * 0.8f + sendLoss * 0.2f;
lm's avatar
lm committed
166 167
}

pixhawk's avatar
pixhawk committed
168 169
void UASInfoWidget::setVoltage(UASInterface* uas, double voltage)
{
170
    Q_UNUSED(uas);
pixhawk's avatar
pixhawk committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    this->voltage = voltage;
}

void UASInfoWidget::setChargeLevel(UASInterface* uas, double chargeLevel)
{
    if (activeUAS == uas)
    {
        this->chargeLevel = chargeLevel;
    }
}

void UASInfoWidget::setTimeRemaining(UASInterface* uas, double seconds)
{
    if (activeUAS == uas)
    {
        this->timeRemaining = seconds;
    }
}

void UASInfoWidget::refresh()
{
    ui.voltageLabel->setText(QString::number(this->voltage, 'f', voltageDecimals));
193
    ui.batteryBar->setValue(qMax(0,qMin(static_cast<int>(this->chargeLevel), 100)));
pixhawk's avatar
pixhawk committed
194 195

    ui.loadLabel->setText(QString::number(this->load, 'f', loadDecimals));
196
    ui.loadBar->setValue(qMax(0, qMin(static_cast<int>(this->load), 100)));
lm's avatar
lm committed
197

198 199
    ui.receiveLossBar->setValue(qMax(0, qMin(static_cast<int>(receiveLoss), 100)));
    ui.receiveLossLabel->setText(QString::number(receiveLoss, 'f', 2));
lm's avatar
lm committed
200 201 202

    ui.sendLossBar->setValue(sendLoss);
    ui.sendLossLabel->setText(QString::number(sendLoss, 'f', 2));
203 204

    QString errorString;
205 206 207 208 209 210 211 212 213 214 215 216
    QMapIterator<QString, int> i(errors);
    while (i.hasNext())
    {
        i.next();
        errorString += QString(i.key() + ": %1 ").arg(i.value());

        // FIXME
        errorString.replace("IMU:", "");


    }
    ui.errorLabel->setText(errorString);
pixhawk's avatar
pixhawk committed
217
}