Skip to content
Snippets Groups Projects
QGCXPlaneLink.cc 36 KiB
Newer Older
  • Learn to ignore specific revisions
  •         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(),
    
    DonLakeFlyer's avatar
    DonLakeFlyer committed
                            _vehicle->heading()->rawValue().toDouble());
    
    }
    
    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";
    
    Bart Slinger's avatar
    Bart Slinger committed
        } else {
    
            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));