HSIDisplay.cc 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*=====================================================================

PIXHAWK Micro Air Vehicle Flying Robotics Toolkit

(c) 2009, 2010 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>

This file is part of the PIXHAWK project

    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.

    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.

    You should have received a copy of the GNU General Public License
    along with PIXHAWK. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Implementation of Horizontal Situation Indicator class
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QFile>
#include <QStringList>
34
#include <QPainter>
35 36 37 38 39 40
#include "UASManager.h"
#include "HSIDisplay.h"
#include "MG.h"

#include <QDebug>

41
HSIDisplay::HSIDisplay(QWidget *parent) :
lm's avatar
lm committed
42
        HDDisplay(NULL, parent),
pixhawk's avatar
pixhawk committed
43
        gpsSatellites(),
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        satellitesUsed(0),
        attXSet(0),
        attYSet(0),
        attYawSet(0),
        altitudeSet(1.0),
        posXSet(0),
        posYSet(0),
        posZSet(0),
        attXSaturation(0.33),
        attYSaturation(0.33),
        attYawSaturation(0.33),
        posXSaturation(1.0),
        posYSaturation(1.0),
        altitudeSaturation(1.0),
        lat(0),
        lon(0),
        alt(0),
        globalAvailable(0),
        x(0),
        y(0),
        z(0),
        localAvailable(0)
66
{
lm's avatar
lm committed
67
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
68 69
}

lm's avatar
lm committed
70 71 72 73 74
void HSIDisplay::paintEvent(QPaintEvent * event)
{
    Q_UNUSED(event);
    //paintGL();
    static quint64 interval = 0;
75
    //qDebug() << "INTERVAL:" << MG::TIME::getGroundTimeNow() - interval << __FILE__ << __LINE__;
lm's avatar
lm committed
76 77 78 79
    interval = MG::TIME::getGroundTimeNow();
    paintDisplay();
}

80 81
void HSIDisplay::paintDisplay()
{
82 83 84 85 86 87 88 89 90
    // Center location of the HSI gauge items

    float xCenterPos = vwidth/2.0f;
    float yCenterPos = vheight/2.0f;

    // Size of the ring instrument
    const float margin = 0.1f;  // 10% margin of total width on each side
    float baseRadius = (vwidth - vwidth * 2.0f * margin) / 2.0f;

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    quint64 refreshInterval = 100;
    quint64 currTime = MG::TIME::getGroundTimeNow();
    if (currTime - lastPaintTime < refreshInterval)
    {
        // FIXME Need to find the source of the spurious paint events
        //return;
    }
    lastPaintTime = currTime;
    // Draw instruments
    // TESTING THIS SHOULD BE MOVED INTO A QGRAPHICSVIEW
    // Update scaling factor
    // adjust scaling to fit both horizontally and vertically
    scalingFactor = this->width()/vwidth;
    double scalingFactorH = this->height()/vheight;
    if (scalingFactorH < scalingFactor) scalingFactor = scalingFactorH;

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
    painter.fillRect(QRect(0, 0, width(), height()), backgroundColor);
lm's avatar
lm committed
111 112
    const QColor ringColor = QColor(200, 250, 200);

113 114
    // Draw base instrument
    // ----------------------
lm's avatar
lm committed
115 116 117 118 119 120 121
    const int ringCount = 2;
    for (int i = 0; i < ringCount; i++)
    {
        float radius = (vwidth - vwidth * 2.0f * margin) / (2.0f * i+1) / 2.0f;
        drawCircle(vwidth/2.0f, vheight/2.0f, radius, 0.1f, ringColor, &painter);
    }

lm's avatar
lm committed
122 123 124
    // Draw center indicator
    drawCircle(vwidth/2.0f, vheight/2.0f, 1.0f, 0.1f, ringColor, &painter);

125 126 127 128 129 130 131
    // Draw orientation labels
    paintText(tr("N"), ringColor, 3.5f, xCenterPos - 1.0f, yCenterPos - baseRadius - 5.5f, &painter);
    paintText(tr("S"), ringColor, 3.5f, xCenterPos - 1.0f, yCenterPos + baseRadius + 1.5f, &painter);
    paintText(tr("E"), ringColor, 3.5f, xCenterPos + baseRadius + 2.0f, yCenterPos - 1.75f, &painter);
    paintText(tr("W"), ringColor, 3.5f, xCenterPos - baseRadius - 5.5f, yCenterPos - 1.75f, &painter);

    // ----------------------
132

133 134 135
    // Draw state indicator

    // Draw position
136 137 138 139 140 141
    QColor positionColor(20, 20, 200);
    drawPositionSetpoint(xCenterPos, yCenterPos, baseRadius, positionColor, &painter);

    // Draw attitude
    QColor attitudeColor(200, 20, 20);
    drawPositionSetpoint(xCenterPos, yCenterPos, baseRadius, attitudeColor, &painter);
142 143


lm's avatar
lm committed
144

145 146 147 148 149 150 151 152 153 154 155
    // Draw satellites
    drawGPS();

    if (localAvailable > 0)
    {
        QString str;
        str.sprintf("%05.2f %05.2f %05.2f m", x, y, z);
        paintText(str, ringColor, 4.5f, xCenterPos + baseRadius - 5.5f, yCenterPos + baseRadius - 20.75f, &painter);
    }


156 157 158 159 160
    //drawSystemIndicator(10.0f-gaugeWidth/2.0f, 20.0f, 10.0f, 40.0f, 15.0f, &painter);
    //drawGauge(15.0f, 15.0f, gaugeWidth/2.0f, 0, 1.0f, "thrust", values.value("thrust", 0.0f), gaugeColor, &painter, qMakePair(0.45f, 0.8f), qMakePair(0.8f, 1.0f), true);
    //drawGauge(15.0f+gaugeWidth*1.7f, 15.0f, gaugeWidth/2.0f, 0, 10.0f, "altitude", values.value("altitude", 0.0f), gaugeColor, &painter, qMakePair(1.0f, 2.5f), qMakePair(0.0f, 0.5f), true);

    // Left spacing from border / other gauges, measured from left edge to center
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    //    float leftSpacing = gaugeWidth * spacing;
    //    float xCoord = leftSpacing + gaugeWidth/2.0f;
    //
    //    float topSpacing = leftSpacing;
    //    float yCoord = topSpacing + gaugeWidth/2.0f;
    //
    //    for (int i = 0; i < acceptList->size(); ++i)
    //    {
    //        QString value = acceptList->at(i);
    //        drawGauge(xCoord, yCoord, gaugeWidth/2.0f, minValues.value(value, -1.0f), maxValues.value(value, 1.0f), value, values.value(value, minValues.value(value, 0.0f)), gaugeColor, &painter, goodRanges.value(value, qMakePair(0.0f, 0.5f)), critRanges.value(value, qMakePair(0.7f, 1.0f)), true);
    //        xCoord += gaugeWidth + leftSpacing;
    //        // Move one row down if necessary
    //        if (xCoord + gaugeWidth > vwidth)
    //        {
    //            yCoord += topSpacing + gaugeWidth;
    //            xCoord = leftSpacing + gaugeWidth/2.0f;
    //        }
    //    }
179 180 181 182 183 184 185 186
}

