Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for list of vehicle names #2936

Merged
merged 3 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AirLib/include/api/RpcLibClientBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ namespace airlib
bool isRecording();

void simSetWind(const Vector3r& wind) 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 @@ -82,6 +82,7 @@ namespace airlib
virtual bool isRecording() const = 0;

virtual void setWind(const Vector3r& wind) const = 0;
virtual vector<string> listVehicles() const = 0;

virtual std::string getSettingsString() const = 0;
};
Expand Down
11 changes: 10 additions & 1 deletion AirLib/include/common/common_utils/UniqueValueMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,20 @@ class UniqueValueMap
return vals_.size();
}

std::vector<TKey> keys() const
{
std::vector<TKey> ret;
for (const auto& element : map_) {
ret.push_back(element.first);
}
return ret;
}

//TODO: add erase methods

private:
std::map<TKey, TVal> map_;
std::set<TVal> vals_;
};
}
#endif
#endif
5 changes: 5 additions & 0 deletions AirLib/src/api/RpcLibClientBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ __pragma(warning(disable : 4239))
pimpl_->client.call("simSetWind", conv_wind);
}

vector<string> RpcLibClientBase::listVehicles()
{
return pimpl_->client.call("listVehicles").as<vector<string>>();
}

std::string RpcLibClientBase::getSettingsString() const
{
return pimpl_->client.call("getSettingsString").as<std::string>();
Expand Down
4 changes: 4 additions & 0 deletions AirLib/src/api/RpcLibServerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ namespace airlib
getWorldSimApi()->setWind(wind.to());
});

pimpl_->server.bind("listVehicles", [&]() -> vector<string> {
return getWorldSimApi()->listVehicles();
});

pimpl_->server.bind("getSettingsString", [&]() -> std::string {
return getWorldSimApi()->getSettingsString();
});
Expand Down
11 changes: 10 additions & 1 deletion PythonClient/airsim/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,13 +851,22 @@ def simAddVehicle(self, vehicle_name, vehicle_type, pose, pawn_path = ""):
vehicle_name (str): Name of the vehicle being created
vehicle_type (str): Type of vehicle, e.g. "simpleflight"
pose (Pose): Initial pose of the vehicle
pawn_path (str): Vehicle blueprint path, default empty wbich uses the default blueprint for the vehicle type
pawn_path (str, optional): Vehicle blueprint path, default empty wbich uses the default blueprint for the vehicle type

Returns:
bool: Whether vehicle was created
"""
return self.client.call('simAddVehicle', vehicle_name, vehicle_type, pose, pawn_path)

def listVehicles(self):
"""
Lists the names of current vehicles

Returns:
list[str]: List containing names of all vehicles
"""
return self.client.call('listVehicles')

def getSettingsString(self):
"""
Fetch the settings text being used by AirSim
Expand Down
12 changes: 11 additions & 1 deletion Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,17 @@ bool WorldSimApi::isRecording() const
void WorldSimApi::setWind(const Vector3r& wind) const
{
simmode_->setWind(wind);
};
}

std::vector<std::string> 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;
}

bool WorldSimApi::addVehicle(const std::string& vehicle_name, const std::string& vehicle_type, const WorldSimApi::Pose& pose, const std::string& pawn_path)
{
Expand Down
1 change: 1 addition & 0 deletions Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ 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;
virtual bool addVehicle(const std::string& vehicle_name, const std::string& vehicle_type, const Pose& pose, const std::string& pawn_path = "") override;
virtual std::vector<std::string> listVehicles() const override;

virtual std::string getSettingsString() const override;

Expand Down
16 changes: 16 additions & 0 deletions Unreal/Plugins/AirSim/Source/WorldSimApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,22 @@ void WorldSimApi::setWind(const Vector3r& wind) const
simmode_->setWind(wind);
}

std::vector<std::string> WorldSimApi::listVehicles() const
{
std::vector<std::string> vehicle_names;

UAirBlueprintLib::RunCommandOnGameThread([this, &vehicle_names]() {
vehicle_names = (simmode_->getApiProvider()->getVehicleSimApis()).keys();
},
true);

// 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;
}

std::string WorldSimApi::getSettingsString() const
{
return msr::airlib::AirSimSettings::singleton().settings_text_;
Expand Down
1 change: 1 addition & 0 deletions Unreal/Plugins/AirSim/Source/WorldSimApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ 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;
virtual std::vector<std::string> listVehicles() const override;

virtual std::string getSettingsString() const override;

Expand Down
7 changes: 7 additions & 0 deletions docs/multi_vehicle.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,12 @@ The new APIs since AirSim 1.2 allows you to specify `vehicle_name`. This name co

[Example code for multirotors](https://github.com/Microsoft/AirSim/tree/master/PythonClient//multirotor/multi_agent_drone.py)

Using APIs for multi-vehicles requires specifying the `vehicle_name`, which needs to be hardcoded in the script or requires parsing of the settings file. There's also a simple API `listVehicles()` which returns a list (vector in C++) of strings containing names of the current vehicles. For example, with the above settings for 2 Cars -

```
>>> client.listVehicles()
['Car1', 'Car2']
```

### Demo
[![AirSimMultiple Vehicles Demo Video](images/demo_multi_vehicles.png)](https://youtu.be/35dgcuLuF5M)