Skip to content

[Sensor] Decouple sensor initialization from Agent#1064

Merged
vauduong merged 36 commits intofacebookresearch:masterfrom
vauduong:decouple-sensors
Feb 12, 2021
Merged

[Sensor] Decouple sensor initialization from Agent#1064
vauduong merged 36 commits intofacebookresearch:masterfrom
vauduong:decouple-sensors

Conversation

@vauduong
Copy link
Copy Markdown
Contributor

@vauduong vauduong commented Jan 29, 2021

Motivation and Context

Currently, agents are constructed using a valid scene node reference and an agent configuration in the c++ habitat-sim function Simulator::addAgent. The agent configuration specifies sensor specifications, and the sensors are initialized in the agent constructor using the provided scene node.

This PR just focuses on the C++ side, but another PR can be made in the future to bring the functionality to python.

Sensors can be constructed using any valid scene node, so we wish to decouple sensor initialization from the agent. In this PR, we:

  • create a factory class SensorFactory, which contains a static method to return a SensorSuite given a reference to a SceneNode and reference to a vector of SensorSpec::ptrs called SensorSetup.
  • Add method SensorSuite::merge to merge the sensors from another SensorSuite into its existing map of sensors
  • SensorFactory is called in Simulator::AddAgent to provide sensors.
  • Add method Simulator::addSensorToObject to add a sensor at a node, given the node's objectId and the specification of the sensor
  • Add test in SimTest to test adding a sensor to an object
  • Add test in SensorTest to test SensorFactory methods
  • Methods in viewer to demo adding sensors to objects has been moved to https://github.com/vauduong/habitat-sim/tree/add-sensors-to-object-viewer-demo

SensorCameraWeeklyUpdates 3 pptx

To demo this functionality, methods were added to Simulator to keep track of Sensors and get Sensors by object ID; and methods were added to Viewer to add CameraSensors to objects and toggle between agent and object cameras. You can see a video of a banana being pushed here:

BananaDemo.mov

How Has This Been Tested

