【无人机学习之QGroundControl】android端App初解2-APMPowerComponent(含QML的介绍)

本文详细解读QGroundControl android版的电源组件(APMPowerComponent),包括电源界面、QML元素介绍、相关文件解析,以及QML中的下拉框spinner和参数编辑器的使用。同时,探讨了QGroundControl中QML与C++的交互,并提供了源码链接供深入学习。
摘要由CSDN通过智能技术生成

█ 【无人机学习之QGroundControl】android端App初解2-APMPowerComponent(含QML的介绍)


█ 系列文章目录

提示:这里是收集了无人机的相关文章


█ 文章目录


█ 读前说明

  • 本文通过学习别人写demo,学习一些课件,参考一些博客,学习相关知识,如有涉及侵权请告知
  • 本文可能只简单罗列了一些相关的代码实现过程,复制了一些大神的高论,如内容有误请自行辨别
  • 涉及到的逻辑以及说明可能只做了简单的介绍,主要当做笔记,了解过程而已,如有不同看法,欢迎下方评论
  • 本文源码:https://github.com/mavlink/qgroundcontrol
  • 本文UI:https://github.com/mavlink/qgroundcontrol/blob/master/src/ui
  • QGC中UI设计的主要模式是用QML编写的UI页面,多次与用C ++编写的定制“控制器”进行通信。类似MVC的设计模式。

提示:QGroundControl是使用QT & c++ 编写的


█ QML的介绍

  • 可视元素具有几何坐标,会在屏幕上占据一块显示区域。
基本元素说明
Item基本的项元素,所有可视化项都继承Item
Rectangle基本的可视化矩形元素
Gradient定义一个两种颜色的渐变过程
GradientStop定义个颜色,被Gradient使用
Image在场景中使用位图
BorderImage(特殊的项) 定义一张图片并当做边界
AnimatedImage为播放动画存储一系列的帧
Text在场景中使用文本
TextInput显示可编辑为文本
IntValidatorint验证器
DoubleValidatordouble 验证器
RegExpValidator验证字符串正则表达式
TextEdit显示多行可编辑文本
视图说明
ListView提供一个链表显示模型试图
GridView提供一个网格显示模型试图
PathView提供一个内容沿着路径来显示的模型
Path定义一个PathView使用的轨迹
PathLine定义一个线性的轨迹
PathQuad定义一个二次贝塞尔曲线的轨迹
PathCubic定义一个三次贝塞尔曲线的轨迹
PathAttribute允许绑定一个属性上,具体看例子
PathPercent修改item分配的轨迹 不是很明了其中的意思
WebView允许添加网页内容到一个canvas上
定位器说明
Column整理它的子列(纵)
Row整理它的子行(横)
Grid设置它的子到一个网格上
Flow目的是不让他的子项重叠在一起
锚点(anchor)说明:元素布局
topanchors.top、parent.top
bottom
left左边
right右边
horizontalCenter水平居中
verticalCenter垂直居中
baseline基准线
  • 不可视元素,通常提供一种作用于可视元素的功能。
基本的交互项说明
MouseArea鼠标句柄交互,如点击事件
FocusScope键盘焦点句柄
Flickable提供一种浏览整张图片的一部分的效果
Flipable提供一个平面,可以进行翻转看前面或后面
数据项说明
Binding在创建的时候绑定一些数据到一些属性
ListModel定义链表数据
ListElement定义ListModel的一个数据项
VisualItemModel包含可视化项(visual items)到一个view中,相当是一个容器
VisualDataModel包含一个model和一个delegate,model包含需要的数据,delegate设计显示的项的信息
Package把VisualDataModel共享给多个view
XmlListModel特殊的一个模式使用XPath表达式,使用xml来设置元素
XmlRoleXmlListModel的一个特殊的角色
实用项说明
Connections明确连接信号和信号句柄
Component封装QML items 想一个组件一样
Timer提供时间触发器
QtObject基本的元素只包含objectName属性
Qtqml全局Qt object提供使用的枚举和函数
WorkerScript允许在QML使用线程
Loader控制载入item或组件
Repeater使用一个模型创建多个组件
SystemPalette为Qt palettes提供一个通道
FontLoader载入字体根据名字或URL
LayoutItem允许声明UI元素插入到qtGraphicsView 布局中

