diff --git a/AirLib/include/api/RpcLibClientBase.hpp b/AirLib/include/api/RpcLibClientBase.hpp index 7f98072743..06341a6aac 100644 --- a/AirLib/include/api/RpcLibClientBase.hpp +++ b/AirLib/include/api/RpcLibClientBase.hpp @@ -114,6 +114,8 @@ class RpcLibClientBase { void stopRecording(); bool isRecording(); + vector listVehicles(); + protected: void* getClient(); const void* getClient() const; diff --git a/AirLib/include/api/WorldSimApiBase.hpp b/AirLib/include/api/WorldSimApiBase.hpp index 8607c44ba1..a9450c4329 100644 --- a/AirLib/include/api/WorldSimApiBase.hpp +++ b/AirLib/include/api/WorldSimApiBase.hpp @@ -70,6 +70,8 @@ class WorldSimApiBase { virtual void startRecording() = 0; virtual void stopRecording() = 0; virtual bool isRecording() const = 0; + + virtual vector listVehicles() const = 0; }; diff --git a/AirLib/include/common/common_utils/UniqueValueMap.hpp b/AirLib/include/common/common_utils/UniqueValueMap.hpp index 62b9d04c6f..cff3e2ce42 100644 --- a/AirLib/include/common/common_utils/UniqueValueMap.hpp +++ b/AirLib/include/common/common_utils/UniqueValueMap.hpp @@ -71,6 +71,15 @@ class UniqueValueMap { return vals_.size(); } + std::vector keys() const + { + std::vector ret; + for (const auto& element: map_) { + ret.push_back(element.first); + } + return ret; + } + //TODO: add erase methods private: diff --git a/AirLib/src/api/RpcLibClientBase.cpp b/AirLib/src/api/RpcLibClientBase.cpp index be9c01fe80..8df3c14f64 100644 --- a/AirLib/src/api/RpcLibClientBase.cpp +++ b/AirLib/src/api/RpcLibClientBase.cpp @@ -432,6 +432,11 @@ bool RpcLibClientBase::isRecording() return pimpl_->client.call("isRecording").as(); } +vector RpcLibClientBase::listVehicles() +{ + return pimpl_->client.call("listVehicles").as>(); +} + void* RpcLibClientBase::getClient() { return &pimpl_->client; diff --git a/AirLib/src/api/RpcLibServerBase.cpp b/AirLib/src/api/RpcLibServerBase.cpp index 3123e95f18..2927a33d3f 100644 --- a/AirLib/src/api/RpcLibServerBase.cpp +++ b/AirLib/src/api/RpcLibServerBase.cpp @@ -366,6 +366,10 @@ RpcLibServerBase::RpcLibServerBase(ApiProvider* api_provider, const std::string& return getWorldSimApi()->isRecording(); }); + pimpl_->server.bind("listVehicles", [&]() -> vector { + return getWorldSimApi()->listVehicles(); + }); + //if we don't suppress then server will bomb out for exceptions raised by any method pimpl_->server.suppress_exceptions(true); } diff --git a/PythonClient/airsim/client.py b/PythonClient/airsim/client.py index a6553093bd..09d2530c89 100644 --- a/PythonClient/airsim/client.py +++ b/PythonClient/airsim/client.py @@ -750,6 +750,15 @@ def isRecording(self): """ return self.client.call('isRecording') + def listVehicles(self): + """ + Lists the names of current vehicles + + Returns: + list[str]: List containing names of all vehicles + """ + return self.client.call('listVehicles') + # ----------------------------------- Multirotor APIs --------------------------------------------- class MultirotorClient(VehicleClient, object): def __init__(self, ip = "", port = 41451, timeout_value = 3600): diff --git a/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp b/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp index 4a60712435..2c9c2b1229 100644 --- a/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp +++ b/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp @@ -410,3 +410,13 @@ bool WorldSimApi::isRecording() const { return simmode_->isRecording(); } + +std::vector WorldSimApi::listVehicles() const +{ + auto vehicle_names = (simmode_->getApiProvider()->getVehicleSimApis()).keys(); + // Remove '' from the list, representing default vehicle + auto position = std::find(vehicle_names.begin(), vehicle_names.end(), ""); + if (position != vehicle_names.end()) + vehicle_names.erase(position); + return vehicle_names; +} diff --git a/Unreal/Plugins/AirSim/Source/WorldSimApi.h b/Unreal/Plugins/AirSim/Source/WorldSimApi.h index a3aef5861c..6d01928c6e 100644 --- a/Unreal/Plugins/AirSim/Source/WorldSimApi.h +++ b/Unreal/Plugins/AirSim/Source/WorldSimApi.h @@ -63,6 +63,8 @@ class WorldSimApi : public msr::airlib::WorldSimApiBase { virtual void stopRecording() override; virtual bool isRecording() const override; + virtual std::vector listVehicles() const override; + private: AActor* createNewActor(const FActorSpawnParameters& spawn_params, const FTransform& actor_transform, const Vector3r& scale, UStaticMesh* static_mesh); void spawnPlayer();