/**
 *
 * @param uas the UAS/MAV to monitor/display with the HUD
 */
void HSIDisplay::setActiveUAS(UASInterface* uas)
{
187
    HDDisplay::setActiveUAS(uas);
188 189 190 191 192 193 194
    //qDebug() << "ATTEMPTING TO SET UAS";
    if (this->uas != NULL && this->uas != uas)
    {
        // Disconnect any previously connected active MAV
        //disconnect(uas, SIGNAL(valueChanged(UASInterface*,QString,double,quint64)), this, SLOT(updateValue(UASInterface*,QString,double,quint64)));
    }

lm's avatar
lm committed
195
    connect(uas, SIGNAL(gpsSatelliteStatusChanged(int,int,float,float,float,bool)), this, SLOT(updateSatellite(int,int,float,float,float,bool)));
196 197 198
    connect(uas, SIGNAL(localPositionChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateLocalPosition(UASInterface*,double,double,double,quint64)));
    connect(uas, SIGNAL(globalPositionChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateGlobalPosition(UASInterface*,double,double,double,quint64)));
    connect(uas, SIGNAL(attitudeThrustSetPointChanged(UASInterface*,double,double,double,double,quint64)), this, SLOT(updateAttitudeSetpoints(UASInterface*,double,double,double,double,quint64)));
lm's avatar
lm committed
199

200 201 202 203 204 205 206 207 208 209
    // Now connect the new UAS

    //if (this->uas != uas)
    // {
    //qDebug() << "UAS SET!" << "ID:" << uas->getUASID();
    // Setup communication
    //connect(uas, SIGNAL(valueChanged(UASInterface*,QString,double,quint64)), this, SLOT(updateValue(UASInterface*,QString,double,quint64)));
    //}
}

210
void HSIDisplay::updateAttitudeSetpoints(UASInterface* uas, double rollDesired, double pitchDesired, double yawDesired, double thrustDesired, quint64 usec)
211
{
212 213 214 215 216 217
    Q_UNUSED(uas);
    Q_UNUSED(usec);
    attXSet = pitchDesired;
    attYSet = rollDesired;
    attYawSet = yawDesired;
    altitudeSet = thrustDesired;
218 219
}