█ APMPowerComponent

1.电源界面

在这里插入图片描述

2.相关文件

在这里插入图片描述

3.APMPowerComponent.qml

此文件在qgroundcontrol-master\src\AutoPilotPlugins\APM\APMPowerComponent.qml(注意是APM不是PX4,看自己的硬件)

            GridLayout {
                columns:        3
                rowSpacing:     _margins
                columnSpacing:  _margins

                QGCLabel { text: qsTr("Battery monitor:") }

                FactComboBox {
                    id:         monitorCombo
                    fact:       battMonitor
                    indexModel: false
                }

                QGCLabel {
                    Layout.row:     1
                    Layout.column:  0
                    text:           qsTr("Battery capacity:")
                }

                FactTextField {
                    id:     capacityField
                    width:  _fieldWidth
                    fact:   battCapacity
                }

                QGCLabel {
                    Layout.row:     2
                    Layout.column:  0
                    text:           qsTr("Minimum arming voltage:")
                }

                FactTextField {
                    id:     armVoltField
                    width:  _fieldWidth
                    fact:   armVoltMin
                }

                QGCLabel {
                    Layout.row:     3
                    Layout.column:  0
                    text:           qsTr("Power sensor:")
                }

                QGCComboBox {
                    id:                     sensorCombo
                    Layout.minimumWidth:    _fieldWidth
                    model:                  sensorModel// 下一步需要查看的信息
                    textRole:               "text"

                    onActivated: {
                        if (index < sensorModel.count - 1) {
                            battVoltPin.value = sensorModel.get(index).voltPin
                            battCurrPin.value = sensorModel.get(index).currPin
                            battVoltMult.value = sensorModel.get(index).voltMult
                            battAmpPerVolt.value = sensorModel.get(index).ampPerVolt
                            battAmpOffset.value = sensorModel.get(index).ampOffset
                        } else {

                        }
                    }
                }

                QGCLabel {
                    Layout.row:     4
                    Layout.column:  0
                    text:           qsTr("Current pin:")
                    visible:        _showAdvanced
                }

                FactComboBox {
                    Layout.minimumWidth:    _fieldWidth
                    fact:                   battCurrPin
                    indexModel:             false
                    visible:                _showAdvanced
                }

                QGCLabel {
                    Layout.row:     5
                    Layout.column:  0
                    text:           qsTr("Voltage pin:")
                    visible:        _showAdvanced
                }

                FactComboBox {
                    Layout.minimumWidth:    _fieldWidth
                    fact:                   battVoltPin
                    indexModel:             false
                    visible:                _showAdvanced
                }

                QGCLabel {
                    Layout.row:     6
                    Layout.column:  0
                    text:           qsTr("Voltage multiplier:")
                    visible:        _showAdvanced
                }

                FactTextField {
                    width:      _fieldWidth
                    fact:       battVoltMult
                    visible:    _showAdvanced
                }

                QGCButton {
                    text:       qsTr("Calculate")
                    visible:    _showAdvanced

                    onClicked: {// 下一步需要查看的信息
                        _calcVoltageDlgVehicleVoltage = vehicleVoltage
                        _calcVoltageDlgBattVoltMultParam = battVoltMult
                        mainWindow.showComponentDialog(calcVoltageMultiplierDlgComponent, qsTr("Calculate Voltage Multiplier"), mainWindow.showDialogDefaultWidth, StandardButton.Close)
                    }

                }

                QGCLabel {
                    Layout.columnSpan:  3
                    Layout.fillWidth:   true
                    font.pointSize:     ScreenTools.smallFontPointSize
                    wrapMode:           Text.WordWrap
                    text:               qsTr("If the battery voltage reported by the vehicle is largely different than the voltage read externally using a voltmeter you can adjust the voltage multiplier value to correct this. Click the Calculate button for help with calculating a new value.")
                    visible:            _showAdvanced
                }

                QGCLabel {
                    text:       qsTr("Amps per volt:")
                    visible:    _showAdvanced
                }

                FactTextField {
                    width:      _fieldWidth
                    fact:       battAmpPerVolt
                    visible:    _showAdvanced
                }

                QGCButton {
                    text:       qsTr("Calculate")
                    visible:    _showAdvanced

                    onClicked: {
                        _calcAmpsPerVoltDlgVehicleCurrent = vehicleCurrent
                        _calcAmpsPerVoltDlgBattAmpPerVoltParam = battAmpPerVolt
                        mainWindow.showComponentDialog(calcAmpsPerVoltDlgComponent, qsTr("Calculate Amps per Volt"), mainWindow.showDialogDefaultWidth, StandardButton.Close)
                    }
                }

                QGCLabel {
                    Layout.columnSpan:  3
                    Layout.fillWidth:   true
                    font.pointSize:     ScreenTools.smallFontPointSize
                    wrapMode:           Text.WordWrap
                    text:               qsTr("If the current draw reported by the vehicle is largely different than the current read externally using a current meter you can adjust the amps per volt value to correct this. Click the Calculate button for help with calculating a new value.")
                    visible:            _showAdvanced
                }

                QGCLabel {
                    text:       qsTr("Amps Offset:")
                    visible:    _showAdvanced
                }

                FactTextField {
                    width:      _fieldWidth
                    fact:       battAmpOffset
                    visible:    _showAdvanced
                }

                QGCLabel {
                    Layout.columnSpan:  3
                    Layout.fillWidth:   true
                    font.pointSize:     ScreenTools.smallFontPointSize
                    wrapMode:           Text.WordWrap
                    text:               qsTr("If the vehicle reports a high current read when there is little or no current going through it, adjust the Amps Offset. It should be equal to the voltage reported by the sensor when the current is zero.")
                    visible:            _showAdvanced
                }

            } // GridLayout

