OpalLink.cc 14.4 KB
Newer Older
pixhawk's avatar
pixhawk committed
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 27 28 29
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 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
 *   @brief Implementation of class OpalLink
 *   @author Bryan Godbolt <godbolt@ualberta.ca>
 */

30 31
#include "OpalLink.h"

32 33 34 35 36
OpalLink::OpalLink() :
        connectState(false),
        heartbeatTimer(new QTimer(this)),
        heartbeatRate(MAVLINK_HEARTBEAT_DEFAULT_RATE),
        m_heartbeatsEnabled(true),
37
        getSignalsTimer(new QTimer(this)),
Bryan Godbolt's avatar
Bryan Godbolt committed
38
        getSignalsPeriod(10),
39 40 41
        receiveBuffer(new QQueue<QByteArray>()),
        systemID(1),
        componentID(1)
42
{
43
    start(QThread::LowPriority);
Bryan Godbolt's avatar
Bryan Godbolt committed
44 45 46 47 48

    // Set unique ID and add link to the list of links
    this->id = getNextLinkId();
    this->name = tr("OpalRT link ") + QString::number(getId());
    LinkManager::instance()->add(this);
49 50 51

    // Start heartbeat timer, emitting a heartbeat at the configured rate
    QObject::connect(heartbeatTimer, SIGNAL(timeout()), this, SLOT(heartbeat()));
52 53

    QObject::connect(getSignalsTimer, SIGNAL(timeout()), this, SLOT(getSignals()));
54 55
}

56 57 58 59 60 61 62 63

/*
 *
  Communication
 *
 */

qint64 OpalLink::bytesAvailable()
64
{
65
    return 0;
Bryan Godbolt's avatar
Bryan Godbolt committed
66 67
}

68
void OpalLink::writeBytes(const char *bytes, qint64 length)
Bryan Godbolt's avatar
Bryan Godbolt committed
69
{
70 71 72 73 74 75 76 77 78 79 80 81 82 83
    /* decode the message */
    mavlink_message_t msg;
    mavlink_status_t status;        
    int decodeSuccess = 0;
    for (int i=0; (!(decodeSuccess=mavlink_parse_char(this->getId(), bytes[i], &msg, &status))&& i<length); ++i);    

    /* perform the appropriate action */
    if (decodeSuccess)
    {
        switch(msg.msgid)
        {
        case MAVLINK_MSG_ID_PARAM_REQUEST_LIST:
            {
                qDebug() << "OpalLink::writeBytes(): request params";
84
//                getParameterList();
85

86
                mavlink_message_t param;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
//                char paramName[] = "NAV_FILT_INIT";
//                mavlink_msg_param_value_pack(systemID, componentID, &param,
//                                             (int8_t*)(paramName),
//                                             0,
//                                             1,
//                                             0);
//                receiveMessage(param);

                OpalRT::ParameterList::const_iterator paramIter;
                for (paramIter = params->begin(); paramIter != params->end(); ++paramIter)
                {
                    mavlink_msg_param_value_pack(systemID,
                                                 (*paramIter).getComponentID(),
                                                 &param,
                                                 (*paramIter).getParamID().toInt8_t(),
                                                 (static_cast<OpalRT::Parameter>(*paramIter)).getValue(),
                                                 params->count(),
                                                 params->indexOf(*paramIter));
                    receiveMessage(param);
                }


109 110 111 112 113 114 115
            }
        case MAVLINK_MSG_ID_PARAM_SET:
            {
                qDebug() << "OpalLink::writeBytes(): Attempt to set a parameter";
                mavlink_param_set_t param;
                mavlink_msg_param_set_decode(&msg, &param);
                QString paramName((char*)param.param_id);
Bryan Godbolt's avatar
Bryan Godbolt committed
116
                qDebug() << "OpalLink::writeBytes():paramName: " << paramName;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                if (paramName == "NAV_FILT_INIT")
                {
                    if (param.param_value == 1 || param.param_value == 0)
                    {
                        double values[2] = {};
                        values[0] = param.param_value;
                        setSignals(values);
                    }
                    else
                    {
                        qDebug() << "OpalLink::writeBytes(): Param NAV_FILT_INIT must be 1 or 0";
                    }
                }
            }
            break;
        default:
            {
                qDebug() << "OpalLink::writeBytes(): Unknown mavlink packet";
            }
        }
    }
Bryan Godbolt's avatar
Bryan Godbolt committed
138 139
}

140