220
void HSIDisplay::updatePositionSetpoints(int uasid, double xDesired, double yDesired, double zDesired, quint64 usec)
221
{
222 223 224 225 226
    Q_UNUSED(usec);
    Q_UNUSED(uasid);
    posXSet = xDesired;
    posYSet = yDesired;
    posZSet = zDesired;
227 228 229 230
}

void HSIDisplay::updateLocalPosition(UASInterface*, double x, double y, double z, quint64 usec)
{
231 232 233 234
    this->x = x;
    this->y = y;
    this->z = z;
    localAvailable = usec;
235 236 237 238
}

void HSIDisplay::updateGlobalPosition(UASInterface*, double lat, double lon, double alt, quint64 usec)
{
239 240 241 242
    this->lat = lat;
    this->lon = lon;
    this->alt = alt;
    globalAvailable = usec;
243 244
}

lm's avatar
lm committed
245
void HSIDisplay::updateSatellite(int uasid, int satid, float elevation, float azimuth, float snr, bool used)
lm's avatar
lm committed
246 247
{
    Q_UNUSED(uasid);
lm's avatar
lm committed
248
    //qDebug() << "UPDATED SATELLITE";
lm's avatar
lm committed
249
    // If slot is empty, insert object
lm's avatar
lm committed
250
    if (gpsSatellites.contains(satid))
lm's avatar
lm committed
251
    {
lm's avatar
lm committed
252
        gpsSatellites.value(satid)->update(satid, elevation, azimuth, snr, used);
lm's avatar
lm committed
253 254 255
    }
    else
    {
lm's avatar
lm committed
256
        gpsSatellites.insert(satid, new GPSSatellite(satid, elevation, azimuth, snr, used));
lm's avatar
lm committed
257 258 259 260 261
    }
}

QColor HSIDisplay::getColorForSNR(float snr)
{
lm's avatar
lm committed
262
    QColor color;
lm's avatar
lm committed
263 264 265 266 267
    if (snr > 0 && snr < 30)
    {
        color = QColor(250, 10, 10);
    }
    else if (snr >= 30 && snr < 35)
lm's avatar
lm committed
268
    {
lm's avatar
lm committed
269
        color = QColor(230, 230, 10);
lm's avatar
lm committed
270
    }
lm's avatar
lm committed
271
    else if (snr >= 35 && snr < 40)
lm's avatar
lm committed
272
    {
lm's avatar
lm committed
273
        color = QColor(90, 200, 90);
lm's avatar
lm committed
274
    }
lm's avatar
lm committed
275
    else if (snr >= 40)
lm's avatar
lm committed
276
    {
lm's avatar
lm committed
277
        color = QColor(20, 200, 20);
lm's avatar
lm committed
278 279 280 281 282 283
    }
    else
    {
        color = QColor(180, 180, 180);
    }
    return color;
lm's avatar
lm committed
284 285
}

286 287
void HSIDisplay::drawGPS()
{
lm's avatar
lm committed
288 289
    float xCenter = vwidth/2.0f;
    float yCenter = vwidth/2.0f;
lm's avatar
lm committed
290 291 292 293 294 295
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::HighQualityAntialiasing, true);

    // Max satellite circle radius

lm's avatar
lm committed
296 297 298
    const float margin = 0.15f;  // 20% margin of total width on each side
    float radius = (vwidth - vwidth * 2.0f * margin) / 2.0f;
    quint64 currTime = MG::TIME::getGroundTimeNowUsecs();
lm's avatar
lm committed
299

pixhawk's avatar
pixhawk committed
300 301 302 303 304
    // Draw satellite labels
    //    QString label;
    //    label.sprintf("%05.1f", value);
    //    paintText(label, color, 4.5f, xRef-7.5f, yRef-2.0f, painter);

lm's avatar
lm committed
305 306
    QMapIterator<int, GPSSatellite*> i(gpsSatellites);
    while (i.hasNext())