4.下拉框spinner

在这里插入图片描述

             ListModel {
                id: sensorModel// 下拉框spinner 

                ListElement {
                    text:       qsTr("Power Module 90A")
                    voltPin:    2
                    currPin:    3
                    voltMult:   10.1
                    ampPerVolt: 17.0
                    ampOffset:  0
                }

                ListElement {
                    text:       qsTr("Power Module HV")
                    voltPin:    2
                    currPin:    3
                    voltMult:   12.02
                    ampPerVolt: 39.877
                    ampOffset:  0
                }

                ListElement {
                    text:       qsTr("3DR Iris")
                    voltPin:    2
                    currPin:    3
                    voltMult:   12.02
                    ampPerVolt: 17.0
                    ampOffset:  0
                }

                ListElement {
                    text:       qsTr("Blue Robotics Power Sense Module R2")
                    voltPin:    2
                    currPin:    3
                    voltMult:   11.000
                    ampPerVolt: 37.8788
                    ampOffset:  0.330
                }

                ListElement {
                    text:       qsTr("Other")
                }
            }
                 QGCComboBox {
                    id:                     sensorCombo
                    Layout.minimumWidth:    _fieldWidth
                    model:                  sensorModel // 下拉框spinner 
                    textRole:               "text"

                    onActivated: {
                        if (index < sensorModel.count - 1) {
                            battVoltPin.value = sensorModel.get(index).voltPin
                            battCurrPin.value = sensorModel.get(index).currPin
                            battVoltMult.value = sensorModel.get(index).voltMult
                            battAmpPerVolt.value = sensorModel.get(index).ampPerVolt
                            battAmpOffset.value = sensorModel.get(index).ampOffset
                        } else {

                        }
                    }
                }