141
void OpalLink::readBytes()
Bryan Godbolt's avatar
Bryan Godbolt committed
142
{
143
    receiveDataMutex.lock();
Bryan Godbolt's avatar
Bryan Godbolt committed
144 145
//    qDebug() << "OpalLink::readBytes(): Reading a message.  size of buffer: " << receiveBuffer->count();
//    QByteArray message = receiveBuffer->dequeue();
146 147


Bryan Godbolt's avatar
Bryan Godbolt committed
148
    emit bytesReceived(this, receiveBuffer->dequeue());
149 150
    receiveDataMutex.unlock();

Bryan Godbolt's avatar
Bryan Godbolt committed
151 152
}

153
void OpalLink::receiveMessage(mavlink_message_t message)
Bryan Godbolt's avatar
Bryan Godbolt committed
154 155
{

156 157 158 159 160 161 162
    // Create buffer
    char buffer[MAVLINK_MAX_PACKET_LEN];
    // Write message into buffer, prepending start sign
    int len = mavlink_msg_to_send_buffer((uint8_t*)(buffer), &message);
    // If link is connected
    if (isConnected())
    {
Bryan Godbolt's avatar
Bryan Godbolt committed
163
        receiveDataMutex.lock();
164
        receiveBuffer->enqueue(QByteArray(buffer, len));
Bryan Godbolt's avatar
Bryan Godbolt committed
165
        receiveDataMutex.unlock();
166
        readBytes();
167 168
    }

Bryan Godbolt's avatar
Bryan Godbolt committed
169 170
}

171
void OpalLink::heartbeat()
Bryan Godbolt's avatar
Bryan Godbolt committed
172
{
173 174 175

    if (m_heartbeatsEnabled)
    {
Bryan Godbolt's avatar
Bryan Godbolt committed
176
//        qDebug() << "OpalLink::heartbeat(): Generate a heartbeat";
177 178 179 180 181
        mavlink_message_t beat;
        mavlink_msg_heartbeat_pack(systemID, componentID,&beat, MAV_HELICOPTER, MAV_AUTOPILOT_GENERIC);
        receiveMessage(beat);
    }

Bryan Godbolt's avatar
Bryan Godbolt committed
182
}
183 184
void OpalLink::setSignals(double *values)
{
Bryan Godbolt's avatar
Bryan Godbolt committed
185
    unsigned short numSignals = 2;
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    unsigned short logicalId = 1;
    unsigned short signalIndex[] = {0,1};
//    double values[] = {0.5, // ch 1
//                       0.5, // ch2
//                       0.5, // ch3
//                       0.5, // ch4
//                       0.5, // ch5
//                       0.5, // ch6
//                       0.5, // ch7
//                       0.5, // ch8
//                       0.5}; // ch9

    int returnValue;
    returnValue =  OpalSetSignals( numSignals, logicalId, signalIndex, values);
    if (returnValue != EOK)
    {
        setLastErrorMsg();
203
        displayLastErrorMsg();
204 205
    }
}
206
void OpalLink::getSignals()
Bryan Godbolt's avatar
Bryan Godbolt committed
207
{
Bryan Godbolt's avatar
Bryan Godbolt committed
208
//    getSignalsMutex.lock();
209
//    qDebug() <<  "OpalLink::getSignals(): Attempting to acquire signals";
Bryan Godbolt's avatar
Bryan Godbolt committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228


    unsigned long  timeout = 0;
    unsigned short acqGroup = 0; //this is actually group 1 in the model
    unsigned short allocatedSignals = NUM_OUTPUT_SIGNALS;
    unsigned short *numSignals = new unsigned short(0);
    double *timestep = new double(0);
    double values[NUM_OUTPUT_SIGNALS] = {};
    unsigned short *lastValues = new unsigned short(false);
    unsigned short *decimation = new unsigned short(0);

    while (!(*lastValues))
    {
        int returnVal = OpalGetSignals(timeout, acqGroup, allocatedSignals, numSignals, timestep,
                                       values, lastValues, decimation);

        if (returnVal == EOK )
        {
            //        qDebug() << "OpalLink::getSignals: Timestep=" << *timestep;// << ", Last? " << (bool)(*lastValues);
229
            /* Send position info to qgroundcontrol */
Bryan Godbolt's avatar
Bryan Godbolt committed
230 231 232 233 234 235 236 237 238 239
            mavlink_message_t local_position;
            mavlink_msg_local_position_pack(systemID, componentID, &local_position,
                                            (*timestep)*1000000,
                                            values[OpalRT::X_POS],
                                            values[OpalRT::Y_POS],
                                            values[OpalRT::Z_POS],
                                            values[OpalRT::X_VEL],
                                            values[OpalRT::Y_VEL],
                                            values[OpalRT::Z_VEL]);
            receiveMessage(local_position);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

            /* send attitude info to qgroundcontrol */
            mavlink_message_t attitude;
            mavlink_msg_attitude_pack(systemID, componentID, &attitude,
                                      (*timestep)*1000000,
                                      values[OpalRT::ROLL],
                                      values[OpalRT::PITCH],
                                      values[OpalRT::YAW],
                                      0, // rollspeed
                                      0, // pitchspeed
                                      0 // yawspeed
                                      );
            receiveMessage(attitude);

            /* send bias info to qgroundcontrol */
            mavlink_message_t bias;
            mavlink_msg_nav_filter_bias_pack(systemID, componentID, &bias,
                                             (*timestep)*1000000,
                                             values[OpalRT::B_F_0],
                                             values[OpalRT::B_F_1],
                                             values[OpalRT::B_F_2],
                                             values[OpalRT::B_W_0],
                                             values[OpalRT::B_W_1],
                                             values[OpalRT::B_W_2]
                                             );
            receiveMessage(bias);
Bryan Godbolt's avatar
Bryan Godbolt committed
266
        }
267 268
//        else if (returnVal == EAGAIN)
//        {
Bryan Godbolt's avatar
Bryan Godbolt committed
269
//            qDebug() << "OpalLink::getSignals: Data was not ready";
270
//        }
Bryan Godbolt's avatar
Bryan Godbolt committed
271 272 273 274
        // if returnVal == EAGAIN => data just wasn't ready
        else if (returnVal != EAGAIN)
        {
            getSignalsTimer->stop();
275
            displayLastErrorMsg();
Bryan Godbolt's avatar
Bryan Godbolt committed
276 277 278 279 280 281 282 283 284 285
        }
    }

    /* deallocate used memory */

    delete numSignals;
    delete timestep;
    delete lastValues;
    delete decimation;
//    getSignalsMutex.unlock();
286

Bryan Godbolt's avatar
Bryan Godbolt committed
287 288
}

