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

An application can be written to run as either a task (a module with its own stack and process priority) or as a work queue task (a module that runs on a work queue thread, sharing the stack and thread priorit with other tasks on the work queue). In most cases a work queue task can be used, as this minimizes resource usage.

注解

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.

# Work Queue Task

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

PX4 固件中包含了一个模版文件: src/templates/module (opens new window) ,基于该模版编写的应用(模块)可以在应用自己的栈上执行 任务

The example shows how. In summary:

  1. Specify the dependency on the work queue library in the cmake definition file (CMakeLists.txt (opens new window)):
    ...
    DEPENDS
       px4_work_queue
    
  2. In addition to ModuleBase, the task should also derive from ScheduledWorkItem (included from ScheduledWorkItem.hpp (opens new window))
  3. Specify the queue to add the task to in the constructor initialisation. The work_item (opens new window) example adds itself to the wq_configurations::test1 work queue as shown below:
    WorkItemExample::WorkItemExample() :
        ModuleParams(nullptr),
        ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::test1)
    {
    }
    

注解

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

  1. Implement the ScheduledWorkItem::Run() method to perform "work".
  2. Implement the task_spawn method, specifying that the task is a work queue (using the task_id_is_work_queue id.
  3. Schedule the work queue task using one of the scheduling methods (in the example we use ScheduleOnInterval from within the init method).

# Tasks

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 (opens new window).

The template demonstrates the following additional features/aspects that are required or are useful for a full application:

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