-
Notifications
You must be signed in to change notification settings - Fork 0
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
Class-based loadable agent params #410
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,10 @@ | |
#include "backend/simulation_run_stats.h" | ||
#include "backend/simulation_runner.h" | ||
|
||
#include "../include/delphyne/linb-any" | ||
#include "include/delphyne/agent_plugin_base.h" | ||
#include "src/agents/mobil_car.h" | ||
#include "src/agents/rail_car.h" | ||
#include "src/agents/trajectory_car.h" | ||
|
||
namespace py = pybind11; | ||
|
||
|
@@ -26,6 +29,10 @@ using delphyne::AutomotiveSimulator; | |
using delphyne::RoadBuilder; | ||
using delphyne::SimulatorRunner; | ||
using delphyne::InteractiveSimulationStats; | ||
using delphyne::AgentPluginParams; | ||
using delphyne::RailCarAgentParams; | ||
using delphyne::TrajectoryCarAgentParams; | ||
using delphyne::MobilCarAgentParams; | ||
using drake::automotive::LaneDirection; | ||
using drake::automotive::MaliputRailcarParams; | ||
using drake::automotive::MaliputRailcarState; | ||
|
@@ -37,6 +44,20 @@ PYBIND11_MODULE(python_bindings, m) { | |
py::module::import("pydrake.systems.framework"); | ||
py::module::import("pydrake.maliput.api"); | ||
|
||
py::class_<AgentPluginParams>(m, "AgentPluginParams"); | ||
|
||
py::class_<RailCarAgentParams, AgentPluginParams>(m, "RailCarAgentParams") | ||
.def(py::init< | ||
std::unique_ptr<drake::automotive::LaneDirection>, | ||
std::unique_ptr<drake::automotive::MaliputRailcarParams<double>>>()); | ||
|
||
py::class_<TrajectoryCarAgentParams, AgentPluginParams>( | ||
m, "TrajectoryCarAgentParams") | ||
.def(py::init<std::unique_ptr<drake::automotive::Curve2<double>>>()); | ||
|
||
py::class_<MobilCarAgentParams, AgentPluginParams>(m, "MobilCarAgentParams") | ||
.def(py::init<bool>()); | ||
|
||
py::class_<MaliputRailcarState<double>, BasicVector<double>>( | ||
m, "MaliputRailcarState") | ||
.def(py::init<>()) | ||
|
@@ -58,22 +79,6 @@ PYBIND11_MODULE(python_bindings, m) { | |
&MaliputRailcarParams<double>::velocity_limit_kp, | ||
&MaliputRailcarParams<double>::set_velocity_limit_kp); | ||
|
||
// TODO(basicNew): Properly fill this binding with the remaining methods and | ||
// overloaded constructors. | ||
// Note: Since we are using linb::any in combination with bare pointers to | ||
// pass generic parameters to the loadable agents module, we have to make | ||
// sure python doesn't garbage collect temp objects while they are being | ||
// used on the C++ side, hence the `py::keep_alive`. See | ||
// http://pybind11.readthedocs.io/en/stable/advanced/functions.html#keep-alive | ||
py::class_<linb::any>(m, "Any") | ||
.def(py::init<bool&&>()) | ||
// Keep alive, ownership: `self` keeps `RoadGeometry` alive. | ||
.def(py::init<const RoadGeometry*&&>(), py::keep_alive<1, 2>()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if we go in this direction, you'll probably want to keep anything maliput related (road related) around so that the user can dig into the roads (i.e. world ground truth) in scenario setup, or callback scripts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I follow; is this related to the python bindings discussion we were having on slack or is it a different thing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was working under the mistaken assumption that the road was instantiated in python. From a look at the PR though, looks like it's getting built by RoadBuilder, which means being instantiated in c++ with ownership transferred to the simulator. This is fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, got it. |
||
// Keep alive, ownership: `self` keeps `LaneDirection` alive. | ||
.def(py::init<LaneDirection*&&>(), py::keep_alive<1, 2>()) | ||
// Keep alive, ownership: `self` keeps `MaliputRailcarParams` alive. | ||
.def(py::init<MaliputRailcarParams<double>*&&>(), py::keep_alive<1, 2>()); | ||
|
||
py::class_<InteractiveSimulationStats>(m, "InteractiveSimulationStats") | ||
.def(py::init<>()) | ||
.def("TotalElapsedSimtime", | ||
|
@@ -116,31 +121,28 @@ PYBIND11_MODULE(python_bindings, m) { | |
.def("AddMonolaneFromFile", &RoadBuilder<double>::AddMonolaneFromFile) | ||
.def("AddMultilaneFromFile", &RoadBuilder<double>::AddMultilaneFromFile); | ||
|
||
// Note: Since AddLoadableAgent uses a map of (string, linb::any) in | ||
// combination with bare pointers to pass generic parameters, we have to make | ||
// sure python doesn't garbage collect temp objects while they are being | ||
// used on the C++ side, hence the `py::keep_alive`. See | ||
// http://pybind11.readthedocs.io/en/stable/advanced/functions.html#keep-alive | ||
py::class_<AutomotiveSimulator<double>>(m, "AutomotiveSimulator") | ||
.def(py::init( | ||
[](void) { return std::make_unique<AutomotiveSimulator<double>>(); })) | ||
.def("Start", &AutomotiveSimulator<double>::Start) | ||
.def("AddLoadableAgent", | ||
py::overload_cast< | ||
const std::string&, const std::map<std::string, linb::any>&, | ||
const std::string&, | ||
std::unique_ptr<drake::systems::BasicVector<double>>>( | ||
&AutomotiveSimulator<double>::AddLoadableAgent), | ||
py::keep_alive<1, 3>()) // Keep alive, ownership: `self` keeps | ||
// `parameters` alive. | ||
const std::string&, const std::string&, | ||
std::unique_ptr<drake::systems::BasicVector<double>>, | ||
const RoadGeometry*>( | ||
&AutomotiveSimulator<double>::AddLoadableAgent)) | ||
.def("AddLoadableAgent", | ||
py::overload_cast< | ||
const std::string&, const std::string&, | ||
const std::map<std::string, linb::any>&, const std::string&, | ||
std::unique_ptr<drake::systems::BasicVector<double>>>( | ||
&AutomotiveSimulator<double>::AddLoadableAgent), | ||
py::keep_alive<1, 3>()); // Keep alive, ownership: `self` keeps | ||
// `parameters` alive. | ||
std::unique_ptr<drake::systems::BasicVector<double>>, | ||
const RoadGeometry*, std::unique_ptr<AgentPluginParams>>( | ||
&AutomotiveSimulator<double>::AddLoadableAgent)) | ||
.def("AddLoadableAgent", | ||
py::overload_cast< | ||
const std::string&, const std::string&, const std::string&, | ||
std::unique_ptr<drake::systems::BasicVector<double>>, | ||
const RoadGeometry*, std::unique_ptr<AgentPluginParams>>( | ||
&AutomotiveSimulator<double>::AddLoadableAgent)); | ||
} | ||
|
||
} // namespace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@basicNew how do you feel about dropping this method and coercing the user to always specify both
plugin_library_name
andplugin_name
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is actually quite handy to have this; as a matter of fact, we are not using the "full" version of it anywhere :). I would vote for leaving it.