爲了方便大家深入瞭解飛控工作原理與編程,ArduPilot 提供了許多示例,位於 /Library 文件中,本文將以串口爲例,爲大家展示如何利用這些示例進一步深入開發 APM 飛控。
官方文檔內容,方便大家對照查閱:https://ardupilot.org/dev/docs/learning-ardupilot-the-example-sketches.html
1,首先進入源碼主目錄,如何瞭解目前有哪裏示例可以使用呢 ?通過如下指令:
./waf list | grep 'examples'
系統將會列出可以使用的示例,隨着版本的更新,示例將會越來越多。
2,我們將以 UART_test 爲例,演示整個調試流程, 找到示例源碼 UART_test.cpp,位於
/ardupilot/libraries/AP_HAL/examples/UART_test/UART_test.cpp
打開源代碼,可以看到內容是重複輸出代碼編號
void loop(void)
{
test_uart(hal.uartA, "uartA");
test_uart(hal.uartB, "uartB");
test_uart(hal.uartC, "uartC");
test_uart(hal.uartD, "uartD");
test_uart(hal.uartE, "uartE");
// also do a raw printf() on some platforms, which prints to the
// debug console
#if HAL_OS_POSIX_IO
::printf("Hello on debug console at %.3f seconds\n", (double)(AP_HAL::millis() * 0.001f));
#endif
hal.scheduler->delay(1000);
}
3,嘗試修改程序,然後編譯程序並且下載
3.1,回到源碼主目錄
3.2,配置硬件,一般不變化不用重新配置,使用指令 ./waf configure --board px4-v2
root@ubuntu:/home/saffron/lrr/ardupilot# ./waf configure --board px4-v2
Setting top to : /home/saffron/lrr/ardupilot
Setting out to : /home/saffron/lrr/ardupilot/build
Autoconfiguration : enabled
Setting board to : px4-v2
Checking for program 'arm-none-eabi-ar' : /opt/gcc-arm-none-eabi-6-2017-q2-update/bin/arm-none-eabi-ar
Using toolchain : arm-none-eabi
Checking for 'g++' (C++ compiler) : /usr/lib/ccache/arm-none-eabi-g++
Checking for 'gcc' (C compiler) : /usr/lib/ccache/arm-none-eabi-gcc
Checking for c flags '-MMD' : yes
Checking for cxx flags '-MMD' : yes
Checking for HAVE_CMATH_ISFINITE : no
Checking for HAVE_CMATH_ISINF : no
Checking for HAVE_CMATH_ISNAN : no
Checking for NEED_CMATH_ISFINITE_STD_NAMESPACE : no
Checking for NEED_CMATH_ISINF_STD_NAMESPACE : no
Checking for NEED_CMATH_ISNAN_STD_NAMESPACE : no
Checking for header endian.h : not found
Checking for header byteswap.h : not found
Checking for program 'cmake' : /usr/bin/cmake
Checking cmake version : 3.10.2
Checking for program 'ninja, ninja-build' : not found
Checking for program 'make' : /usr/bin/make
Checking for program 'python' : /usr/bin/python
Checking for python version >= 2.7.0 : 2.7.17
Checking for program 'python' : /usr/bin/python
Checking for python version >= 2.7.0 : 2.7.17
Source is git repository : yes
Update submodules : yes
Checking for program 'git' : /usr/bin/git
Gtest : STM32 boards currently don't support compiling gtest
Checking for program 'arm-none-eabi-size' : /opt/gcc-arm-none-eabi-6-2017-q2-update/bin/arm-none-eabi-size
Benchmarks : disabled
Unit tests : disabled
Checking for program 'rsync' : /usr/bin/rsync
'configure' finished successfully (1.258s)
3.3,編譯與下載指定代碼,指令 ./waf build --target examples/UART_test --upload
root@ubuntu:/home/saffron/lrr/ardupilot# ./waf build --target examples/UART_test --upload
Waf: Entering directory `/home/saffron/lrr/ardupilot/build/px4-v2'
[ 8/15] CMake Configure px4
-- nuttx-px4fmu-v2-apm
-- could not find gdbtui
-- could not find ddd
-- could not find gdbtui
-- could not find ddd
-- The ASM compiler identification is GNU
-- Found assembler: /usr/lib/ccache/arm-none-eabi-gcc
-- Found PythonInterp: /usr/bin/python (found version "2.7.17")
-- GIT_DESC = 1e7ed30b1710b55f8a7509800187d51e63bb2143
-- Configuring done
-- Generating done
-- Build files have been written to: /home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware
INCLUDE_DIRS=/home/saffron/lrr/ardupilot/modules/PX4Firmware/src;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/src;/home/saffron/lrr/ardupilot/modules/PX4Firmware/src/modules;/home/saffron/lrr/ardupilot/modules/PX4Firmware/src/include;/home/saffron/lrr/ardupilot/modules/PX4Firmware/src/lib;/home/saffron/lrr/ardupilot/modules/PX4Firmware/src/platforms;/home/saffron/lrr/ardupilot/modules/PX4Firmware/src/drivers/boards/px4fmu-v2;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/src/modules/px4_messages;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/src/modules;/home/saffron/lrr/ardupilot/modules/PX4Firmware/mavlink/include/mavlink;/home/saffron/lrr/ardupilot/modules/PX4Firmware/src/lib/DriverFramework/framework/include;src/lib/matrix;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/px4fmu-v2/NuttX/nuttx-export/include;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/px4fmu-v2/NuttX/nuttx-export/include/cxx;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/px4fmu-v2/NuttX/nuttx-export/arch/chip;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/px4fmu-v2/NuttX/nuttx-export/arch/common
ADDING DRIVERS
[14/15] CMake Build px4 msg_gen
Scanning dependencies of target git_genmsg
[ 0%] Built target git_genmsg
Scanning dependencies of target git_gencpp
[ 33%] Built target git_gencpp
Scanning dependencies of target msg_gen
[100%] Built target msg_gen
[15/15] CMake Build px4 prebuild_targets
Scanning dependencies of target __nuttx_patch_px4fmu-v2
[ 0%] Built target __nuttx_patch_px4fmu-v2
Scanning dependencies of target __nuttx_copy_px4fmu-v2
[ 0%] Built target __nuttx_copy_px4fmu-v2
Scanning dependencies of target nuttx_export_px4fmu-v2
[100%] Built target nuttx_export_px4fmu-v2
Scanning dependencies of target prebuild_targets
[100%] Built target prebuild_targets
[470/479] Compiling libraries/AP_HAL/examples/UART_test/UART_test.cpp
[471/479] CMake Configure px4
-- nuttx-px4fmu-v2-apm
-- could not find gdbtui
-- could not find ddd
-- could not find gdbtui
-- could not find ddd
-- The ASM compiler identification is GNU
-- Found assembler: /usr/lib/ccache/arm-none-eabi-gcc
-- Found PythonInterp: /usr/bin/python (found version "2.7.17")
-- GIT_DESC = 1e7ed30b1710b55f8a7509800187d51e63bb2143
-- Configuring done
-- Generating done
[472/479] Linking build/px4-v2/lib/examples/libUART_test.a
-- Build files have been written to: /home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware
INCLUDE_DIRS=/home/saffron/lrr/ardupilot/modules/PX4Firmware/src;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/src;/home/saffron/lrr/ardupilot/modules/PX4Firmware/src/modules;/home/saffron/lrr/ardupilot/modules/PX4Firmware/src/include;/home/saffron/lrr/ardupilot/modules/PX4Firmware/src/lib;/home/saffron/lrr/ardupilot/modules/PX4Firmware/src/platforms;/home/saffron/lrr/ardupilot/modules/PX4Firmware/src/drivers/boards/px4fmu-v2;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/src/modules/px4_messages;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/src/modules;/home/saffron/lrr/ardupilot/modules/PX4Firmware/mavlink/include/mavlink;/home/saffron/lrr/ardupilot/modules/PX4Firmware/src/lib/DriverFramework/framework/include;src/lib/matrix;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/px4fmu-v2/NuttX/nuttx-export/include;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/px4fmu-v2/NuttX/nuttx-export/include/cxx;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/px4fmu-v2/NuttX/nuttx-export/arch/chip;/home/saffron/lrr/ardupilot/build/px4-v2/modules/PX4Firmware/px4fmu-v2/NuttX/nuttx-export/arch/common
ADDING DRIVERS
[473/479] CMake Build px4 fw_io
Loaded firmware for 9,0, size: 289632 bytes, waiting for the bootloader...
If the board does not respond within 1-2 seconds, unplug and re-plug the USB connector.
Found board 9,0 bootloader rev 4 on /dev/serial/by-id/usb-3D_Robotics_PX4_BL_FMU_v2.x_0-if00
50583400 00ac2600 00100000 00ffffff ffffffff ffffffff ffffffff ffffffff 66ed47ff ff73cc15 c8ad940c dbc59f39 d6c20e06 f953d3ef f3073019 d035ab0d 3f60334e 10dda9f8 cdb0cbbd 42cdc6b6 3ba305f7 81532581 84ee3da6 23bc6340 8321be68 edd356c9 1e3b8f5c 5e07decc 9c6be5a2 458a1513 4bbbbc21 eda35ce5 a8b840a5 ef019ca5 c89bb183 bb00f0c0 06db1a26 7375ff57 1ca41d94 24aa662e ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff type: PX4
idtype: =00
vid: 000026ac
pid: 00000010
coa: Zu1H//9zzBXIrZQM28WfOdbCDgb5U9Pv8wcwGdA1qw0/YDNOEN2p+M2wy71Czca2O6MF94FTJYGE7j2mI7xjQIMhvmjt01bJHjuPXF4H3syca+WiRYoVE0u7vCHto1zlqLhApe8BnKXIm7GDuwDwwAbbGiZzdf9XHKQdlCSqZi4=
sn: 0038001f3432470d31323533
Erase : [====================] 100.0%
Program: [====================] 100.0%
Verify : [====================] 100.0%
Rebooting.
[100%] Built target upload
Attempting reboot on /dev/serial/by-id/usb-3D_Robotics_PX4_FMU_v2.x_0-if00 with baudrate=57600...
If the board does not respond, unplug and re-plug the USB connector.
Waf: Leaving directory `/home/saffron/lrr/ardupilot/build/px4-v2'
BUILD SUMMARY
Build directory: /home/saffron/lrr/ardupilot/build/px4-v2
Target Text Data BSS Total
-----------------------------------------------
examples/UART_test 286872 2760 39356 328988
PX4
The ELF files are pointed by the path in the "Target" column. The .px4 files are in
the same directory of their corresponding ELF files.
PX4IO
Binary Text Data BSS Total
-----------------------------------------------
px4-extra-files/px4io 39604 600 3134 43338
Build commands will be stored in build/px4-v2/compile_commands.json
'build' finished successfully (35.825s)
4,下載成功後,查看代碼運行結果,通過串口調試助手查看
可以看出調試代碼已經可以正常運行了。
本文章通過串口調試示例,引導大家深入學習 APM 編程。