PX4源码开发人员文档(四)——创建后台程序(应用)

2023-05-16

origin: http://blog.csdn.net/lkk05/article/details/48659059

Unix和其他多任务计算机操作系统中,后台程序是指,作为后台进程运行的计算机,而不是由交互用户直接控制。

后台程序概念的主要好处是,后台程序可以直接启动,而不需要将其发送到精确的用户或者shell的后台(然而,这不适用于Nuttx),其状态可以在运行的时候,通过shell查询。也可以终止。

Step 1: 创建一个小的标准应用

根据 FirstOnboard Application Tutorial (Hello Sky)教程(见PX4源码开发人员文档(二)),这是一个基本程序(简化):

[cpp] view plain copy
  1. ..  
  2. __EXPORT int px4_daemon_app_main(int argc, char *argv[]);  
  3. ..  
  4. int px4_daemon_app_main(int argc, char *argv[])  
  5. {  
  6.     while (true) {  
  7.         warnx("Hello Daemon!\n");  
  8.         sleep(1);  
  9.     }  
  10.     return 0;  
  11. }  

这个应用的问题非常明显,如果不使用&启动,将会阻塞shellNuttx,并不如此,并且会出于small footprint和可靠性的原因,支持CTRL-Z / fg / bg)。为了回避这个问题,下面部分将应用转换为一个后台程序。


Step 2: 创建后台进程管理函数

主函数由后台进程管理函数替代,旧的主函数的内容现在位于后台任务/进程中

[cpp] view plain copy
  1. #include <systemlib/systemlib.h>  
  2.    
  3. ..  
  4. __EXPORT int px4_daemon_app_main(int argc, char *argv[]);  
  5. ..  
  6. int mavlink_thread_main(int argc, char *argv[]);  
  7. ..  
  8. int mavlink_thread_main(int argc, char *argv[])  
  9. {  
  10.     while (true) {  
  11.         warnx("Hello Daemon!\n");  
  12.         sleep(1);  
  13.         if (thread_should_exit) break;  
  14.     }  
  15.    
  16.     return 0;  
  17. }  
  18. ..  
  19. int px4_daemon_app_main(int argc, char *argv[])  
  20. {  
  21.     if (argc < 1)  
  22.         usage("missing command");  
  23.    
  24.     if (!strcmp(argv[1], "start")) {  
  25.    
  26.         if (thread_running) {  
  27.             warnx("daemon already running\n");  
  28.             /* this is not an error */  
  29.             exit(0);  
  30.         }  
  31.    
  32.         thread_should_exit = false;  
  33.         daemon_task = task_spawn_cmd("daemon",  
  34.                          SCHED_RR,  
  35.                          SCHED_PRIORITY_DEFAULT,  
  36.                          4096,  
  37.                          px4_daemon_thread_main,  
  38.                          (argv) ? (const char **)&argv[2] : (const char **)NULL);  
  39.         thread_running = true;  
  40.         exit(0);  
  41.     }  
  42.    
  43.     usage("unrecognized command");  
  44.     exit(1);  
  45. }  

这将会启动一个新的任务,具有4096字节的堆栈,并传递非后台程序的具体指令行选项到后台主函数。典型的调用如下所示:

[plain] view plain copy
  1. px4_daemon_app start  

上面的代码没有报告状态,并且没有对多次调用后台进程进行保护。

Step 3: 添加停止/状态指令以及安全保护

具有合适的启动/停止/状态建立和附加安全保护的完整px4_daemon_app代码如下:

