SysStatusSensorInfo.cc 4.38 KB
Newer Older
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/****************************************************************************
 *
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "SysStatusSensorInfo.h"

#include <QDebug>

SysStatusSensorInfo::SysStatusSensorInfo(QObject* parent)
    : QObject(parent)
{

}

void SysStatusSensorInfo::update(const mavlink_sys_status_t& sysStatus)
{
    bool dirty = false;

    // Walk the bits
    for (int bitPosition=0; bitPosition<32; bitPosition++) {
        MAV_SYS_STATUS_SENSOR sensorBitMask = static_cast<MAV_SYS_STATUS_SENSOR>(1 << bitPosition);
        if (sysStatus.onboard_control_sensors_present & sensorBitMask) {
            if (_sensorInfoMap.contains(sensorBitMask)) {
                SensorInfo_t& sensorInfo = _sensorInfoMap[sensorBitMask];

                bool newEnabled = sysStatus.onboard_control_sensors_enabled & sensorBitMask;
                if (sensorInfo.enabled != newEnabled) {
                    dirty = true;
                    sensorInfo.enabled = newEnabled;
                }

                bool newHealthy = sysStatus.onboard_control_sensors_health & sensorBitMask;
                if (sensorInfo.healthy != newHealthy) {
                    dirty = true;
                    sensorInfo.healthy = newHealthy;
                }
            } else {
                dirty = true;
                SensorInfo_t sensorInfo = { !!(sysStatus.onboard_control_sensors_enabled & sensorBitMask), !!(sysStatus.onboard_control_sensors_health & sensorBitMask) };
                _sensorInfoMap[sensorBitMask] = sensorInfo;
            }
        } else {
            if (_sensorInfoMap.contains(sensorBitMask)) {
                dirty = true;
                _sensorInfoMap.remove(sensorBitMask);
            }
        }
    }

    if (dirty) {
        emit sensorInfoChanged();
    }
}

QStringList SysStatusSensorInfo::sensorNames (void) const
{
    QStringList rgNames;

    // List ordering is unhealthy, healthy, disabled
    for (int i=0; i<_sensorInfoMap.keys().count(); i++) {
        const MAV_SYS_STATUS_SENSOR sensorBitMask   = _sensorInfoMap.keys()[i];
        const SensorInfo_t&         sensorInfo      = _sensorInfoMap[sensorBitMask];

        if (sensorInfo.enabled && !sensorInfo.healthy) {
            rgNames.append(QGCMAVLink::mavSysStatusSensorToString(sensorBitMask));
        }
    }
    for (int i=0; i<_sensorInfoMap.keys().count(); i++) {
        const MAV_SYS_STATUS_SENSOR sensorBitMask   = _sensorInfoMap.keys()[i];
        const SensorInfo_t&         sensorInfo      = _sensorInfoMap[sensorBitMask];

        if (sensorInfo.enabled && sensorInfo.healthy) {
            rgNames.append(QGCMAVLink::mavSysStatusSensorToString(sensorBitMask));
        }
    }
    for (int i=0; i<_sensorInfoMap.keys().count(); i++) {
        const MAV_SYS_STATUS_SENSOR sensorBitMask   = _sensorInfoMap.keys()[i];
        const SensorInfo_t&         sensorInfo      = _sensorInfoMap[sensorBitMask];

        if (!sensorInfo.enabled) {
            rgNames.append(QGCMAVLink::mavSysStatusSensorToString(sensorBitMask));
        }
    }

    return rgNames;
}

QStringList SysStatusSensorInfo::sensorStatus(void) const
{
    QStringList rgStatus;

    // List ordering is unhealthy, healthy, disabled
    for (int i=0; i<_sensorInfoMap.keys().count(); i++) {
        const MAV_SYS_STATUS_SENSOR sensorBitMask   = _sensorInfoMap.keys()[i];
        const SensorInfo_t&         sensorInfo      = _sensorInfoMap[sensorBitMask];

        if (sensorInfo.enabled && !sensorInfo.healthy) {
            rgStatus.append(tr("Error"));
        }
    }
    for (int i=0; i<_sensorInfoMap.keys().count(); i++) {
        const MAV_SYS_STATUS_SENSOR sensorBitMask   = _sensorInfoMap.keys()[i];
        const SensorInfo_t&         sensorInfo      = _sensorInfoMap[sensorBitMask];

        if (sensorInfo.enabled && sensorInfo.healthy) {
            rgStatus.append(tr("Normal"));
        }
    }
    for (int i=0; i<_sensorInfoMap.keys().count(); i++) {
        const MAV_SYS_STATUS_SENSOR sensorBitMask   = _sensorInfoMap.keys()[i];
        const SensorInfo_t&         sensorInfo      = _sensorInfoMap[sensorBitMask];

        if (!sensorInfo.enabled) {
            rgStatus.append(tr("Disabled"));
        }
    }

    return rgStatus;
}