UASInfoWidget.cc 4.11 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 26 27 28 29 30 31 32 33 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/**
 * @file
 *   @brief Brief Description
 *
 *   @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;
    this->loadDecimals = 2;

    this->voltage = 0;
    this->chargeLevel = 0;
    this->load = 0;

    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
86
        connect(uas, SIGNAL(batteryChanged(UASInterface*,double,double,int)), this, SLOT(updateBattery(UASInterface*,double,double,int)));
pixhawk's avatar
pixhawk committed
87
        connect(uas, SIGNAL(dropRateChanged(int,float,float)), this, SLOT(updateDropRate(int,float,float)));
lm's avatar
lm committed
88 89 90 91
        connect(uas, SIGNAL(loadChanged(UASInterface*, double)), this, SLOT(updateCPULoad(UASInterface*,double)));

        // Set this UAS as active if it is the first one
        if (activeUAS == 0) activeUAS = uas;
pixhawk's avatar
pixhawk committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    }
}

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

lm's avatar
lm committed
107 108 109
/**
 *
 */
pixhawk's avatar
pixhawk committed
110 111 112 113
void UASInfoWidget::updateCPULoad(UASInterface* uas, double load)
{
    if (activeUAS == uas)
    {
lm's avatar
lm committed
114
        this->load = load*100.0f;
pixhawk's avatar
pixhawk committed
115 116 117
    }
}

lm's avatar
lm committed
118 119
void UASInfoWidget::updateDropRate(int sysId, float receiveDrop, float sendDrop)
{
120
    Q_UNUSED(sysId);
pixhawk's avatar
pixhawk committed
121 122 123 124
    ui.receiveLossBar->setValue(receiveDrop);
    ui.receiveLossLabel->setText(QString::number(receiveDrop) + "%");
    ui.sendLossBar->setValue(sendDrop);
    ui.sendLossLabel->setText(QString::number(receiveDrop) + "%");
lm's avatar
lm committed
125 126
}

pixhawk's avatar
pixhawk committed
127 128
void UASInfoWidget::setVoltage(UASInterface* uas, double voltage)
{
129
    Q_UNUSED(uas);
pixhawk's avatar
pixhawk committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    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));
}