[cpp] view plain copy
  1. /** 
  2.  * @file px4_daemon_app.c 
  3.  * daemon application example for PX4 autopilot 
  4.  * 
  5.  * @author Example User <mail@example.com> 
  6.  */  
  7.    
  8. #include <stdio.h>  
  9. #include <stdlib.h>  
  10. #include <string.h>  
  11. #include <unistd.h>  
  12.    
  13. #include <px4_config.h>  
  14. #include <nuttx/sched.h>  
  15.    
  16. #include <systemlib/systemlib.h>  
  17. #include <systemlib/err.h>  
  18.    
  19. static bool thread_should_exit = false;     /**< daemon exit flag */  
  20. static bool thread_running = false;     /**< daemon status flag */  
  21. static int daemon_task;             /**< Handle of daemon task / thread */  
  22.    
  23. /** 
  24.  * daemon management function. 
  25.  */  
  26. __EXPORT int px4_daemon_app_main(int argc, char *argv[]);  
  27.    
  28. /** 
  29.  * Mainloop of daemon. 
  30.  */  
  31. int px4_daemon_thread_main(int argc, char *argv[]);  
  32.    
  33. /** 
  34.  * Print the correct usage. 
  35.  */  
  36. static void usage(const char *reason);  
  37.    
  38. static void  
  39. usage(const char *reason)  
  40. {  
  41.     if (reason) {  
  42.         warnx("%s\n", reason);  
  43.     }  
  44.    
  45.     warnx("usage: daemon {start|stop|status} [-p <additional params>]\n\n");  
  46. }  
  47.    
  48. /** 
  49.  * The daemon app only briefly exists to start 
  50.  * the background job. The stack size assigned in the 
  51.  * Makefile does only apply to this management task. 
  52.  * 
  53.  * The actual stack size should be set in the call 
  54.  * to task_create(). 
  55.  */  
  56. int px4_daemon_app_main(int argc, char *argv[])  
  57. {  
  58.     if (argc < 2) {  
  59.         usage("missing command");  
  60.         return 1;  
  61.     }  
  62.    
  63.     if (!strcmp(argv[1], "start")) {  
  64.    
  65.         if (thread_running) {  
  66.             warnx("daemon already running\n");  
  67.             /* this is not an error */  
  68.             return 0;  
  69.         }  
  70.    
  71.         thread_should_exit = false;  
  72.         daemon_task = px4_task_spawn_cmd("daemon",  
  73.                          SCHED_DEFAULT,  
  74.                          SCHED_PRIORITY_DEFAULT,  
  75.                          2000,  
  76.                          px4_daemon_thread_main,  
  77.                          (argv) ? (char *const *)&argv[2] : (char *const *)NULL);  
  78.         return 0;  
  79.     }  
  80.    
  81.     if (!strcmp(argv[1], "stop")) {  
  82.         thread_should_exit = true;  
  83.         return 0;  
  84.     }  
  85.    
  86.     if (!strcmp(argv[1], "status")) {  
  87.         if (thread_running) {  
  88.             warnx("\trunning\n");  
  89.    
  90.         } else {  
  91.             warnx("\tnot started\n");  
  92.         }  
  93.    
  94.         return 0;  
  95.     }  
  96.    
  97.     usage("unrecognized command");  
  98.     return 1;  
  99. }  
  100.    
  101. int px4_daemon_thread_main(int argc, char *argv[])  
  102. {  
  103.    
  104.     warnx("[daemon] starting\n");  
  105.    
  106.     thread_running = true;  
  107.    
  108.     while (!thread_should_exit) {  
  109.         warnx("Hello daemon!\n");  
  110.         sleep(10);  
  111.     }  
  112.    
  113.     warnx("[daemon] exiting.\n");  
  114.    
  115.     thread_running = false;  
  116.    
  117.     return 0;  
  118. }  

代码测试将会产生如下的输出:

[plain] view plain copy
  1. nsh> px4_daemon_app start  
  2. [daemon] starting  
  3. Hello Daemon!  

