PX4源码开发人员文档(二) - Hello Sky

2019-08-31 09:57

前提

? ? ?

用UART1连接PX4FMU和计算机 安装PX4Toolchain 注册Github账户

Step 1: 准备源码文件

为了方便管理代码,可以使用GIT 版本控制系统,在 GitHub上 fork和更新源码。 不注册GitHub的话,可以在PX4 console中输入下面的命令:

1.

git clone https://github.com/PX4/Firmware

更新the git submodules,在 PX4 console上输入:

1. 2. 3.

cd Firmware git submodule init git submodule update

文件准本完成,编译操作系统,输入:

1.

make archives

当submodules或者NuttX 配置改变,重新编译。

在Firmware/src/examples/ 新建目录px4_simple_app,在目录中新建文件module.mk并添加如下内容:

1. 2.

MODULE_COMMAND = px4_simple_app SRCS = px4_simple_app.c

Step 2: 最小程序

在px4_simple_app目录下创建px4_simple_app.c文件。 遵循PX4 CodingStyle 编辑如下代码:

1. 2. 3.

/**

* @file px4_simple_app.c

* Minimal application example for PX4 autopilot.

4. */ 5.

6. #include 7. #include 8. #include 9.

10. __EXPORT int px4_simple_app_main(int argc, char *argv[]); 11.

12. int px4_simple_app_main(int argc, char *argv[]) 13. {

14. printf(\); 15. return OK; 16. }

Step 3: 在NuttShell中注册应用并build

完成了应用并可以运行,但是没有在NuttShell 命令中注册。为将应用编译到固件当中,将其填加到需要build的模块中。该配置位于:

Firmware/makefiles/nuttx/config_px4fmu-v2_default.mk 在上面的文件中的任意位置,为应用创建一行:

1.

MODULES += examples/px4_simple_app

然后编译,并清除已经build的应用:

1. 2.

make clean

make px4fmu-v2_default -j4

如果没有注册新的app,只需要对新增的builds执行下述命令:

1.

make px4fmu-v2_default -j4

Step 4:加载应用并测试

使能uploader 然后重置电路板:

1.

make upload px4fmu-v2_default -j4

在重置电路板之前显示如下一系列信息:

1. 2.

Generating /Users/user/src/Firmware/Images/px4fmu.px4 Loaded firmware for 5,0, waiting for the bootloader...

电路板充值并上载后,打印:

1. 2. 3. 4. 5.

Found board 5,0 on /dev/tty.usbmodem1 erase... program... verify... done, rebooting.

运行px4 Toolchain 下的TeraTerm,运行File/New connection,选择飞控所在的串口,点击OK。按回车,出现nsh>.

在NSH连接的情况下,切换到shell.如果没有看到输出,敲击Enter键,会得到NSH提示; 或者也可以,使用QGroundControl进行NuttShell (NSH),使用Mini-USB连接Pixhawk,按照下图进行。点击QGroundControl上的终端输出,并敲击Enter键。

1.

nsh>

输入 help,并敲击Enter键:

1. nsh> help

2. help usage: help [-v] [] 3.

4. [ df kill mkfifo ps sleep 5. ? echo losetup mkrd pwd test 6. cat exec ls mh rm umount 7. cd exit mb mount rmdir unset 8. cp free mkdir mv set usleep 9. dd help mkfatfs mw sh xd 10.

11. Builtin Apps: 12. reboot 13. perf 14. top 15. ..

16. px4_simple_app

17. .. 18. sercon 19. serdis

px4_simple_app 现在成为可用的指令。输入px4_simple_app并敲击Enter键:

1. 2.

nsh> px4_simple_app Hello Sky!

Step 5: 订阅传感器数据

为了做有用的事情,应用需要订阅subscribe输入并发布publish输出 (e.g. 电机 或伺服指令). PX4平台真正的硬件抽象(true hardware abstraction )开始有用——不需要与传感器驱动以任何方式相互作用,并且电路板或者传感器升级后不需要升级。

PX4中应用间独立的消息通道被称作topics. 在这里,感兴趣的是 sensor_combined topic, 其调用整个系统的同步传感器数据。 订阅到topic非常快和干净:

1. 2. 3.

#include ..

int sensor_sub_fd = orb_subscribe(ORB_ID(sensor_combined));

sensor_sub_fd 是一个文件描述符,非常高效的执行一块等待新的数据的数据块。当前的进程进入休眠,当有新的数据可用时,由调度程序自动地唤醒,在等待的时候,不占用任何CPU周期。为实现这点,使用poll(),调用 POSIX system 。 将poll() 添加到subscription看上去像:

1. 2. 3. 4. 5. 6.

#include

#include ..

int sensor_sub_fd = orb_subscribe(ORB_ID(sensor_combined));

/* one could wait for multiple topics with this technique, just using one here */

7. struct pollfd fds[] = {

8. { .fd = sensor_sub_fd, .events = POLLIN }, 9. }; 10.

11. while (true) {

12. /* wait for sensor update of 1 file descriptor for 1000 ms (1 secon

d) */

13. int poll_ret = poll(fds, 1, 1000); 14. ..

15. if (fds[0].revents & POLLIN) {

16. /* obtained data for the first file descriptor */ 17. struct sensor_combined_s raw;

18. /* copy sensors raw data into local buffer */

19. orb_copy(ORB_ID(sensor_combined), sensor_sub_fd, &raw); 20. printf(\

f\\n\,

21. (double)raw.accelerometer_m_s2[0

],

22. (double)raw.accelerometer_m_s2[1

],

23. (double)raw.accelerometer_m_s2[2

]);

24. } 25. }

px4_simple_app 的完整代码如下:

1. /**

2. * @file px4_simple_app.c

3. * Minimal application example for PX4 autopilot 4. *

5. * @author Example User 6. */ 7.

8. #include 9. #include 10. #include 11. #include 12.

13. #include

14. #include 15.

16. __EXPORT int px4_simple_app_main(int argc, char *argv[]); 17.

18. int px4_simple_app_main(int argc, char *argv[]) 19. {

20. printf(\); 21.


PX4源码开发人员文档(二) - Hello Sky.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:机械设计-习题集答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: