UASManager.h 10.3 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 - 2011 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

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

/**
 * @file
26
 *   @brief Definition of class UASManager
pixhawk's avatar
pixhawk committed
27 28 29 30 31 32 33
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#ifndef _UASMANAGER_H_
#define _UASMANAGER_H_

34
#include "UASManagerInterface.h"
pixhawk's avatar
pixhawk committed
35 36 37 38
#include <QThread>
#include <QList>
#include <QMutex>
#include <UASInterface.h>
39
#include "../../libs/eigen/Eigen/Eigen"
40
#include "QGCGeo.h"
pixhawk's avatar
pixhawk committed
41 42

/**
43
 * @brief Central manager for all connected aerial vehicles
pixhawk's avatar
pixhawk committed
44 45 46 47
 *
 * This class keeps a list of all connected / configured UASs. It also stores which
 * UAS is currently select with respect to user input or manual controls.
 **/
48
class UASManager : public UASManagerInterface
pixhawk's avatar
pixhawk committed
49 50 51 52
{
    Q_OBJECT

public:
53
    static UASManagerInterface* instance();
pixhawk's avatar
pixhawk committed
54
    ~UASManager();
55 56 57 58 59
    
    /**
     * @brief Sets a mock UASManager to be returned when a call is made to instance()
     **/
    static void setMockUASManager(UASManagerInterface* mockUASManager);
pixhawk's avatar
pixhawk committed
60 61 62 63 64 65 66

    /**
     * @brief Get the currently selected UAS
     *
     * @return NULL pointer if no UAS exists, active UAS else
     **/
    UASInterface* getActiveUAS();
67 68 69 70 71 72
    /**
     * @brief getActiveUASWaypointManager
     * @return uas->getUASWaypointManager(), or if not connected, a singleton instance of a UASWaypointManager.
     */
    UASWaypointManager *getActiveUASWaypointManager();

73
    UASInterface* silentGetActiveUAS();
pixhawk's avatar
pixhawk committed
74 75 76 77 78 79 80 81 82 83 84
    /**
     * @brief Get the UAS with this id
     *
     * Although not enforced by this implementation, the IDs are constrained to be
     * in the range of 1 - 127 by the MAVLINK protocol.
     *
     * @param id unique system / aircraft id
     * @return UAS with the given ID, NULL pointer else
     **/
    UASInterface* getUASForId(int id);

lm's avatar
lm committed
85
    QList<UASInterface*> getUASList();
86
    /** @brief Get home position latitude */
87 88 89
    double getHomeLatitude() const {
        return homeLat;
    }
90
    /** @brief Get home position longitude */
91 92 93
    double getHomeLongitude() const {
        return homeLon;
    }
94
    /** @brief Get home position altitude */
95 96 97
    double getHomeAltitude() const {
        return homeAlt;
    }
lm's avatar
lm committed
98

99 100 101 102 103 104
    /** @brief Get the home position coordinate frame */
    int getHomeFrame() const
    {
        return homeFrame;
    }

105 106 107 108 109 110
    /** @brief Convert WGS84 coordinates to earth centric frame */
    Eigen::Vector3d wgs84ToEcef(const double & latitude, const double & longitude, const double & altitude);
    /** @brief Convert earth centric frame to EAST-NORTH-UP frame (x-y-z directions */
    Eigen::Vector3d ecefToEnu(const Eigen::Vector3d & ecef);
    /** @brief Convert WGS84 lat/lon coordinates to carthesian coordinates with home position as origin */
    void wgs84ToEnu(const double& lat, const double& lon, const double& alt, double* east, double* north, double* up);
LM's avatar
LM committed
111 112 113 114
    /** @brief Convert x,y,z coordinates to lat / lon / alt coordinates in east-north-up frame */
    void enuToWgs84(const double& x, const double& y, const double& z, double* lat, double* lon, double* alt);
    /** @brief Convert x,y,z coordinates to lat / lon / alt coordinates in north-east-down frame */
    void nedToWgs84(const double& x, const double& y, const double& z, double* lat, double* lon, double* alt);
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    void getLocalNEDSafetyLimits(double* x1, double* y1, double* z1, double* x2, double* y2, double* z2)
    {
        *x1 = nedSafetyLimitPosition1.x();
        *y1 = nedSafetyLimitPosition1.y();
        *z1 = nedSafetyLimitPosition1.z();

        *x2 = nedSafetyLimitPosition2.x();
        *y2 = nedSafetyLimitPosition2.y();
        *z2 = nedSafetyLimitPosition2.z();
    }

    /** @brief Check if a position is in the local NED safety limits */
    bool isInLocalNEDSafetyLimits(double x, double y, double z)
    {
        if (x < nedSafetyLimitPosition1.x() &&
            y > nedSafetyLimitPosition1.y() &&
            z < nedSafetyLimitPosition1.z() &&
            x > nedSafetyLimitPosition2.x() &&
            y < nedSafetyLimitPosition2.y() &&
            z > nedSafetyLimitPosition2.z())
        {
            // Within limits
            return true;
        }
        else
        {
            // Not within limits
            return false;
        }
    }

147 148
//    void wgs84ToNed(const double& lat, const double& lon, const double& alt, double* north, double* east, double* down);

pixhawk's avatar
pixhawk committed
149 150 151 152 153 154 155 156 157 158 159

public slots:

    /**
     * @brief Add a new UAS to the list
     *
     * This command will only be executed if this UAS does not yet exist.
     * @param UAS unmanned air system to add
     **/
    void addUAS(UASInterface* UAS);

160
    /** @brief Remove a system from the list. If this is the active UAS, it switches to another one calling setActiveUAS. Also triggers the UAS to kill itself. */
161
    void removeUAS(UASInterface* uas);
162

pixhawk's avatar
pixhawk committed
163 164

    /**
165
      * @brief Set a UAS as currently selected. NULL is a valid value for when no other valid UAS's are available.
pixhawk's avatar
pixhawk committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
      *
      * @param UAS Unmanned Air System to set
      **/
    void setActiveUAS(UASInterface* UAS);


    /**
     * @brief Launch the active UAS
     *
     * The groundstation has always one Unmanned Air System selected.
     * All commands are executed on the UAS in focus. This command starts
     * the launch sequence.
     *
     * @return True if the UAS could be launched, false else
     */
    bool launchActiveUAS();

    bool haltActiveUAS();

    bool continueActiveUAS();

    /**
     * @brief Land the active UAS
     *
     * The groundstation has always one Unmanned Air System selected.
     * All commands are executed on the UAS in focus. This command starts
     * the land sequence. Depending on the onboard control, this could mean
     * returning to the landing spot as well as descending on the current
     * position.
     *
     * @return True if the UAS could be landed, false else
     */
    bool returnActiveUAS();


    /**
     * @brief EMERGENCY: Stop active UAS
     *
     * The groundstation has always one Unmanned Air System selected.
     * All commands are executed on the UAS in focus. This command
     * starts an emergency landing. Depending on the onboard control,
     * this usually means descending rapidly on the current position.
     *
     * @warning This command can severely damage the UAS!
     *
     * @return True if the UAS could be landed, false else
     */
    bool stopActiveUAS();

    /**
     * @brief EMERGENCY: Kill active UAS
     *
     * The groundstation has always one Unmanned Air System selected.
     * All commands are executed on the UAS in focus. This command
     * shuts off all onboard motors immediately. This leads to a
     * system crash, but might prevent external damage, e.g. to people.
     * This command is secured by an additional popup message window.
     *
     * @warning THIS COMMAND RESULTS IN THE LOSS OF THE SYSTEM!
     *
     * @return True if the UAS could be landed, false else
     */
    bool killActiveUAS();

    /**
     * @brief Configure the currently active UAS
     *
     * This command will bring up the configuration dialog for the particular UAS.
     */
    void configureActiveUAS();

pixhawk's avatar
pixhawk committed
237
    /** @brief Shut down the onboard operating system down */
pixhawk's avatar
pixhawk committed
238 239
    bool shutdownActiveUAS();

240
    /** @brief Set the current home position, but do not change it on the UAVs */
241
    bool setHomePosition(double lat, double lon, double alt);
242

243 244 245
    /** @brief Set the current home position on all UAVs*/
    bool setHomePositionAndNotify(double lat, double lon, double alt);

246 247 248
    /** @brief Set the safety limits in local position frame */
    void setLocalNEDSafetyBorders(double x1, double y1, double z1, double x2, double y2, double z2);

249 250 251
    /** @brief Update home position based on the position from one of the UAVs */
    void uavChangedHomePosition(int uav, double lat, double lon, double alt);

252 253 254 255 256
    /** @brief Load settings */
    void loadSettings();
    /** @brief Store settings */
    void storeSettings();

pixhawk's avatar
pixhawk committed
257 258 259

protected:
    UASManager();
260
    QList<UASInterface*> systems;
pixhawk's avatar
pixhawk committed
261
    UASInterface* activeUAS;
262
    UASWaypointManager *offlineUASWaypointManager;
pixhawk's avatar
pixhawk committed
263
    QMutex activeUASMutex;
264 265 266
    double homeLat;
    double homeLon;
    double homeAlt;
267
    int homeFrame;
268 269
    Eigen::Quaterniond ecef_ref_orientation_;
    Eigen::Vector3d ecef_ref_point_;
270 271
    Eigen::Vector3d nedSafetyLimitPosition1;
    Eigen::Vector3d nedSafetyLimitPosition2;
272 273

    void initReference(const double & latitude, const double & longitude, const double & altitude);
274 275 276
    
private:
    static UASManagerInterface* _mockUASManager;
pixhawk's avatar
pixhawk committed
277 278

signals:
279

280
    /** A new system was created */
pixhawk's avatar
pixhawk committed
281
    void UASCreated(UASInterface* UAS);
282 283
    /** A system was deleted */
    void UASDeleted(UASInterface* UAS);
284
    /** @brief The UAS currently under main operator control changed */
pixhawk's avatar
pixhawk committed
285
    void activeUASSet(UASInterface* UAS);
286
    /** @brief The UAS currently under main operator control changed */
287
    void activeUASSet(int systemId);
288
    /** @brief The UAS currently under main operator control changed */
289 290
    void activeUASSetListIndex(int listIndex);
    /** @brief The UAS currently under main operator control changed */
291 292 293
    void activeUASStatusChanged(UASInterface* UAS, bool active);
    /** @brief The UAS currently under main operator control changed */
    void activeUASStatusChanged(int systemId, bool active);
294 295
    /** @brief Current home position changed */
    void homePositionChanged(double lat, double lon, double alt);
296 297 298 299 300 301
public:
    /* Need to align struct pointer to prevent a memory assertion:
     * See http://eigen.tuxfamily.org/dox-devel/TopicUnalignedArrayAssert.html
     * for details
     */
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
pixhawk's avatar
pixhawk committed
302 303 304
};

#endif // _UASMANAGER_H_