mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-12-03 15:00:47 +08:00
feat(zephyr): fly a quadcopter with SIH in QEMU
Add the sih label of the generic Zephyr board, which runs the multicopter flight stack with the simulator in hardware (SIH) and MAVLink, as airframe 1100 does on a flight controller. The startup script of a label is init/rc.<label>.px4, if it exists.
The application builds it with the sih file suffix:
west build -b mps2/an386 PX4-Autopilot/platforms/zephyr/app -t run -- -DFILE_SUFFIX=sih
or "make zephyr_sih". QEMU emulates a 250 MHz CPU, and MAVLink is sent to the host with QEMU user networking, for QGroundControl to connect. A twister test takes off, hovers and lands from the Zephyr shell.
The Zephyr build fetches the MAVLink submodule. The generic board has no SD card, so the SD card arming check is left out.
Assisted-by: Claude:opus-5.5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
This commit is contained in:
1 parent
240898b79d
commit
2a8f9b294f
10 files changed
+205
-1
No files matched your search
@@ -270,7 +270,7 @@ updateconfig:
|
||||
|
||||
# Zephyr targets, built with west in a Zephyr workspace (see platforms/zephyr)
|
||||
# --------------------------------------------------------------------
|
||||
.PHONY: zephyr zephyr_qemu zephyr_tests
|
||||
.PHONY: zephyr zephyr_qemu zephyr_sih zephyr_tests
|
||||
|
||||
ZEPHYR_BOARD ?= mps2/an386
|
||||
ZEPHYR_BUILD_DIR = $(SRC_DIR)/build/zephyr_$(subst /,_,$(ZEPHYR_BOARD))
|
||||
@@ -281,6 +281,9 @@ zephyr:
|
||||
zephyr_qemu:
|
||||
@west build -b mps2/an386 -d "$(SRC_DIR)/build/zephyr_mps2_an386" "$(SRC_DIR)/platforms/zephyr/app" -t run
|
||||
|
||||
zephyr_sih:
|
||||
@west build -b mps2/an386 -d "$(SRC_DIR)/build/zephyr_mps2_an386_sih" "$(SRC_DIR)/platforms/zephyr/app" -t run -- -DFILE_SUFFIX=sih
|
||||
|
||||
zephyr_tests:
|
||||
@west twister -T "$(SRC_DIR)/platforms/zephyr" -p mps2/an386 -O "$(SRC_DIR)/build/zephyr_twister"
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
CONFIG_PLATFORM_ZEPHYR=y
|
||||
CONFIG_BOARD_NO_SDCARD=y
|
||||
CONFIG_BOARD_ROMFSROOT=""
|
||||
CONFIG_SYSTEMCMDS_PARAM=y
|
||||
CONFIG_SYSTEMCMDS_PERF=y
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
# Quadcopter flown by the simulator in hardware (SIH), as airframe 1100
|
||||
param set-default MAV_TYPE 2
|
||||
param set-default IMU_GYRO_RATEMAX 800
|
||||
param set-default RTL_RETURN_ALT 30
|
||||
param set-default RTL_DESCEND_ALT 10
|
||||
param set-default EKF2_RNG_FOG 1
|
||||
param set-default SYS_HITL 2
|
||||
param set-default SIH_VEHICLE_TYPE 0
|
||||
param set-default CA_ROTOR_COUNT 4
|
||||
param set-default CA_ROTOR0_PX 1
|
||||
param set-default CA_ROTOR0_PY 1
|
||||
param set-default CA_ROTOR1_PX -1
|
||||
param set-default CA_ROTOR1_PY -1
|
||||
param set-default CA_ROTOR2_PX 1
|
||||
param set-default CA_ROTOR2_PY -1
|
||||
param set-default CA_ROTOR2_KM -0.05
|
||||
param set-default CA_ROTOR3_PX -1
|
||||
param set-default CA_ROTOR3_PY 1
|
||||
param set-default CA_ROTOR3_KM -0.05
|
||||
param set-default HIL_ACT_FUNC1 101
|
||||
param set-default HIL_ACT_FUNC2 102
|
||||
param set-default HIL_ACT_FUNC3 103
|
||||
param set-default HIL_ACT_FUNC4 104
|
||||
param set-default CBRK_SUPPLY_CHK 894281
|
||||
param set-default COM_RC_IN_MODE 4
|
||||
ver all
|
||||
dataman start -r
|
||||
battery_simulator start
|
||||
rc_update start
|
||||
manual_control start
|
||||
sensors start -h
|
||||
simulator_sih start
|
||||
sensor_baro_sim start
|
||||
sensor_mag_sim start
|
||||
sensor_gps_sim start
|
||||
commander start -h
|
||||
pwm_out_sim start -m hil
|
||||
ekf2 start
|
||||
control_allocator start
|
||||
mc_rate_control start
|
||||
mc_att_control start
|
||||
mc_hover_thrust_estimator start
|
||||
flight_mode_manager start
|
||||
mc_pos_control start
|
||||
land_detector start multicopter
|
||||
navigator start
|
||||
# ground station on the QEMU host
|
||||
mavlink start -u 14556 -o 14550 -t 10.0.2.2 -r 400000
|
||||
mavlink boot_complete
|
||||
@@ -0,0 +1,17 @@
|
||||
CONFIG_MODULES_COMMANDER=y
|
||||
CONFIG_MODULES_CONTROL_ALLOCATOR=y
|
||||
CONFIG_MODULES_DATAMAN=y
|
||||
CONFIG_MODULES_EKF2=y
|
||||
CONFIG_MODULES_FLIGHT_MODE_MANAGER=y
|
||||
CONFIG_MODULES_LAND_DETECTOR=y
|
||||
CONFIG_MODULES_MANUAL_CONTROL=y
|
||||
CONFIG_MODULES_MC_ATT_CONTROL=y
|
||||
CONFIG_MODULES_MC_HOVER_THRUST_ESTIMATOR=y
|
||||
CONFIG_MODULES_MC_POS_CONTROL=y
|
||||
CONFIG_MODULES_MAVLINK=y
|
||||
CONFIG_MODULES_MC_RATE_CONTROL=y
|
||||
CONFIG_MODULES_NAVIGATOR=y
|
||||
CONFIG_MODULES_RC_UPDATE=y
|
||||
CONFIG_MODULES_SENSORS=y
|
||||
CONFIG_MODULES_SIMULATION_BATTERY_SIMULATOR=y
|
||||
CONFIG_MODULES_SIMULATION_SIMULATOR_SIH=y
|
||||
@@ -47,6 +47,9 @@ foreach(module ${module_libraries})
|
||||
endforeach()
|
||||
|
||||
set(startup_script ${PX4_BOARD_DIR}/init/rc.px4)
|
||||
if(EXISTS ${PX4_BOARD_DIR}/init/rc.${PX4_BOARD_LABEL}.px4)
|
||||
set(startup_script ${PX4_BOARD_DIR}/init/rc.${PX4_BOARD_LABEL}.px4)
|
||||
endif()
|
||||
set(startup_script_string)
|
||||
if(EXISTS ${startup_script})
|
||||
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${startup_script})
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
# a 250 MHz CPU, the speed of a flight controller
|
||||
CONFIG_QEMU_ICOUNT=y
|
||||
CONFIG_QEMU_ICOUNT_SHIFT=2
|
||||
|
||||
CONFIG_TEST_RANDOM_GENERATOR=y
|
||||
|
||||
# QEMU user networking, where the host is 10.0.2.2
|
||||
CONFIG_NET_L2_ETHERNET=y
|
||||
CONFIG_NET_QEMU_USER=y
|
||||
CONFIG_NET_CONFIG_SETTINGS=y
|
||||
CONFIG_NET_CONFIG_MY_IPV4_ADDR="10.0.2.15"
|
||||
CONFIG_NET_CONFIG_MY_IPV4_NETMASK="255.255.255.0"
|
||||
CONFIG_NET_CONFIG_MY_IPV4_GW="10.0.2.2"
|
||||
@@ -0,0 +1,14 @@
|
||||
CONFIG_PX4=y
|
||||
CONFIG_SHELL=y
|
||||
CONFIG_LOG=y
|
||||
CONFIG_PX4_BOARD_CONFIG="zephyr_generic_sih"
|
||||
CONFIG_HEAP_MEM_POOL_SIZE=1048576
|
||||
|
||||
CONFIG_NETWORKING=y
|
||||
CONFIG_ETH_DRIVER=y
|
||||
CONFIG_NET_IPV4=y
|
||||
CONFIG_NET_IPV6=n
|
||||
CONFIG_NET_UDP=y
|
||||
CONFIG_NET_SOCKETS=y
|
||||
CONFIG_NET_CONTEXT_SNDTIMEO=y
|
||||
CONFIG_XSI_STREAMS=y
|
||||
@@ -0,0 +1,91 @@
|
||||
############################################################################
|
||||
#
|
||||
# Copyright (c) 2026 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
"""
|
||||
Fly the SIH quadcopter from the Zephyr shell: take off, hover and land.
|
||||
"""
|
||||
|
||||
import re
|
||||
import time
|
||||
|
||||
from twister_harness import Shell
|
||||
|
||||
|
||||
def px4(shell: Shell, command: str) -> list[str]:
|
||||
try:
|
||||
return shell.exec_command(f'px4 {command}')
|
||||
except AssertionError:
|
||||
# a PX4 message was printed over the echo of the command
|
||||
return []
|
||||
|
||||
|
||||
def listener_value(shell: Shell, topic: str, field: str) -> float | None:
|
||||
for line in px4(shell, f'listener {topic}'):
|
||||
# a log message printed in the middle of a line leaves it without a value
|
||||
match = re.match(rf'\s+{field}: (-?[0-9.]+(?:e[-+]?[0-9]+)?)\s*$', line)
|
||||
|
||||
if match:
|
||||
return float(match.group(1))
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def wait_until(condition, timeout: float, what: str):
|
||||
end = time.time() + timeout
|
||||
|
||||
while time.time() < end:
|
||||
if condition():
|
||||
return
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
raise AssertionError(f'timeout waiting for {what}')
|
||||
|
||||
|
||||
def commander_output(shell: Shell, command: str) -> str:
|
||||
return '\n'.join(px4(shell, f'commander {command}'))
|
||||
|
||||
|
||||
def test_takeoff_and_land(shell: Shell):
|
||||
wait_until(lambda: 'Preflight check: OK' in commander_output(shell, 'check'), 300, 'the preflight checks')
|
||||
|
||||
commander_output(shell, 'takeoff')
|
||||
|
||||
def climbed():
|
||||
z = listener_value(shell, 'vehicle_local_position', 'z')
|
||||
return z is not None and z < -2.0
|
||||
|
||||
wait_until(climbed, 120, 'the takeoff')
|
||||
|
||||
commander_output(shell, 'land')
|
||||
wait_until(lambda: 'INFO [commander] Disarmed' in commander_output(shell, 'status'), 120, 'the landing')
|
||||
@@ -17,3 +17,15 @@ tests:
|
||||
regex:
|
||||
- "HW arch: ZEPHYR_GENERIC"
|
||||
- "OS: Zephyr"
|
||||
px4.app.sih:
|
||||
platform_allow:
|
||||
- mps2/an386
|
||||
integration_platforms:
|
||||
- mps2/an386
|
||||
extra_args:
|
||||
- FILE_SUFFIX=sih
|
||||
timeout: 600
|
||||
harness: pytest
|
||||
harness_config:
|
||||
pytest_root:
|
||||
- "pytest/test_sih.py"
|
||||
@@ -51,6 +51,7 @@ unset(PX4_CONFIG_FILE CACHE)
|
||||
set(PX4_GIT_SUBMODULES
|
||||
src/lib/events/libevents
|
||||
src/lib/heatshrink/heatshrink
|
||||
src/modules/mavlink/mavlink
|
||||
)
|
||||
|
||||
zephyr_include_directories(include)
|
||||
|
||||
Reference in new issue
Block a user