HUD.h 10.8 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2
/*=====================================================================

3
QGroundControl Open Source Ground Control Station
pixhawk's avatar
pixhawk committed
4

5
(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
pixhawk's avatar
pixhawk committed
6

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

9
    QGROUNDCONTROL is free software: you can redistribute it and/or modify
pixhawk's avatar
pixhawk committed
10 11 12 13
    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.

14
    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
15 16 17 18 19
    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
20
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
pixhawk's avatar
pixhawk committed
21 22 23 24 25 26 27 28 29 30 31

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

/**
 * @file
 *   @brief Definition of Head up display
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

32 33
#ifndef HUD_H
#define HUD_H
pixhawk's avatar
pixhawk committed
34 35

#include <QImage>
36
#include <QWidget>
37
#include <QLabel>
pixhawk's avatar
pixhawk committed
38 39 40
#include <QPainter>
#include <QFontDatabase>
#include <QTimer>
41
#include <QVector3D>
pixhawk's avatar
pixhawk committed
42 43 44
#include "UASInterface.h"

/**
45
 * @brief Displays a Head Up Display (HUD)
pixhawk's avatar
pixhawk committed
46
 *
47 48
 * This class represents a head up display (HUD) and draws this HUD in an OpenGL widget (QGLWidget).
 * It can superimpose the HUD over the current live image stream (any arriving image stream will be auto-
pixhawk's avatar
pixhawk committed
49 50
 * matically used as background), or it draws the classic blue-brown background known from instruments.
 */
