File indexing completed on 2024-12-29 05:20:09

0001 /*
0002  * Copyright 2019 Patrick José Pereira <patrickjp@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #pragma once
0022 
0023 #include "abstractvehicle.h"
0024 
0025 #include "mavlinkpluginconfig.h"
0026 #include "mavlinkudpconnection.h"
0027 
0028 #include <QTimer>
0029 #include <QVector>
0030 
0031 /**
0032  * A class for managing mavlink vehicle.
0033  */
0034 class MAVLinkVehicle : public Kirogi::AbstractVehicle
0035 {
0036     Q_OBJECT
0037 
0038 public:
0039     struct Configuration {
0040         uint8_t sysid; ///< System id that is used to identify vehicle on network.
0041         uint8_t compid; ///< Default component id of vehicle.
0042         MAV_TYPE type; ///< Type of vehicle.
0043         QString name;
0044     };
0045 
0046     MAVLinkVehicle(Configuration configuration, MAVLinkConnection *connection, QObject *parent = nullptr);
0047     ~MAVLinkVehicle() override;
0048 
0049     void processMessage(MAVLinkConnection *connection, const mavlink_message_t &message);
0050 
0051     QString name() const override;
0052     QString iconName() const override;
0053 
0054     Kirogi::AbstractVehicle::VehicleType vehicleType() const override;
0055 
0056     QList<Kirogi::AbstractVehicle::VehicleAction> supportedActions() const override;
0057 
0058     Q_INVOKABLE void requestAction(Kirogi::AbstractVehicle::VehicleAction action) override;
0059     Q_INVOKABLE void pilot(qint8 roll, qint8 pitch, qint8 yaw, qint8 gaz) override;
0060 
0061     float roll() const override;
0062     float pitch() const override;
0063     float yaw() const override;
0064 
0065     int signalStrength() const override;
0066     int batteryLevel() const override;
0067 
0068     bool gpsSupported() const override;
0069     bool gpsFix() const override;
0070     QGeoCoordinate gpsPosition() const override;
0071     float altitude() const override;
0072 
0073     QString videoSource() const override;
0074 
0075 private Q_SLOTS:
0076     void fetchParameters();
0077 
0078 private:
0079     struct AltitudeSource {
0080         bool altitudeMessage{false};
0081         float altitude{0.0f};
0082     };
0083 
0084     struct CommandQueueItem {
0085         bool command_int;
0086         float params[7];
0087         MAV_CMD command;
0088         uint8_t target_system;
0089         uint8_t target_component;
0090         uint8_t confirmation;
0091         MAV_FRAME frame; // Coordinate system of the command. (COMMAND_INT)
0092         uint8_t current; // False: 0, True: 1.
0093         uint8_t autocontinue; // Continue to next wp automatically.
0094     };
0095 
0096     void sendCommandInQueue();
0097 
0098     void handleHeartbeat(const mavlink_message_t &message);
0099     void handleCommandAck(const mavlink_message_t &message);
0100     void handleRcChannelsScaled(const mavlink_message_t &message);
0101     void handleRcChannelsRaw(const mavlink_message_t &message);
0102     void handleRcChannels(const mavlink_message_t &message);
0103     void handleAltitude(const mavlink_message_t &message);
0104     void handleAttitude(const mavlink_message_t &message);
0105     void handleBatteryStatus(const mavlink_message_t &message);
0106     void handleGlobalPositionInt(const mavlink_message_t &message);
0107     void handleParamValue(const mavlink_message_t &message);
0108 
0109     MAVLinkConnection *m_connection;
0110     Configuration m_configuration;
0111 
0112     QVector<CommandQueueItem> m_commandQueue;
0113     QTimer m_commandResendTimer;
0114     uint8_t m_commandResendCount;
0115 
0116     AltitudeSource m_altitudeSource;
0117     QString m_name;
0118     float m_roll;
0119     float m_pitch;
0120     float m_yaw;
0121     int m_signalStrength;
0122     int m_batteryLevel;
0123     bool m_gpsFix;
0124     QGeoCoordinate m_gpsPosition;
0125 };