当前位置:网站首页>Px4 module design part 13: workqueue design
Px4 module design part 13: workqueue design
2022-07-27 22:52:00 【lida2003】
PX4 Module design 13 :WorkQueue Design
1. WorkQueue start-up
WorkQueue yes PX4 Flight control software Common( public ) Components , By function px4::WorkQueueManagerStart It started , Please refer to PX4 Module design ten :PX4 The boot process .
board_app_initialize
└──> px4_platform_init
└──> px4::WorkQueueManagerStart
notes 1:Nuttx The system is supported WorkQueue Of . however PX4 stay common( public ) The component layer implements a similar WorkQueue The function of , Instead of directly using Nuttx Systematic WorkQueue. It is preliminarily suspected that this result was caused by historical time .
notes 2:Nuttx The first version is in 2007 Released in , Start supporting 2-3 individual MCU((i.e. 8051 and ARM7);PX4-AutoPilot yes 2009 The project started in ,2013 year ETH Zurich ( Federal Polytechnic University of Zurich ) Computer vision and geometry Laboratory Lorenz Meier , Released the first generation of experimental version : Dual flight control processors PX4FMU/PX4IO Hardware .
So to sum up , For the following reasons
1) Its historical reasons ,PX4-AutoPilot Common components of WorkQueue Independent implementation ;
2) Flight control mode adopts C++ Class inheritance for development and management ;
3)uORB Message component adopts C++ Class to manage ;
4)HRT High precision timing acquisition trigger management ;
WorkQueue The overall management of the company combines the above historical reasons , take C/C++ Design , class , Inherit , And kernel mode / User mode . The overall feeling is extremely complex , This part of the content is really not flattering !!!(—AnyWay, Historical reasons !!!—)
2. WorkQueue Interface
2.1 Basic interface
The most basic WorkQueue The management interface is not responsible , Mainly Start/Stop/Status Three .
int WorkQueueManagerStart() //WorkQueue Manage startup tasks
int WorkQueueManagerStop() // As a basic component, it basically does not need Stop, At least there is no Stop The place of .
int WorkQueueManagerStatus() //WorkQueue State query
2.2 Auxiliary interface
const wq_config_t & device_bus_to_wq(uint32_t device_id_int) //device_bus turn wq To configure
const wq_config_t & serial_port_to_wq(const char *serial) //serial_port turn wq To configure
const wq_config_t & ins_instance_to_wq(uint8_t instance) //instance turn wq To configure
static WorkQueue * FindWorkQueueByName(const char *name) // Check by name WorkQueue
WorkQueue * WorkQueueFindOrCreate(const wq_config_t &new_wq) // Find or create WorkQueue
2.3 WorkQueue Task function
WorkQueue At present, it is in support of Flat and Protected Build Two compilation modes , The most significant difference between different compilation modes is Flat Build Use pthread_create Set up tasks , and Protected Build Use px4_task_spawn_cmd Set up tasks .
2.3.1 Flat Build
static void * WorkQueueRunner(void *context)
2.3.2 Protected Build
The internal implementation of this function will call again Flat Build Function of ( At this time, the running code space will be in kernel mode ).
inline static int WorkQueueRunner(int argc, char *argv[])
2.4 Key interface analysis
2.4.1 WorkQueueManagerStart
WorkQueueManagerStart
├──> <_wq_manager_should_exit.load() && (_wq_manager_create_queue == nullptr)>
│ ├──> _wq_manager_should_exit.store(false);
│ ├──> int task_id = px4_task_spawn_cmd("wq:manager",SCHED_DEFAULT,SCHED_PRIORITY_MAX,PX4_STACK_ADJUSTED(1280),(px4_main_t)&WorkQueueManagerRun,nullptr);
│ └──> <task_id < 0>
│ ├──> _wq_manager_should_exit.store(true);
│ ├──> PX4_ERR("task start failed (%i)", task_id);
│ └──> return -errno;
├──> else
│ ├──> PX4_WARN("already running");
│ └──> return PX4_ERROR;
└──> return PX4_OK;
2.4.2 WorkQueueManagerRun
WorkQueueManagerRun
├──> _wq_manager_wqs_list = new BlockingList<WorkQueue *>();
├──> _wq_manager_create_queue = new BlockingQueue<const wq_config_t *, 1>();
├──> <while (!_wq_manager_should_exit.load())>
│ ├──> const wq_config_t *wq = _wq_manager_create_queue->pop(); // When there is no work queue When , Management tasks are always blocked here .
│ └──> <wq != nullptr> // It should not be empty , Fault tolerance in case of segment errors , Inside is the establishment of new work queue
│ ├──> [stack, priority, etc] // A little ....
│ ├──> [Flat Build, pthread_create WorkQueueRunner]
│ ├──> [Protected Build, px4_task_spawn_cmd WorkQueueRunner]
│ ├──> <pid > 0>
│ │ └──> PX4_DEBUG("starting: %s, priority: %d, stack: %zu bytes", wq->name, sched_priority, stacksize);
│ └──> <else>
│ └──> PX4_ERR("failed to create thread for %s (%i): %s", wq->name, pid, strerror(pid));
└──> return 0;
2.4.3 WorkQueueRunner
WorkQueueRunner
├──> wq_config_t *config = static_cast<wq_config_t *>(context);
├──> WorkQueue wq(*config);
├──> _wq_manager_wqs_list->add(&wq); // add to work queue list
├──> wq.Run(); // Here is the
├──> _wq_manager_wqs_list->remove(&wq); // remove from work queue list
└──> return nullptr;
2.4.4 WorkQueue::Run
WorkQueue::Run
├──> <while (!should_exit())>
│ ├──> do {} while (px4_sem_wait(&_process_lock) != 0); // loop as the wait may be interrupted by a signal
│ ├──> work_lock();
│ └──> <while (!_q.empty())>
│ ├──> WorkItem *work = _q.pop();
│ ├──> work_unlock(); // unlock work queue to run (item may requeue itself)
│ ├──> work->RunPreamble();
│ ├──> work->Run(); // What really needs to be implemented Run function , It's usually inheritance WorkItem The object of
│ ├──> work_lock(); // re-lock
│ └──> work_unlock();
└──> PX4_DEBUG("%s: exiting", _config.name);
2.4.5 WorkQueueFindOrCreate
WorkQueueFindOrCreate
├──> <_wq_manager_create_queue == nullptr>
│ ├──> PX4_ERR("not running");
│ └──> return nullptr;
├──> WorkQueue *wq = FindWorkQueueByName(new_wq.name); // search list for existing work queue
├──> <wq == nullptr>
│ ├──> _wq_manager_create_queue->push(&new_wq); // It's important here , Only push 了 ,WorkQueueManagerRun It can only be carried out inside .
│ ├──> _uint64_t t = 0;
│ └──> _<while (wq == nullptr && t < 10_s)> // we wait until new wq is created, then return
│ ├──> t += 1_ms;
│ ├──> px4_usleep(1_ms);
│ ├──> wq = FindWorkQueueByName(new_wq.name);
│ └──> <wq == nullptr>
│ └──> PX4_ERR("failed to create %s", new_wq.name);
└──> return wq;
3. summary
Work queue , Actually, I'm not responsible . and PX4 Why does your work queue look complex , Mainly work queues and actual business coupling . Here we haven't put uORB Put your subscription content inside , If you combine this part , Plus the mutual switching of multiple inherited businesses , It seems more complicated .
So we especially emphasize that the design needs loose coupling , Try to be modular , Clear interface design , Define the frame design .
4. Reference material
【1】PX4 A brief introduction to the open source software framework
【2】Nuttx WorkQueue
边栏推荐
猜你喜欢