5.参数编辑器

在这里插入图片描述

				QGCLabel {
                    Layout.row:     6
                    Layout.column:  0
                    text:           qsTr("Voltage multiplier:")
                    visible:        _showAdvanced
                }

                FactTextField {
                    width:      _fieldWidth
                    fact:       battVoltMult
                    visible:    _showAdvanced
                }

                QGCButton {
                    text:       qsTr("Calculate")
                    visible:    _showAdvanced

                    onClicked: {
                        _calcVoltageDlgVehicleVoltage = vehicleVoltage
                        _calcVoltageDlgBattVoltMultParam = battVoltMult
                        // calcVoltageMultiplierDlgComponent是对话框的id
                        mainWindow.showComponentDialog(calcVoltageMultiplierDlgComponent, qsTr("Calculate Voltage Multiplier"), mainWindow.showDialogDefaultWidth, StandardButton.Close)
                    }
                }
 // Must be set prior to use of calcVoltageMultiplierDlgComponent
    property Fact _calcVoltageDlgVehicleVoltage
    property Fact _calcVoltageDlgBattVoltMultParam

    Component {
        id: calcVoltageMultiplierDlgComponent// 对话框的id

        QGCViewDialog {
            id: calcVoltageMultiplierDlg

            QGCFlickable {
                anchors.fill:   parent
                contentHeight:  column.height
                contentWidth:   column.width

                Column {
                    id:         column
                    width:      calcVoltageMultiplierDlg.width
                    spacing:    ScreenTools.defaultFontPixelHeight

                    QGCLabel {
                        width:      parent.width
                        wrapMode:   Text.WordWrap
                        text:       qsTr("Measure battery voltage using an external voltmeter and enter the value below. Click Calculate to set the new adjusted voltage multiplier.")
                    }

                    Grid {
                        columns: 2
                        spacing: ScreenTools.defaultFontPixelHeight / 2
                        verticalItemAlignment: Grid.AlignVCenter

                        QGCLabel {
                            text: qsTr("Measured voltage:")
                        }
                        QGCTextField { id: measuredVoltage }

                        QGCLabel { text: qsTr("Vehicle voltage:") }
                        FactLabel { fact: _calcVoltageDlgVehicleVoltage }

                        QGCLabel { text: qsTr("Voltage multiplier:") }
                        FactLabel { fact: _calcVoltageDlgBattVoltMultParam }
                    }

                    QGCButton {
                        text: qsTr("Calculate And Set")

                        onClicked:  {
                            var measuredVoltageValue = parseFloat(measuredVoltage.text)
                            if (measuredVoltageValue == 0 || isNaN(measuredVoltageValue)) {
                                return
                            }
                            var newVoltageMultiplier = (measuredVoltageValue * _calcVoltageDlgBattVoltMultParam.value) / _calcVoltageDlgVehicleVoltage.value
                            if (newVoltageMultiplier > 0) {
                                _calcVoltageDlgBattVoltMultParam.value = newVoltageMultiplier
                            }
                        }
                    }
                } // Column
            } // QGCFlickable
        } // QGCViewDialog
    } // Component - calcVoltageMultiplierDlgComponent

6.APMPowerComponent.cc

此文件在qgroundcontrol-master\src\AutoPilotPlugins\APM\APMPowerComponent.cc(注意是APM不是PX4,看自己的硬件)

图片和qml设置

APMPowerComponent::APMPowerComponent(Vehicle* vehicle, AutoPilotPlugin* autopilot, QObject* parent)
    : VehicleComponent(vehicle, autopilot, parent),
    _name(tr("Power"))
{
}

QString APMPowerComponent::name(void) const
{
    return _name;
}

