MissionManager.h 5.14 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
/*=====================================================================
 
 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"
36
#include "LinkInterface.h"
Don Gagne's avatar
Don Gagne committed
37 38 39 40 41

class Vehicle;

Q_DECLARE_LOGGING_CATEGORY(MissionManagerLog)

42
class MissionManager : public QObject
Don Gagne's avatar
Don Gagne committed
43 44 45 46 47 48 49 50
{
    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
51
    Q_PROPERTY(bool                 inProgress      READ inProgress     NOTIFY inProgressChanged)
Don Gagne's avatar
Don Gagne committed
52
    Q_PROPERTY(QmlObjectListModel*  missionItems    READ missionItems   CONSTANT)
53
    Q_PROPERTY(int                  currentItem     READ currentItem    NOTIFY currentItemChanged)
Don Gagne's avatar
Don Gagne committed
54 55 56
    
    // Property accessors
    
57
    bool inProgress(void);
Don Gagne's avatar
Don Gagne committed
58
    QmlObjectListModel* missionItems(void) { return &_missionItems; }
59
    int currentItem(void) { return _currentMissionItem; }
Don Gagne's avatar
Don Gagne committed
60 61
    
    // C++ methods
Don Gagne's avatar
Don Gagne committed
62 63 64 65
    
    void requestMissionItems(void);
    
    /// Writes the specified set of mission items to the vehicle
Don Gagne's avatar
Don Gagne committed
66
    ///     @oaram missionItems Items to send to vehicle
67
    void writeMissionItems(const QmlObjectListModel& missionItems);
Don Gagne's avatar
Don Gagne committed
68 69 70 71
    
    /// Returns a copy of the current set of mission items. Caller is responsible for
    /// freeing returned object.
    QmlObjectListModel* copyMissionItems(void);
72 73 74 75 76 77 78 79 80 81
    
    /// Error codes returned in error signal
    typedef enum {
        InternalError,
        AckTimeoutError,        ///< Timed out waiting for response from vehicle
        ProtocolOrderError,     ///< Incorrect protocol sequence from vehicle
        RequestRangeError,      ///< Vehicle requested item out of range
        ItemMismatchError,      ///< Vehicle returned item with seq # different than requested
        VehicleError,           ///< Vehicle returned error
        MissingRequestsError,   ///< Vehicle did not request all items during write sequence
82
        MaxRetryExceeded,       ///< Retry failed
83
    } ErrorCode_t;
Don Gagne's avatar
Don Gagne committed
84

Don Gagne's avatar
Don Gagne committed
85 86 87 88
    // These values are public so the unit test can set appropriate signal wait times
    static const int _ackTimeoutMilliseconds= 2000;
    static const int _maxRetryCount = 5;
    
Don Gagne's avatar
Don Gagne committed
89 90
signals:
    void newMissionItemsAvailable(void);
Don Gagne's avatar
Don Gagne committed
91
    void inProgressChanged(bool inProgress);
92
    void error(int errorCode, const QString& errorMsg);
93
    void currentItemChanged(int currentItem);
Don Gagne's avatar
Don Gagne committed
94
    
Don Gagne's avatar
Don Gagne committed
95 96 97 98 99 100 101 102 103 104 105 106
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;
    
107
    void _startAckTimeout(AckType_t ack);
Don Gagne's avatar
Don Gagne committed
108
    bool _stopAckTimeout(AckType_t expectedAck);
109
    void _readTransactionComplete(void);
Don Gagne's avatar
Don Gagne committed
110 111 112 113
    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);
114
    void _handleMissionCurrent(const mavlink_message_t& message);
115
    void _requestNextMissionItem(void);
Don Gagne's avatar
Don Gagne committed
116
    void _clearMissionItems(void);
117
    void _sendError(ErrorCode_t errorCode, const QString& errorMsg);
Don Gagne's avatar
Don Gagne committed
118
    QString _ackTypeToString(AckType_t ackType);
Don Gagne's avatar
Don Gagne committed
119
    QString _missionResultToString(MAV_MISSION_RESULT result);
120
    void _finishTransaction(bool success);
Don Gagne's avatar
Don Gagne committed
121 122 123

private:
    Vehicle*            _vehicle;
124
    LinkInterface*      _dedicatedLink;
Don Gagne's avatar
Don Gagne committed
125 126 127
    
    QTimer*             _ackTimeoutTimer;
    AckType_t           _retryAck;
128
    int                 _requestItemRetryCount;
Don Gagne's avatar
Don Gagne committed
129
    
130 131 132 133
    bool        _readTransactionInProgress;
    bool        _writeTransactionInProgress;
    QList<int>  _itemIndicesToWrite;    ///< List of mission items which still need to be written to vehicle
    QList<int>  _itemIndicesToRead;     ///< List of mission items which still need to be requested from vehicle
Don Gagne's avatar
Don Gagne committed
134 135 136 137
    
    QMutex _dataMutex;
    
    QmlObjectListModel  _missionItems;
138
    int                 _currentMissionItem;
Don Gagne's avatar
Don Gagne committed
139 140
};

141
#endif