lm's avatar
lm committed
307
    {
lm's avatar
lm committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        i.next();
        GPSSatellite* sat = i.value();

        // Check if update is not older than 5 seconds, else delete satellite
        if (sat->lastUpdate + 1000000 < currTime)
        {
            // Delete and go to next satellite
            gpsSatellites.remove(i.key());
            if (i.hasNext())
            {
                i.next();
                sat = i.value();
            }
            else
            {
                continue;
            }
        }

lm's avatar
lm committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        if (sat)
        {
            // Draw satellite
            QBrush brush;
            QColor color = getColorForSNR(sat->snr);
            brush.setColor(color);
            if (sat->used)
            {
                brush.setStyle(Qt::SolidPattern);
            }
            else
            {
                brush.setStyle(Qt::NoBrush);
            }
            painter.setPen(Qt::SolidLine);
            painter.setPen(color);
            painter.setBrush(brush);

lm's avatar
lm committed
345 346
            float xPos = xCenter + (sin(((sat->azimuth/255.0f)*360.0f)/180.0f * M_PI) * cos(sat->elevation/180.0f * M_PI)) * radius;
            float yPos = yCenter - (cos(((sat->azimuth/255.0f)*360.0f)/180.0f * M_PI) * cos(sat->elevation/180.0f * M_PI)) * radius;
lm's avatar
lm committed
347

lm's avatar
lm committed
348
            drawCircle(xPos, yPos, vwidth*0.02f, 1.0f, color, &painter);
lm's avatar
lm committed
349
            paintText(QString::number(sat->id), QColor(255, 255, 255), 2.9f, xPos+1.7f, yPos+2.0f, &painter);
lm's avatar
lm committed
350 351
        }
    }
352 353 354 355
}

void HSIDisplay::drawObjects()
{
356

357 358
}

359
void HSIDisplay::drawPositionSetpoint(float xRef, float yRef, float radius, const QColor& color, QPainter* painter)
360
{
361 362 363
    // Draw the needle
    const float maxWidth = radius / 10.0f;
    const float minWidth = maxWidth * 0.3f;
364

365 366 367
    float angle = asin(posXSet) + acos(posYSet);

    QPolygonF p(6);
368

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    p.replace(0, QPointF(xRef-maxWidth/2.0f, yRef-radius * 0.5f));
    p.replace(1, QPointF(xRef-minWidth/2.0f, yRef-radius * 0.9f));
    p.replace(2, QPointF(xRef+minWidth/2.0f, yRef-radius * 0.9f));
    p.replace(3, QPointF(xRef+maxWidth/2.0f, yRef-radius * 0.5f));
    p.replace(4, QPointF(xRef,               yRef-radius * 0.46f));
    p.replace(5, QPointF(xRef-maxWidth/2.0f, yRef-radius * 0.5f));

    rotatePolygonClockWiseRad(p, angle, QPointF(xRef, yRef));

    QBrush indexBrush;
    indexBrush.setColor(color);
    indexBrush.setStyle(Qt::SolidPattern);
    painter->setPen(Qt::SolidLine);
    painter->setPen(color);
    painter->setBrush(indexBrush);
    drawPolygon(p, painter);
}

void HSIDisplay::drawAttitudeSetpoint(float xRef, float yRef, float radius, const QColor& color, QPainter* painter)
{
389 390 391 392
    // Draw the needle
    const float maxWidth = radius / 10.0f;
    const float minWidth = maxWidth * 0.3f;

393 394
    float angle = asin(attXSet) + acos(attYSet);

395 396 397 398 399 400 401 402 403
    QPolygonF p(6);

    p.replace(0, QPointF(xRef-maxWidth/2.0f, yRef-radius * 0.5f));
    p.replace(1, QPointF(xRef-minWidth/2.0f, yRef-radius * 0.9f));
    p.replace(2, QPointF(xRef+minWidth/2.0f, yRef-radius * 0.9f));
    p.replace(3, QPointF(xRef+maxWidth/2.0f, yRef-radius * 0.5f));
    p.replace(4, QPointF(xRef,               yRef-radius * 0.46f));
    p.replace(5, QPointF(xRef-maxWidth/2.0f, yRef-radius * 0.5f));

404
    rotatePolygonClockWiseRad(p, angle, QPointF(xRef, yRef));
405 406

    QBrush indexBrush;
407
    indexBrush.setColor(color);
408 409
    indexBrush.setStyle(Qt::SolidPattern);
    painter->setPen(Qt::SolidLine);
410
    painter->setPen(color);
411 412
    painter->setBrush(indexBrush);
    drawPolygon(p, painter);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

    // TODO Draw Yaw indicator
}

void HSIDisplay::drawAltitudeSetpoint(float xRef, float yRef, float radius, const QColor& color, QPainter* painter)
{
    // Draw the circle
    QPen circlePen(Qt::SolidLine);
    circlePen.setWidth(refLineWidthToPen(0.5f));
    circlePen.setColor(color);
    painter->setBrush(Qt::NoBrush);
    painter->setPen(circlePen);
    drawCircle(xRef, yRef, radius, 200.0f, color, painter);
    //drawCircle(xRef, yRef, radius, 200.0f, 170.0f, 1.0f, color, painter);

    //    // Draw the value
    //    QString label;
    //    label.sprintf("%05.1f", value);
    //    paintText(label, color, 4.5f, xRef-7.5f, yRef-2.0f, painter);
432
}