Skip to content

适用于开发完整应用的模版

一个应用程序可以写作一个 任务 (一个有自己的堆栈和处理优先级的模块)或作为 工作队列任务 (一个运行在工作队列线程上的模块, 与工作队列上的其他任务分享堆栈和线程优先级)。 在大多数情况下,可以使用工作队列任务,因为这会减少资源的使用。

Architectural Overview > Runtime Environment provides more information about tasks and work queue tasks.

All the things learned in the First Application Tutorial are relevant for writing a full application.

工作队列任务

PX4-Autopilot contains a template for writing a new application (module) that runs as a work queue task: src/examples/work_item.

工作队列任务应用程序与普通(任务)应用程序相同。 除了它需要指定它是一个工作队列任务,并在初始化期间运行调度它本身。

示例显示了如何操作。 总结:

  1. Specify the dependency on the work queue library in the cmake definition file (CMakeLists.txt):

    ...
    DEPENDS
       px4_work_queue
  2. In addition to ModuleBase, the task should also derive from ScheduledWorkItem (included from ScheduledWorkItem.hpp)

  3. 在构造函数初始化中指定要添加任务的队列。 The work_item example adds itself to the wq_configurations::test1 work queue as shown below:

    cpp
    WorkItemExample::WorkItemExample() :
        ModuleParams(nullptr),
        ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::test1)
    {
    }

    The available work queues (wq_configurations) are listed in WorkQueueManager.hpp.

:::

  1. 实现 ScheduledWorkitem:::Run() 方法来执行"work"。
  2. 实现task_spawn 方法,指定任务是一个工作队列(使用 task_id_is_work_queue id)。
  3. 使用其中一种调度方法使工作队列任务开始调度(本例中我们在使用init 方法中使用 ScheduleOnInterval )。

任务

PX4/PX4-Autopilot contains a template for writing a new application (module) that runs as a task on its own stack: src/templates/template_module.

该模板演示了完整应用程序所需或有用的以下附加功能/方面:

  • 访问参数并对参数更新做出反应。
  • 订阅、等待 topic 更新。
  • 通过 start/stop/status 控制后台运行的任务。 module start [<arguments>] 命令可以直接加入 启动脚本 中。
  • 命令行参数解析。
  • 文档记录:PRINT_MODULE_* 方法有两个用处(该 API 在 源代码 中有详细记录):
    • 它们可用于在控制台键入 module help 指令后输出命令行指令的用法。
    • 通过脚本自动提取他们来生成 模块 & 命令参考 页面。