为了使用这一APP,只需在Firmware/makefiles/config_px4fmu_default.mk中,取消对这一示例部分的注释。


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PX4源码开发人员文档(四)——创建后台程序(应用) 的相关文章

  • VR/AR技术杂选

    相机频率 xff1a 一般来说 xff0c 相机频率60Hz是指相机的帧率为60fps xff0c 即frame per second 每秒钟60帧 红外探测器 xff1a 分为两种 xff0c 一种是基于光电特性 xff0c 一种是基于热
  • 【图像】光谱波长分布图

    可见光范围内的颜色倒序为 赤橙黄绿青蓝紫 猜你喜欢 xff1a x1f447 x1f3fb 图像 一个像素占几个字节 xff1f 多少比特 xff1f 图像 尺度不变特征变换算法 xff08 SIFT xff09 基于小波变换的图像边缘检测
  • 百度2014校园招聘-研发工程师笔试题(济南站)

    一 xff0c 简答题 30分 1 xff0c 当前计算机系统一般会采用层次结构存储数据 xff0c 请介绍下典型计算机存储系统一般分为哪几个层次 xff0c 为什么采用分层存储数据能有效提高程序的执行效率 xff1f xff08 10分
  • js中,export和module.export的区别

    说明 导出模块就是导出对象 xff0c export和module exports两者区别 xff1a export是设置导出模块对象的指定属性module export既可以设置导出模块的所有属性 xff0c 又可以设置导出模块的指定属性
  • ERROR:Session/line number was not unique in database. History logging moved to new session.

    摘要 xff1a 遇到此类错误 xff0c 可以通过分段调试的方法找到引发错误的位置 引发错误的原因不详 xff0c 可能很基础 Distribution of the peak number file 61 pd read excel 3
  • ubuntu xfce4和vncserver

    安装xfce4 sudo apt get install xfce4 如果你想创建一个新的用户 xff0c 而不是将桌面使用root权限登录 xff0c 可以执行下面的代码 xff1a 安装vncserver sudo apt instal
  • harmonyOS hdc配置以及自动签名

    hdc是sdk tools中自带的命令 xff0c 你没有配置系统环境变量指定它所在的目录 xff0c 肯定不能直接到处任意调用啊 xff0c 你需要进入到hdc exe所在路径的当前路径下才能去调用它 xff0c 或者你把它的路径加入到系
  • Vue脚手架(Vue-cli)安装

    脚手架是Vue官方提供的标准化开发工具 开发平台 官方文档开始 vue cli cli c command l line 行 interface 命令行接口工具 第一步 仅第一次执行 全局安装 64 vue cli npm install
  • 使用vscode开发配置uni-app(小程序)

    这个文件是用VsCode写uniapp小程序的步骤笔记 安装Vue脚手架 vue cli npm install g 64 vue cli 通过脚手架创建uni app项目 vue create p dcloudio uni preset
  • uniapp image组件的基本使用

    image组件的基本使用 就是用来显示图片的 src 来设置我们图像的路径 属性名类型默认值说明平台差异说明srcString图片资源地址 lt template gt lt div gt lt view gt lt image src 6
  • 将本机做成虚拟镜像文件(使用VMware vCenter Converter收取镜像)

    下载地址 xff08 需要账号 xff09 xff1a https customerconnect vmware com downloads info slug infrastructure operations management vm
  • uniapp 网络请求 get请求

    网络请求 在uni中可以调用uni request方法进行请求网络请求 需要注意的是 xff1a 在小程序中网络相关的API在使用需要配置域名白名单 官方文档 如果发起请求就调用我们这个uni request OBJECT 发送get请求
  • electron之旅(二)react使用

    首先使用react模板 我们这里使用的是vite和yarn span class token function yarn span create vite span class token comment 创建vite的react js模板
  • flutter学习之旅(二)

    如果不知道怎么安装编写可以查看这篇 创建项目 另一个创建方法 flutter create 项目名 热部署 vscode 热部署 vscode很简单 xff1a 可以通过Debug进行调试 使用flutter查看设备 flutter dev
  • Flutter学习之旅 - Scaffold属性Drawer侧边栏

    span class token class name Scaffold span span class token punctuation span appBar span class token punctuation span spa
  • Flutter学习之旅 - AppBar、TabBar、TabBarView实现头部顶部滑动导航

    文章目录 AppBar自定义顶部按钮图标 颜色取消debug图标TabBar TabBarView来实现顶部导航PreferredSize组件改变TabBar导航样式自定义KeepAliveWrapper缓存页面如何获取tab下的索引值销毁
  • Flutter学习之旅 - 路由

    文章目录 Flutter路由介绍普通路由普通路由传值 命名路由将 96 routes 96 的配置提到外面 使用的是Map 命名路由传值 路由跳转返回上一级路由替换路由返回到根路由返回Tabs后到指定页面 Flutter路由介绍 flutt
  • Ubuntu 和 Debian 的关系

    转自 xff1a http people ubuntu com happyaron udc cn lucid html ch11s09 html Debian 于 1993年8月16日 由一名美国普渡大学学生 Ian Murdock 首次发
  • Makefile学习笔记

    主要参考文档 xff1a 跟我一起写makefile xff0c 这里 有一篇 谈谈职业规划 CSDN对陈皓的采访 xff0c 被采访的大牛就是这个文档的作者 xff0c 他的CSDN专栏 本文的示例工程及Makefile 在这里 一 关于
  • 运行的docker增加端口映射

    1 运行了一个centos7的容器 xff0c 22端口映射给宿主机5002端口 xff1a docker span class token function ps span span class token operator span s

