File indexing completed on 2024-06-16 05:22:59

0001 /*
0002  * Copyright 2019 Eike Hein <hein@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 <memory>
0024 
0025 #include <QObject>
0026 
0027 #include "abstractvehicle.h"
0028 
0029 #include <QHostAddress>
0030 #include <QPointer>
0031 #include <QQueue>
0032 #include <QTimer>
0033 #include <QUdpSocket>
0034 
0035 struct RyzeTelloCommand {
0036     QString text;
0037     int retry = 0; // NOTE: Setting to -1 before sending means infinite retries.
0038 };
0039 
0040 class RyzeTelloConnection : public QObject
0041 {
0042     Q_OBJECT
0043 
0044 public:
0045     explicit RyzeTelloConnection(const QString &vehicleName, QObject *parent = nullptr);
0046     ~RyzeTelloConnection();
0047 
0048 public Q_SLOTS:
0049     void handshake();
0050     void reset();
0051     void sendCommand(const QString &command, bool retryForever = false);
0052     void pilot(qint8 roll, qint8 pitch, qint8 yaw, qint8 gaz);
0053 
0054 Q_SIGNALS:
0055     void stateChanged(Kirogi::AbstractVehicle::ConnectionState state);
0056     void responseReceived(const QString &response);
0057     void stateReceived(const QByteArray &state);
0058 
0059 private Q_SLOTS:
0060     void receiveData();
0061     void receiveState();
0062     void pumpCommandQueue();
0063     void sendPilotingCommand();
0064 
0065 private:
0066     void initSockets();
0067 
0068     QString m_vehicleName;
0069 
0070     QHostAddress m_address;
0071     QPointer<QUdpSocket> m_controlSocket;
0072     QPointer<QUdpSocket> m_stateSocket;
0073 
0074     QQueue<RyzeTelloCommand> m_commandQueue;
0075     std::unique_ptr<QTimer> m_commandQueueTimer;
0076 
0077     std::unique_ptr<QTimer> m_pilotingTimer;
0078     qint8 m_roll;
0079     qint8 m_pitch;
0080     qint8 m_yaw;
0081     qint8 m_gaz;
0082 };