赞
踩
教程链接:https://docs.px4.io/main/zh/dev_setup/building_px4.html
由于我使用的是pixhawk 2.4.8,应该使用以下命令:
cd /home/…/PX4-AutoPilot
make px4_fmu-v2_default
编译结束后,可以看到build文件夹内的生成的代码。
飞控板使用USB连接电脑,在make命令后附加upload命令,通过USB将编译好的二进制文件上传到自动驾驶仪硬件。
make px4_fmu-v2_default upload
VSCode是官方支持(并推荐)的用于PX4开发的IDE。它易于设置,可用于模拟和硬件环境编译PX4。
找到源码: PX4-Autopilot/src/examples/px4_simple_app,可以查看PX4的基本结构。
mkdir /home/…/PX4-AutoPilot/src/examples/px4_hello_sky
cd /home/…/PX4-AutoPilot/src/examples/px4_hello_sky
gedit px4_hello_sky.c
由于我们使用的是PX4开源代码,因此需要按照规则,添加注释以表示PX4的贡献。
/**************************************************************************** * * Copyright (c) 2012-2022 PX4 Development Team. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name PX4 nor the names of its contributors may be * used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************/
再添加头部注释:
/**
* @file px4_simple_app.c
* Minimal application example for PX4 autopilot
*
* @author Example User <mail@example.com>
*/
代码:
#include <px4_platform_common/log.h>
__EXPORT int px4_hello_sky_main(int argc, char *argv[]);
int px4_hello_sky_main(int argc, char *argv[])
{
PX4_INFO("Hello Sky!");
return OK;
}
函数必须以<module_name>_main
格式命名并从模块中导出__EXPORT
。
提示:PX4_INFO
相当于输出到PX4 shell的printf
(包含在px4_platform_common/log.h中)。 这里有不同的日志级别:PX4_INFO
、PX4_WARN
、PX4_ERR
、PX4_DEBUG
。 警告和错误会额外添加到ULog
并显示在Flight Review
中。
在px4_hello_sky这个文件夹下:
gedit CMakeLists.txt
复制以下文本到.txt内:
############################################################################ # # Copyright (c) 2015 PX4 Development Team. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # 3. Neither the name PX4 nor the names of its contributors may be # used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # ############################################################################ px4_add_module( MODULE examples__px4_hello_sky MAIN px4_hello_sky STACK_MAIN 2000 SRCS px4_hello_sky.c DEPENDS )
注意:
px4_add_module()
方法根据模块描述生成静态库。MODULE
块是模块的唯一固件名称(按照惯例,模块名称的前缀是src之后的父路径)MAIN
块列出了模块的入口点,它将命令注册到 NuttX,以便可以从 PX4 shell 或 SITL 控制台调用它。px4_add_module()
文件格式在 PX4-Autopilot/cmake/px4_add_module.cmake
里有明确说明。 为了实现这一功能,我们使用了 POSIX 系统调用函数 poll()
。px4_add_module
指定DYNAMIC
选项,则会在 POSIX 平台上创建一个动态库而不是静态库(这些可以在无需重新编译 PX4 的情况下加载,并作为二进制文件而不是源代码共享给其他人)。 您的应用程序不会成为内置命令,而是生成一个名为examples__px4_simple_app.px4mod
的文件。 您可以通过在运行时使用以下dyn
命令加载文件来运行您的命令:dyn ./examples__px4_simple_app.px4mod
在相同文件夹内,创建一个名为Kconfig的Kconfig定义文件:
gedit Kconfig
复制以下内容到文件内:
bool "PX4 Hello sky"
default n
---help---
Enable PX4 hello sky
编写文件到此为止,以下需要编译应用程序/固件。为了运行它,您首先需要确保它是作为 PX4 的一部分构建的。 应用程序被将依据目标的适当板级cmake文件添加到编译/固件中:
PX4-Autopilot/boards/px4/sitl/default.px4board
PX4-Autopilot/boards/px4/fmu-v2/default.px4board
PX4-Autopilot/boards/px4/fmu-v4/default.px4board
PX4-Autopilot/boards/
在相应的.px4board
文件中添加相应的Kconfig密钥CONFIG_EXAMPLES_PX4_HELLO_SKY=y
(推荐)
或执行boardconfig make px4_fmu-v2_default boardconfig
:
examples --->
[x] PX4 Hello sky ----
.../PX4-AutoPilot/boards/px4/fmu-v2/default.px4board
文件最后一行添加了CONFIG_EXAMPLES_PX4_HELLO_SKY=y
,如下图所示。.../PX4-AutoPilot/boards/px4/sitl/default.px4board
文件最后一行添加了CONFIG_EXAMPLES_PX4_HELLO_SKY=y
。使用特定板的命令构建示例:
jMAVSim 模拟器: make px4_sitl_default jmavsim
Gazebo模拟器:make px4_sitl_default gazebo
Pixhawk v1/2:make px4_fmu-v2_default
(或者仅使用make px4_fmu-v2
)
Pixhawk v3: make px4_fmu-v4_default
其它板: 构建代码
编译完px4_sitl_default
后,可以看到下图:
在pxh>
后输入:help
启用上传器,然后重启飞控板:
Pixhawk v1/2: make px4_fmu-v2_default upload
Pixhawk v3: make px4_fmu-v4_default upload
重启飞控板之前,它应该打印一些编译消息,并在最后打印:
Loaded firmware for X,X, waiting for the bootloader…
一旦飞控板被重启并完成了固件的上传,命令行界面将输出:
Erase : [] 100.0%
Program: [] 100.0%
Verify : [====================] 100.0%
Rebooting.
[100%] Built target upload
现在通过串口或USB连接到系统控制台。 按ENTER将调出 shell 提示:
nsh>
输入“help”并按回车键,查看命令:
nsh> help help usage: help [-v] [<cmd>] [ df kill mkfifo ps sleep ? echo losetup mkrd pwd test cat exec ls mh rm umount cd exit mb mount rmdir unset cp free mkdir mv set usleep dd help mkfatfs mw sh xd Builtin Apps: reboot perf top .. px4_hello_sky .. sercon serdis
请注意,px4_hello_sky
现在是可用命令的一部分。 通过键入px4_hello_sky
并输入回车启动它:
px4_hellosky
,.c文件名为:px4_hellosky.c
mkdir /home/…/PX4-AutoPilot/src/examples/px4_hellosky
cd /home/…/PX4-AutoPilot/src/examples/px4_hellosky
gedit px4_hellosky.c
/** * @file px4_hellosky.c * Minimal application example for PX4 autopilot * * @author Example User <mail@example.com> */ #include <px4_platform_common/log.h> __EXPORT int px4_hellosky_main(int argc, char *argv[]); int px4_hellosky_main(int argc, char *argv[]) { PX4_INFO("Hello Sky!"); return 0; }
CmakeLists.txt
gedit CmakeLists.txt
px4_add_module(
MODULE examples__px4_hellosky
MAIN px4_hellosky
SRCS
px4_hellosky.c
DEPENDS
)
注意:
MODULE
后面为上一级文件夹名称和这一级文件夹名称,中间用两个“_”隔开。MAIN
后面为这一级文件夹名称SRCS
后面为.c文件名称。5.进入…/PX4-Autopilot/boards/px4/sitl/文件夹
打开default.cmake文件:
在EXAMPLES下添加:px4_hellosky
这个文件夹名称
make px4_sitl_default jmavsim
打开QGC
等到初始化完成后,在终端输入:
help
就可以看到自己刚刚编写的第一个程序px4_hellosky
px4_hellosky
就可以看到INFO输出信息:Hello Sky!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。