PX4AirframeLoader.cc 9.72 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
/*=====================================================================

 QGroundControl Open Source Ground Control Station

 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

 This file is part of the QGROUNDCONTROL project

 QGROUNDCONTROL 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.

 QGROUNDCONTROL 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 QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

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

/// @file
///     @author Don Gagne <don@thegagnes.com>

27
#include "PX4AirframeLoader.h"
28 29 30
#include "QGCApplication.h"
#include "QGCLoggingCategory.h"
#include "AirframeComponentAirframes.h"
31

32 33 34 35 36 37
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QDebug>

QGC_LOGGING_CATEGORY(PX4AirframeLoaderLog, "PX4AirframeLoaderLog")
38

39 40 41 42
bool PX4AirframeLoader::_airframeMetaDataLoaded = false;

PX4AirframeLoader::PX4AirframeLoader(AutoPilotPlugin* autopilot, UASInterface* uas, QObject* parent)
{
Lorenz Meier's avatar
Lorenz Meier committed
43 44 45
    Q_UNUSED(autopilot);
    Q_UNUSED(uas);
    Q_UNUSED(parent);
46
    Q_ASSERT(uas);
47 48
}

49 50 51 52
/// Load Airframe Fact meta data
///
/// The meta data comes from firmware airframes.xml file.
void PX4AirframeLoader::loadAirframeFactMetaData(void)
53
{
54 55 56 57 58
    if (_airframeMetaDataLoaded) {
        return;
    }

    qCDebug(PX4AirframeLoaderLog) << "Loading PX4 airframe fact meta data";
Lorenz Meier's avatar
Lorenz Meier committed
59
    qDebug() << "LOADING META DATA";
60

Lorenz Meier's avatar
Lorenz Meier committed
61 62 63 64 65 66 67 68 69 70 71 72
    Q_ASSERT(AirframeComponentAirframes::get().count() == 0);

    QString airframeFilename;

    // We want unit test builds to always use the resource based meta data to provide repeatable results
    if (!qgcApp()->runningUnitTests()) {
        // First look for meta data that comes from a firmware download. Fall back to resource if not there.
        QSettings settings;
        QDir parameterDir = QFileInfo(settings.fileName()).dir();
        airframeFilename = parameterDir.filePath("PX4AirframeFactMetaData.xml");
    }
    if (airframeFilename.isEmpty() || !QFile(airframeFilename).exists()) {
73
        airframeFilename = ":/AutoPilotPlugins/PX4/AirframeFactMetaData.xml";
Lorenz Meier's avatar
Lorenz Meier committed
74 75 76 77 78 79
    }

    qCDebug(PX4AirframeLoaderLog) << "Loading meta data file:" << airframeFilename;

    QFile xmlFile(airframeFilename);
    Q_ASSERT(xmlFile.exists());
80

Lorenz Meier's avatar
Lorenz Meier committed
81 82 83
    bool success = xmlFile.open(QIODevice::ReadOnly);
    Q_UNUSED(success);
    Q_ASSERT(success);
84

Lorenz Meier's avatar
Lorenz Meier committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    if (!success) {
        qWarning() << "Failed opening airframe XML";
        return;
    }

    QXmlStreamReader xml(xmlFile.readAll());
    xmlFile.close();
    if (xml.hasError()) {
        qWarning() << "Badly formed XML" << xml.errorString();
        return;
    }

    QString         airframeGroup;
    QString         image;
    QString         errorString;
    int             xmlState = XmlStateNone;
    bool            badMetaData = true;

    while (!xml.atEnd()) {
        if (xml.isStartElement()) {
            QString elementName = xml.name().toString();

            if (elementName == "airframes") {
                if (xmlState != XmlStateNone) {
                    qWarning() << "Badly formed XML";
                    return;
                }
                xmlState = XmlStateFoundAirframes;

            } else if (elementName == "version") {
                if (xmlState != XmlStateFoundAirframes) {
                    qWarning() << "Badly formed XML";
                    return;
                }
                xmlState = XmlStateFoundVersion;

                bool convertOk;
                QString strVersion = xml.readElementText();
                int intVersion = strVersion.toInt(&convertOk);
                if (!convertOk) {
                    qWarning() << "Badly formed XML";
                    return;
                }
                if (intVersion < 1) {
                    // We can't read these old files
                    qDebug() << "Airframe version stamp too old, skipping load. Found:" << intVersion << "Want: 3 File:" << airframeFilename;
                    return;
                }


            } else if (elementName == "airframe_group") {
                if (xmlState != XmlStateFoundVersion) {
                    // We didn't get a version stamp, assume older version we can't read
                    qDebug() << "Parameter version stamp not found, skipping load" << airframeFilename;
                    return;
                }
                xmlState = XmlStateFoundGroup;

                if (!xml.attributes().hasAttribute("name") || !xml.attributes().hasAttribute("image")) {
                    qWarning() << "Badly formed XML";
                    return;
                }
                airframeGroup = xml.attributes().value("name").toString();
                image = xml.attributes().value("image").toString();
                qCDebug(PX4AirframeLoaderLog) << "Found group: " << airframeGroup;

            } else if (elementName == "airframe") {
                if (xmlState != XmlStateFoundGroup) {
                    qWarning() << "Badly formed XML";
                    return;
                }
                xmlState = XmlStateFoundAirframe;

                if (!xml.attributes().hasAttribute("name") || !xml.attributes().hasAttribute("id")) {
                    qWarning() << "Badly formed XML";
                    return;
                }

                QString name = xml.attributes().value("name").toString();
                QString id = xml.attributes().value("id").toString();

                qCDebug(PX4AirframeLoaderLog) << "Found airframe name:" << name << " type:" << airframeGroup << " id:" << id;

                // Now that we know type we can airframe meta data object and add it to the system
                AirframeComponentAirframes::insert(airframeGroup, image, name, id.toInt());

            } else {
                // We should be getting meta data now
                if (xmlState != XmlStateFoundAirframe) {
                    qWarning() << "Badly formed XML";
                    return;
                }

                if (!badMetaData) {

                    // We eventually want this, just not yet now
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
//                    if (elementName == "short_desc") {
//                        Q_ASSERT(metaData);
//                        QString text = xml.readElementText();
//                        text = text.replace("\n", " ");
//                        qCDebug(PX4AirframeLoaderLog) << "Short description:" << text;
//                        metaData->setShortDescription(text);

//                    } else if (elementName == "long_desc") {
//                        Q_ASSERT(metaData);
//                        QString text = xml.readElementText();
//                        text = text.replace("\n", " ");
//                        qCDebug(PX4AirframeLoaderLog) << "Long description:" << text;
//                        metaData->setLongDescription(text);

//                    } else if (elementName == "min") {
//                        Q_ASSERT(metaData);
//                        QString text = xml.readElementText();
//                        qCDebug(PX4AirframeLoaderLog) << "Min:" << text;

//                        QVariant varMin;
//                        if (metaData->convertAndValidate(text, true /* convertOnly */, varMin, errorString)) {
//                            metaData->setMin(varMin);
//                        } else {
//                            qCWarning(PX4AirframeLoaderLog) << "Invalid min value, name:" << metaData->name() << " type:" << metaData->type() << " min:" << text << " error:" << errorString;
//                        }

//                    } else if (elementName == "max") {
//                        Q_ASSERT(metaData);
//                        QString text = xml.readElementText();
//                        qCDebug(PX4AirframeLoaderLog) << "Max:" << text;

//                        QVariant varMax;
//                        if (metaData->convertAndValidate(text, true /* convertOnly */, varMax, errorString)) {
//                            metaData->setMax(varMax);
//                        } else {
//                            qCWarning(PX4AirframeLoaderLog) << "Invalid max value, name:" << metaData->name() << " type:" << metaData->type() << " max:" << text << " error:" << errorString;
//                        }

//                    } else if (elementName == "unit") {
//                        Q_ASSERT(metaData);
//                        QString text = xml.readElementText();
//                        qCDebug(PX4AirframeLoaderLog) << "Unit:" << text;
//                        metaData->setUnits(text);

//                    } else {
Lorenz Meier's avatar
Lorenz Meier committed
226
                        qDebug() << "Unknown element in XML: " << elementName;
227
//                    }
Lorenz Meier's avatar
Lorenz Meier committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
                }
            }
        } else if (xml.isEndElement()) {
            QString elementName = xml.name().toString();

            if (elementName == "airframe") {
                // Done loading this airframe, validate

                // Reset for next airframe
                badMetaData = false;
                xmlState = XmlStateFoundGroup;
            } else if (elementName == "airframe_group") {
                xmlState = XmlStateFoundVersion;
            } else if (elementName == "airframes") {
                xmlState = XmlStateFoundAirframes;
            }
        }
        xml.readNext();
    }

    _airframeMetaDataLoaded = true;
249 250
}

251 252
void PX4AirframeLoader::clearStaticData(void)
{
Lorenz Meier's avatar
Lorenz Meier committed
253
    AirframeComponentAirframes::clear();
254 255
    _airframeMetaDataLoaded = false;
}