51
class HUD : public QLabel
pixhawk's avatar
pixhawk committed
52 53 54
{
    Q_OBJECT
public:
55 56
    HUD(int width = 640, int height = 480, QWidget* parent = NULL);
    ~HUD();
pixhawk's avatar
pixhawk committed
57 58

    void setImageSize(int width, int height, int depth, int channels);
59
    void resize(int w, int h);
pixhawk's avatar
pixhawk committed
60 61

public slots:
62
//    void initializeGL();
63
    //void paintGL();
pixhawk's avatar
pixhawk committed
64 65

    /** @brief Set the currently monitored UAS */
66
    virtual void setActiveUAS(UASInterface* uas);
pixhawk's avatar
pixhawk committed
67

68
    /** @brief Attitude from main autopilot / system state */
pixhawk's avatar
pixhawk committed
69
    void updateAttitude(UASInterface* uas, double roll, double pitch, double yaw, quint64 timestamp);
70 71
    /** @brief Attitude from one specific component / redundant autopilot */
    void updateAttitude(UASInterface* uas, int component, double roll, double pitch, double yaw, quint64 timestamp);
72
//    void updateAttitudeThrustSetPoint(UASInterface*, double rollDesired, double pitchDesired, double yawDesired, double thrustDesired, quint64 usec);
dongfang's avatar
dongfang committed
73
    void updateBattery(UASInterface*, double, double, double, int);
pixhawk's avatar
pixhawk committed
74 75 76 77 78
    void receiveHeartbeat(UASInterface*);
    void updateThrust(UASInterface*, double);
    void updateLocalPosition(UASInterface*,double,double,double,quint64);
    void updateGlobalPosition(UASInterface*,double,double,double,quint64);
    void updateSpeed(UASInterface*,double,double,double,quint64);
79
    void updateState(UASInterface*,QString);
80
    void updateMode(int id,QString mode, QString description);
pixhawk's avatar
pixhawk committed
81
    void updateLoad(UASInterface*, double);
82
    void selectWaypoint(int uasId, int id);
pixhawk's avatar
pixhawk committed
83

84
    void startImage(quint64 timestamp);
pixhawk's avatar
pixhawk committed
85 86 87 88 89
    void startImage(int imgid, int width, int height, int depth, int channels);
    void setPixels(int imgid, const unsigned char* imageData, int length, int startIndex);
    void finishImage();
    void saveImage();
    void saveImage(QString fileName);
90
    void saveImages(bool save);
91 92 93
    /** @brief Select directory where to load the offline files from */
    void selectOfflineDirectory();
    /** @brief Enable the HUD instruments */
94
    void enableHUDInstruments(bool enabled);
95 96
    /** @brief Enable Video */
    void enableVideo(bool enabled);
LM's avatar
LM committed
97 98
    /** @brief Copy an image from the current active UAS */
    void copyImage();
99

pixhawk's avatar
pixhawk committed
100 101 102 103 104 105

protected slots:
    void paintRollPitchStrips();
    void paintPitchLines(float pitch, QPainter* painter);
    /** @brief Paint text on top of the image and OpenGL drawings */
    void paintText(QString text, QColor color, float fontSize, float refX, float refY, QPainter* painter);
106
    /** @brief Setup the OpenGL view for drawing a sub-component of the HUD */
pixhawk's avatar
pixhawk committed
107
    void setupGLView(float referencePositionX, float referencePositionY, float referenceWidth, float referenceHeight);
108
    void paintHUD();
pixhawk's avatar
pixhawk committed
109 110 111 112 113 114 115
    void paintPitchLinePos(QString text, float refPosX, float refPosY, QPainter* painter);
    void paintPitchLineNeg(QString text, float refPosX, float refPosY, QPainter* painter);

    void drawLine(float refX1, float refY1, float refX2, float refY2, float width, const QColor& color, QPainter* painter);
    void drawEllipse(float refX, float refY, float radiusX, float radiusY, float startDeg, float endDeg, float lineWidth, const QColor& color, QPainter* painter);
    void drawCircle(float refX, float refY, float radius, float startDeg, float endDeg, float lineWidth, const QColor& color, QPainter* painter);

116
    void drawChangeRateStrip(float xRef, float yRef, float height, float minRate, float maxRate, float value, QPainter* painter,bool reverse = false);
pixhawk's avatar
pixhawk committed
117 118 119 120
    void drawChangeIndicatorGauge(float xRef, float yRef, float radius, float expectedMaxChange, float value, const QColor& color, QPainter* painter, bool solid=true);

    void drawPolygon(QPolygonF refPolygon, QPainter* painter);

LM's avatar
LM committed
121 122 123
signals:
    void visibilityChanged(bool visible);

pixhawk's avatar
pixhawk committed
124 125 126 127 128 129 130 131 132 133
protected:
    void commitRawDataToGL();
    /** @brief Convert reference coordinates to screen coordinates */
    float refToScreenX(float x);
    /** @brief Convert reference coordinates to screen coordinates */
    float refToScreenY(float y);
    /** @brief Convert mm line widths to QPen line widths */
    float refLineWidthToPen(float line);
    /** @brief Rotate a polygon around a point clockwise */
    void rotatePolygonClockWiseRad(QPolygonF& p, float angle, QPointF origin);
134 135
    /** @brief Preferred Size */
    QSize sizeHint() const;
136
    /** @brief Start updating widget */
137
    void showEvent(QShowEvent* event);
138 139
    /** @brief Stop updating widget */
    void hideEvent(QHideEvent* event);
140 141
    void contextMenuEvent (QContextMenuEvent* event);
    void createActions();
142

143
    static const int updateInterval = 100;
pixhawk's avatar
pixhawk committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

    QImage* image; ///< Double buffer image
    QImage glImage; ///< The background / camera image
    UASInterface* uas; ///< The uas currently monitored
    float yawInt; ///< The yaw integral. Used to damp the yaw indication.
    QString mode; ///< The current vehicle mode
    QString state; ///< The current vehicle state
    QString fuelStatus; ///< Current fuel level / battery voltage
    double scalingFactor; ///< Factor used to scale all absolute values to screen coordinates
    float xCenterOffset, yCenterOffset; ///< Offset from center of window in mm coordinates
    float vwidth; ///< Virtual width of this window, 200 mm per default. This allows to hardcode positions and aspect ratios. This virtual image plane is then scaled to the window size.
    float vheight; ///< Virtual height of this window, 150 mm per default
    float vGaugeSpacing; ///< Virtual spacing of the gauges from the center, 50 mm per default
    float vPitchPerDeg; ///< Virtual pitch to mm conversion. Currently one degree is 3 mm up/down in the pitch markings

159 160
    int xCenter; ///< Center of the HUD instrument in pixel coordinates. Allows to off-center the whole instrument in its OpenGL window, e.g. to fit another instrument
    int yCenter; ///< Center of the HUD instrument in pixel coordinates. Allows to off-center the whole instrument in its OpenGL window, e.g. to fit another instrument
pixhawk's avatar
pixhawk committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174

    // Image buffers
    unsigned char* rawBuffer1; ///< Double buffer 1 for the image
    unsigned char* rawBuffer2; ///< Double buffer 2 for the image
    unsigned char* rawImage;   ///< Pointer to current complete image
    int rawLastIndex;          ///< The last byte index received of the image
    int rawExpectedBytes;      ///< Number of raw image bytes expected. Calculated by: image depth * channels * widht * height / 8
    int bytesPerLine;          ///< Bytes per image line. Is calculated as: image depth * channels * width / 8
    bool imageStarted;         ///< If an image is currently in transmission
    int receivedDepth;         ///< Image depth in bit for the current image
    int receivedChannels;      ///< Number of color channels
    int receivedWidth;         ///< Width in pixels of the current image
    int receivedHeight;        ///< Height in pixels of the current image

175 176
    // HUD colors
    QColor defaultColor;       ///< Color for most HUD elements, e.g. pitch lines, center cross, change rate gauges
pixhawk's avatar
pixhawk committed
177 178 179 180 181 182 183 184 185 186
    QColor setPointColor;      ///< Color for the current control set point, e.g. yaw desired
    QColor warningColor;       ///< Color for warning messages
    QColor criticalColor;      ///< Color for caution messages
    QColor infoColor;          ///< Color for normal/default messages
    QColor fuelColor;          ///< Current color for the fuel message, can be info, warning or critical color

    // Blink rates
    int warningBlinkRate;      ///< Blink rate of warning messages, will be rounded to the refresh rate

    QTimer* refreshTimer;      ///< The main timer, controls the update rate
187 188 189
    QPainter* HUDPainter;
    QFont font;                ///< The HUD font, per default the free Bitstream Vera SANS, which is very close to actual HUD fonts
    QFontDatabase fontDatabase;///< Font database, only used to load the TrueType font file (the HUD font is directly loaded from file rather than from the system)
pixhawk's avatar
pixhawk committed
190 191 192
    bool noCamera;             ///< No camera images available, draw the ground/sky box to indicate the horizon
    bool hardwareAcceleration; ///< Enable hardware acceleration

193 194 195
    float strongStrokeWidth;   ///< Strong line stroke width, used throughout the HUD
    float normalStrokeWidth;   ///< Normal line stroke width, used throughout the HUD
    float fineStrokeWidth;     ///< Fine line stroke width, used throughout the HUD
pixhawk's avatar
pixhawk committed
196

197
    QString waypointName;      ///< Waypoint name displayed in HUD
198 199 200
    float roll;
    float pitch;
    float yaw;
201
    QMap<uint8_t, QVector3D> attitudes;
202 203 204 205 206 207 208 209 210 211
    float rollLP;
    float pitchLP;
    float yawLP;
    double yawDiff;
    double xPos;
    double yPos;
    double zPos;
    double xSpeed;
    double ySpeed;
    double zSpeed;
pixhawk's avatar
pixhawk committed
212 213 214
    quint64 lastSpeedUpdate;
    double totalSpeed;
    double totalAcc;
215 216 217 218
    double lat;
    double lon;
    double alt;
    float load;
219 220
    QString offlineDirectory;
    QString nextOfflineImage;
221
    bool HUDInstrumentsEnabled;
222
    bool videoEnabled;
223
    bool dataStreamEnabled;
224
    bool imageLoggingEnabled;
225 226 227 228 229 230
    float xImageFactor;
    float yImageFactor;
    QAction* enableHUDAction;
    QAction* enableVideoAction;
    QAction* selectOfflineDirectoryAction;
    QAction* selectVideoChannelAction;
231
    QAction* selectSaveDirectoryAction;
pixhawk's avatar
pixhawk committed
232
    void paintEvent(QPaintEvent *event);
233
    bool imageRequested;
234 235
    QString imageLogDirectory;
    unsigned int imageLogCounter;
pixhawk's avatar
pixhawk committed
236 237
};

238
#endif // HUD_H