Skip to content
QGCXPlaneLink.cc 35.8 KiB
Newer Older
 * @return True if connection has been disconnected, false if connection couldn't be disconnected.
 **/
bool QGCXPlaneLink::disconnectSimulation()
{
        emit simulationDisconnected();
        emit simulationConnected(false);
Bart Slinger's avatar
Bart Slinger committed
void QGCXPlaneLink::selectAirframe(const QString &plane)
Lorenz Meier's avatar
Lorenz Meier committed
        if (plane.contains("MK") && airframeID != AIRFRAME_QUAD_X_MK_10INCH_I2C)
        {
            airframeID = AIRFRAME_QUAD_X_MK_10INCH_I2C;
Lorenz Meier's avatar
Lorenz Meier committed
            emit airframeChanged("QRO_X / MK");
Bart Slinger's avatar
Bart Slinger committed

Lorenz Meier's avatar
Lorenz Meier committed
        else if (plane.contains("ARDRONE") && airframeID != AIRFRAME_QUAD_X_ARDRONE)
        {
            airframeID = AIRFRAME_QUAD_X_ARDRONE;
Lorenz Meier's avatar
Lorenz Meier committed
            emit airframeChanged("QRO_X / ARDRONE");
Bart Slinger's avatar
Bart Slinger committed

Lorenz Meier's avatar
Lorenz Meier committed
            bool changed = (airframeID != AIRFRAME_QUAD_DJI_F450_PWM);
            airframeID = AIRFRAME_QUAD_DJI_F450_PWM;
Bart Slinger's avatar
Bart Slinger committed

Lorenz Meier's avatar
Lorenz Meier committed
            if (changed) emit airframeChanged("QRO_X / DJI-F450 / PWM");
Bart Slinger's avatar
Bart Slinger committed

Lorenz Meier's avatar
Lorenz Meier committed
    else
    {
Lorenz Meier's avatar
Lorenz Meier committed
        bool changed = (airframeID != AIRFRAME_UNKNOWN);
Lorenz Meier's avatar
Lorenz Meier committed
        airframeID = AIRFRAME_UNKNOWN;
Bart Slinger's avatar
Bart Slinger committed

Lorenz Meier's avatar
Lorenz Meier committed
        if (changed) emit airframeChanged("X Plane default");
Lorenz Meier's avatar
Lorenz Meier committed
    }
}

void QGCXPlaneLink::setPositionAttitude(double lat, double lon, double alt, double roll, double pitch, double yaw)
{
Bart Slinger's avatar
Bart Slinger committed
#pragma pack(push, 1)
        char header[5];
        quint32 p;
        double lat_lon_ele[3];
        float psi_the_phi[3];
        float gear_flap_vect[3];
    } pos;
Bart Slinger's avatar
Bart Slinger committed
#pragma pack(pop)
    pos.header[0] = 'V';
    pos.header[1] = 'E';
    pos.header[2] = 'H';
    pos.header[3] = '1';
    pos.header[4] = '0';
    pos.p = 0;
    pos.lat_lon_ele[0] = lat;
    pos.lat_lon_ele[1] = lon;
    pos.lat_lon_ele[2] = alt;
    pos.psi_the_phi[0] = roll;
    pos.psi_the_phi[1] = pitch;
    pos.psi_the_phi[2] = yaw;
    pos.gear_flap_vect[0] = 0.0f;
    pos.gear_flap_vect[1] = 0.0f;
    pos.gear_flap_vect[2] = 0.0f;

Bart Slinger's avatar
Bart Slinger committed
    writeBytesSafe((const char *)&pos, sizeof(pos));

//    pos.header[0] = 'V';
//    pos.header[1] = 'E';
//    pos.header[2] = 'H';
//    pos.header[3] = '1';
//    pos.header[4] = '0';
//    pos.p = 0;
//    pos.lat_lon_ele[0] = -999;
//    pos.lat_lon_ele[1] = -999;
//    pos.lat_lon_ele[2] = -999;
//    pos.psi_the_phi[0] = -999;
//    pos.psi_the_phi[1] = -999;
//    pos.psi_the_phi[2] = -999;
//    pos.gear_flap_vect[0] = -999;
//    pos.gear_flap_vect[1] = -999;
//    pos.gear_flap_vect[2] = -999;

//    writeBytesSafe((const char*)&pos, sizeof(pos));
}

/**
 * Sets a random position with an offset of max 1/1000 degree
 * and max 100 m altitude
 */
void QGCXPlaneLink::setRandomPosition()
{
    // Initialize generator
    srand(0);

Bart Slinger's avatar
Bart Slinger committed
    double offLat = rand() / static_cast<double>(RAND_MAX) / 500.0 + 1.0 / 500.0;
    double offLon = rand() / static_cast<double>(RAND_MAX) / 500.0 + 1.0 / 500.0;
    double offAlt = rand() / static_cast<double>(RAND_MAX) * 200.0 + 100.0;

Don Gagne's avatar
Don Gagne committed
    if (_vehicle->altitudeAMSL()->rawValue().toDouble() + offAlt < 0)
    setPositionAttitude(_vehicle->latitude() + offLat,
                        _vehicle->longitude() + offLon,
Don Gagne's avatar
Don Gagne committed
                        _vehicle->altitudeAMSL()->rawValue().toDouble() + offAlt,
                        _vehicle->roll()->rawValue().toDouble(),
                        _vehicle->pitch()->rawValue().toDouble(),
                        _vehicle->uas()->getYaw());
}

void QGCXPlaneLink::setRandomAttitude()
{
    // Initialize generator
    srand(0);

    double roll = rand() / static_cast<double>(RAND_MAX) * 2.0 - 1.0;
    double pitch = rand() / static_cast<double>(RAND_MAX) * 2.0 - 1.0;
    double yaw = rand() / static_cast<double>(RAND_MAX) * 2.0 - 1.0;

    setPositionAttitude(_vehicle->latitude(),
                        _vehicle->longitude(),
Don Gagne's avatar
Don Gagne committed
                        _vehicle->altitudeAMSL()->rawValue().toDouble(),
/**
 * @brief Connect the connection.
 *
 * @return True if connection has been established, false if connection couldn't be established.
 **/
bool QGCXPlaneLink::connectSimulation()
{
Bart Slinger's avatar
Bart Slinger committed
    if (connectState)
    {
        qDebug() << "Simulation already active";
        qDebug() << "STARTING X-PLANE LINK, CONNECTING TO" << remoteHost << ":" << remotePort;
        // XXX Hack
        storeSettings();

        start(HighPriority);
    }
}

/**
 * @brief Check if connection is active.
 *
 * @return True if link is connected, false otherwise.
 **/
bool QGCXPlaneLink::isConnected()
{
    return connectState;
}

QString QGCXPlaneLink::getName()
{
    return name;
}

void QGCXPlaneLink::setName(QString name)
{
    this->name = name;
    //    emit nameChanged(this->name);
}

void QGCXPlaneLink::sendDataRef(QString ref, float value)
{
Bart Slinger's avatar
Bart Slinger committed
#pragma pack(push, 1)
    struct payload
    {
        char b[5];
        float value;
        char name[500];
    } dref;
Bart Slinger's avatar
Bart Slinger committed
#pragma pack(pop)

    dref.b[0] = 'D';
    dref.b[1] = 'R';
    dref.b[2] = 'E';
    dref.b[3] = 'F';
    dref.b[4] = '0';

    /* Set value */
    dref.value = value;

    /* Fill name with zeroes */
    memset(dref.name, 0, sizeof(dref.name));

    /* Set dref name */

    /* Send command */
    QByteArray ba = ref.toUtf8();
Bart Slinger's avatar
Bart Slinger committed

    if (ba.length() > 500)
    {
Bart Slinger's avatar
Bart Slinger committed
    for (int i = 0; i < ba.length(); i++)
    {
Bart Slinger's avatar
Bart Slinger committed

    writeBytesSafe((const char *)&dref, sizeof(dref));