fix:新增例程

This commit is contained in:
2025-07-25 17:54:28 +08:00
parent 78481c265e
commit 637343d716
108 changed files with 5755 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
{
"VisionSensors":[
{
"SeqID":0, //视觉传感器序号0 1 2 3 ...排序。如果填0则自动递增排序。
"TypeID":1, //视觉传感器类型1:RGB2:深度3:灰度4:分割5:测距7:深度转点云20-23:激光雷达40-41:热力红外(收费版)
"TargetCopter":1, //传感器绑定的CopterID号注:免费版只支持绑定1号飞机
"TargetMountType":0, //绑定方式0:固定飞机几何中心, 1:固定飞机底部中心2:固定地面上3:弱固定飞机上姿态不随动4:绑定其他视觉传感器上
"JointType":1,
"DataWidth":640, //图像像素长度
"DataHeight":480,//图像像素宽度
"DataCheckFreq":30,//图像检查更新频率如果发现UE渲染更新了取决于UE刷新帧率会立刻发出数据。UE刷新率+DataCheckFreq检查频率共同决定图像延迟。
"SendProtocol":[0,0,0,0,0,0,0,0],// SendProtocol[0]表示传输协议0共享内存仅限Windows下获取图像1UDP网络传输模式图片使用jpeg压缩点云直传2UDP直传图片不压缩3UDP直传图片png压缩。注0-1适用所有传感器2-3选项仅限图像类传感器。
//SendProtocol[1-4]位对应IP地址位表示请求返回的IP地址。默认都填0或127.0.0.1会自动请求UE返回图像到本电脑SendProtocol[5]端口位指定传感器图像回传端口需要为每个传感器设置不同端口。默认填0会自动使用9999+SeqID的递增端口号。
"CameraFOV":90, //视觉传感器的FOV视场角和焦距呈现一定数值关系能间接修改焦距。
"EularOrQuat":0, //使用欧拉角SensorAngEular还是四元数SensorAngQuat来设置视觉传感器姿态默认使用0欧拉角。
"SensorPosXYZ":[0.3,0,0], //视觉传感器的安装位置和TargetMountType对应偏移中心单位米
"SensorAngQuat":[0,0,0,0], // 视觉传感器的安装姿态,用四元数方式表示
"SensorAngEular":[0,0,0],// 视觉传感器的安装姿态,用欧拉角方式表示,单位角度制
"otherParams":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] //其他参数
}
]
}

View File

@@ -0,0 +1,5 @@
if not defined PSP_PATH (
SET PSP_PATH=C:\PX4PSP
SET PSP_PATH_LINUX=/mnt/c/PX4PSP
)
start cmd.exe /k "echo Python3.8 environment has been set with openCV+pymavlink+numpy+pyulog etc. && echo You can use pip or pip3 command to install other libraries && echo Put Python38Run.bat into your code folder && echo Use the command: 'python XXX.py' to run the script with Python && SET PATH=%PSP_PATH%\Python38;%PSP_PATH%\Python38\Scripts;%CD%;%PATH%"

View File

@@ -0,0 +1,12 @@
@echo off
REM Set the path of the RflySim tools
if not defined PSP_PATH (
SET PSP_PATH=C:\PX4PSP
SET PSP_PATH_LINUX=/mnt/c/PX4PSP
)
cd /d %PSP_PATH%\VcXsrv
tasklist|find /i "vcxsrv.exe" >nul || Xlaunch.exe -run config1.xlaunch
cd /d %~dp0
wsl -d RflySim-20.04

View File

@@ -0,0 +1,20 @@
@echo off
REM Set the path of the RflySim tools
if not defined PSP_PATH (
SET PSP_PATH=C:\PX4PSP
SET PSP_PATH_LINUX=/mnt/c/PX4PSP
)
tasklist|find /i "vcxsrv.exe" >nul && taskkill /f /IM "vcxsrv.exe" >nul
tasklist|find /i "vcxsrv.exe" >nul && taskkill /f /IM "vcxsrv.exe" >nul
cd /d %PSP_PATH%\VcXsrv
Xlaunch.exe -run config.xlaunch
choice /t 1 /d y /n >nul
cd /d %~dp0
wsl -d RflySim-20.04 ~/StartUI.sh
choice /t 1 /d y /n >nul
tasklist|find /i "vcxsrv.exe" && taskkill /f /IM "vcxsrv.exe"

View File