289

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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
void OpalLink::getParameterList()
{
    /* inputs */
    unsigned short allocatedParams=0;
    unsigned short allocatedPathLen=0;
    unsigned short allocatedNameLen=0;
    unsigned short allocatedVarLen=0;

    /* outputs */
    unsigned short numParams;
    unsigned short *idParam=NULL;
    unsigned short maxPathLen;
    char **path=NULL;
    unsigned short maxNameLen;
    char **name=NULL;
    unsigned short maxVarLen;
    char **var=NULL;

    int returnValue;

    returnValue = OpalGetParameterList(allocatedParams, &numParams, idParam,
                             allocatedPathLen, &maxPathLen, path,
                             allocatedNameLen, &maxNameLen, name,
                             allocatedVarLen, &maxVarLen, var);
    if (returnValue!=E2BIG)
    {
        setLastErrorMsg();
        displayLastErrorMsg();
        return;
    }

    // allocate memory for parameter list

    idParam = new unsigned short[numParams];
    allocatedParams = numParams;

    path = new char*[numParams];
    for (int i=0; i<numParams; i++)
        path[i]=new char[maxPathLen];
    allocatedPathLen = maxPathLen;

    name = new char*[numParams];
    for (int i=0; i<numParams; i++)
        name[i] = new char[maxNameLen];
    allocatedNameLen = maxNameLen;

    var = new char*[numParams];
    for (int i=0; i<numParams; i++)
        var[i] = new char[maxVarLen];
    allocatedVarLen = maxVarLen;

    returnValue = OpalGetParameterList(allocatedParams, &numParams, idParam,
                             allocatedPathLen, &maxPathLen, path,
                             allocatedNameLen, &maxNameLen, name,
                             allocatedVarLen, &maxVarLen, var);

    if (returnValue != EOK)
    {
        setLastErrorMsg();
        displayLastErrorMsg();
        return;
    }

    qDebug() << "Num params: " << numParams << endl;
    qDebug() << "Name\tPath\tVar" << endl;
    for (int i=0; i<numParams; i++)
        qDebug() << qSetFieldWidth(20) << name[i] << qSetFieldWidth(5) << idParam[i]
                << qSetFieldWidth(50) << path[i];

}

361 362 363 364 365 366
/*
 *
  Administrative
 *
 */
