MissionManager.h 3.94 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

#ifndef MissionManager_H
#define MissionManager_H

#include <QObject>
#include <QLoggingCategory>
#include <QThread>
#include <QMutex>
#include <QTimer>

#include "QmlObjectListModel.h"
#include "QGCMAVLink.h"
#include "QGCLoggingCategory.h"

class Vehicle;

Q_DECLARE_LOGGING_CATEGORY(MissionManagerLog)

41
class MissionManager : public QObject
Don Gagne's avatar
Don Gagne committed
42 43 44 45 46 47 48 49
{
    Q_OBJECT
    
public:
    /// @param uas Uas which this set of facts is associated with
    MissionManager(Vehicle* vehicle);
    ~MissionManager();
    
Don Gagne's avatar
Don Gagne committed
50
    Q_PROPERTY(bool                 inProgress      READ inProgress     NOTIFY inProgressChanged)
Don Gagne's avatar
Don Gagne committed
51 52
    Q_PROPERTY(QmlObjectListModel*  missionItems    READ missionItems   CONSTANT)
    Q_PROPERTY(bool                 canEdit         READ canEdit        NOTIFY  canEditChanged)
Don Gagne's avatar
Don Gagne committed
53 54 55 56 57
    
    // Property accessors
    
    bool inProgress(void) { return _retryAck != AckNone; }
    QmlObjectListModel* missionItems(void) { return &_missionItems; }
Don Gagne's avatar
Don Gagne committed
58 59 60
    bool canEdit(void) { return _canEdit; }
    
    // C++ methods
Don Gagne's avatar
Don Gagne committed
61 62 63 64 65 66 67 68 69
    
    void requestMissionItems(void);
    
    /// Writes the specified set of mission items to the vehicle
    void writeMissionItems(const QmlObjectListModel& missionItems);
    
    /// Returns a copy of the current set of mission items. Caller is responsible for
    /// freeing returned object.
    QmlObjectListModel* copyMissionItems(void);
Don Gagne's avatar
Don Gagne committed
70

Don Gagne's avatar
Don Gagne committed
71
signals:
Don Gagne's avatar
Don Gagne committed
72 73
    // Public signals
    void canEditChanged(bool canEdit);
Don Gagne's avatar
Don Gagne committed
74
    void newMissionItemsAvailable(void);
Don Gagne's avatar
Don Gagne committed
75
    void inProgressChanged(bool inProgress);
Don Gagne's avatar
Don Gagne committed
76
    
Don Gagne's avatar
Don Gagne committed
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
private slots:
    void _mavlinkMessageReceived(const mavlink_message_t& message);
    void _ackTimeout(void);
    
private:
    typedef enum {
        AckNone,            ///< State machine is idle
        AckMissionCount,    ///< MISSION_COUNT message expected
        AckMissionItem,     ///< MISSION_ITEM expected
        AckMissionRequest,  ///< MISSION_REQUEST is expected, or MISSION_ACK to end sequence
    } AckType_t;
    
    void _startAckTimeout(AckType_t ack, const mavlink_message_t& message);
    bool _stopAckTimeout(AckType_t expectedAck);
    void _sendTransactionComplete(void);
    void _handleMissionCount(const mavlink_message_t& message);
    void _handleMissionItem(const mavlink_message_t& message);
    void _handleMissionRequest(const mavlink_message_t& message);
    void _handleMissionAck(const mavlink_message_t& message);
    void _requestNextMissionItem(int sequenceNumber);
    void _clearMissionItems(void);

private:
    Vehicle*            _vehicle;
    
    int                 _cMissionItems;     ///< Mission items on vehicle
Don Gagne's avatar
Don Gagne committed
103
    bool                _canEdit;           ///< true: Mission items are editable in the ui
Don Gagne's avatar
Don Gagne committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

    QTimer*             _ackTimeoutTimer;
    AckType_t           _retryAck;
    mavlink_message_t   _retryMessage;
    int                 _retryCount;
    
    int                 _expectedSequenceNumber;
    
    QMutex _dataMutex;
    
    QmlObjectListModel  _missionItems;
    
    static const int _ackTimeoutMilliseconds= 1000;
    static const int _maxRetryCount = 5;
};

#endif