随机推荐

  • 23.易混淆命令(apt-get、wget、git clone、pip与pip3区别、apt-get和pip区别)

    摘要 xff1a 本文详细介绍了Ubuntu系统下apt get wget git clone pip与pip3 apt get和pip几组概念的区别 1 apt get 参考文献 xff1a apt get 是AdvancedPackag
  • ssh实现免密登录(文中附上脚本)

    1 为什么要互信 很多时候 xff0c 我们经常需要登录同一个服务器或者客户端 xff0c 但是输入密码很繁琐 xff0c 此时我们就需要能免密登录某些服务器或客户端 下面我们就来看怎么简单实现免密登录 有时候我们在shell脚本中会不断去
  • MapReduce概述及工作流程

    内容 mapreduce原语 xff08 独创 xff09 mapreduce工作流程 xff08 重点 xff09 MR作业提交流程 xff08 重点 xff09 YARN RM HA搭建 xff08 熟练 xff09 运行自带的word
  • IIC总线

    1 概念 IIC总线是PHLIPS公司在八十年代初推出的一种串行的半双工同步总线 xff0c 主要用于连接整体电路 同一块板子两个芯片之间的通信是通过IIC总线进行的 xff08 stm32mp157a lt IIC gt SI7006 I
  • 函数拟合3

    所谓函数拟合 xff0c 就是给定一些输入点 xff0c 输出一个函数曲线 选择的基函数会直接影响线性组合函数的表达能力 当采样点较多 xff0c 而系数较少时 xff0c 会出现欠拟合 xff0c 表达能力不够 当采样点较少 xff0c
  • LDM命令

    http blog 163 com oy mcu blog static 16864297220120193458892 LDM STM指令主要用于现场保护 xff0c 数据复制 xff0c 参数传送等 STMFD指令 STMFD Rn r
  • LE Audio进入商用阶段

    LE Audio进入商用阶段 xff0c TWS耳机要变天了 36氪 蓝牙协议十年来的最大更新 xff0c LE Audio进入商用测试阶段 全球最畅销的IoT设备是什么 xff1f 我很轻松就能告诉你答案 xff1a AirPods 作为
  • CAN XL :CAN协议家族新成员

    十年之前 xff0c 你不认识我 xff0c 我也不认识CAN FD 如今 xff0c CAN FD已经陆续进入乘用车领域 xff0c 几乎所有汽车制造商都将在未来几年内逐步推出搭载CAN FD的乘用车 那十年之后 xff0c 车载网络又会
  • 【整理】嵌入式系统的各种常见外设

    原文地址 xff1a http www crifan com summary embedded system various peripherals 最后更新 xff1a 2013 11 14 TODO xff1a 1 添加更多的常见的外设
  • DDR controller driver

    在SOC中 xff0c DDR是很重要的 xff0c 需要在uboot中进行初始化 xff01 但是DDR异常的复杂 DDR controller也异常的复杂 xff0c 以candence DDR controller为例 xff0c 这
  • 一文看懂IC芯片生产流程:从设计到制造与封装

    origin http forum esm cn com FORUM POST 1000163993 1201257744 0 HTM ga 61 1 101949507 338942905 1436813394 芯片制造的过程就如同用乐高
  • 2015中国国内元器件分销商10亿俱乐部20强榜单

    origin http www v4 cc News 916429 html 元器件分销市场 xff0c 从欧美安富利 xff0c 艾睿 xff0c 富昌等巨头跨度到台湾大联大 xff0c 文晔等新势力 xff0c 花了30年时间 随着电子
  • openvswitch 通过ofproto/trace trace跟踪数据包匹配的流表

    目录 1 解决的问题需求 当vm互访不通时 xff0c 不知道是哪天流表出问题 xff0c 可以通过 ovs提供的工具模拟虚拟机实例发出的数据包来跟踪数据包经过的流表路径 2 使用方法 xff08 一 xff09 解决的问题需求 我们在使用
  • 关于ethercat开发的一些感想

    origin http blog csdn net embededvc article details 50364977 从去年到现在 xff0c 整整一年经历了从ethercat主站到伺服从站的实现过程 xff0c 包括全程负责从站的et
  • modem manager与network manager

    modem manager ModemManager is a DBus system bus activated service meaning it 39 s started automatically when a request a
  • 调试px4串口升级固件

    最近在调试px4的bootloader 实现uart 串口升级 硬件版本为pixhawk bootloader地址为https github com PX4 Bootloader git px4代码地址为https github com P
  • 文章风格: 一级标题使用蓝色字体,二级和三级使用黑色,重点部分使用红色或黄色标记,正文采用浅灰色

    我今天给自己立个规矩 xff0c 以后我自己写的技术类文章 xff0c 一级标题使用蓝色字体 xff0c 二级和三级使用黑色 xff0c 重点部分使用红色或黄色标记 xff0c 正文采用浅灰色 2012 05 03
  • Xlib Programming Manual

    最近看了王垠 写的那篇清华退学的文章 xff0c 看到了他研究linux的过程 xff0c 文中提到了x Windows 我也总想搞一搞这个东西 xff0c 但是不知从何入手 它推荐这本书Xlib Programming Manual xf
  • 第二章 PX4-Pixhawk-RCS启动文件解析

    origin http blog csdn net qq 18112493 article category 6851622 第二章 PX4 RCS 启动文件解析 RCS 的启动类似于 Linux 的 shell 文件 xff0c 如果不知
  • PX4源码开发人员文档(四)——创建后台程序(应用)

    origin http blog csdn net lkk05 article details 48659059 在 Unix 和其他多任务计算机操作系统中 xff0c 后台程序是指 xff0c 作为后台进程运行的计算机 xff0c 而不是