MAVLinkSimulationMAV.cc 3.41 KB
Newer Older
1
#include <QDebug>
2
#include <cmath>
3 4 5 6 7 8 9

#include "MAVLinkSimulationMAV.h"

MAVLinkSimulationMAV::MAVLinkSimulationMAV(MAVLinkSimulationLink *parent, int systemid) :
    QObject(parent),
    link(parent),
    planner(parent, systemid),
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    systemid(systemid),
    timer25Hz(0),
    timer10Hz(0),
    timer1Hz(0),
    latitude(0.0),
    longitude(0.0),
    altitude(0.0),
    x(0.0),
    y(0.0),
    z(0.0),
    roll(0.0),
    pitch(0.0),
    yaw(0.0),
    globalNavigation(true),
    firstWP(false),
    previousSPX(0.0),
    previousSPY(0.0),
    previousSPZ(0.0),
    previousSPYaw(0.0),
    nextSPX(0.0),
    nextSPY(0.0),
    nextSPZ(0.0),
    nextSPYaw(0.0)
33
{
34
    // Please note: The waypoint planner is running
35
    connect(&mainloopTimer, SIGNAL(timeout()), this, SLOT(mainloop()));
36
    connect(&planner, SIGNAL(messageSent(mavlink_message_t)), this, SLOT(handleMessage(mavlink_message_t)));
37
    connect(link, SIGNAL(messageReceived(mavlink_message_t)), this, SLOT(handleMessage(mavlink_message_t)));
38
    mainloopTimer.start(20);
39 40 41 42 43
    mainloop();
}

void MAVLinkSimulationMAV::mainloop()
{
44 45 46 47 48 49 50
    // Calculate new simulator values
//    double maxSpeed = 0.0001; // rad/s in earth coordinate frame

//        double xNew = // (nextSPX - previousSPX)

    if (!firstWP)
    {
51 52 53 54 55 56 57 58 59
        double xm = (nextSPX - x) * 0.01;
        double ym = (nextSPY - y) * 0.01;
        double zm = (nextSPZ - z) * 0.1;

        x += xm;
        y += ym;
        z += zm;

        //if (xm < 0.001) xm
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    }
    else
    {
        x = nextSPX;
        y = nextSPY;
        z = nextSPZ;
        firstWP = false;
    }

    // 1 Hz execution
    if (timer1Hz <= 0)
    {
        mavlink_message_t msg;
        mavlink_msg_heartbeat_pack(systemid, MAV_COMP_ID_IMU, &msg, MAV_FIXED_WING, MAV_AUTOPILOT_PIXHAWK);
        link->sendMAVLinkMessage(&msg);
        timer1Hz = 50;
    }

    // 10 Hz execution
    if (timer10Hz <= 0)
    {
        mavlink_message_t msg;
        mavlink_global_position_int_t pos;
        pos.alt = z*1000.0;
        pos.lat = y*1E7;
        pos.lon = x*1E7;
        pos.vx = 0;
        pos.vy = 0;
        pos.vz = 0;
        mavlink_msg_global_position_int_encode(systemid, MAV_COMP_ID_IMU, &msg, &pos);
        link->sendMAVLinkMessage(&msg);
        timer10Hz = 5;
    }

    // 25 Hz execution
    if (timer25Hz <= 0)
    {
        timer25Hz = 2;
    }

    timer1Hz--;
    timer10Hz--;
    timer25Hz--;
103 104 105 106
}

void MAVLinkSimulationMAV::handleMessage(const mavlink_message_t& msg)
{
107
    //qDebug() << "MAV:" << systemid << "RECEIVED MESSAGE FROM" << msg.sysid << "COMP" << msg.compid;
108 109 110 111 112

    switch(msg.msgid)
    {
    case MAVLINK_MSG_ID_ATTITUDE:
        break;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    case MAVLINK_MSG_ID_LOCAL_POSITION_SETPOINT_SET:
        {
            mavlink_local_position_setpoint_set_t sp;
            mavlink_msg_local_position_setpoint_set_decode(&msg, &sp);
            if (sp.target_system == this->systemid)
            {
                previousSPX = nextSPX;
                previousSPY = nextSPY;
                previousSPZ = nextSPZ;
                nextSPX = sp.x;
                nextSPY = sp.y;
                nextSPZ = sp.z;

                // Rotary wing
                //nextSPYaw = sp.yaw;

                // Airplane
                yaw = atan2(previousSPX-nextSPX, previousSPY-nextSPY);

                if (!firstWP) firstWP = true;
            }
            //qDebug() << "UPDATED SP:" << "X" << nextSPX << "Y" << nextSPY << "Z" << nextSPZ;
        }
        break;
137 138
    }
}