QString APMPowerComponent::description(void) const
{
    return tr("The Power Component is used to setup battery parameters.");
}

QString APMPowerComponent::iconResource(void) const
{
    return QStringLiteral("/qmlimages/PowerComponentIcon.png");
}

QUrl APMPowerComponent::setupSource(void) const
{
    return QUrl::fromUserInput(QStringLiteral("qrc:/qml/APMPowerComponent.qml"));
}

QUrl APMPowerComponent::summaryQmlSource(void) const
{
    return QUrl::fromUserInput(QStringLiteral("qrc:/qml/APMPowerComponentSummary.qml"));// 下一步需要查看的信息
}

7.APMPowerComponentSummary.qml

此文件在qgroundcontrol-master\src\AutoPilotPlugins\APM\APMPowerComponentSummary.qml(注意是APM不是PX4,看自己的硬件)

目前还不太清楚做什么

Item {
    anchors.fill:   parent

    FactPanelController { id: controller; }

    property Fact _batt1Monitor:            controller.getParameterFact(-1, "BATT_MONITOR")
    property Fact _batt2Monitor:            controller.getParameterFact(-1, "BATT2_MONITOR", false /* reportMissing */)
    property bool _batt2MonitorAvailable:   controller.parameterExists(-1, "BATT2_MONITOR")
    property bool _batt1MonitorEnabled:     _batt1Monitor.rawValue !== 0
    property bool _batt2MonitorEnabled:     _batt2MonitorAvailable && _batt2Monitor.rawValue !== 0
    property Fact _battCapacity:            controller.getParameterFact(-1, "BATT_CAPACITY", false /* reportMissing */)
    property Fact _batt2Capacity:           controller.getParameterFact(-1, "BATT2_CAPACITY", false /* reportMissing */)
    property bool _battCapacityAvailable:   controller.parameterExists(-1, "BATT_CAPACITY")

    Column {
        anchors.fill:       parent

        VehicleSummaryRow {
            labelText: qsTr("Batt1 monitor")
            valueText: _batt1Monitor.enumStringValue
        }

        VehicleSummaryRow {
            labelText: qsTr("Batt1 capacity")
            valueText:  _batt1MonitorEnabled ? _battCapacity.valueString + " " + _battCapacity.units : ""
            visible:    _batt1MonitorEnabled
        }

        VehicleSummaryRow {
            labelText:  qsTr("Batt2 monitor")
            valueText:  _batt2MonitorAvailable ? _batt2Monitor.enumStringValue : ""
            visible:    _batt2MonitorAvailable
        }

        VehicleSummaryRow {
            labelText:  qsTr("Batt2 capacity")
            valueText:  _batt2MonitorEnabled ? _batt2Capacity.valueString + " " + _batt2Capacity.units : ""
            visible:    _batt2MonitorEnabled
        }
    }
}


█ 相关资料

提示:这里是参考的相关文章

  1. 2018-03-08 QGC 连接功能 底层执行逻辑_/* */-CSDN博客
  2. 2019-01-15 QT Qml 的qmlRegisterUncreatableType()函数_小马哔哔-CSDN博客
  3. 2018-03-04 QGroundControl 开发人员指南_/* */-CSDN博客_qgroundcontrol
  4. 2019-05-11 Qt学习笔记:多语言文件.qm的生成和使用_chase_hung的博客-CSDN博客:生成ts文件,修改ts文件,生成qm文件,加载qm语言包

█ 免责声明

博主分享的所有文章内容,部分参考网上教程,引用大神高论,部分亲身实践,记下笔录,内容可能存在诸多不实之处,还望海涵,本内容仅供学习研究使用,切勿用于商业用途,若您是部分内容的作者,不喜欢此内容被分享出来,可联系博主说明相关情况通知删除,感谢您的理解与支持!

提示:转载请注明出处:
https://blog.csdn.net/ljb568838953/article/details/112903990

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值