Airsim 提供的这个文件可供初学者进行学习,小洛进行代码调试后,得到以下几部分信息
信息如下(不同的颜色主要用于区分信息):
'''hello drone代码分析'''
import setup_path
import airsim
import numpy as np
import os
import tempfile
import pprint
import cv2
'''连接airsim环境'''
Client=airsim.MultirotorClient()
Client.confirmConnection()
Client.enableApiControl(True)
'''解除之前的操作'''
Client.armDisarm(True)
state=Client.getMultirotorState()
s=pprint.pformat(state)
print("state= ",state)
'''wait_key函数不知怎么不能使用,只能将其注释掉,完成takeoff的操作,后续代码的该函数也注释掉正常运行'''
'''airsim.wait_key('Press any key to takeoff')'''
client.takeoffAsync().join()
'''将UAV以5m/s的速度移动到(-10,10,-10)'''
Client.moveToPositionAsync(-10,10,-10,5).join()
'''悬停'''
Client.hoverAsync().join()
'''查看state变化情况'''
state=Client.getMultirotorState()
print(pprint.pformat(state))
'''可获得位置坐标'''
position=state.kinematics_estimated.position
print(pprint.pformat(position))
通过上述语句可获得当前的位置坐标:
cameraType = "depth"
cameraTypeMap = {
"depth": airsim.ImageType.DepthVis,
"segmentation": airsim.ImageType.Segmentation,
"seg": airsim.ImageType.Segmentation,
"scene": airsim.ImageType.Scene,
"disparity": airsim.ImageType.DisparityNormalized,
"normals": airsim.ImageType.SurfaceNormals
}
rawImage = Client.simGetImage("0", cameraTypeMap[cameraType])
png = cv2.imdecode(airsim.string_to_uint8_array(rawImage), cv2.IMREAD_UNCHANGED)
cv2.imshow("Depth", png)
filepath='image/1.png'
cv2.imwrite(filepath,png)
到这hello_drone.py代码解析结束了,主要学习了如何实现UAV的takeoff、到达指定位置函数、获取当前状态、存储各种类型的图片,希望对大家有帮助!