From e6f606959edf0580b5e9adf893f3d040bc64272b Mon Sep 17 00:00:00 2001 From: Rajat Singhal Date: Fri, 15 Jan 2021 21:02:35 +0530 Subject: [PATCH] Add getCameraInfo API for external cameras --- AirLib/include/api/RpcLibClientBase.hpp | 2 +- AirLib/include/api/WorldSimApiBase.hpp | 4 ++++ AirLib/src/api/RpcLibClientBase.cpp | 4 ++-- AirLib/src/api/RpcLibServerBase.cpp | 5 +++-- PythonClient/airsim/client.py | 4 ++-- Unreal/Plugins/AirSim/Source/PawnSimApi.cpp | 3 +-- .../Plugins/AirSim/Source/SimMode/SimModeBase.cpp | 8 ++++++++ Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.h | 13 +++++++++++++ Unreal/Plugins/AirSim/Source/WorldSimApi.cpp | 10 ++++++++++ Unreal/Plugins/AirSim/Source/WorldSimApi.h | 5 +++++ 10 files changed, 49 insertions(+), 9 deletions(-) diff --git a/AirLib/include/api/RpcLibClientBase.hpp b/AirLib/include/api/RpcLibClientBase.hpp index 1aacc78ab4..a1df871a82 100644 --- a/AirLib/include/api/RpcLibClientBase.hpp +++ b/AirLib/include/api/RpcLibClientBase.hpp @@ -102,7 +102,7 @@ class RpcLibClientBase { CollisionInfo simGetCollisionInfo(const std::string& vehicle_name = "") const; - CameraInfo simGetCameraInfo(const std::string& camera_name, const std::string& vehicle_name = "") const; + CameraInfo simGetCameraInfo(const std::string& camera_name, const std::string& vehicle_name = "", bool external = false) const; void simSetDistortionParam(const std::string& camera_name, const std::string& param_name, float value, const std::string& vehicle_name = ""); std::vector simGetDistortionParams(const std::string& camera_name, const std::string& vehicle_name = ""); void simSetCameraPose(const std::string& camera_name, const Pose& pose, const std::string& vehicle_name = ""); diff --git a/AirLib/include/api/WorldSimApiBase.hpp b/AirLib/include/api/WorldSimApiBase.hpp index eb9da9e44c..e11905805c 100644 --- a/AirLib/include/api/WorldSimApiBase.hpp +++ b/AirLib/include/api/WorldSimApiBase.hpp @@ -74,6 +74,10 @@ class WorldSimApiBase { virtual bool isRecording() const = 0; virtual void setWind(const Vector3r& wind) const = 0; + + // Image APIs + virtual CameraInfo getCameraInfo(const std::string& camera_name, const std::string& vehicle_name="", bool external=false) const = 0; + }; diff --git a/AirLib/src/api/RpcLibClientBase.cpp b/AirLib/src/api/RpcLibClientBase.cpp index 5283ca7fce..6f1cdbb3fd 100644 --- a/AirLib/src/api/RpcLibClientBase.cpp +++ b/AirLib/src/api/RpcLibClientBase.cpp @@ -376,9 +376,9 @@ bool RpcLibClientBase::simSetObjectScale(const std::string& object_name, const m return pimpl_->client.call("simSetObjectScale", object_name, RpcLibAdapatorsBase::Vector3r(scale)).as(); } -CameraInfo RpcLibClientBase::simGetCameraInfo(const std::string& camera_name, const std::string& vehicle_name) const +CameraInfo RpcLibClientBase::simGetCameraInfo(const std::string& camera_name, const std::string& vehicle_name, bool external) const { - return pimpl_->client.call("simGetCameraInfo", camera_name, vehicle_name).as().to(); + return pimpl_->client.call("simGetCameraInfo", camera_name, vehicle_name, external).as().to(); } void RpcLibClientBase::simSetCameraPose(const std::string& camera_name, const Pose& pose, const std::string& vehicle_name) diff --git a/AirLib/src/api/RpcLibServerBase.cpp b/AirLib/src/api/RpcLibServerBase.cpp index 1acc3ba4c2..28cda3b2f3 100644 --- a/AirLib/src/api/RpcLibServerBase.cpp +++ b/AirLib/src/api/RpcLibServerBase.cpp @@ -233,8 +233,9 @@ RpcLibServerBase::RpcLibServerBase(ApiProvider* api_provider, const std::string& return RpcLibAdapatorsBase::DistanceSensorData(distance_sensor_data); }); - pimpl_->server.bind("simGetCameraInfo", [&](const std::string& camera_name, const std::string& vehicle_name) -> RpcLibAdapatorsBase::CameraInfo { - const auto& camera_info = getVehicleSimApi(vehicle_name)->getCameraInfo(camera_name); + pimpl_->server.bind("simGetCameraInfo", [&](const std::string& camera_name, const std::string& vehicle_name, bool external) -> RpcLibAdapatorsBase::CameraInfo { + // const auto& camera_info = getVehicleSimApi(vehicle_name)->getCameraInfo(camera_name); + const auto& camera_info = getWorldSimApi()->getCameraInfo(camera_name, vehicle_name, external); return RpcLibAdapatorsBase::CameraInfo(camera_info); }); diff --git a/PythonClient/airsim/client.py b/PythonClient/airsim/client.py index 141633e03b..de3640c17d 100644 --- a/PythonClient/airsim/client.py +++ b/PythonClient/airsim/client.py @@ -468,7 +468,7 @@ def simPrintLogMessage(self, message, message_param = "", severity = 0): """ self.client.call('simPrintLogMessage', message, message_param, severity) - def simGetCameraInfo(self, camera_name, vehicle_name = ''): + def simGetCameraInfo(self, camera_name, vehicle_name = '', external=False): """ Get details about the camera @@ -480,7 +480,7 @@ def simGetCameraInfo(self, camera_name, vehicle_name = ''): CameraInfo: """ # TODO: below str() conversion is only needed for legacy reason and should be removed in future - return CameraInfo.from_msgpack(self.client.call('simGetCameraInfo', str(camera_name), vehicle_name)) + return CameraInfo.from_msgpack(self.client.call('simGetCameraInfo', str(camera_name), vehicle_name, external)) def simGetDistortionParams(self, camera_name, vehicle_name = ''): """ diff --git a/Unreal/Plugins/AirSim/Source/PawnSimApi.cpp b/Unreal/Plugins/AirSim/Source/PawnSimApi.cpp index d32371cf23..8ffc3fdb15 100644 --- a/Unreal/Plugins/AirSim/Source/PawnSimApi.cpp +++ b/Unreal/Plugins/AirSim/Source/PawnSimApi.cpp @@ -400,8 +400,7 @@ void PawnSimApi::plot(std::istream& s, FColor color, const Vector3r& offset) msr::airlib::CameraInfo PawnSimApi::getCameraInfo(const std::string& camera_name) const { - const APIPCamera* camera = getCamera(camera_name); - return camera->getCameraInfo(); + return getCamera(camera_name)->getCameraInfo(); } void PawnSimApi::setCameraPose(const std::string& camera_name, const msr::airlib::Pose& pose) diff --git a/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp b/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp index ba1bc0c0b3..a7c134eaec 100644 --- a/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp +++ b/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp @@ -498,6 +498,14 @@ bool ASimModeBase::isRecording() const return FRecordingThread::isRecording(); } +msr::airlib::CameraInfo ASimModeBase::getCameraInfo(const std::string& camera_name, const std::string& vehicle_name, bool external) const +{ + if (external) + return getExternalCamera(camera_name)->getCameraInfo(); + else + return getVehicleSimApi(vehicle_name)->getCameraInfo(camera_name); +} + //API server start/stop void ASimModeBase::startApiServer() { diff --git a/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.h b/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.h index 061f524f34..de719f5cd7 100644 --- a/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.h +++ b/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.h @@ -70,6 +70,8 @@ class AIRSIM_API ASimModeBase : public AActor virtual void stopRecording(); virtual bool isRecording() const; + virtual msr::airlib::CameraInfo getCameraInfo(const std::string& camera_name, const std::string& vehicle_name, bool external) const; + void startApiServer(); void stopApiServer(); bool isApiServerStarted(); @@ -89,6 +91,17 @@ class AIRSIM_API ASimModeBase : public AActor return static_cast(api_provider_->getVehicleSimApi(vehicle_name)); } + const APIPCamera* getExternalCamera(const std::string& camera_name) const + { + return external_cameras_.findOrDefault(camera_name, nullptr); + } + + APIPCamera* getExternalCamera(const std::string& camera_name) + { + return const_cast( + static_cast(this)->getExternalCamera(camera_name)); + } + TMap asset_map; TMap scene_object_map; diff --git a/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp b/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp index 935782dd4c..71fe5737a2 100644 --- a/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp +++ b/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp @@ -558,3 +558,13 @@ void WorldSimApi::setWind(const Vector3r& wind) const { simmode_->setWind(wind); } + +CameraInfo WorldSimApi::getCameraInfo(const std::string& camera_name, const std::string& vehicle_name, bool external) const +{ + CameraInfo info; + UAirBlueprintLib::RunCommandOnGameThread([this, &camera_name, &vehicle_name, external, &info]() { + info = simmode_->getCameraInfo(camera_name, vehicle_name, external); + }, true); + + return info; +} diff --git a/Unreal/Plugins/AirSim/Source/WorldSimApi.h b/Unreal/Plugins/AirSim/Source/WorldSimApi.h index 2a96852343..1f75250275 100644 --- a/Unreal/Plugins/AirSim/Source/WorldSimApi.h +++ b/Unreal/Plugins/AirSim/Source/WorldSimApi.h @@ -67,6 +67,11 @@ class WorldSimApi : public msr::airlib::WorldSimApiBase { virtual void setWind(const Vector3r& wind) const override; virtual bool createVoxelGrid(const Vector3r& position, const int& x_size, const int& y_size, const int& z_size, const float& res, const std::string& output_file) override; + // Image APIs + virtual msr::airlib::CameraInfo getCameraInfo(const std::string& camera_name, const std::string& vehicle_name = "", + bool external = false) const override; + + private: AActor* createNewActor(const FActorSpawnParameters& spawn_params, const FTransform& actor_transform, const Vector3r& scale, UStaticMesh* static_mesh); void spawnPlayer();