Introduction to PX4 firmware

1. Basic concept [1-2]:

  • Operating system: NuttX
  • PX4FMU (STM32F427): 主處理器
  • PX4IO (STM32F100): 協處理器 (Code: Firmware/src/modules/px4iofirmware)            *PX4IO firmware is automatically wrote by the PX4FMU
  • Bootloader:  PX4FMU, PX4IO 啟動前會運行的程式(初始化)
  • The processing entrance of PX4 flight controller after the bootloader: Firmware/ROMFS/px4fmu_common/init.d/rcS (啟動腳本: 指定想要啟動那些模組,如果有定義的模組,在rcS中start即可使用)

2. Some of important folder for development

  • Src: we can find all source code here.
  • Src/Drivers: including all sensors' driver.
  • Src/Exmaples: including some simple examples for developers. (Good one for beginner: px4_simple_app)
  • Src/Modules: the core of px4, including all modules used in the flight controller.
  • Src/Systemcmds: including all commands for flight controller.
  • Mavlink: we can modify and add mavlink message here. (mavlink topic are messages passed from GCS or companion computer to px4)
  • Cmake/configs: Including various compiling scripts for compiling on different hardware.
  • Romfs/px4fmu_common/init.d/rcS: System starting script.
  • Msg: we can find all uORB topic and add extra uORB topic here. (uORB topic are messages passed inside the px4 between modules, which means the data exchange between threads)

3. Some test of px4 using GCS (Connect px4 and GCS through USB cable is needed)

  • Mavlink console: talk to px4(Nuttx system) with command (Useful command: help, top, ctrl+c, XXX start, XXX stop, XXX status) *XXX are modules' name, we can find details in the Src/Modules/XXX/XXX_main

4. Firmware upgrade:

  • Method1: make OOO => make OOO upload  *OOO depends on used hardware  (Connect px4 and compiling PC through USB cable is neeeded) *Practice: Try to test the procedure with px4_simple_app module.
  • Method2: make OOO => choose compiled .bin file (Using command: find -name *.bin in the build OOO folder to find the complete file path) for the "custom firmware file..." option in the GCS GUI. (Connect px4 and GCS through USB cable is needed) 

5. uORB: a module for data transmitting between threads (modules) [3]

  • poll
  • subscribe
  • copy
  • advertise
  • publish
  • set_interval
  • advertise_multi
  • unsubscribe
  • check
  • stat
  • exists
  • priority

How to create a user defined uORB message and use it?

  1. Create a  XXX.msg file with defined parameters in the Firmware/msg folder
  2. Add a XXX.msg in the CMakeList.txt file 
  3. Compile the firmware: make OOO...
  4. Check if the corresponding headfile of XXX.msg: XXX.h is automatically created in the path: Firmware/build_OOO/src/modules/uORB/topics
  5. Create a user defined folder in the path: Firmware/src/modules/ ex: uorb_hello
  6. Create a .c file and CMakeList.txt in the uorb_test folder ex:uorb_hello.c
  7. Declare the .h file: #include<uORB/topics/XXX.h>
  8. Using uORB operators mentioned above to write desired action in uorb_hello.c file
  9. Edit the CMakeList.txt in the uorb_test folder
  10. Add uorb_hello in the corresponding cmake file for compiling firmware located in the path: Firmware/cmake/configs/
  11. Compile again and test the function using GCS terminal (Remember to check if a uORB topic is already published before we subscribing it) 
  12. Finally, if we want the px4 to execute the module automatically, we need to edit the rcS file.

6. Mavlink: a protocol for sending data to px4

  • nsh > mavlink status for checking current mavlink status
  • Details of mavlink messages: refer to [4]
  • mavlink library [5]: we can look into the details of each mavlink message in /common/mavlink_msg_XXX.h
  • The GCS side: first check if receive the hearbeat package from px4, if yes, then keep requesting data stream using  mavlink.GenerateMAVlinkPacket. Besides, we can also send commands follow mavlink protocol to flight controller directly.
  • The px4 side: main loop: task_main, setting frequency: configure_stream, MavlinkStream::update for keeping sending mavlink messages. ( file path: firmware/src/modules/mavlink/Mavlink_main.cpp). Details of messages sent from px4 to GCS are written in the: firmware/src/modules/mavlink/Mavlink_message.cpp ( All mavlink messages are inhibited from class: MavlinkStream, streams_list: control the messages sent frequency and sequence)

How to write a user defined mavlink message?

  1. Discard unnessary mavlink messages: method1: don't configure the configure_stream function, method2: uncomment the message in the stream_list.
  2. Create a .xml file in the mavlink/message_definations/v1.0. 
  3. Using mavgenerate.py located in mavlink library to create a corresponding .h file.
  4. Copy the .h file to the path: Firmware/mavlink/include/mavlink/v1.0/common/
  5. Add the path of copied .h in the common.h file.
  6. Add a class of user defined message refer to another message class. ( file path: firmware/src/modules/mavlink/Mavlink_message.cpp )
  7. Add user defined messages in the stream_list. ( file path: firmware/src/modules/mavlink/Mavlink_message.cpp )
  8. Configure the user-defined message using configure_stream function. ( file path: firmware/src/modules/mavlink/Mavlink_main.cpp)

Reference:
[1] pixhawk PX4FMU和PX4IO最底层启动过程分析 
[2] Pixhawk v2 Feature Overview
[3] uORB深入理解和应用
[4] MAVLink Messages
[5] https://github.com/mavlink/c_library_v2

留言

這個網誌中的熱門文章

Tuing PID parameters in QGroundcontrol (2)

Useful PX4 Parameters

Burn linux image to eMMC storage on Banana Pi M3