@@ -0,0 +1,162 @@
@ECHO OFF
REM Run script as administrator
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c ""%~s0"" ::","","runas",1)(window.close)&&exit
REM The text start with 'REM' is annotation, the following options are corresponding to Options on CopterSim
REM Start index of vehicle number (should larger than 0)
REM This option is useful for simulation with multi-computers
SET /a START_INDEX=1
REM Set the vehicleType/ClassID of vehicle 3D display in RflySim3D
SET /a CLASS_3D_ID=-1
REM Set use DLL model name or not, use number index or name string
REM This option is useful for simulation with other types of vehicles instead of multicopters
set DLLModel=0
REM Set the simulation mode on CopterSim, use number index or name string
REM e.g., SimMode=2 equals to SimMode=PX4_SITL_RFLY
set SimMode=2
REM Set the vehicle-model (airframe) of PX4 SITL simulation, the default airframe is a quadcopter: iris
REM Check folder Firmware\ROMFS\px4fmu_common\init.d-posix for supported airframes (Note: You can also create your airframe file here)
REM E.g., fixed-wing aircraft: PX4SitlFrame=plane; small cars: PX4SitlFrame=rover
set PX4SitlFrame=iris
REM Set the map, use index or name of the map on CopterSim
REM e.g., UE4_MAP=1 equals to UE4_MAP=Grasslands
SET UE4_MAP=VisionRingBlank
REM Set the origin x,y position (m) and yaw angle (degree) at the map
SET /a ORIGIN_POS_X=0
SET /a ORIGIN_POS_Y=0
SET /a ORIGIN_YAW=0
REM Set the interval between two vehicle, unit:m
SET /a VEHICLE_INTERVAL=2
REM Set broadcast to other computer; 0: only this computer, 1: broadcast; or use IP address to increase speed
REM e.g., IS_BROADCAST=0 equals to IS_BROADCAST=127.0.0.1, IS_BROADCAST=1 equals to IS_BROADCAST=255.255.255.255
SET IS_BROADCAST=0
REM Set UDP data mode; 0: UDP_FULL, 1:UDP_Simple, 2: Mavlink_Full, 3: Mavlink_simple. input number or string
REM 4:Mavlink_NoSend, 5:Mavlink_NoGPS, 6:Mavlink_Vision (NoGPS and set PX4 EKF)
SET UDPSIMMODE=2
REM Set the path of the RflySim tools
if not defined PSP_PATH (
SET PSP_PATH=C:\PX4PSP
SET PSP_PATH_LINUX=/mnt/c/PX4PSP
)
:Top
ECHO.
ECHO ---------------------------------------
REM Max vehicle number 50
SET /a MAX_VEHICLE=50
REM SET /P VehicleNum=Please input UAV swarm number:
SET /A VehicleNum=1
SET /A Evaluated=VehicleNum
if %Evaluated% EQU %VehicleNum% (
IF %VehicleNum% GTR 0 (
IF %VehicleNum% GTR %MAX_VEHICLE% (
ECHO The vehicle number is too large, which may cause a crash
pause
)
GOTO StartSim
)
ECHO Not a positive integer
GOTO Top
) ELSE (
ECHO Not a integer
GOTO Top
)
:StartSim
SET /A VehicleTotalNum=%VehicleNum% + %START_INDEX% - 1
if not defined TOTOAL_COPTER (
SET /A TOTOAL_COPTER=%VehicleTotalNum%
)
set /a sqrtNum=1
set /a squareNum=1
:loopSqrt
set /a squareNum=%sqrtNum% * %sqrtNum%
if %squareNum% EQU %TOTOAL_COPTER% (
goto loopSqrtEnd
)
if %squareNum% GTR %TOTOAL_COPTER% (
goto loopSqrtEnd
)
set /a sqrtNum=%sqrtNum%+1
goto loopSqrt
:loopSqrtEnd
REM QGCPath
tasklist|find /i "QGroundControl.exe" || start %PSP_PATH%\QGroundControl\QGroundControl.exe -noComPix
ECHO Start QGroundControl
REM UE4Path
cd /d %PSP_PATH%\RflySim3D
tasklist|find /i "RflySim3D.exe" || start %PSP_PATH%\Rflysim3D\RflySim3D.exe
choice /t 5 /d y /n >nul
tasklist|find /i "CopterSim.exe" && taskkill /im "CopterSim.exe"
ECHO Kill all CopterSims
REM CptSmPath
cd /d %PSP_PATH%\CopterSim
set /a cntr=%START_INDEX%
set /a endNum=%VehicleTotalNum%+1
:loopBegin
set /a PosXX=((%cntr%-1) / %sqrtNum%)*%VEHICLE_INTERVAL% + %ORIGIN_POS_X%
set /a PosYY=((%cntr%-1) %% %sqrtNum%)*%VEHICLE_INTERVAL% + %ORIGIN_POS_Y%
start /realtime CopterSim.exe 1 %cntr% %CLASS_3D_ID% %DLLModel% %SimMode% %UE4_MAP% %IS_BROADCAST% %PosXX% %PosYY% %ORIGIN_YAW% 1 %UDPSIMMODE%
choice /t 1 /d y /n >nul
set /a cntr=%cntr%+1
if %cntr% EQU %endNum% goto loopEnd
goto loopBegin
:loopEnd
REM Set ToolChainType 1:Win10WSL 3:Cygwin
SET /a ToolChainType=1
if "%IS_BROADCAST%" == "0" (
SET IS_BROADCAST=0
) else (
SET IS_BROADCAST=1
)
SET WINDOWSPATH=%PATH%
if %ToolChainType% EQU 1 (
wsl -d RflySim-20.04 echo Starting PX4 Build; cd %PSP_PATH_LINUX%/Firmware; ./BkFile/EnvOri.sh; export PATH=$HOME/ninja:$HOME/gcc-arm-none-eabi-7-2017-q4-major/bin:$PATH;make px4_sitl_default; ./Tools/sitl_multiple_run_rfly.sh %VehicleNum% %START_INDEX% %PX4SitlFrame%;echo Press any key to exit; read -n 1
) else (
REM CYGPath
cd /d %PSP_PATH%\CygwinToolchain
CALL setPX4Env.bat
bash -l -i -c 'echo Starting SITL SIMULATION; cd %PSP_PATH_LINUX%/Firmware; ./BkFile/EnvOri.sh; pwd; make px4_sitl_default; ./Tools/sitl_multiple_run_rfly.sh %VehicleNum% %START_INDEX% %PX4SitlFrame%;echo Press any key to exit; read -n 1; pkill -x px4 || true;'
)
SET PATH=%WINDOWSPATH%
REM kill all applications when press a key
tasklist|find /i "CopterSim.exe" && taskkill /im "CopterSim.exe"
tasklist|find /i "QGroundControl.exe" && taskkill /f /im "QGroundControl.exe"
tasklist|find /i "RflySim3D.exe" && taskkill /f /im "RflySim3D.exe"
tasklist|find /i "python.exe" && taskkill /f /im "python.exe"
tasklist|find /i "cmd.exe" && taskkill /f /im "cmd.exe"
ECHO Start End.

