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,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%"

Binary file not shown.

View File

@@ -0,0 +1,197 @@
# import required libraries
import time
import numpy as np
import cv2
# import RflySim APIs
import PX4MavCtrlV4 as PX4MavCtrl
import ScreenCapApiV4 as sca
import UE4CtrlAPI
ue = UE4CtrlAPI.UE4CtrlAPI()
# Function to calculate the location and radius of red ball
def calc_centroid(img):
"""Get the centroid and area of Red in the image"""
low_range = np.array([0,0,80])
high_range = np.array([100,100,255])
th = cv2.inRange(img, low_range, high_range)
dilated = cv2.dilate(th, cv2.getStructuringElement(
cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
cv2.imshow("dilated", dilated)
cv2.waitKey(1)
M = cv2.moments(dilated, binaryImage=True)
if M["m00"] >= min_prop*width*height:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
return [cx, cy, M["m00"]]
else:
return [-1, -1, -1]
# Function to obtain velocity commands for Pixhawk
# according to the image processing results
def controller(p_i):
# if the object is not in the image, search in clockwise
if p_i[0] < 0 or p_i[1] < 0:
return [0, 0, 0, 1]
# found
ex = p_i[0] - width / 2
ey = p_i[1] - height / 2
vx = 2 if p_i[2] < max_prop*width*height else 0
vy = 0
vz = K_z * ey
yawrate = K_yawrate * ex
# return forward, rightward, downward, and rightward-yaw
# velocity control sigals
return [vx, vy, vz, yawrate]
# Process image to obtain vehicle velocity control command
def procssImage():
img_bgr=sca.getCVImg(ImgInfo1)
p_i = calc_centroid(img_bgr)
ctrl = controller(p_i)
return ctrl
# saturation function to limit the maximum velocity
def sat(inPwm,thres=1):
outPwm= inPwm
for i in range(len(inPwm)):
if inPwm[i]>thres:
outPwm[i] = thres
elif inPwm[i]<-thres:
outPwm[i] = -thres
return outPwm
# Create MAVLink control API instance
mav = PX4MavCtrl.PX4MavCtrler(1)
# Init MAVLink data receiving loop
mav.InitMavLoop()
isEmptyData = False
lastTime = time.time()
startTime = time.time()
# time interval of the timer
timeInterval = 1/30.0 #here is 0.0333s (30Hz)
flag = 0
# parameters
width = 720
height = 405
channel = 4
min_prop = 0.000001
max_prop = 0.3
K_z = 0.003 * 640 / height
K_yawrate = 0.005 * 480 / width
# send command to all RflySim3D windows to switch to the blank grass scene
# by default, there are two windows, the first is front camera
# for vison control, and the second window for observation
ue.sendUE4Cmd('RflyChangeMapbyName VisionRingBlank')
time.sleep(1)
# create a ball, set its position and altitude, use the default red color
ue.sendUE4Pos(100,152,0,[3,0,-2],[0,0,0])
time.sleep(0.5)
# Change the target vehicle to copterID=1's vehicle
ue.sendUE4Cmd('RflyChangeViewKeyCmd B 1',0)
time.sleep(0.5)
# Switch its viewpoint to oboard #1 (front camera view)
ue.sendUE4Cmd('RflyChangeViewKeyCmd V 1',0)
time.sleep(0.5)
# move the camera to the position [0.3,0,0.05] related to the body
ue.sendUE4Cmd('RflyCameraPosAng 0.3 0 0.05 0 0 0',0)
time.sleep(0.5)
# set the RflySim3D window size to 720x405
# Send command to UE4 Window 1 to change resolution
ue.sendUE4Cmd('r.setres 720x405w',1)
time.sleep(2)
# Send command to UE4 Window 1 to change resolution
ue.sendUE4Cmd('r.setres 720x405w',0)
time.sleep(2)
# Get handles of all UE4/RflySim3D windows
window_hwnds = sca.getWndHandls()
try:
# Move UE4 windows 0 to desired position and keep top
xx,yy=sca.moveWd(window_hwnds[0],0,0,True)
time.sleep(0.5)
# Move UE4 windows 0 to desired position (next to windows 0) and keep top
xx,yy=sca.moveWd(window_hwnds[1],xx,0,False)
except Exception as e :
print('Please move the window manually')
xx=720
yy=405
# Get the info of the first window (index=0)
ImgInfo1 = sca.getHwndInfo(window_hwnds[0])
num=0
lastClock=time.time()
# Start a endless loop with 30Hz, timeInterval=1/30.0
ctrlLast = [0,0,0,0]
while True:
lastTime = lastTime + timeInterval
sleepTime = lastTime - time.time()
if sleepTime > 0:
time.sleep(sleepTime) # sleep until the desired clock
else:
lastTime = time.time()
# The following code will be executed 30Hz (0.0333s)
num=num+1
if num%100==0:
tiem=time.time()
print('MainThreadFPS: '+str(100/(tiem-lastClock)))
lastClock=tiem
if time.time() - startTime > 5 and flag == 0:
# The following code will be executed at 5s
print("5s, Arm the drone")
mav.initOffboard()
flag = 1
mav.SendMavArm(True) # Arm the drone
print("Arm the drone!, and fly to NED 0,0,-5")
mav.SendPosNED(0, 0, -5, 0) # Fly to target position [0, 0, -5], i.e., take off to 5m
if time.time() - startTime > 15 and flag == 1:
flag = 2
# The following code will be executed at 15s
mav.SendPosNED(-30,-5, -5, 0) # Fly to target position [-30,-5, -5]
print("15s, fly to pos: -30,-5, -5")
if time.time() - startTime > 25 and flag == 2:
flag = 3
# Show CV image and set the position
img_bgr=sca.getCVImg(ImgInfo1)
cv2.imshow("dilated", img_bgr)
cv2.waitKey(1)
time.sleep(0.5)
cv2.moveWindow("dilated",0,yy)
print("25s, start to shoot the ball.")
if time.time() - startTime > 25 and flag == 3:
ctrlNow = procssImage()
ctrl = sat(ctrlNow,5)
# add a inertial component here to restrain the speed variation rate
if ctrl[0]-ctrlLast[0] > 0.5:
ctrl[0]=ctrlLast[0]+0.05
elif ctrl[0]-ctrlLast[0] < -0.5:
ctrl[0]=ctrlLast[0]-0.05
if ctrl[1]-ctrlLast[1] > 0.5:
ctrl[1]=ctrlLast[1]+0.05
elif ctrl[1]-ctrlLast[1] < -0.5:
ctrl[1]=ctrlLast[1]-0.05
ctrlLast = ctrl
# if control signals is obtained, send to Pixhawk
if not isEmptyData:
mav.SendVelFRD(ctrl[0], ctrl[1], ctrl[2], ctrl[3])

View File

@@ -0,0 +1,178 @@
@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., 0:PX4_HITL, 1:PX4_SITL, 2:PX4_SITL_RFLY, 3:"Simulink&DLL_SIL", 4:PX4_HITL_NET, 5:EXT_HITL_COM, 6:EXT_SIM_NET
set SimMode=0
REM SET the baudrate for Pixhawk COM
SET /a BaudRate=921600
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
)
ECHO.
ECHO ---------------------------------------
REM Get the Com port number
for /f "delims=" %%t in ('%PSP_PATH%\CopterSim\GetComList.exe 3 %BaudRate%') do set ComInfoStr=%%t
for /f "tokens=1,2,3 delims=#" %%a in ("%ComInfoStr%") do (
set ComNumExe=%%a
set ComNameList=%%b
set ComInfoList=%%c
)
echo Please input the Pixhawk COM port list for HIL
echo Use ',' as the separator if more than one Pixhawk
echo E.g., input 3 for COM3 of Pixhawk on the computer
echo Input 3,6,7 for COM3, COM6 and COM7 of Pixhawks
ECHO ---------------------------------------
set remain=%ComInfoList%
echo All COM ports on this computer are:
echo.
:loopInfo
for /f "tokens=1* delims=;" %%a in ("%remain%") do (
echo %%a
set remain=%%b
)
if defined remain goto :loopInfo
echo.
ECHO ---------------------------------------
if %ComNumExe% EQU 0 (
echo Warning: there is no available COM port
) else (
echo Recommended COM list input is: %ComNameList%
)
ECHO.
ECHO ---------------------------------------
SET /P ComNum=My COM list for HITL simulation is:
SET string=%ComNum%
set subStr = ""
set /a VehicleNum=0
:split
for /f "tokens=1,* delims=," %%i in ("%string%") do (
set subStr=%%i
set string=%%j
)
set /a eValue=subStr
if not %eValue% EQU %subStr% (
echo Error: Input '%subStr%' is not a integer!
goto EOF
)
set /a VehicleNum = VehicleNum +1
if not "%string%"=="" goto split
REM cho total com number is %VehicleNum%
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 UE4Path
cd /d %PSP_PATH%\RflySim3D
tasklist|find /i "RflySim3D.exe" || start %PSP_PATH%\RflySim3D\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
SET string=%ComNum%
:split1
for /f "tokens=1,* delims=," %%i in ("%string%") do (
set subStr=%%i
set string=%%j
)
set /a PosXX=((%cntr%-1) / %sqrtNum%)*%VEHICLE_INTERVAL% + %ORIGIN_POS_X%
set /a PosYY=((%cntr%-1) %% %sqrtNum%)*%VEHICLE_INTERVAL% + %ORIGIN_POS_Y%
REM echo start CopterSim
start /realtime CopterSim.exe 1 %cntr% %CLASS_3D_ID% %DLLModel% %SimMode% %UE4_MAP% %IS_BROADCAST% %PosXX% %PosYY% %ORIGIN_YAW% %subStr%:%BaudRate% %UDPSIMMODE%
choice /t 2 /d y /n >nul
set /a cntr=%cntr%+1
REM TIMEOUT /T 1
if not "%string%"=="" goto split1
REM QGCPath
tasklist|find /i "QGroundControl.exe" || start %PSP_PATH%\QGroundControl\QGroundControl.exe -noComPix
ECHO Start QGroundControl
pause
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,163 @@
@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
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,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"