UASManager.cc 9.24 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2
/*=====================================================================

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

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

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

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

lm's avatar
lm committed
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
lm's avatar
lm committed
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 Implementation of class UASManager
pixhawk's avatar
pixhawk committed
27 28 29 30 31 32 33 34
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QList>
#include <QApplication>
#include <QMessageBox>
#include <QTimer>
35
#include <QSettings>
pixhawk's avatar
pixhawk committed
36
#include "UAS.h"
37 38 39
#include "UASInterface.h"
#include "UASManager.h"
#include "QGC.h"
pixhawk's avatar
pixhawk committed
40

41 42
UASManager* UASManager::instance()
{
pixhawk's avatar
pixhawk committed
43 44 45 46
    static UASManager* _instance = 0;
    if(_instance == 0) {
        _instance = new UASManager();

pixhawk's avatar
pixhawk committed
47 48
        // Set the application as parent to ensure that this object
        // will be destroyed when the main application exits
pixhawk's avatar
pixhawk committed
49 50 51 52 53
        _instance->setParent(qApp);
    }
    return _instance;
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
void UASManager::storeSettings()
{
    QSettings settings;
    settings.beginGroup("QGC_UASMANAGER");
    settings.setValue("HOMELAT", homeLat);
    settings.setValue("HOMELON", homeLon);
    settings.setValue("HOMEALT", homeAlt);
    settings.endGroup();
    settings.sync();
}

void UASManager::loadSettings()
{
    QSettings settings;
    settings.sync();
    settings.beginGroup("QGC_UASMANAGER");
70
    bool changed =  setHomePosition(settings.value("HOMELAT", homeLat).toDouble(),
71 72
                    settings.value("HOMELON", homeLon).toDouble(),
                    settings.value("HOMEALT", homeAlt).toDouble());
73 74 75 76 77 78 79 80

    // Make sure to fire the change - this will
    // make sure widgets get the signal once
    if (!changed)
    {
        emit homePositionChanged(homeLat, homeLon, homeAlt);
    }

81 82 83
    settings.endGroup();
}

84
bool UASManager::setHomePosition(double lat, double lon, double alt)
85 86
{
    // Checking for NaN and infitiny
87 88 89 90 91 92
    // and checking for borders
    bool changed = false;
    if (!isnan(lat) && !isnan(lon) && !isnan(alt)
        && !isinf(lat) && !isinf(lon) && !isinf(alt)
        && lat <= 90.0 && lat >= -90.0 && lon <= 180.0 && lon >= -180.0)
        {
93

94 95 96 97
        if (homeLat != lat) changed = true;
        if (homeLon != lon) changed = true;
        if (homeAlt != alt) changed = true;

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        if (changed)
        {
            homeLat = lat;
            homeLon = lon;
            homeAlt = alt;
            emit homePositionChanged(homeLat, homeLon, homeAlt);

            // Update all UAVs
            foreach (UASInterface* mav, systems)
            {
                mav->setHomePosition(homeLat, homeLon, homeAlt);
            }
        }
    }
    return changed;
}
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/**
 * This function will change QGC's home position on a number of conditions only
 */
void UASManager::uavChangedHomePosition(int uav, double lat, double lon, double alt)
{
    // FIXME: Accept any home position change for now from the active UAS
    // this means that the currently select UAS can change the home location
    // of the whole swarm. This makes sense, but more control might be needed
    if (uav == activeUAS->getUASID())
    {
        if (setHomePosition(lat, lon, alt))
        {
            foreach (UASInterface* mav, systems)
            {
                // Only update the other systems, not the original source
                if (mav->getUASID() != uav)
                {
                    mav->setHomePosition(homeLat, homeLon, homeAlt);
                }
            }
        }
136 137 138
    }
}

pixhawk's avatar
pixhawk committed
139 140 141 142 143 144
/**
 * @brief Private singleton constructor
 *
 * This class implements the singleton design pattern and has therefore only a private constructor.
 **/
UASManager::UASManager() :
145 146 147 148
    activeUAS(NULL),
    homeLat(47.3769),
    homeLon(8.549444),
    homeAlt(470.0)
pixhawk's avatar
pixhawk committed
149 150
{
    start(QThread::LowPriority);
151
    loadSettings();
pixhawk's avatar
pixhawk committed
152 153 154 155
}

UASManager::~UASManager()
{
156
    storeSettings();
157
    // Delete all systems
158
    foreach (UASInterface* mav, systems) {
159 160
        delete mav;
    }
pixhawk's avatar
pixhawk committed
161 162 163 164 165
}


void UASManager::run()
{
lm's avatar
lm committed
166 167 168 169 170
//    forever
//    {
//        QGC::SLEEP::msleep(5000);
//    }
    exec();
pixhawk's avatar
pixhawk committed
171 172 173 174
}

