forked from microsoft/AirSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9998b74
commit 4d4c54e
Showing
14 changed files
with
269 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# to install this run 'pip install mprpc' | ||
from mprpc import RPCClient | ||
import numpy as np | ||
|
||
class AirSimImageType: | ||
Scene = 1 | ||
Depth = 2 | ||
Segmentation = 4 | ||
|
||
class DrivetrainType: | ||
MaxDegreeOfFreedom = 0 | ||
ForwardOnly = 1 | ||
|
||
class YawMode: | ||
is_rate = True | ||
yaw_or_rate = 0.0 | ||
def __init__(self, is_rate, yaw_or_rate): | ||
self.is_rate = is_rate | ||
self.yaw_or_rate = yaw_or_rate | ||
|
||
class AirSimClient: | ||
def __init__(self, ip): | ||
if (ip == ""): | ||
ip = "127.0.0.1" | ||
self.client = RPCClient(ip, 41451) | ||
|
||
# basic flight control | ||
def arm(self): | ||
return self.client.call('armDisarm', True) | ||
|
||
def disarm(self): | ||
return self.client.call('armDisarm', False) | ||
|
||
def takeoff(self): | ||
return self.client.call('takeoff', 10) | ||
|
||
def land(self): | ||
return self.client.call('land') | ||
|
||
def goHome(self): | ||
return self.client.call('goHome') | ||
|
||
def hover(self): | ||
return self.client.call('hover') | ||
|
||
|
||
# query position of drone | ||
def getPosition(self): | ||
return self.client.call('getPosition') | ||
def getVelocity(self): | ||
return self.client.call('getVelocity') | ||
def getOrientation(self): | ||
return self.client.call('getOrientation') | ||
def getGpsLocation(self): | ||
return self.client.call('getGpsLocation') | ||
def getHomePoint(self): | ||
return self.client.call('getHomePoint') | ||
|
||
# special offboard control | ||
def isOffboardMode(self): | ||
return self.client.call('isOffboardMode') | ||
def moveByVelocity(self, vx, vy, vz, duration, drivetrain, yaw_mode): | ||
return self.client.call('moveByVelocity', vx, vy, vz, duration, drivetrain, (yaw_mode.is_rate, yaw_mode.yaw_or_rate)) | ||
def moveByVelocityZ(self, vx, vy, z, duration, drivetrain, yaw_mode): | ||
return self.client.call('moveByVelocityZ', vx, vy, z, duration, drivetrain, (yaw_mode.is_rate, yaw_mode.yaw_or_rate)) | ||
def moveOnPath(self, path, velocity, drivetrain, yaw_mode, lookahead, adaptive_lookahead): | ||
return self.client.call('moveOnPath', path, velocity, drivetrain, (yaw_mode.is_rate, yaw_mode.yaw_or_rate), lookahead, adaptive_lookahead) | ||
def moveToZ(self, z, velocity, yaw_mode, lookahead, adaptive_lookahead): | ||
return self.client.call('moveToZ', z, velocity, (yaw_mode.is_rate, yaw_mode.yaw_or_rate), lookahead, adaptive_lookahead) | ||
def rotateToYaw(self, yaw, margin): | ||
return self.client.call('rotateToYaw', yaw, margin) | ||
def rotateByYawRate(self, yaw_rate, duration): | ||
return self.client.call('rotateByYawRate', yaw_rate, duration) | ||
|
||
# camera control | ||
# image_type uses one of the AirSimImageType members | ||
def setImageTypeForCamera(self, camera_id, image_type): | ||
return self.client.call('setImageTypeForCamera', camera_id, image_type) | ||
def getImageTypeForCamera(self, camera_id): | ||
return self.client.call('getImageTypeForCamera', camera_id) | ||
def getImageForCamera(self, camera_id, image_type): | ||
return np.fromstring(self.client.call('getImageForCamera', camera_id, image_type), np.int8) | ||
|
||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<ProjectGuid>e2049e20-b6dd-474e-8bca-1c8dc54725aa</ProjectGuid> | ||
<ProjectHome>.</ProjectHome> | ||
<StartupFile>camera.py</StartupFile> | ||
<SearchPath> | ||
</SearchPath> | ||
<WorkingDirectory>.</WorkingDirectory> | ||
<OutputPath>.</OutputPath> | ||
<Name>PythonClient</Name> | ||
<RootNamespace>PythonClient</RootNamespace> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="box.py"> | ||
<SubType>Code</SubType> | ||
</Compile> | ||
<Compile Include="camera.py"> | ||
<SubType>Code</SubType> | ||
</Compile> | ||
<Compile Include="path.py"> | ||
<SubType>Code</SubType> | ||
</Compile> | ||
<Compile Include="PythonClient.py" /> | ||
<Compile Include="test.py"> | ||
<SubType>Code</SubType> | ||
</Compile> | ||
</ItemGroup> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> | ||
<PtvsTargetsFile>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets</PtvsTargetsFile> | ||
</PropertyGroup> | ||
<Import Condition="Exists($(PtvsTargetsFile))" Project="$(PtvsTargetsFile)" /> | ||
<Import Condition="!Exists($(PtvsTargetsFile))" Project="$(MSBuildToolsPath)\Microsoft.Common.targets" /> | ||
<!-- Uncomment the CoreCompile target to enable the Build command in | ||
Visual Studio and specify your pre- and post-build commands in | ||
the BeforeBuild and AfterBuild targets below. --> | ||
<!--<Target Name="CoreCompile" />--> | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from PythonClient import * | ||
import sys | ||
import time | ||
|
||
client = AirSimClient('127.0.0.1') | ||
|
||
|
||
print("Flying a small square box using moveByVelocityZ") | ||
client.moveByVelocityZ(1,0,-5,5, DrivetrainType.MaxDegreeOfFreedom, YawMode(False, 0)) | ||
time.sleep(2) | ||
client.moveByVelocityZ(0,1,-5,5, DrivetrainType.MaxDegreeOfFreedom, YawMode(False, 90)) | ||
time.sleep(2) | ||
client.moveByVelocityZ(-1,0,-5,5, DrivetrainType.MaxDegreeOfFreedom, YawMode(False, 180)) | ||
time.sleep(2) | ||
client.moveByVelocityZ(0,-1,-5,5, DrivetrainType.MaxDegreeOfFreedom, YawMode(False, 270)) | ||
time.sleep(2) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# use open cv to show new images from AirSim | ||
|
||
from PythonClient import * | ||
import cv2 | ||
import time | ||
|
||
client = AirSimClient('127.0.0.1') | ||
|
||
# you must first press "1" in the AirSim view to turn on the depth capture | ||
|
||
# get depth image | ||
client.setImageTypeForCamera(0, AirSimImageType.Depth) | ||
time.sleep(1) # give it time to render | ||
|
||
while True: | ||
rawImage = client.getImageForCamera(0, AirSimImageType.Depth) | ||
png = cv2.imdecode(rawImage, cv2.IMREAD_UNCHANGED) | ||
cv2.imshow("Traffic", png) | ||
|
||
key = cv2.waitKey(1) & 0xFF; | ||
if (key == 27 or key == ord('q') or key == ord('x')): | ||
break; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from PythonClient import * | ||
import sys | ||
import time | ||
|
||
client = AirSimClient('127.0.0.1') | ||
|
||
# see https://github.com/Microsoft/AirSim/wiki/moveOnPath-demo | ||
|
||
client.moveOnPath([(0,-253,-5),(125,-253,-5),(125,0,-5),(0,0,-5)], 12, DrivetrainType.ForwardOnly, YawMode(False,0), 15, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from PythonClient import * | ||
import sys | ||
import time | ||
|
||
client = AirSimClient('127.0.0.1') | ||
|
||
print("Waiting for home GPS location to be set...") | ||
home = client.getHomePoint() | ||
while (home[0] == 0 and home[1] == 0 and home[2] == 0): | ||
time.sleep(1) | ||
home = client.getHomePoint() | ||
|
||
print("Home lat=%g, lon=%g, alt=%g" % home) | ||
|
||
if (not client.arm()): | ||
print("failed to arm the drone") | ||
sys.exit(1); | ||
|
||
if (not client.takeoff()): | ||
print("failed to takeoff") | ||
sys.exit(1); | ||
|
||
print("Should now be flying...") | ||
time.sleep(5) | ||
|
||
if (not client.land()): | ||
print("failed to land") | ||
sys.exit(1); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
## Using Python with AirSim | ||
|
||
You can call AirSim from Python to control the drone and get images back. | ||
|
||
First install the following Python package: | ||
|
||
```` | ||
pip install mprpc | ||
```` | ||
|
||
Now you can run the samples in the Python folder. For example `path.py` runs my [moveOnPath demo](https://github.com/Microsoft/AirSim/wiki/moveOnPath-demo) | ||
and `camera.py` captures the depth camera and presents it in an opencv window. | ||
|
||
## Windows | ||
|
||
On windows you may need to install the Microsoft Visual C++ 9.0 from [http://aka.ms/vcpython27](http://aka.ms/vcpython27). | ||
|