UASControlParameters.cpp 6.55 KB
Newer Older
Mariano Lizarraga's avatar
Mariano Lizarraga committed
1
#include "UASControlParameters.h"
2 3 4 5
#include "ui_UASControlParameters.h"

#define CONTROL_MODE_LOCKED "MODE LOCKED"
#define CONTROL_MODE_MANUAL "MODE MANUAL"
6 7

#ifdef MAVLINK_ENABLED_SLUGS
8 9 10 11
#define CONTROL_MODE_GUIDED "MODE MID-L CMDS"
#define CONTROL_MODE_AUTO   "MODE WAYPOINT"
#define CONTROL_MODE_TEST1  "MODE PASST"
#define CONTROL_MODE_TEST2  "MODE SEL PT"
12
#else
13 14 15 16
#define CONTROL_MODE_GUIDED "MODE GUIDED"
#define CONTROL_MODE_AUTO   "MODE AUTO"
#define CONTROL_MODE_TEST1  "MODE TEST1"
#define CONTROL_MODE_TEST2  "MODE TEST2"
17 18
#endif

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#define CONTROL_MODE_READY  "MODE TEST3"
#define CONTROL_MODE_RC_TRAINING  "RC SIMULATION"

#define CONTROL_MODE_LOCKED_INDEX 1
#define CONTROL_MODE_MANUAL_INDEX 2
#define CONTROL_MODE_GUIDED_INDEX 3
#define CONTROL_MODE_AUTO_INDEX   4
#define CONTROL_MODE_TEST1_INDEX  5
#define CONTROL_MODE_TEST2_INDEX  6
#define CONTROL_MODE_TEST3_INDEX  7
#define CONTROL_MODE_READY_INDEX  8
#define CONTROL_MODE_RC_TRAINING_INDEX  9

UASControlParameters::UASControlParameters(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UASControlParameters)
{
    ui->setupUi(this);

38 39
    ui->btSetCtrl->setStatusTip(tr("Set Passthrough"));

40 41
    connect(ui->btGetCommands, SIGNAL(clicked()), this, SLOT(getCommands()));

42
    connect(ui->btSetCtrl, SIGNAL(clicked()), this, SLOT(setPassthrough()));
43 44 45 46 47 48 49 50 51 52 53
}

UASControlParameters::~UASControlParameters()
{
    delete ui;
}

void UASControlParameters::changedMode(int mode)
{
    QString modeTemp;

54
    switch (mode) {
55 56 57 58 59 60
    case (uint8_t)MAV_MODE_LOCKED:
        modeTemp = "LOCKED MODE";
        break;
    case (uint8_t)MAV_MODE_MANUAL:
        modeTemp = "MANUAL MODE";
        break;
61
#ifdef MAVLINK_ENABLED_SLUGS
62
    case (uint8_t)MAV_MODE_AUTO:
63
        modeTemp = "WAYPOINT MODE";
64 65
        break;
    case (uint8_t)MAV_MODE_GUIDED:
66
        modeTemp = "MID-L CMDS MODE";
67
        break;
68 69 70 71 72 73

    case (uint8_t)MAV_MODE_TEST1:
        modeTemp = "PASST MODE";
        break;
    case (uint8_t)MAV_MODE_TEST2:
        modeTemp = "SEL PT MODE";
74
        break;
75 76
#else
    case (uint8_t)MAV_MODE_AUTO:
77
        modeTemp = "AUTO MODE";
78 79
        break;
    case (uint8_t)MAV_MODE_GUIDED:
80
        modeTemp = "GUIDED MODE";
81 82
        break;

83
    case (uint8_t)MAV_MODE_TEST1:
84
        modeTemp = "TEST1 MODE";
85 86
        break;
    case (uint8_t)MAV_MODE_TEST2:
87
        modeTemp = "TEST2 MODE";
88 89 90 91 92
        break;
#endif
    case (uint8_t)MAV_MODE_READY:
        modeTemp = "READY MODE";
        break;
93 94 95 96 97 98 99 100 101 102
        break;
    case (uint8_t)MAV_MODE_TEST3:
        modeTemp = "TEST3 MODE";
        break;
    case (uint8_t)MAV_MODE_RC_TRAINING:
        modeTemp = "RC TRAINING MODE";
        break;
    default:
        modeTemp = "UNINIT MODE";
        break;
103 104
    }

105

106
    if(modeTemp != this->mode) {
107
        ui->lbMode->setStyleSheet("background-color: rgb(165, 42, 42)");
108
    } else {
109
        ui->lbMode->setStyleSheet("background-color: rgb(85, 107, 47)");
110 111 112 113 114
    }
}

