首先QGC是基于QT平台开发,个人理解软件架构即为项目前后端结构,以及前后端数据交互的逻辑。下面是对QGroundControl源码的一些个人理解,写这个博客只是为了记录下来,防止时间久了忘记,过程中看了一些大佬的博客来帮助理解,感谢一下大佬:
火山上的企鹅
阿木实验室
望天边星宿
前言
- 前端qml结构
QGC的前端主要为五个主要页面和一个顶栏:
MainToolBar(顶栏)
AppSetting.qml
SetupView.qml
PlanView.qml
FlightDispalyView.qml
AnalyzeView.qml
- 后端类结构
前端显示与后端逻辑进行数据交互,即实现了QGC地面站功能。
架构理解
架构理解可以从QGC的运行main函数开始逐步理解。
- main.cc
在main.cc中有几个关键程序
创建QGCApplication实例,QGCApplication是QGroundControl中的核心类,提供了应用程序的生命周期管理、连接管理、飞行日志管理、地图和任务管理、设置和配置管理,以及警报和通知管理等功能。它扮演着关键角色。这个类只提供了唯一的实例,另外提供了一个访问它的全局函数QGCApplication* qgcApp(void){}
QGCApplication* app = new QGCApplication(argc, argv, runUnitTests);
在_initCommon()中将C++类注册,在前端qml中可以调用,实现前后端数据交互。在
qgroundcontrolQmlGlobalSingletonFactory中将QGroundControlQmlGlobal类实例化为单例工厂QGroundControl。
app->_initCommon()
{
static const char* kRefOnly = "Reference only";
static const char* kQGroundControl = "QGroundControl";
static const char* kQGCControllers = "QGroundControl.Controllers";
static const char* kQGCVehicle = "QGroundControl.Vehicle";
QSettings settings;
// Register our Qml objects
qmlRegisterType<QGCPalette> ("QGroundControl.Palette", 1, 0, "QGCPalette");
qmlRegisterType<QGCMapPalette> ("QGroundControl.Palette", 1, 0, "QGCMapPalette");
qmlRegisterUncreatableType<Vehicle> (kQGCVehicle, 1, 0, "Vehicle", kRefOnly);
qmlRegisterUncreatableType<MissionManager> (kQGCVehicle, 1, 0, "MissionManager", kRefOnly);
qmlRegisterUncreatableType<ParameterManager> (kQGCVehicle, 1, 0, "ParameterManager", kRefOnly);
qmlRegisterUncreatableType<VehicleObjectAvoidance> (kQGCVehicle, 1, 0, "VehicleObjectAvoidance", kRefOnly);
qmlRegisterUncreatableType<QGCCameraManager> (kQGCVehicle, 1, 0, "QGCCameraManager", kRefOnly);
qmlRegisterUncreatableType<QGCCameraControl> (kQGCVehicle, 1, 0, "QGCCameraControl", kRefOnly);
qmlRegisterUncreatableType<QGCVideoStreamInfo> (kQGCVehicle, 1, 0, "QGCVideoStreamInfo", kRefOnly);
qmlRegisterUncreatableType<LinkInterface> (kQGCVehicle, 1, 0, "LinkInterface", kRefOnly);
qmlRegisterUncreatableType<MissionController> (kQGCControllers, 1, 0, "MissionController", kRefOnly);
qmlRegisterUncreatableType<GeoFenceController> (kQGCControllers, 1, 0, "GeoFenceController", kRefOnly);
qmlRegisterUncreatableType<RallyPointController> (kQGCControllers, 1, 0, "RallyPointController", kRefOnly);
qmlRegisterUncreatableType<MissionItem> (kQGroundControl, 1, 0, "MissionItem", kRefOnly);
qmlRegisterUncreatableType<VisualMissionItem> (kQGroundControl, 1, 0, "VisualMissionItem", kRefOnly);
qmlRegisterUncreatableType<CoordinateVector> (kQGroundControl, 1, 0, "CoordinateVector", kRefOnly);
qmlRegisterUncreatableType<QmlObjectListModel> (kQGroundControl, 1, 0, "QmlObjectListModel", kRefOnly);
qmlRegisterUncreatableType<MissionCommandTree> (kQGroundControl, 1, 0, "MissionCommandTree", kRefOnly);
qmlRegisterUncreatableType<CameraCalc> (kQGroundControl, 1, 0, "CameraCalc", kRefOnly);
qmlRegisterUncreatableType<LogReplayLink> (kQGroundControl, 1, 0, "LogReplayLink", kRefOnly);
qmlRegisterType<LogReplayLinkController> (kQGroundControl, 1, 0, "LogReplayLinkController");
#if defined(QGC_ENABLE_MAVLINK_INSPECTOR)
qmlRegisterUncreatableType<MAVLinkChartController> (kQGroundControl, 1, 0, "MAVLinkChart", kRefOnly);
#endif
#if defined(QGC_ENABLE_PAIRING)
qmlRegisterUncreatableType<PairingManager> (kQGroundControl, 1, 0, "PairingManager", kRefOnly);
#endif
qmlRegisterUncreatableType<AutoPilotPlugin> ("QGroundControl.AutoPilotPlugin", 1, 0, "AutoPilotPlugin", kRefOnly);
qmlRegisterUncreatableType<VehicleComponent> ("QGroundControl.AutoPilotPlugin", 1, 0, "VehicleComponent", kRefOnly);
qmlRegisterUncreatableType<JoystickManager> ("QGroundControl.JoystickManager", 1, 0, "JoystickManager", kRefOnly);
qmlRegisterUncreatableType<Joystick> ("QGroundControl.JoystickManager", 1, 0, "Joystick", kRefOnly);
qmlRegisterUncreatableType<QGCPositionManager> ("QGroundControl.QGCPositionManager", 1, 0, "QGCPositionManager", kRefOnly);
qmlRegisterUncreatableType<FactValueSliderListModel>("QGroundControl.FactControls", 1, 0, "FactValueSliderListModel", kRefOnly);
qmlRegisterUncreatableType<QGCMapPolygon> ("QGroundControl.FlightMap", 1, 0, "QGCMapPolygon", kRefOnly);
qmlRegisterUncreatableType<QGCGeoBoundingCube> ("QGroundControl.FlightMap", 1, 0, "QGCGeoBoundingCube", kRefOnly);
qmlRegisterUncreatableType<TrajectoryPoints> ("QGroundControl.FlightMap", 1, 0, "TrajectoryPoints", kRefOnly);
qmlRegisterType<QGCMapCircle> ("QGroundControl.FlightMap", 1, 0, "QGCMapCircle");
qmlRegisterType<ParameterEditorController> (kQGCControllers, 1, 0, "ParameterEditorController");
qmlRegisterType<ESP8266ComponentController> (kQGCControllers, 1, 0, "ESP8266ComponentController");
qmlRegisterType<ScreenToolsController> (kQGCControllers, 1, 0, "ScreenToolsController");
qmlRegisterType<PlanMasterController> (kQGCControllers, 1, 0, "PlanMasterController");
qmlRegisterType<ValuesWidgetController> (kQGCControllers, 1, 0, "ValuesWidgetController");
qmlRegisterType<QGCFileDialogController> (kQGCControllers, 1, 0, "QGCFileDialogController");
qmlRegisterType<RCChannelMonitorController> (kQGCControllers, 1, 0, "RCChannelMonitorController");
qmlRegisterType<JoystickConfigController> (kQGCControllers, 1, 0, "JoystickConfigController");
qmlRegisterType<LogDownloadController> (kQGCControllers, 1, 0, "LogDownloadController");
qmlRegisterType<SyslinkComponentController> (kQGCControllers, 1, 0, "SyslinkComponentController");
qmlRegisterType<EditPositionDialogController> (kQGCControllers, 1, 0, "EditPositionDialogController");
#ifndef __mobile__
#ifndef NO_SERIAL_LINK
qmlRegisterType<FirmwareUpgradeController> (kQGCControllers, 1, 0, "FirmwareUpgradeController");
#endif
#endif
qmlRegisterType<GeoTagController> (kQGCControllers, 1, 0, "GeoTagController");
qmlRegisterType<MavlinkConsoleController> (kQGCControllers, 1, 0, "MavlinkConsoleController");
#if defined(QGC_ENABLE_MAVLINK_INSPECTOR)
qmlRegisterType<MAVLinkInspectorController> (kQGCControllers, 1, 0, "MAVLinkInspectorController");
#endif
// Register Qml Singletons
qmlRegisterSingletonType<QGroundControlQmlGlobal> ("QGroundControl", 1, 0, "QGroundControl", qgroundcontrolQmlGlobalSingletonFactory);
qmlRegisterSingletonType<ScreenToolsController> ("QGroundControl.ScreenToolsController", 1, 0, "ScreenToolsController", screenToolsControllerSingletonFactory);
qmlRegisterSingletonType<ShapeFileHelper> ("QGroundControl.ShapeFileHelper", 1, 0, "ShapeFileHelper", shapeFileHelperSingletonFactory);
}
在_initForNormalAppBoot()中进行启动初始化,加载qml文件。
app->_initForNormalAppBoot()
QQmlApplicationEngine* QGCCorePlugin::createRootWindow(QObject *parent)
{
QQmlApplicationEngine* pEngine = new QQmlApplicationEngine(parent);
pEngine->addImportPath("qrc:/qml");
// 注册为对象在qml中直接使用,不需要import
pEngine->rootContext()->setContextProperty("joystickManager", qgcApp()->toolbox()->joystickManager());
pEngine->rootContext()->setContextProperty("debugMessageModel", AppMessages::getModel());
pEngine->load(QUrl(QStringLiteral("qrc:/qml/MainRootWindow.qml")));
return pEngine;
}
- 整体架构
想要搞明白整体架构就需要理解QGCApplication类、QGCToolBox类、QGroundControlQmlGlobal类和QGCtool类。
QGCApplication类中新建了QGCToolbox类,在QGCToolbox类初始化时需要传入QGCApplication类,这样通过qgcAPP()->toolBox()就可以获取到功能类的所有属性。
QGCToolBox类是一个大的工具类,QGC的所有功能类都在这个toolBox中进行实例化,使用toolBox提供的接口即可调用类的属性和方法。
在QGroundControlQmlGlobal中通过Q_PROPERTY将功能类的属性和方法共前端可以调用,setToolbox()方法将所有功能类使用指针地址连接到QGCToolBox定义的功能类,所以QGroundControlQmlGlobal是前端获取后端数据的总接口。上边的单例工厂实例化将QGroundControlQmlGlobal实例化为QGroundControl,则前端qml通过import 即可实现调用C++类。
还有一个QGCTool类,所有的功能类都继承了QGCTool。在QGCTool类型定义了QGCApplication* _app;和QGCToolbox* _toolbox;和(QGCApplication* app, QGCToolbox* toolbox)方法,即每个功能类都都有QGCApplication和QGCToolbox,并且都指向同一个QGCApplication和QGCToolbox,即QGCApplication中的_app和_toolBox.这样实现了各个功能类的互通。
QGCToolbox::QGCToolbox(QGCApplication* app)
{
// SettingsManager must be first so settings are available to any subsequent tools
_settingsManager = new SettingsManager (app, this);
//-- Scan and load plugins
_scanAndLoadPlugins(app);
_audioOutput = new AudioOutput (app, this);
_factSystem = new FactSystem (app, this);
_firmwarePluginManager = new FirmwarePluginManager (app, this);
#ifndef __mobile__
_gpsManager = new GPSManager (app, this);
#endif
_imageProvider = new QGCImageProvider (app, this);
_joystickManager = new JoystickManager (app, this);
_linkManager = new LinkManager (app, this);
_mavlinkProtocol = new MAVLinkProtocol (app, this);
_missionCommandTree = new MissionCommandTree (app, this);
_multiVehicleManager = new MultiVehicleManager (app, this);
_mapEngineManager = new QGCMapEngineManager (app, this);
_uasMessageHandler = new UASMessageHandler (app, this);
_qgcPositionManager = new QGCPositionManager (app, this);
_followMe = new FollowMe (app, this);
_videoManager = new VideoManager (app, this);
_mavlinkLogManager = new MAVLinkLogManager (app, this);
_adsbVehicleManager = new ADSBVehicleManager (app, this);
#if defined(QGC_ENABLE_PAIRING)
_pairingManager = new PairingManager (app, this);
#endif
//-- Airmap Manager
//-- This should be "pluggable" so an arbitrary AirSpace manager can be used
//-- For now, we instantiate the one and only AirMap provider
#if defined(QGC_AIRMAP_ENABLED)
_airspaceManager = new AirMapManager (app, this);
#else
_airspaceManager = new AirspaceManager (app, this);
#endif
#if defined(QGC_GST_TAISYNC_ENABLED)
_taisyncManager = new TaisyncManager (app, this);
#endif
#if defined(QGC_GST_MICROHARD_ENABLED)
_microhardManager = new MicrohardManager (app, this);
#endif
}
void QGCToolbox::setChildToolboxes(void)
{
// SettingsManager must be first so settings are available to any subsequent tools
_settingsManager->setToolbox(this);
_corePlugin->setToolbox(this);
_audioOutput->setToolbox(this);
_factSystem->setToolbox(this);
_firmwarePluginManager->setToolbox(this);
#ifndef __mobile__
_gpsManager->setToolbox(this);
#endif
_imageProvider->setToolbox(this);
_joystickManager->setToolbox(this);
_linkManager->setToolbox(this);
_mavlinkProtocol->setToolbox(this);
_missionCommandTree->setToolbox(this);
_multiVehicleManager->setToolbox(this);
_mapEngineManager->setToolbox(this);
_uasMessageHandler->setToolbox(this);
_followMe->setToolbox(this);
_qgcPositionManager->setToolbox(this);
_videoManager->setToolbox(this);
_mavlinkLogManager->setToolbox(this);
_airspaceManager->setToolbox(this);
_adsbVehicleManager->setToolbox(this);
#if defined(QGC_GST_TAISYNC_ENABLED)
_taisyncManager->setToolbox(this);
#endif
#if defined(QGC_GST_MICROHARD_ENABLED)
_microhardManager->setToolbox(this);
#endif
#if defined(QGC_ENABLE_PAIRING)
_pairingManager->setToolbox(this);
#endif
}
void QGCToolbox::_scanAndLoadPlugins(QGCApplication* app)
{
#if defined (QGC_CUSTOM_BUILD)
//-- Create custom plugin (Static)
_corePlugin = (QGCCorePlugin*) new CUSTOMCLASS(app, app->toolbox());
if(_corePlugin) {
return;
}
#endif
//-- No plugins found, use default instance
_corePlugin = new QGCCorePlugin(app, app->toolbox());
}
在前端获取后端类的属性的总借口即为QGroundControl,例如
property var _vehicle: QGroundControl.multiVehicleManager.getVehicleById(1)
在功能类中向获取其他功能类的属性即使用qgcApp()->toolbox(),如果功能类继承了QGCTool则直接可以使用_toolbox->...即可。
_mavlink = qgcApp()->toolbox()->mavlinkProtocol();
_mavlinkProtocol = _toolbox->mavlinkProtocol();
其实只要理解这几个总文件之间的关系后,就理解了QGC的整体结构。_app和_toolbox是QGC整个软件的总接口,_app和_toolbox都在QGCApplication类中,然后所有功能类都在QGCToolBox类中新建,想要获取功能类的属性就需要通过_toolbox。然后QGroundControlQmlGlobal中通过指针地址获取到了所有功能类,然后又通过Q_PROPERTY将类以属性的形式开放给前端,再加上单例模式直接在前端import QGroundControl即可获取到后端属性。这样即完成了前后端数据连通,也解决了各个功能类之间的属性获取。
以上是个人理解,很多地理解的不是太透彻,等有新的理解再更新。有错误的地方也望大佬指正,共同进步。