From 4d4c54ec34f369cd79957f06fffceacf1f7b5e25 Mon Sep 17 00:00:00 2001 From: clovett Date: Wed, 17 May 2017 16:08:43 -0700 Subject: [PATCH] Add some Python samples. --- AirLib/include/controllers/DroneCommon.hpp | 2 +- AirLib/include/rpc/RpcLibClient.hpp | 8 +- .../src/controllers/DroneControllerBase.cpp | 2 +- AirSim.sln | 26 ++++++ DroneShell/src/main.cpp | 2 +- PythonClient/PythonClient.py | 84 ++++++++++++++++++ PythonClient/PythonClient.pyc | Bin 0 -> 5745 bytes PythonClient/PythonClient.pyproj | 53 +++++++++++ PythonClient/box.py | 17 ++++ PythonClient/camera.py | 22 +++++ PythonClient/path.py | 9 ++ PythonClient/test.py | 31 +++++++ docs/px4.md | 4 +- docs/python.md | 17 ++++ 14 files changed, 269 insertions(+), 8 deletions(-) create mode 100644 PythonClient/PythonClient.py create mode 100644 PythonClient/PythonClient.pyc create mode 100644 PythonClient/PythonClient.pyproj create mode 100644 PythonClient/box.py create mode 100644 PythonClient/camera.py create mode 100644 PythonClient/path.py create mode 100644 PythonClient/test.py create mode 100644 docs/python.md diff --git a/AirLib/include/controllers/DroneCommon.hpp b/AirLib/include/controllers/DroneCommon.hpp index e3516fccce..4b152f1527 100644 --- a/AirLib/include/controllers/DroneCommon.hpp +++ b/AirLib/include/controllers/DroneCommon.hpp @@ -8,7 +8,7 @@ namespace msr { namespace airlib { enum class DrivetrainType { - MaxDegreeOfFreedome = 0, + MaxDegreeOfFreedom = 0, ForwardOnly }; diff --git a/AirLib/include/rpc/RpcLibClient.hpp b/AirLib/include/rpc/RpcLibClient.hpp index dfd88450d9..c6f52b0653 100644 --- a/AirLib/include/rpc/RpcLibClient.hpp +++ b/AirLib/include/rpc/RpcLibClient.hpp @@ -28,13 +28,13 @@ class RpcLibClient { bool moveByAngle(float pitch, float roll, float z, float yaw, float duration); bool moveByVelocity(float vx, float vy, float vz, float duration, - DrivetrainType drivetrain = DrivetrainType::MaxDegreeOfFreedome, const YawMode& yaw_mode = YawMode()); + DrivetrainType drivetrain = DrivetrainType::MaxDegreeOfFreedom, const YawMode& yaw_mode = YawMode()); bool moveByVelocityZ(float vx, float vy, float z, float duration, - DrivetrainType drivetrain = DrivetrainType::MaxDegreeOfFreedome, const YawMode& yaw_mode = YawMode()); + DrivetrainType drivetrain = DrivetrainType::MaxDegreeOfFreedom, const YawMode& yaw_mode = YawMode()); bool moveOnPath(const vector& path, float velocity, - DrivetrainType drivetrain = DrivetrainType::MaxDegreeOfFreedome, const YawMode& yaw_mode = YawMode(), float lookahead = -1, float adaptive_lookahead = 1); + DrivetrainType drivetrain = DrivetrainType::MaxDegreeOfFreedom, const YawMode& yaw_mode = YawMode(), float lookahead = -1, float adaptive_lookahead = 1); bool moveToPosition(float x, float y, float z, float velocity, - DrivetrainType drivetrain = DrivetrainType::MaxDegreeOfFreedome, const YawMode& yaw_mode = YawMode(), float lookahead = -1, float adaptive_lookahead = 1); + DrivetrainType drivetrain = DrivetrainType::MaxDegreeOfFreedom, const YawMode& yaw_mode = YawMode(), float lookahead = -1, float adaptive_lookahead = 1); bool moveToZ(float z, float velocity, const YawMode& yaw_mode = YawMode(), float lookahead = -1, float adaptive_lookahead = 1); bool moveByManual(float vx_max, float vy_max, float z_min, DrivetrainType drivetrain, const YawMode& yaw_mode, float duration); diff --git a/AirLib/src/controllers/DroneControllerBase.cpp b/AirLib/src/controllers/DroneControllerBase.cpp index 02460126e7..4115d339ed 100644 --- a/AirLib/src/controllers/DroneControllerBase.cpp +++ b/AirLib/src/controllers/DroneControllerBase.cpp @@ -288,7 +288,7 @@ bool DroneControllerBase::moveToZ(float z, float velocity, const YawMode& yaw_mo { Vector2r cur_xy = getPositionXY(); vector path { Vector3r(cur_xy.x(), cur_xy.y(), z) }; - return moveOnPath(path, velocity, DrivetrainType::MaxDegreeOfFreedome, yaw_mode, lookahead, adaptive_lookahead, + return moveOnPath(path, velocity, DrivetrainType::MaxDegreeOfFreedom, yaw_mode, lookahead, adaptive_lookahead, cancelable_action); } diff --git a/AirSim.sln b/AirSim.sln index c18da16b7b..7223d540df 100644 --- a/AirSim.sln +++ b/AirSim.sln @@ -70,78 +70,104 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SimMode", "SimMode", "{6175 Unreal\Plugins\AirSim\Source\SimMode\SimModeWorldMultiRotor.h = Unreal\Plugins\AirSim\Source\SimMode\SimModeWorldMultiRotor.h EndProjectSection EndProject +Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "PythonClient", "PythonClient\PythonClient.pyproj", "{E2049E20-B6DD-474E-8BCA-1C8DC54725AA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU Debug|x64 = Debug|x64 Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9FE9234B-373A-4D5A-AD6B-FB0B593312DD}.Debug|Any CPU.ActiveCfg = Debug|Win32 {9FE9234B-373A-4D5A-AD6B-FB0B593312DD}.Debug|x64.ActiveCfg = Debug|x64 {9FE9234B-373A-4D5A-AD6B-FB0B593312DD}.Debug|x64.Build.0 = Debug|x64 {9FE9234B-373A-4D5A-AD6B-FB0B593312DD}.Debug|x86.ActiveCfg = Debug|Win32 {9FE9234B-373A-4D5A-AD6B-FB0B593312DD}.Debug|x86.Build.0 = Debug|Win32 + {9FE9234B-373A-4D5A-AD6B-FB0B593312DD}.Release|Any CPU.ActiveCfg = Release|Win32 {9FE9234B-373A-4D5A-AD6B-FB0B593312DD}.Release|x64.ActiveCfg = Release|x64 {9FE9234B-373A-4D5A-AD6B-FB0B593312DD}.Release|x64.Build.0 = Release|x64 {9FE9234B-373A-4D5A-AD6B-FB0B593312DD}.Release|x86.ActiveCfg = Release|Win32 {9FE9234B-373A-4D5A-AD6B-FB0B593312DD}.Release|x86.Build.0 = Release|Win32 + {4BFB7231-077A-4671-BD21-D3ADE3EA36E7}.Debug|Any CPU.ActiveCfg = Debug|Win32 {4BFB7231-077A-4671-BD21-D3ADE3EA36E7}.Debug|x64.ActiveCfg = Debug|x64 {4BFB7231-077A-4671-BD21-D3ADE3EA36E7}.Debug|x64.Build.0 = Debug|x64 {4BFB7231-077A-4671-BD21-D3ADE3EA36E7}.Debug|x86.ActiveCfg = Debug|Win32 {4BFB7231-077A-4671-BD21-D3ADE3EA36E7}.Debug|x86.Build.0 = Debug|Win32 + {4BFB7231-077A-4671-BD21-D3ADE3EA36E7}.Release|Any CPU.ActiveCfg = Release|Win32 {4BFB7231-077A-4671-BD21-D3ADE3EA36E7}.Release|x64.ActiveCfg = Release|x64 {4BFB7231-077A-4671-BD21-D3ADE3EA36E7}.Release|x64.Build.0 = Release|x64 {4BFB7231-077A-4671-BD21-D3ADE3EA36E7}.Release|x86.ActiveCfg = Release|Win32 {4BFB7231-077A-4671-BD21-D3ADE3EA36E7}.Release|x86.Build.0 = Release|Win32 + {98BB426F-6FB5-4754-81BC-BB481900E135}.Debug|Any CPU.ActiveCfg = Debug|Win32 {98BB426F-6FB5-4754-81BC-BB481900E135}.Debug|x64.ActiveCfg = Debug|x64 {98BB426F-6FB5-4754-81BC-BB481900E135}.Debug|x64.Build.0 = Debug|x64 {98BB426F-6FB5-4754-81BC-BB481900E135}.Debug|x86.ActiveCfg = Debug|Win32 {98BB426F-6FB5-4754-81BC-BB481900E135}.Debug|x86.Build.0 = Debug|Win32 + {98BB426F-6FB5-4754-81BC-BB481900E135}.Release|Any CPU.ActiveCfg = Release|Win32 {98BB426F-6FB5-4754-81BC-BB481900E135}.Release|x64.ActiveCfg = Release|x64 {98BB426F-6FB5-4754-81BC-BB481900E135}.Release|x64.Build.0 = Release|x64 {98BB426F-6FB5-4754-81BC-BB481900E135}.Release|x86.ActiveCfg = Release|Win32 {98BB426F-6FB5-4754-81BC-BB481900E135}.Release|x86.Build.0 = Release|Win32 + {8510C7A4-BF63-41D2-94F6-D8731D137A5A}.Debug|Any CPU.ActiveCfg = Debug|Win32 {8510C7A4-BF63-41D2-94F6-D8731D137A5A}.Debug|x64.ActiveCfg = Debug|x64 {8510C7A4-BF63-41D2-94F6-D8731D137A5A}.Debug|x64.Build.0 = Debug|x64 {8510C7A4-BF63-41D2-94F6-D8731D137A5A}.Debug|x86.ActiveCfg = Debug|Win32 {8510C7A4-BF63-41D2-94F6-D8731D137A5A}.Debug|x86.Build.0 = Debug|Win32 + {8510C7A4-BF63-41D2-94F6-D8731D137A5A}.Release|Any CPU.ActiveCfg = Release|Win32 {8510C7A4-BF63-41D2-94F6-D8731D137A5A}.Release|x64.ActiveCfg = Release|x64 {8510C7A4-BF63-41D2-94F6-D8731D137A5A}.Release|x64.Build.0 = Release|x64 {8510C7A4-BF63-41D2-94F6-D8731D137A5A}.Release|x86.ActiveCfg = Release|Win32 {8510C7A4-BF63-41D2-94F6-D8731D137A5A}.Release|x86.Build.0 = Release|Win32 + {A050F015-87E2-498F-866A-2E5AF65AF7AC}.Debug|Any CPU.ActiveCfg = Debug|Win32 {A050F015-87E2-498F-866A-2E5AF65AF7AC}.Debug|x64.ActiveCfg = Debug|x64 {A050F015-87E2-498F-866A-2E5AF65AF7AC}.Debug|x64.Build.0 = Debug|x64 {A050F015-87E2-498F-866A-2E5AF65AF7AC}.Debug|x86.ActiveCfg = Debug|Win32 {A050F015-87E2-498F-866A-2E5AF65AF7AC}.Debug|x86.Build.0 = Debug|Win32 + {A050F015-87E2-498F-866A-2E5AF65AF7AC}.Release|Any CPU.ActiveCfg = Release|Win32 {A050F015-87E2-498F-866A-2E5AF65AF7AC}.Release|x64.ActiveCfg = Release|x64 {A050F015-87E2-498F-866A-2E5AF65AF7AC}.Release|x64.Build.0 = Release|x64 {A050F015-87E2-498F-866A-2E5AF65AF7AC}.Release|x86.ActiveCfg = Release|Win32 {A050F015-87E2-498F-866A-2E5AF65AF7AC}.Release|x86.Build.0 = Release|Win32 + {25EB67BE-468A-4AA5-910F-07EFD58C5516}.Debug|Any CPU.ActiveCfg = Debug|Win32 {25EB67BE-468A-4AA5-910F-07EFD58C5516}.Debug|x64.ActiveCfg = Debug|x64 {25EB67BE-468A-4AA5-910F-07EFD58C5516}.Debug|x64.Build.0 = Debug|x64 {25EB67BE-468A-4AA5-910F-07EFD58C5516}.Debug|x86.ActiveCfg = Debug|Win32 {25EB67BE-468A-4AA5-910F-07EFD58C5516}.Debug|x86.Build.0 = Debug|Win32 + {25EB67BE-468A-4AA5-910F-07EFD58C5516}.Release|Any CPU.ActiveCfg = Release|Win32 {25EB67BE-468A-4AA5-910F-07EFD58C5516}.Release|x64.ActiveCfg = Release|x64 {25EB67BE-468A-4AA5-910F-07EFD58C5516}.Release|x64.Build.0 = Release|x64 {25EB67BE-468A-4AA5-910F-07EFD58C5516}.Release|x86.ActiveCfg = Release|Win32 {25EB67BE-468A-4AA5-910F-07EFD58C5516}.Release|x86.Build.0 = Release|Win32 + {C679466F-9D35-4AFC-B9AE-F9FB5448FB99}.Debug|Any CPU.ActiveCfg = Debug|Win32 {C679466F-9D35-4AFC-B9AE-F9FB5448FB99}.Debug|x64.ActiveCfg = Debug|x64 {C679466F-9D35-4AFC-B9AE-F9FB5448FB99}.Debug|x64.Build.0 = Debug|x64 {C679466F-9D35-4AFC-B9AE-F9FB5448FB99}.Debug|x86.ActiveCfg = Debug|Win32 {C679466F-9D35-4AFC-B9AE-F9FB5448FB99}.Debug|x86.Build.0 = Debug|Win32 + {C679466F-9D35-4AFC-B9AE-F9FB5448FB99}.Release|Any CPU.ActiveCfg = Release|Win32 {C679466F-9D35-4AFC-B9AE-F9FB5448FB99}.Release|x64.ActiveCfg = Release|x64 {C679466F-9D35-4AFC-B9AE-F9FB5448FB99}.Release|x64.Build.0 = Release|x64 {C679466F-9D35-4AFC-B9AE-F9FB5448FB99}.Release|x86.ActiveCfg = Release|Win32 {C679466F-9D35-4AFC-B9AE-F9FB5448FB99}.Release|x86.Build.0 = Release|Win32 + {2A61ED54-2B66-4B9B-99FA-299DD0EF57CD}.Debug|Any CPU.ActiveCfg = Debug|Win32 {2A61ED54-2B66-4B9B-99FA-299DD0EF57CD}.Debug|x64.ActiveCfg = Debug|x64 {2A61ED54-2B66-4B9B-99FA-299DD0EF57CD}.Debug|x64.Build.0 = Debug|x64 {2A61ED54-2B66-4B9B-99FA-299DD0EF57CD}.Debug|x86.ActiveCfg = Debug|Win32 {2A61ED54-2B66-4B9B-99FA-299DD0EF57CD}.Debug|x86.Build.0 = Debug|Win32 + {2A61ED54-2B66-4B9B-99FA-299DD0EF57CD}.Release|Any CPU.ActiveCfg = Release|Win32 {2A61ED54-2B66-4B9B-99FA-299DD0EF57CD}.Release|x64.ActiveCfg = Release|x64 {2A61ED54-2B66-4B9B-99FA-299DD0EF57CD}.Release|x64.Build.0 = Release|x64 {2A61ED54-2B66-4B9B-99FA-299DD0EF57CD}.Release|x86.ActiveCfg = Release|Win32 {2A61ED54-2B66-4B9B-99FA-299DD0EF57CD}.Release|x86.Build.0 = Release|Win32 + {E2049E20-B6DD-474E-8BCA-1C8DC54725AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E2049E20-B6DD-474E-8BCA-1C8DC54725AA}.Debug|x64.ActiveCfg = Debug|Any CPU + {E2049E20-B6DD-474E-8BCA-1C8DC54725AA}.Debug|x86.ActiveCfg = Debug|Any CPU + {E2049E20-B6DD-474E-8BCA-1C8DC54725AA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E2049E20-B6DD-474E-8BCA-1C8DC54725AA}.Release|x64.ActiveCfg = Release|Any CPU + {E2049E20-B6DD-474E-8BCA-1C8DC54725AA}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/DroneShell/src/main.cpp b/DroneShell/src/main.cpp index 81aad85b17..e23ef2191d 100644 --- a/DroneShell/src/main.cpp +++ b/DroneShell/src/main.cpp @@ -1114,7 +1114,7 @@ See RecordPose for information about log file format") VectorMath::toEulerianAngle(quaternion, pitch, roll, yaw); context->client.moveToPosition(local_point.x(), local_point.y(), local_point.z(), velocity, - DrivetrainType::MaxDegreeOfFreedome, YawMode(false, yaw), lookahead, adaptive_lookahead); + DrivetrainType::MaxDegreeOfFreedom, YawMode(false, yaw), lookahead, adaptive_lookahead); } } }); diff --git a/PythonClient/PythonClient.py b/PythonClient/PythonClient.py new file mode 100644 index 0000000000..12c247db09 --- /dev/null +++ b/PythonClient/PythonClient.py @@ -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) + + diff --git a/PythonClient/PythonClient.pyc b/PythonClient/PythonClient.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7df444c60ca85dfa1e844e0be4ae57b52ecea06a GIT binary patch literal 5745 zcmcIo>uwWA6rS}h7ZOMa&>JljTGgc$1=JQw5u!Ra?@AE5odv(BzDQ1BAlp7EL4-I?!P=bSD7Ge7&sUuW;P)ZkOb{~ffX zi_YNZC{Tf;8jf-}E~#!wHOk81cTAaz`c0_=rS3N>%2bt^Q>K?K*k$d9z<)S3y^=qO;TJn$6G+yk^tR${>i$ZkI#eu-)=P zPpDO|XLsxz=e$k;WUlq2u#U0HyW|pvt2ZBXeET59{9vnZccL(5=9$6eUSAUuw@GT~ zY7W2lIG2e1Jern6hC%jvl({#IXgm)%hMEAqa+h@zWaDRGB42F`?#30;-%IF{lTCd8cCx{ z}eO+GC$!p7MZwB!uBeMjv>kswM!h#72)Ic>=EmKgk`8N1dKe*5m@W)#aFTxXm#^S^)&Ki1(OFDN{jw1UU6O zSnLAYC_;)=jCcXWu*wgdOy9KEt{a*Oz~V9cF3uw{RznK10cIr5FL*{ z$Zj)c4w+_ZM=w5xkT2g1;P<`c$EYw*B8FhGLz*R9A8?F1D3A-1?t5FQom zC!6i|FA>sKrj`=`$OwFE2q2S4Hl_g*g)f5`j>61Ckvo*tenst9)qYJq#1MU1wQ+<@ zWiguY1H_!=e&#?7+qrw^8DX6}%E>Ir#NLziub=)sm1NWGBC$m|A|;H;iU+yIOcync zSNL4l7Xjcl+F=koIx#TwcvcLJv}VZ;(-+A>e?*D!9I{MCgmYF>trx@-<|D$K_ zBlfPjiKdTnfgJMLX1R88jQ6Pxz8*@;SZ^${7nhmE#C!3Yb7qI~sMk zSn8~#;CoUQ1a92%hh7$pTLthKTth1ko1T`!UhV_72L6{rr@b?5uv!-9DlYJ^u+-f* zLe3%}@oe_;2X87?@V+K?Uyh&gxh3~BeIqf^=c}Wp#k-^8tX#*c>qD$CpO^X1O^4QUfi;z81*2IlXr|_x8L(!eshP~_^X!qN6v*%<9ayjIk9x^RC8u(Gv(}Cohc2zm5kzy%#ZAw4yUn*6wW~}?zJ>V x=`h?4@Yazjjl6o)jK`X1lji;-OO45b98|Z`x2LzMgoFz(ICG_?>QZHC>OX{t7ta6y literal 0 HcmV?d00001 diff --git a/PythonClient/PythonClient.pyproj b/PythonClient/PythonClient.pyproj new file mode 100644 index 0000000000..1d902b03a7 --- /dev/null +++ b/PythonClient/PythonClient.pyproj @@ -0,0 +1,53 @@ + + + + Debug + 2.0 + e2049e20-b6dd-474e-8bca-1c8dc54725aa + . + camera.py + + + . + . + PythonClient + PythonClient + + + true + false + + + true + false + + + + Code + + + Code + + + Code + + + + Code + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets + + + + + + + + + + \ No newline at end of file diff --git a/PythonClient/box.py b/PythonClient/box.py new file mode 100644 index 0000000000..42278fb9ec --- /dev/null +++ b/PythonClient/box.py @@ -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) + diff --git a/PythonClient/camera.py b/PythonClient/camera.py new file mode 100644 index 0000000000..9b12391dc4 --- /dev/null +++ b/PythonClient/camera.py @@ -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; diff --git a/PythonClient/path.py b/PythonClient/path.py new file mode 100644 index 0000000000..56f45d87ae --- /dev/null +++ b/PythonClient/path.py @@ -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) diff --git a/PythonClient/test.py b/PythonClient/test.py new file mode 100644 index 0000000000..90bf81bad7 --- /dev/null +++ b/PythonClient/test.py @@ -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); + + + diff --git a/docs/px4.md b/docs/px4.md index 765f436d7f..e43ef3ce9d 100644 --- a/docs/px4.md +++ b/docs/px4.md @@ -201,4 +201,6 @@ gcc-arm-none-eabi-5_4-2017q2/bin ```` If you add this folder to your PATH using the usual `export PATH=...` trick then the PX4 build will be able to find -and run this compiler. After that, you can run `make px4fmu-v2_default` in BashOnWindows. +and run this compiler. After that, you can run `make px4fmu-v2_default` in BashOnWindows and the firmware will appear +here: `build_px4fmu-v2_default/src/firmware/nuttx/px4fmu-v2_default.px4`. You can then flash this new firmware on your +Pixhawk using QGroundControl. diff --git a/docs/python.md b/docs/python.md new file mode 100644 index 0000000000..eb28d5c6b9 --- /dev/null +++ b/docs/python.md @@ -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). +