Build and run with demo in viewer
Add test to add object in SimTest
Add test to test SensorFactory in SensorTest

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have completed my CLA (see CONTRIBUTING)
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Jan 29, 2021
@vauduong vauduong requested a review from jturner65 January 29, 2021 17:39
Comment thread src/esp/sensor/SensorFactory.cpp Outdated
scene::SceneNode& node,
const sensor::SensorSetup& sensorSetup) {
sensor::SensorSuite sensorSuite = sensor::SensorSuite();
for (sensor::SensorSpec::ptr spec : sensorSetup) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already decoupled on the Python side FYI. It's even factored out on the Python side to a sensor can be added without reconfiguring the sim. Seems like this should be refactored to also have a function to add a single sensor like we have on the Python side agent.add_sensor()...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Skylion007
If possible, I sincerely hope you can help us to take a look at the internal task (I will send you the task number via slack), where we gave concrete implementation steps. Check if something is contradictory to what has been done in the Python side. Thanks!

Copy link
Copy Markdown
Contributor

@jturner65 jturner65 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions.

Comment thread src/esp/sensor/SensorFactory.h Outdated
Comment thread src/esp/sensor/SensorFactory.h Outdated
Comment thread src/esp/agent/Agent.h Outdated
Copy link
Copy Markdown
Contributor

@jturner65 jturner65 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remember to document your added functions.

Copy link
Copy Markdown
Contributor

@jturner65 jturner65 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for clang-tidy

Comment thread src/esp/sensor/SensorFactory.cpp Outdated
Comment thread src/esp/bindings/SimBindings.cpp Outdated
.def_property_readonly(
"gfx_replay_manager", &Simulator::getGfxReplayManager,
R"(Use gfx_replay_manager for replay recording and playback.)")
.def_property_read_only("sensor_suite", &Simulator::getSensorSuite)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to conflict with the Simulator python class. Furthermore, the Python sensorsuite is entirely seperate from the C++ one, we need to do more work before merging them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback Aaron, I see now that the python sensor suite is a Dict[str, hsim.Sensor], while the C++ SensorSuite is std::map<std::string, Sensor::ptr>. In this PR, we are making changes that we'd like to see on the C++ side, but I don't know enough about the python side to know if we'd like to see the same changes. It seems like the functionality to just add sensors already exists, right? Do you think I should modify how python sensors work to add them using this SensorFactory method, or just leave it as is?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I think I understand now, we should implement a similar SensorFactory method on the python side to create sensors.

Comment thread src/esp/bindings/SensorBindings.cpp Outdated
.def("get", &SensorSuite::get, R"(get the sensor by id)");

// ==== SensorFactory ====
py::class_<SensorFactory>(m, "SensorFactory")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a factory method on the Sensorsuite, there is no reason for this particular class to be bound in Python like so. You should add python tests to see the divergent behavior.

@bigbike
Copy link
Copy Markdown
Contributor

bigbike commented Feb 5, 2021

@Skylion007 : when you have time, can you take a look at the slack channel? Thanks!

@vauduong vauduong marked this pull request as ready for review February 9, 2021 10:22
Comment thread src/esp/sim/Simulator.cpp Outdated
Comment thread src/esp/sim/Simulator.cpp Outdated
Comment thread src/esp/sim/Simulator.cpp Outdated
Comment thread src/esp/sim/Simulator.cpp Outdated
Comment thread src/esp/sensor/Sensor.h
Comment thread src/esp/sim/Simulator.h Outdated
* @param resolution vec2i of resolution to set spec to
*
*/
void addSensorToObject(const int objectId,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When designing this API, please take a look at the definitions of the other similar "add" functions, such as addAgent.

The return can be a pointer to that new sensor. So you can memorize this sensor in your demo. You do not need that global sensorSuite_ in the simulator.
The input should be object Id or a scene::SceneNode of that object (You can create an overload function, and use one to call the other.) + a sensorSpec (instead of the pos + orientation + resolution).

Comment thread src/esp/sim/Simulator.h Outdated

std::vector<agent::Agent::ptr> agents_;
// SensorSuite of all sensors available to the simulator
esp::sensor::SensorSuite sensorSuite_;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think you need this sensorSuite_. Maybe you can have one defined in the viewer.cpp for your demo, but not in the simulator.
@aclegg3 : How do you think? I remembered that when we discuss this project, you mentioned once that we might need such a global sensorSuite to collect all the sensors? Can you think of any usage case where such a global sensorSuite is a must-have?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python uses its own for backwards compatability, but there is no reason if you can quickly and easily get all the sensors in the scene.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comments - I included a global sensorSuite_ into Simulator to keep track of all the sensors. I thought about just keeping a pointers to the last added sensor, but later reasoned that it would be useful to be able to get all the sensors, not just the agent sensor and the last added sensor.

I'm happy to change this, but right now it seems like it's not so easy to get all the sensors in the scene.

Let me know any thoughts or if there is a better way to keep track of all the sensors! :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is that why you need to get all the sensors in the scene?
When you add a sensor to an object, the "add" function should give a pointer to this sensor, and you keep it somewhere so that you can use it later.

I am not against to have such a sensorSuite in the viewer.cpp for example, but not here, in the simulator class.

Comment thread src/tests/SimTest.cpp Outdated
Comment thread src/utils/viewer/viewer.cpp Outdated

const int defaultAgentId_ = 0;
esp::agent::Agent::ptr defaultAgent_ = nullptr;
std::string lastAddedObjectName_ = "";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the new code in the viewer.cpp is for your demo only. Perhaps you do not have to commit it in with this PR.
So this PR should include probably everything expect the viewer.cpp.
Then you create a new branch on top of this branch, vauduong:decouple-sensors (careful, not on top of the master) and put the modified viewer.cpp in that new branch (no PR is needed.)

Comment thread src/esp/sim/Simulator.cpp Outdated
Comment thread src/esp/gfx/RenderCamera.h
@vauduong vauduong requested a review from mosra February 9, 2021 21:21
@vauduong
Copy link
Copy Markdown
Contributor Author

vauduong commented Feb 9, 2021

EDIT: Can hold off on review, going to make some changes - getting rid of global SensorSuite
EDIT 2: Changes done
Thanks @bigbike for the feedback! If anyone else could give me another review and feedback on things to fix that would be greatly appreciated as well!

@aclegg3 @eundersander @jturner65 @Skylion007 @mosra @erikwijmans

Comment thread src/utils/viewer/viewer.cpp Outdated
Copy link
Copy Markdown
Contributor

@bigbike bigbike left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is improved a lot. We are almost there. :)

Comment thread src/esp/sim/Simulator.cpp Outdated
Comment thread src/tests/SensorTest.cpp Outdated
Comment thread src/tests/SimTest.cpp Outdated
@vauduong
Copy link
Copy Markdown
Contributor Author

@aclegg3 @eundersander @jturner65 @Skylion007 @mosra @erikwijmans @bigbike
Thanks everyone for the suggestions! viewer code for demo has been moved to https://github.com/vauduong/habitat-sim/tree/add-sensors-to-object-viewer-demo, and I'm looking for any final reviews for this PR before merging.

Copy link
Copy Markdown
Contributor

@aclegg3 aclegg3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. 👍

@vauduong vauduong merged commit 96f5fa1 into facebookresearch:master Feb 12, 2021
vauduong added a commit to vauduong/habitat-sim that referenced this pull request Feb 12, 2021
…1064)

- Create a factory class SensorFactory, which contains a static method to return a SensorSuite given a reference to a - - SceneNode and reference to a vector of SensorSpec::ptrs called SensorSetup.
- Add method SensorSuite::merge to merge the sensors from another SensorSuite into its existing map of sensors
- SensorFactory is called in Simulator::AddAgent to provide sensors.
- Add method Simulator::addSensorToObject to add a sensor at a node, given the node's objectId and the specification of the sensor
- Add tests in SimTest and SensorTest
vauduong added a commit to vauduong/habitat-sim that referenced this pull request Feb 12, 2021
…1064)

- Create a factory class SensorFactory, which contains a static method to return a SensorSuite given a reference to a - - SceneNode and reference to a vector of SensorSpec::ptrs called SensorSetup.
- Add method SensorSuite::merge to merge the sensors from another SensorSuite into its existing map of sensors
- SensorFactory is called in Simulator::AddAgent to provide sensors.
- Add method Simulator::addSensorToObject to add a sensor at a node, given the node's objectId and the specification of the sensor
- Add tests in SimTest and SensorTest
vauduong added a commit to vauduong/habitat-sim that referenced this pull request Feb 12, 2021
…1064)

- Create a factory class SensorFactory, which contains a static method to return a SensorSuite given a reference to a - - SceneNode and reference to a vector of SensorSpec::ptrs called SensorSetup.
- Add method SensorSuite::merge to merge the sensors from another SensorSuite into its existing map of sensors
- SensorFactory is called in Simulator::AddAgent to provide sensors.
- Add method Simulator::addSensorToObject to add a sensor at a node, given the node's objectId and the specification of the sensor
- Add tests in SimTest and SensorTest
Comment thread src/esp/sensor/SensorFactory.h

#include "esp/scene/SceneNode.h"
#include "esp/sensor/CameraSensor.h"
#include "esp/sensor/Sensor.h"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vauduong : This is duplicated header file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants