RadioCalibrationWindow.cc 11.5 KB
Newer Older
1 2 3
#include "RadioCalibrationWindow.h"

RadioCalibrationWindow::RadioCalibrationWindow(QWidget *parent) :
4 5
        QWidget(parent, Qt::Window),
        radio(new RadioCalibrationData())
6
{
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
    QGridLayout *grid = new QGridLayout();

    aileron = new AirfoilServoCalibrator(AirfoilServoCalibrator::AILERON);
    grid->addWidget(aileron, 0, 0, 1, 1, Qt::AlignTop);

    elevator = new AirfoilServoCalibrator(AirfoilServoCalibrator::ELEVATOR);
    grid->addWidget(elevator, 0, 1, 1, 1, Qt::AlignTop);

    rudder = new AirfoilServoCalibrator(AirfoilServoCalibrator::RUDDER);
    grid->addWidget(rudder, 0, 2, 1, 1, Qt::AlignTop);

    gyro = new SwitchCalibrator(tr("Gyro Mode/Gain"));
    grid->addWidget(gyro, 0, 3, 1, 1, Qt::AlignTop);


    pitch = new CurveCalibrator(tr("Collective Pitch"));
    grid->addWidget(pitch, 1, 0, 1, 2);

    throttle = new CurveCalibrator(tr("Throttle"));
    grid->addWidget(throttle, 1, 2, 1, 2);

28 29
    /* Buttons for loading/transmitting calibration data */
    QHBoxLayout *hbox = new QHBoxLayout();
30 31 32 33 34 35 36 37 38
    QPushButton *load = new QPushButton(tr("Load File"));
    QPushButton *save = new QPushButton(tr("Save File"));
    QPushButton *transmit = new QPushButton(tr("Transmit to UAV"));
    QPushButton *get = new QPushButton(tr("Get from UAV"));
    hbox->addWidget(load);
    hbox->addWidget(save);
    hbox->addWidget(transmit);
    hbox->addWidget(get);
    grid->addLayout(hbox, 2, 0, 1, 4);
39
    this->setLayout(grid);
40 41 42 43

    connect(load, SIGNAL(clicked()), this, SLOT(loadFile()));
    connect(save, SIGNAL(clicked()), this, SLOT(saveFile()));
    connect(transmit, SIGNAL(clicked()), this, SLOT(send()));
44 45
    connect(get, SIGNAL(clicked()), this, SLOT(request()));

46
    connect(aileron, SIGNAL(setpointChanged(int,float)), radio, SLOT(setAileron(int,float)));
47 48 49 50 51
    connect(elevator, SIGNAL(setpointChanged(int,float)), radio, SLOT(setElevator(int,float)));
    connect(rudder, SIGNAL(setpointChanged(int,float)), radio, SLOT(setRudder(int,float)));
    connect(gyro, SIGNAL(setpointChanged(int,float)), radio, SLOT(setGyro(int,float)));
    connect(pitch, SIGNAL(setpointChanged(int,float)), radio, SLOT(setPitch(int,float)));
    connect(throttle, SIGNAL(setpointChanged(int,float)), radio, SLOT(setThrottle(int,float)));
52
    setUASId(0);
53 54
}

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
//void RadioCalibrationWindow::setChannelRaw(int ch, float raw)
//{
//    /** this expects a particular channel to function mapping
//       \todo allow run-time channel mapping
//       */
//    switch (ch)
//    {
//    case 0:
//        aileron->channelChanged(raw);
//        break;
//    case 1:
//        elevator->channelChanged(raw);
//        break;
//    case 2:
//        throttle->channelChanged(raw);
//        break;
//    case 3:
//        rudder->channelChanged(raw);
//        break;
//    case 4:
//        gyro->channelChanged(raw);
//        break;
//    case 5:
//        pitch->channelChanged(raw);
//        break;
80 81


82 83
//    }
//}
84

85 86
//void RadioCalibrationWindow::setChannelScaled(int ch, float normalized)
//{
pixhawk's avatar
pixhawk committed
87 88 89
    // FIXME James
    // FIXME Bryan

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
//    /** this expects a particular channel to function mapping
//       \todo allow run-time channel mapping
//       */
//    switch (ch)
//    {
//    case 0:
//        aileron->channelChanged(raw);
//        break;
//    case 1:
//        elevator->channelChanged(raw);
//        break;
//    case 2:
//        throttle->channelChanged(raw);
//        break;
//    case 3:
//        rudder->channelChanged(raw);
//        break;
//    case 4:
//        gyro->channelChanged(raw);
//        break;
//    case 5:
//        pitch->channelChanged(raw);
//        break;


//    }
116
//}
117

118
void RadioCalibrationWindow::setChannel(int ch, float raw)
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
{
    /** this expects a particular channel to function mapping
       \todo allow run-time channel mapping
       */
    switch (ch)
    {
    case 0:
        aileron->channelChanged(raw);
        break;
    case 1:
        elevator->channelChanged(raw);
        break;
    case 2:
        throttle->channelChanged(raw);
        break;
    case 3:
        rudder->channelChanged(raw);
        break;
    case 4:
        gyro->channelChanged(raw);
        break;
    case 5:
        pitch->channelChanged(raw);
        break;


    }
146
}
147

148
void RadioCalibrationWindow::saveFile()
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 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
    QString fileName(QFileDialog::getSaveFileName(this,
                                                  tr("Save RC Calibration"),
                                                  "settings/",
                                                  tr("XML Files (*.xml)")));
    if (fileName.isEmpty())
        return;

    QDomDocument *rcConfig = new QDomDocument();

    QFile rcFile(fileName);
    if (rcFile.exists())
    {
            rcFile.remove();
    }
    if (!rcFile.open(QFile::WriteOnly | QFile::Text))
    {
        qDebug() << __FILE__ << __LINE__ << "could not open"  << rcFile.fileName() << "for writing";
        return;
    }

    QDomElement root;
    rcConfig->appendChild(root=rcConfig->createElement("channels"));
    QDomElement e;
    QDomText t;

    // Aileron
    e = rcConfig->createElement("threeSetpoint");
    e.setAttribute("name", "Aileron");
    e.setAttribute("number", "1");
    t = rcConfig->createTextNode(radio->toString(RadioCalibrationData::AILERON));
    e.appendChild(t);
    root.appendChild(e);
    // Elevator
    e = rcConfig->createElement("threeSetpoint");
    e.setAttribute("name", "Elevator");
    e.setAttribute("number", "2");
    t = rcConfig->createTextNode(radio->toString(RadioCalibrationData::ELEVATOR));
    e.appendChild(t);
    root.appendChild(e);
    // Rudder
    e = rcConfig->createElement("threeSetpoint");
    e.setAttribute("name", "Rudder");
    e.setAttribute("number", "4");
    t = rcConfig->createTextNode(radio->toString(RadioCalibrationData::RUDDER));
    e.appendChild(t);
    root.appendChild(e);
    // Gyro Mode/Gain
    e = rcConfig->createElement("twoSetpoint");
    e.setAttribute("name", "Gyro");
    e.setAttribute("number", "5");
    t = rcConfig->createTextNode(radio->toString(RadioCalibrationData::GYRO));
    e.appendChild(t);
    root.appendChild(e);
    // Throttle
    e = rcConfig->createElement("fiveSetpoint");
    e.setAttribute("name", "Throttle");
    e.setAttribute("number", "3");
    t = rcConfig->createTextNode(radio->toString(RadioCalibrationData::THROTTLE));
    e.appendChild(t);
    root.appendChild(e);
    // Pitch
    e = rcConfig->createElement("fiveSetpoint");
    e.setAttribute("name", "Pitch");
    e.setAttribute("number", "6");
    t = rcConfig->createTextNode(radio->toString(RadioCalibrationData::PITCH));
    e.appendChild(t);
    root.appendChild(e);


    QTextStream out(&rcFile);
    const int IndentSize = 4;
    rcConfig->save(out, IndentSize);
    rcFile.close();

224 225
}

226
void RadioCalibrationWindow::loadFile()
227
{
228 229
    QString fileName(QFileDialog::getOpenFileName(this,
                                                  tr("Load RC Calibration"),
230
                                                  "settings/",
231
                                                  tr("XML Files (*.xml)")));
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

    if (fileName.isEmpty())
        return;

    QFile rcFile(fileName);
    if (!rcFile.exists())
    {        
        return;
    }

    if (!rcFile.open(QIODevice::ReadOnly))
    {
        return;
    }

    QDomDocument *rcConfig = new QDomDocument();

    QString errorStr;
    int errorLine;
    int errorColumn;

    if (!rcConfig->setContent(&rcFile, true, &errorStr, &errorLine,
                                &errorColumn))
    {
        qDebug() << "Error reading XML Parameter File on line: " << errorLine << errorStr;
        return;
    }

260
    rcFile.close();
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
    QDomElement root = rcConfig->documentElement();
    if (root.tagName() != "channels") {
        qDebug() << __FILE__ << __LINE__ << "This is not a Radio Calibration xml file";
        return;
    }


    QPointer<RadioCalibrationData> newRadio = new RadioCalibrationData();
    QDomElement child = root.firstChildElement();
    while (!child.isNull())
    {
        parseSetpoint(child, newRadio);
        child = child.nextSiblingElement();
    }

    receive(newRadio);

    delete newRadio;
    delete rcConfig;
}

