Skip to content
UASInfoWidget.cc 5.24 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/*=====================================================================
lm's avatar
lm committed

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

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

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

pixhawk's avatar
pixhawk committed
    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

pixhawk's avatar
pixhawk committed
    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

pixhawk's avatar
pixhawk committed
    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

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

pixhawk's avatar
pixhawk committed
/**
 * @file
 *   @brief Implementation of class UASInfoWidget
pixhawk's avatar
pixhawk committed
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#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 **/
    this->voltageDecimals = 2;
pixhawk's avatar
pixhawk committed
    this->loadDecimals = 2;

    this->voltage = 0;
    this->chargeLevel = 0;
    this->load = 0;
lm's avatar
lm committed
    receiveLoss = 0;
    sendLoss = 0;
    errors = QMap<QString, int>();
pixhawk's avatar
pixhawk committed

    updateTimer = new QTimer(this);
    connect(updateTimer, SIGNAL(timeout()), this, SLOT(refresh()));
    updateTimer->start(50);

}

UASInfoWidget::~UASInfoWidget()
{

}

void UASInfoWidget::addUAS(UASInterface* uas)
{
    if (uas != NULL)
    {
lm's avatar
lm committed
        connect(uas, SIGNAL(batteryChanged(UASInterface*,double,double,int)), this, SLOT(updateBattery(UASInterface*,double,double,int)));
        connect(uas, SIGNAL(dropRateChanged(int,float)), this, SLOT(updateReceiveLoss(int,float)));
lm's avatar
lm committed
        connect(uas, SIGNAL(loadChanged(UASInterface*, double)), this, SLOT(updateCPULoad(UASInterface*,double)));
        connect(uas, SIGNAL(errCountChanged(int,QString,QString,int)), this, SLOT(updateErrorCount(int,QString,QString,int)));
lm's avatar
lm committed

        // Set this UAS as active if it is the first one
        if (activeUAS == 0) activeUAS = uas;
pixhawk's avatar
pixhawk committed
    }
}

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

void UASInfoWidget::updateErrorCount(int uasid, QString component, QString device, int count)
{
    //qDebug() << __FILE__ << __LINE__ << activeUAS->getUASID() << "=" << uasid;
    if (activeUAS->getUASID() == uasid)
    {
        errors.remove(component + ":" + device);
        errors.insert(component + ":" + device, count);
    }
}

lm's avatar
lm committed
/**
 *
 */
pixhawk's avatar
pixhawk committed
void UASInfoWidget::updateCPULoad(UASInterface* uas, double load)
{
    if (activeUAS == uas)
    {
pixhawk's avatar
pixhawk committed
    }
}

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

pixhawk's avatar
pixhawk committed
/**
  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
{
pixhawk's avatar
pixhawk committed
    Q_UNUSED(uasId);
lm's avatar
lm committed
    this->sendLoss = this->sendLoss * 0.8f + sendLoss * 0.2f;
lm's avatar
lm committed
}

pixhawk's avatar
pixhawk committed
void UASInfoWidget::setVoltage(UASInterface* uas, double voltage)
{
pixhawk's avatar
pixhawk committed
    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));
    ui.batteryBar->setValue(static_cast<int>(this->chargeLevel));

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

    ui.receiveLossBar->setValue(receiveLoss);
    ui.receiveLossLabel->setText(QString::number(receiveLoss,'f', 2));

    ui.sendLossBar->setValue(sendLoss);
    ui.sendLossLabel->setText(QString::number(sendLoss, 'f', 2));
    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
}