Project management tool Zen

七大排序之直接插入排序

你想被开除吗?来看看程序员「离职小技巧」吧

Chapter 3 business function development (choose to export market activities, Apache POI)

CMOS switch (II)_ Parameter extraction

饿了么input输入框设置type=‘number‘时,去掉后面的上下按钮

PyQt5快速开发与实战 4.9 对话框类控件

物联网架构完全指南

带你掌握 Makefile 分析

Quartus:Instantiation of ‘sdram_model_plus‘ failed. The design unit was not found.
随机推荐
紫光FPGA解决口罩难题!助力口罩生产全面提速
摩托罗拉诉海能达案一审结果出炉:海能达被判赔53亿元
已有6名员工确诊!三星第三度关闭龟尾手机工厂!
Uniswap集成sudoswap,能否拉开NFT流动性新序幕?
Jeninkins offline deployment
android 11 安全策略及权限管理
QT常见操作合集
基于MCU的二维码生成及在墨水屏上进行二维码显示
云计算服务主要安全风险及应对措施
The ordinary way of chasing source code
PX4模块设计之十三:WorkQueue设计
2022/5/13 考试总结
PyQt5快速开发与实战 4.9 对话框类控件
初中三年回忆录
三星存储工厂又发生火灾!
多肽KC2S修饰白蛋白纳米粒/靶向肽GX1修饰人血清白蛋白纳米粒探针的研究制备
Kubernetes二进制部署——理论部分
细胞CLE19多肽荧光成像牛血清白蛋白荧光猝灭量子点的制备
技术生涯10年,那些让我心动的技术书
组件的传参