void UASControlParameters::activeUasSet(UASInterface *uas)
{
115
    if(uas) {
116 117 118 119 120
        connect(uas, SIGNAL(globalPositionChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateGlobalPosition(UASInterface*,double,double,double,quint64)));
        connect(uas, SIGNAL(speedChanged(UASInterface*,double,double,double,quint64)), this, SLOT(speedChanged(UASInterface*,double,double,double,quint64)));
        connect(uas, SIGNAL(attitudeChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateAttitude(UASInterface*,double,double,double,quint64)));
        connect(uas, SIGNAL(modeChanged(int,QString,QString)), this, SLOT(updateMode(int,QString,QString)));
        connect(uas, SIGNAL(thrustChanged(UASInterface*,double)), this, SLOT(thrustChanged(UASInterface*,double)) );
121

122 123
        activeUAS= uas;
    }
124 125 126 127
}

void UASControlParameters::updateGlobalPosition(UASInterface * a, double b, double c, double aa, quint64 ab)
{
128 129 130 131
    Q_UNUSED(a);
    Q_UNUSED(b);
    Q_UNUSED(c);
    Q_UNUSED(ab);
132 133 134 135 136
    this->altitude=aa;
}

void UASControlParameters::speedChanged(UASInterface* uas, double vx, double vy, double vz, quint64 time)
{
137 138
    Q_UNUSED(time);
    Q_UNUSED(uas);
139 140 141 142 143 144 145
    this->speed = sqrt(pow(vx, 2.0) + pow(vy, 2.0) + pow(vz, 2.0));
    //ui->sbAirSpeed->setValue(speed);
}

void UASControlParameters::updateAttitude(UASInterface *uas, double roll, double pitch, double yaw, quint64 time)
{
    Q_UNUSED(uas);
146 147
    Q_UNUSED(pitch);
    Q_UNUSED(yaw);
148 149 150 151 152 153
    Q_UNUSED(time);
    //ui->sbTurnRate->setValue(roll);
    this->roll = roll;
}

void UASControlParameters::setCommands()
154
{
155 156
#ifdef MAVLINK_ENABLED_SLUGS
    if(this->activeUAS) {
157
        UAS* myUas= static_cast<UAS*>(this->activeUAS);
158

159
        mavlink_message_t msg;
160

161 162 163
        tempCmds.uCommand = ui->sbAirSpeed->value();
        tempCmds.hCommand = ui->sbHeight->value();
        tempCmds.rCommand = ui->sbTurnRate->value();
164

165 166 167
        mavlink_msg_mid_lvl_cmds_encode(MG::SYSTEM::ID, MG::SYSTEM::COMPID, &msg, &this->tempCmds);
        myUas->sendMessage(msg);
    }
168
#endif
169
}
170 171 172 173 174 175 176 177

void UASControlParameters::getCommands()
{
    ui->sbAirSpeed->setValue(this->speed);
    ui->sbHeight->setValue(this->altitude);
    ui->sbTurnRate->setValue(this->roll);
}

178 179
void UASControlParameters::setPassthrough()
{
180 181
#ifdef MAVLINK_ENABLED_SLUGS
    if(this->activeUAS) {
182
        UAS* myUas= static_cast<UAS*>(this->activeUAS);
183

184
        mavlink_message_t msg;
185

186
        int8_t tmpBit=0;
187

188
        if(ui->cxdle_c->isChecked()) { //left elevator command
189 190
            tmpBit+=8;
        }
191
        if(ui->cxdr_c->isChecked()) { //rudder command
192 193
            tmpBit+=16;
        }
194

195
        if(ui->cxdla_c->isChecked()) { //left aileron command
196 197
            tmpBit+=64;
        }
198
        if(ui->cxdt_c->isChecked()) { //throttle command
199 200
            tmpBit+=128;
        }
201

202 203 204
        generic_16bit r;
        r.b[1] = 0;
        r.b[0] = tmpBit;//255;
205

206 207 208 209 210 211 212
        tempCtrl.target= this->activeUAS->getUASID();
        tempCtrl.bitfieldPt= (uint16_t)r.s;

        mavlink_msg_ctrl_srfc_pt_encode(MG::SYSTEM::ID, MG::SYSTEM::COMPID, &msg, &this->tempCtrl);
        myUas->sendMessage(msg);
        //qDebug()<<tempCtrl.bitfieldPt;
    }
213
#endif
214 215
}

216 217 218 219 220 221
void UASControlParameters::updateMode(int uas,QString mode,QString description)
{
    Q_UNUSED(uas);
    Q_UNUSED(description);
    this->mode = mode;
    ui->lbMode->setText(this->mode);
222

223
    ui->lbMode->setStyleSheet("background-color: rgb(85, 107, 47)");
224 225 226 227 228 229 230
}

void UASControlParameters::thrustChanged(UASInterface *uas, double throttle)
{
    Q_UNUSED(uas);
    this->throttle= throttle;
}