Skip to content

Commit

Permalink
Add simFindLookAtRotation
Browse files Browse the repository at this point in the history
  • Loading branch information
joelreymont committed Feb 13, 2024
1 parent 6bbd11b commit f573de8
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions AirLib/include/api/RpcLibClientBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ namespace airlib
void simSetWind(const Vector3r& wind) const;
void simSetExtForce(const Vector3r& ext_force) const;

Vector3r simFindLookAtRotation(const std::string& vehicle_name, const std::string& object_name) const;

vector<string> listVehicles();

std::string getSettingsString() const;
Expand Down
1 change: 1 addition & 0 deletions AirLib/include/api/WorldSimApiBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ namespace airlib
virtual vector<MeshPositionVertexBuffersResponse> getMeshPositionVertexBuffers() const = 0;

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) = 0;
virtual Vector3r findLookAtRotation(const std::string& vehicle_name, const std::string& object_name) = 0;

// Recording APIs
virtual void startRecording() = 0;
Expand Down
5 changes: 5 additions & 0 deletions AirLib/src/api/RpcLibClientBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,11 @@ __pragma(warning(disable : 4239))
return pimpl_->client.call("simCreateVoxelGrid", RpcLibAdaptorsBase::Vector3r(position), x, y, z, res, output_file).as<bool>();
}

msr::airlib::Vector3r RpcLibClientBase::simFindLookAtRotation(const std::string& vehicle_name, const std::string& object_name) const
{
return pimpl_->client.call("simFindLookAtRotation", vehicle_name, object_name).as<RpcLibAdaptorsBase::Vector3r>().to();
}

void RpcLibClientBase::cancelLastTask(const std::string& vehicle_name)
{
pimpl_->client.call("cancelLastTask", vehicle_name);
Expand Down
5 changes: 5 additions & 0 deletions AirLib/src/api/RpcLibServerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,11 @@ namespace airlib
return getWorldSimApi()->getSettingsString();
});

pimpl_->server.bind("simFindLookAtRotation", [&](const std::string& vehicle_name, const std::string& object_name) -> RpcLibAdaptorsBase::Vector3r {
const auto& rot = getWorldSimApi()->findLookAtRotation(vehicle_name, object_name);
return RpcLibAdaptorsBase::Vector3r(rot);
});

//if we don't suppress then server will bomb out for exceptions raised by any method
pimpl_->server.suppress_exceptions(true);
}
Expand Down
3 changes: 3 additions & 0 deletions PythonClient/airsim/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,9 @@ def simSetExtForce(self, ext_force):
"""
self.client.call('simSetExtForce', ext_force)

def simFindLookAtRotation(self, object_name, vehicle_name = ''):
return self.client.call('simFindLookAtRotation', vehicle_name, object_name)

# ----------------------------------- Multirotor APIs ---------------------------------------------
class MultirotorClient(VehicleClient, object):
def __init__(self, ip = "", port = 41451, timeout_value = 3600):
Expand Down
10 changes: 10 additions & 0 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,13 @@ bool UAirBlueprintLib::CompressUsingImageWrapper(const TArray<uint8>& uncompress

return bSucceeded;
}

void UAirBlueprintLib::FindAllActorByTag(const UObject* context, FName tag, TArray<AActor*>& foundActors)
{
UGameplayStatics::GetAllActorsWithTag(context, tag, foundActors);
}

FRotator UAirBlueprintLib::FindLookAtRotation(AActor* source, AActor* target)
{
return UKismetMathLibrary::FindLookAtRotation(source->GetActorLocation(), target->GetActorLocation());
}
3 changes: 3 additions & 0 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary
UGameplayStatics::GetAllActorsOfClass(context, T::StaticClass(), foundActors);
}

static void FindAllActorByTag(const UObject* context, FName tag, TArray<AActor*>& foundActors);
static FRotator FindLookAtRotation(AActor* source, AActor* target);

static std::vector<std::string> ListMatchingActors(const UObject* context, const std::string& name_regex);
static std::vector<std::string> ListMatchingActorsByTag(const UObject* context, const std::string& tag_regex);

Expand Down
20 changes: 20 additions & 0 deletions Unreal/Plugins/AirSim/Source/WorldSimApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1073,3 +1073,23 @@ std::vector<msr::airlib::DetectionInfo> WorldSimApi::getDetections(ImageCaptureB

return result;
}

Vector3r WorldSimApi::findLookAtRotation(const std::string& vehicle_name, const std::string& object_name)
{
Vector3r result = Vector3r::Zero();
PawnSimApi* pawn = simmode_->getVehicleSimApi(vehicle_name);

if (pawn) {
AActor* source = pawn->getPawn();
UAirBlueprintLib::RunCommandOnGameThread([this, object_name, &source, &result]() {
AActor* target = UAirBlueprintLib::FindActor<AActor>(simmode_, FString(object_name.c_str()));
if (target) {
FRotator rot = UAirBlueprintLib::FindLookAtRotation(source, target);
result = Vector3r(rot.Pitch, rot.Roll, rot.Yaw);
}
},
true);
}

return result;
}
2 changes: 2 additions & 0 deletions Unreal/Plugins/AirSim/Source/WorldSimApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class WorldSimApi : public msr::airlib::WorldSimApiBase
virtual void clearDetectionMeshNames(ImageCaptureBase::ImageType image_type, const CameraDetails& camera_details) override;
virtual std::vector<msr::airlib::DetectionInfo> getDetections(ImageCaptureBase::ImageType image_type, const CameraDetails& camera_details) override;

virtual Vector3r findLookAtRotation(const std::string& vehicle_name, const std::string& object_name);

private:
AActor* createNewStaticMeshActor(const FActorSpawnParameters& spawn_params, const FTransform& actor_transform, const Vector3r& scale, UStaticMesh* static_mesh);
AActor* createNewBPActor(const FActorSpawnParameters& spawn_params, const FTransform& actor_transform, const Vector3r& scale, UBlueprint* blueprint);
Expand Down

0 comments on commit f573de8

Please sign in to comment.