void RadioCalibrationWindow::parseSetpoint(const QDomElement &setpoint, const QPointer<RadioCalibrationData>& newRadio)
{
    QVector<float> setpoints;
    QStringList setpointList = setpoint.text().split(",", QString::SkipEmptyParts);
    foreach (QString setpoint, setpointList)
        setpoints << setpoint.trimmed().toFloat();

289
//    qDebug() << __FILE__ << __LINE__ << ": " << setpoint.tagName() << ": " << setpoint.attribute("name") ;
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    if (setpoint.tagName() == "threeSetpoint")
    {
        if (setpoints.isEmpty())
            setpoints << 0 << 0 << 0;
        for (int i=0; i<3; ++i)
        {
            if (setpoint.attribute("name").toUpper() == "AILERON")
                newRadio->setAileron(i, setpoints[i]);
            else if(setpoint.attribute("name").toUpper() == "ELEVATOR")
                newRadio->setElevator(i, setpoints[i]);
            else if(setpoint.attribute("name").toUpper() == "RUDDER")
                newRadio->setRudder(i, setpoints[i]);
        }
    }    
    else if (setpoint.tagName() == "twoSetpoint")
    {
        if (setpoints.isEmpty())
            setpoints << 0 << 0;
        for (int i=0; i<2; ++i)
        {
            if (setpoint.attribute("name").toUpper() == "GYRO")
                newRadio->setGyro(i, setpoints[i]);
        }
    }
    else if (setpoint.tagName() == "fiveSetpoint")
    {
        if (setpoints.isEmpty())
            setpoints << 0 << 0 << 0 << 0 << 0;
        for (int i=0; i<5; ++i)
        {
            if (setpoint.attribute("name").toUpper() == "PITCH")
                newRadio->setPitch(i, setpoints[i]);
            else if (setpoint.attribute("name").toUpper() == "THROTTLE")
                newRadio->setThrottle(i, setpoints[i]);
        }
    }
326 327
}

328 329
void RadioCalibrationWindow::send()
{
330 331 332
    qDebug() << __FILE__ << __LINE__ << "uasId = " << uasId;
#ifdef MAVLINK_ENABLED_UALBERTA_MESSAGES
    UAS *uas = dynamic_cast<UAS*>(UASManager::instance()->getUASForId(uasId));
333 334 335 336
    if (uas)
    {
        mavlink_message_t msg;
        mavlink_msg_radio_calibration_pack(uasId, 0, &msg,
337 338 339 340 341 342
                                           (*radio)[RadioCalibrationData::AILERON],
                                           (*radio)[RadioCalibrationData::ELEVATOR],
                                           (*radio)[RadioCalibrationData::RUDDER],
                                           (*radio)[RadioCalibrationData::GYRO],
                                           (*radio)[RadioCalibrationData::PITCH],
                                           (*radio)[RadioCalibrationData::THROTTLE]);
343
        uas->sendMessage(msg);
344 345 346 347
    }
#endif
}

348
void RadioCalibrationWindow::request()
349
{
350
    qDebug() << __FILE__ << __LINE__ << "READ FROM UAV";
351 352 353 354 355 356 357
    UAS *uas = dynamic_cast<UAS*>(UASManager::instance()->getUASForId(uasId));
    if (uas)
    {
        mavlink_message_t msg;
        mavlink_msg_action_pack(uasId, 0, &msg, 0, 0, ::MAV_ACTION_CALIBRATE_RC);
        uas->sendMessage(msg);
    }
358 359
}

360
void RadioCalibrationWindow::receive(const QPointer<RadioCalibrationData>& radio)
361
{
362 363 364 365 366 367 368 369 370 371 372 373 374
    if (radio)
    {
        if (this->radio)
            delete this->radio;
        this->radio = new RadioCalibrationData(*radio);

        aileron->set((*radio)(RadioCalibrationData::AILERON));
        elevator->set((*radio)(RadioCalibrationData::ELEVATOR));
        rudder->set((*radio)(RadioCalibrationData::RUDDER));
        gyro->set((*radio)(RadioCalibrationData::GYRO));
        pitch->set((*radio)(RadioCalibrationData::PITCH));
        throttle->set((*radio)(RadioCalibrationData::THROTTLE));
    }        
375
}