Skip to content
UAS.cc 45.9 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/*=====================================================================

PIXHAWK Micro Air Vehicle Flying Robotics Toolkit

(c) 2009, 2010 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>

This file is part of the PIXHAWK project

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

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

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

/**
 * @file
 *   @brief Represents one unmanned aerial vehicle
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QList>
#include <QMessageBox>
#include <QTimer>
#include <iostream>
#include <QDebug>
#include <cmath>
#include "UAS.h"
#include "LinkInterface.h"
#include "UASManager.h"
#include "MG.h"
pixhawk's avatar
pixhawk committed
#include "GAudioOutput.h"
#include "MAVLinkProtocol.h"
#include <mavlink.h>
pixhawk's avatar
pixhawk committed

UAS::UAS(MAVLinkProtocol* protocol, int id) : UASInterface(),
pixhawk's avatar
pixhawk committed
        startTime(MG::TIME::getGroundTimeNow()),
        commStatus(COMM_DISCONNECTED),
        name(""),
        links(new QList<LinkInterface*>()),
        waypointManager(*this),
pixhawk's avatar
pixhawk committed
        thrustSum(0),
pixhawk's avatar
pixhawk committed
        thrustMax(10),
pixhawk's avatar
pixhawk committed
        startVoltage(0),
        currentVoltage(12.0f),
        lpVoltage(12.0f),
pixhawk's avatar
pixhawk committed
        mode(MAV_MODE_UNINIT),
        status(MAV_STATE_UNINIT),
        onboardTimeOffset(0),
pixhawk's avatar
pixhawk committed
        controlRollManual(true),
        controlPitchManual(true),
        controlYawManual(true),
        controlThrustManual(true),
pixhawk's avatar
pixhawk committed
        manualRollAngle(0),
        manualPitchAngle(0),
        manualYawAngle(0),
lm's avatar
lm committed
        manualThrust(0),
        receiveDropRate(0),
lm's avatar
lm committed
        sendDropRate(0),
        lowBattAlarm(false),
        positionLock(false),
        statusTimeout(new QTimer(this))
pixhawk's avatar
pixhawk committed
{
    color = UASInterface::getNextColor();
pixhawk's avatar
pixhawk committed
    setBattery(LIPOLY, 3);
    statusTimeout->setInterval(500);
    connect(statusTimeout, SIGNAL(timeout()), this, SLOT(updateState()));
pixhawk's avatar
pixhawk committed
}

UAS::~UAS()
{
    delete links;
}

pixhawk's avatar
pixhawk committed
{
    return uasId;
}

void UAS::updateState()
{
    // Position lock is set by the MAVLink message handler
    // if no position lock is available, indicate an error
    if (positionLock)
    {
        positionLock = false;
    }
    else
    {
pixhawk's avatar
pixhawk committed
        if (mode > (uint8_t)MAV_MODE_LOCKED && positionLock)
pixhawk's avatar
pixhawk committed
void UAS::setSelected()
{
    UASManager::instance()->setActiveUAS(this);
}

void UAS::receiveMessage(LinkInterface* link, mavlink_message_t message)
{
    if (!links->contains(link))
    {
        addLink(link);
    }

    //qDebug() << "UAS RECEIVED" << message.sysid << message.compid << message.msgid;
pixhawk's avatar
pixhawk committed

pixhawk's avatar
pixhawk committed
    if (message.sysid == uasId)
    {
        QString uasState;
        QString stateDescription;
        QString patternPath;
        switch (message.msgid)
        {
        case MAVLINK_MSG_ID_HEARTBEAT:
            emit heartbeat(this);
            // Set new type if it has changed
pixhawk's avatar
pixhawk committed
            if (this->type != mavlink_msg_heartbeat_get_type(&message))
pixhawk's avatar
pixhawk committed
            {
pixhawk's avatar
pixhawk committed
                this->type = mavlink_msg_heartbeat_get_type(&message);
pixhawk's avatar
pixhawk committed
                emit systemTypeSet(this, type);
            }
            break;
        case MAVLINK_MSG_ID_BOOT:
            getStatusForCode((int)MAV_STATE_BOOT, uasState, stateDescription);
            emit statusChanged(this, uasState, stateDescription);
            onboardTimeOffset = 0; // Reset offset measurement
            break;
pixhawk's avatar
pixhawk committed
        case MAVLINK_MSG_ID_SYS_STATUS:
            {
pixhawk's avatar
pixhawk committed
                mavlink_sys_status_t state;
                mavlink_msg_sys_status_decode(&message, &state);
pixhawk's avatar
pixhawk committed
                // FIXME
                //qDebug() << "SYSTEM NAV MODE:" << state.nav_mode;
pixhawk's avatar
pixhawk committed

pixhawk's avatar
pixhawk committed
                QString audiostring = "System " + QString::number(this->getUASID());
                QString stateAudio = "";
                QString modeAudio = "";
                bool statechanged = false;
                bool modechanged = false;
pixhawk's avatar
pixhawk committed
                if (state.status != this->status)
pixhawk's avatar
pixhawk committed
                {
pixhawk's avatar
pixhawk committed
                    statechanged = true;
lm's avatar
lm committed
                    this->status = (int)state.status;
pixhawk's avatar
pixhawk committed
                    getStatusForCode((int)state.status, uasState, stateDescription);
                    emit statusChanged(this, uasState, stateDescription);
                    emit statusChanged(this->status);
                    stateAudio = " changed status to " + uasState;
lm's avatar
lm committed
                if (this->mode != static_cast<unsigned int>(state.mode))
pixhawk's avatar
pixhawk committed
                {
                    modechanged = true;
lm's avatar
lm committed
                    this->mode = static_cast<unsigned int>(state.mode);
pixhawk's avatar
pixhawk committed
                    QString mode;

lm's avatar
lm committed
                    switch (state.mode)
pixhawk's avatar
pixhawk committed
                    {
lm's avatar
lm committed
                    case (uint8_t)MAV_MODE_LOCKED:
pixhawk's avatar
pixhawk committed
                        mode = "LOCKED MODE";
                        break;
lm's avatar
lm committed
                    case (uint8_t)MAV_MODE_MANUAL:
pixhawk's avatar
pixhawk committed
                        mode = "MANUAL MODE";
                        break;
lm's avatar
lm committed
                    case (uint8_t)MAV_MODE_AUTO:
pixhawk's avatar
pixhawk committed
                        mode = "AUTO MODE";
                        break;
lm's avatar
lm committed
                    case (uint8_t)MAV_MODE_GUIDED:
lm's avatar
lm committed
                    case (uint8_t)MAV_MODE_READY:
lm's avatar
lm committed
                        mode = "READY";
                        break;
lm's avatar
lm committed
                    case (uint8_t)MAV_MODE_TEST1:
pixhawk's avatar
pixhawk committed
                        mode = "TEST1 MODE";
                        break;
lm's avatar
lm committed
                    case (uint8_t)MAV_MODE_TEST2:
pixhawk's avatar
pixhawk committed
                        mode = "TEST2 MODE";
                        break;
lm's avatar
lm committed
                    case (uint8_t)MAV_MODE_TEST3:
pixhawk's avatar
pixhawk committed
                        mode = "TEST3 MODE";
                        break;
Loading
Loading full blame...