void OpalLink::run()
Bryan Godbolt's avatar
Bryan Godbolt committed
367
{
Bryan Godbolt's avatar
Bryan Godbolt committed
368
//    qDebug() << "OpalLink::run():: Starting the thread";
369 370
}

371 372 373
int OpalLink::getId()
{
    return id;
Bryan Godbolt's avatar
Bryan Godbolt committed
374 375
}

376
QString OpalLink::getName()
Bryan Godbolt's avatar
Bryan Godbolt committed
377
{
378
    return name;
Bryan Godbolt's avatar
Bryan Godbolt committed
379 380
}

381
void OpalLink::setName(QString name)
Bryan Godbolt's avatar
Bryan Godbolt committed
382
{
383 384
    this->name = name;
    emit nameChanged(this->name);
Bryan Godbolt's avatar
Bryan Godbolt committed
385 386
}

387 388 389
bool OpalLink::isConnected() {
    //qDebug() << "OpalLink::isConnected:: connectState: " << connectState;
    return connectState;
Bryan Godbolt's avatar
Bryan Godbolt committed
390 391 392 393
}



394 395 396

bool OpalLink::connect()
{
Bryan Godbolt's avatar
Bryan Godbolt committed
397
    short modelState;
398

399 400
    /// \todo allow configuration of instid in window    
    if ((OpalConnect(101, false, &modelState) == EOK) && (OpalGetSignalControl(0, true) == EOK))
Bryan Godbolt's avatar
Bryan Godbolt committed
401
    {
402
        connectState = true;
403 404
        /// \todo try/catch a delete in case params has already been allocated
        params = new OpalRT::ParameterList();
405 406 407
        emit connected();
        heartbeatTimer->start(1000/heartbeatRate);
        getSignalsTimer->start(getSignalsPeriod);
Bryan Godbolt's avatar
Bryan Godbolt committed
408 409 410 411
    }
    else
    {
        connectState = false;
412
        displayLastErrorMsg();
Bryan Godbolt's avatar
Bryan Godbolt committed
413
    }
414 415 416

    emit connected(connectState);
    return connectState;
417 418 419 420 421 422 423
}

bool OpalLink::disconnect()
{
    return false;
}

424
void OpalLink::displayLastErrorMsg()
425
{
426
    static QString lastErrorMsg;
427 428 429 430 431 432 433
    setLastErrorMsg();
    QMessageBox msgBox;
    msgBox.setIcon(QMessageBox::Critical);
    msgBox.setText(lastErrorMsg);
    msgBox.exec();
}

434 435
void OpalLink::setLastErrorMsg()
{
Bryan Godbolt's avatar
Bryan Godbolt committed
436 437
    char buf[512];
    unsigned short len;
438
    static QString lastErrorMsg;
Bryan Godbolt's avatar
Bryan Godbolt committed
439 440 441
    OpalGetLastErrMsg(buf, sizeof(buf), &len);
    lastErrorMsg.clear();
    lastErrorMsg.append(buf);
442 443
}

444 445 446 447 448 449 450 451

/*
 *
  Statisctics
 *
 */

qint64 OpalLink::getNominalDataRate()
452
{
453
    return 0; //unknown
454
}
Bryan Godbolt's avatar
Bryan Godbolt committed
455

456
int OpalLink::getLinkQuality()
Bryan Godbolt's avatar
Bryan Godbolt committed
457
{
458
    return -1; //not supported
Bryan Godbolt's avatar
Bryan Godbolt committed
459 460
}

461
qint64 OpalLink::getTotalUpstream()
Bryan Godbolt's avatar
Bryan Godbolt committed
462
{
463 464 465 466 467
    statisticsMutex.lock();
    qint64 totalUpstream =  bitsSentTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
    return totalUpstream;
}
Bryan Godbolt's avatar
Bryan Godbolt committed
468

469 470 471 472 473
qint64 OpalLink::getTotalDownstream() {
    statisticsMutex.lock();
    qint64 totalDownstream = bitsReceivedTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
    return totalDownstream;
474 475
}

476
qint64 OpalLink::getCurrentUpstream()
477
{
478 479
    return 0; //unknown
}
480

481 482 483 484
qint64 OpalLink::getMaxUpstream()
{
    return 0; //unknown
}
485

486 487
qint64 OpalLink::getBitsSent() {
    return bitsSentTotal;
488 489
}

490 491 492
qint64 OpalLink::getBitsReceived() {
    return bitsReceivedTotal;
}
493 494


495 496 497
bool OpalLink::isFullDuplex()
{
    return false;
Bryan Godbolt's avatar
Bryan Godbolt committed
498
}