void UASManager::addUAS(UASInterface* uas)
{
175 176 177 178 179 180
    // WARNING: The active uas is set here
    // and then announced below. This is necessary
    // to make sure the getActiveUAS() function
    // returns the UAS once the UASCreated() signal
    // is emitted. The code is thus NOT redundant.
    bool firstUAS = false;
181
    if (activeUAS == NULL) {
182 183 184 185
        firstUAS = true;
        activeUAS = uas;
    }

pixhawk's avatar
pixhawk committed
186
    // Only execute if there is no UAS at this index
187
    if (!systems.contains(uas)) {
188
        systems.append(uas);
189
        connect(uas, SIGNAL(destroyed(QObject*)), this, SLOT(removeUAS(QObject*)));
190 191 192 193
        // Set home position on UAV if set in UI
        // - this is done on a per-UAV basis
        // Set home position in UI if UAV chooses a new one (caution! if multiple UAVs are connected, take care!)
        connect(uas, SIGNAL(homePositionChanged(int,double,double,double)), this, SLOT(uavChangedHomePosition(int,double,double,double)));
pixhawk's avatar
pixhawk committed
194 195 196 197
        emit UASCreated(uas);
    }

    // If there is no active UAS yet, set the first one as the active UAS
198
    if (firstUAS) {
199
        setActiveUAS(uas);
pixhawk's avatar
pixhawk committed
200 201 202
    }
}

203 204 205 206
void UASManager::removeUAS(QObject* uas)
{
    UASInterface* mav = qobject_cast<UASInterface*>(uas);

207
    if (mav) {
208 209
        int listindex = systems.indexOf(mav);

210 211
        if (mav == activeUAS) {
            if (systems.count() > 1) {
212
                // We only set a new UAS if more than one is present
213
                if (listindex != 0) {
214 215 216
                    // The system to be removed is not at position 1
                    // set position one as new active system
                    setActiveUAS(systems.first());
217
                } else {
218 219 220 221
                    // The system to be removed is at position 1,
                    // select the next system
                    setActiveUAS(systems.at(1));
                }
222
            } else {
223 224 225 226 227 228 229 230 231
                // TODO send a null pointer if no UAS is present any more
                // This has to be proberly tested however, since it might
                // crash code parts not handling null pointers correctly.
            }
        }
        systems.removeAt(listindex);
    }
}

lm's avatar
lm committed
232 233
QList<UASInterface*> UASManager::getUASList()
{
234
    return systems;
lm's avatar
lm committed
235 236
}

pixhawk's avatar
pixhawk committed
237 238 239 240 241
UASInterface* UASManager::getActiveUAS()
{
    return activeUAS; ///< Return zero pointer if no UAS has been loaded
}

242 243 244 245 246
UASInterface* UASManager::silentGetActiveUAS()
{
    return activeUAS; ///< Return zero pointer if no UAS has been loaded
}

pixhawk's avatar
pixhawk committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
bool UASManager::launchActiveUAS()
{
    // If the active UAS is set, execute command
    if (getActiveUAS()) activeUAS->launch();
    return (activeUAS); ///< Returns true if the UAS exists, false else
}

bool UASManager::haltActiveUAS()
{
    // If the active UAS is set, execute command
    if (getActiveUAS()) activeUAS->halt();
    return (activeUAS); ///< Returns true if the UAS exists, false else
}

bool UASManager::continueActiveUAS()
{
    // If the active UAS is set, execute command
    if (getActiveUAS()) activeUAS->go();
    return (activeUAS); ///< Returns true if the UAS exists, false else
}

bool UASManager::returnActiveUAS()
{
    // If the active UAS is set, execute command
    if (getActiveUAS()) activeUAS->home();
    return (activeUAS); ///< Returns true if the UAS exists, false else
}

bool UASManager::stopActiveUAS()
{
    // If the active UAS is set, execute command
    if (getActiveUAS()) activeUAS->emergencySTOP();
    return (activeUAS); ///< Returns true if the UAS exists, false else
}

bool UASManager::killActiveUAS()
{
    if (getActiveUAS()) activeUAS->emergencyKILL();
    return (activeUAS);
}

bool UASManager::shutdownActiveUAS()
{
    if (getActiveUAS()) activeUAS->shutdown();
    return (activeUAS);
}

void UASManager::configureActiveUAS()
{
    UASInterface* actUAS = getActiveUAS();
297
    if(actUAS) {
pixhawk's avatar
pixhawk committed
298 299 300 301 302 303
        // Do something
    }
}

UASInterface* UASManager::getUASForId(int id)
{
304 305
    UASInterface* system = NULL;

306 307
    foreach(UASInterface* sys, systems) {
        if (sys->getUASID() == id) {
308 309 310 311 312 313
            system = sys;
        }
    }

    // Return NULL if not found
    return system;
pixhawk's avatar
pixhawk committed
314 315
}

pixhawk's avatar
pixhawk committed
316
void UASManager::setActiveUAS(UASInterface* uas)
pixhawk's avatar
pixhawk committed
317
{
318
    if (uas != NULL) {
pixhawk's avatar
pixhawk committed
319
        activeUASMutex.lock();
320
        if (activeUAS != NULL) {
321 322 323
            emit activeUASStatusChanged(activeUAS, false);
            emit activeUASStatusChanged(activeUAS->getUASID(), false);
        }
pixhawk's avatar
pixhawk committed
324 325
        activeUAS = uas;
        activeUASMutex.unlock();
pixhawk's avatar
pixhawk committed
326

327
        activeUAS->setSelected();
pixhawk's avatar
pixhawk committed
328
        emit activeUASSet(uas);
329
        emit activeUASSet(uas->getUASID());
330
        emit activeUASSetListIndex(systems.indexOf(uas));
331 332
        emit activeUASStatusChanged(uas, true);
        emit activeUASStatusChanged(uas->getUASID(), true);
pixhawk's avatar
pixhawk committed
333
    }
pixhawk's avatar
pixhawk committed
334 335
}