View File

@@ -0,0 +1,199 @@
"""
视觉跟随圆移动控制飞机飞行。
圆形案板移动有前后,左右,上下移动
"""
import cv2
import threading
import time
import VisionCaptureApi
import PX4MavCtrlV4 as PX4MavCtrl
import sys
import numpy as np
import keyboard
import UE4CtrlAPI
ue = UE4CtrlAPI.UE4CtrlAPI()
"""
初始化飞机,起飞,创建圆形目标
"""
vis = VisionCaptureApi.VisionCaptureApi()
# ue.sendUE4Cmd(b"r.setres 720x405w", 0)
ue.sendUE4Cmd(b"t.MaxFPS 30", 0)
ue.sendUE4PosNew(3, vehicleType=809, PosE=[2, 0, -2])
vis.jsonLoad()
time.sleep(1)
ue.sendUE4ExtAct(3, ActExt=[1, 0.8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0])
isSuss = vis.sendReqToUE4() # 向RflySim3D发送取图请求并验证
if not isSuss: # 如果请求取图失败,则退出
sys.exit(0)
vis.startImgCap() # 开启取图,并启用共享内存图像转发,转发到填写的目录
time.sleep(2)
mav = PX4MavCtrl.PX4MavCtrler()
mav.InitMavLoop()
time.sleep(2)
mav.initOffboard()
time.sleep(2)
mav.SendMavArm(True)
mav.SendPosNED(0, 0, -2, 0)
time.sleep(10)
detect_flag = False
"""
开辟线线程,控制圆形目标移动
"""
Circle_pose = [2, 0, -2, 0]
def MoveCircle(val):
"""
圆板目标移动没有旋转,只有平移,且始终朝向飞机
"""
global vis
global detect_flag
global CalCirle_pose
offset = 0.01 # 每次移动的距离
yaw_offset = np.deg2rad(1)
drone_pose = ue.getUE4Pos()
# if not detect_flag:
# time.sleep(0.001)
# continue
if val == "right": # 如果按键按下这个值为1
# 向右移动
Circle_pose[1] += offset
if val == "left":
# 向左移动
Circle_pose[1] -= offset
if val == "up":
# 向上移动
Circle_pose[2] -= offset
if val == "down":
# 向下移动
Circle_pose[2] += offset
if val == "front":
# 向前移动
Circle_pose[0] += offset
if val == "back":
# 向后移动
Circle_pose[0] -= offset
ue.sendUE4PosNew(
3,
vehicleType=809,
PosE=[Circle_pose[0], Circle_pose[1], Circle_pose[2]],
AngEuler=[0, 0, Circle_pose[3]],
)
# move_circle_th = threading.Thread(target=MoveCircle)
# move_circle_th.start()
"""
开辟线程,获取图像,并控制飞机移动
"""
def CalCirle(img: np.array) -> tuple():
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 43, 46])
high_hsv = np.array([10, 255, 255])
mask1 = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)
lower2 = np.array([156, 43, 46])
upper2 = np.array([180, 255, 255])
mask2 = cv2.inRange(hsv, lower2, upper2)
mask = mask1 + mask2
nonzero = np.nonzero(mask)
if np.size(nonzero) == 0:
return [-1, -1], [-1, -1], [-1, -1]
row_min = np.min(nonzero[0])
row_max = np.max(nonzero[0])
col_min = np.min(nonzero[1])
col_max = np.max(nonzero[1])
mid_row = int((row_max + row_min) / 2)
mid_col = int((col_max + col_min) / 2)
cv2.line(
img,
(col_min, mid_row),
(col_max, mid_row),
(255, 255, 255),
1,
)
cv2.line(
img,
(mid_col, row_min),
(mid_col, row_max),
(255, 255, 255),
1,
)
cv2.imshow("img ", img)
cv2.waitKey(1)
# cv2.imshow("test", mask1 + mask2)
# cv2.waitKey(1)
return [col_min, col_max], [row_min, row_max], [mid_col, mid_row]
def CtrlDrone():
global vis
global mav
global detect_flag
img = None
init_dist_x = 0
init_dist_y = 0
img_cnt_x = 320
img_cnt_y = 240
tolerance_cnt = 15
tolerance_y = 15
tolerance_x = 20
kx = 0.015
ky = 0.009
kz = 0.005
while True:
if vis.hasData[0]:
img = vis.Img[0]
vis.hasData[0] = False
# cv2.imshow("img", vis.Img[0])
cv2.waitKey(1)
[x_min, x_max], [y_min, y_max], [cnt_x, cnt_y] = CalCirle(img)
if x_min == -1:
time.sleep(0.001)
continue
if not detect_flag:
detect_flag = True
dist_x = x_max - x_min
dist_y = y_max - y_min
if init_dist_x == 0:
init_dist_x = dist_x
if init_dist_y == 0:
init_dist_y = dist_y
vx = 0
vy = 0
vz = 0
yaw_rate = 0
if abs(cnt_x - img_cnt_x) > tolerance_cnt:
vy = ky * (cnt_x - img_cnt_x)
if abs(cnt_y - img_cnt_y) > tolerance_cnt:
vz = kz * (cnt_y - img_cnt_y)
if abs(dist_y - init_dist_y) > tolerance_y:
vx = kx * (init_dist_y - dist_y)
mav.SendVelFRD(vx, vy, vz, 0)
time.sleep(0.001)
ctrl_drone_th = threading.Thread(target=CtrlDrone)
ctrl_drone_th.start()
keyboard.add_hotkey("ctrl+up", MoveCircle, args=("front",))
keyboard.add_hotkey("ctrl+down", MoveCircle, args=("back",))
keyboard.add_hotkey("up", MoveCircle, args=("up",))
keyboard.add_hotkey("down", MoveCircle, args=("down",))
keyboard.add_hotkey("right", MoveCircle, args=("right",))
keyboard.add_hotkey("left", MoveCircle, args=("left",))
keyboard.wait()