Skip to content

Added reset simulation tutorial #1824

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

Merged
merged 4 commits into from
Mar 1, 2023
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
6 changes: 5 additions & 1 deletion examples/plugin/system_plugin/SampleSystem.hh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ namespace sample_system
// and ISystemPostUpdate interfaces.
public gz::sim::ISystemPreUpdate,
public gz::sim::ISystemUpdate,
public gz::sim::ISystemPostUpdate
public gz::sim::ISystemPostUpdate,
public gz::sim::ISystemReset
{
public: SampleSystem2();

Expand All @@ -59,6 +60,9 @@ namespace sample_system

public: void PostUpdate(const gz::sim::UpdateInfo &_info,
const gz::sim::EntityComponentManager &_ecm) override;

public: void Reset(const gz::sim::UpdateInfo &_info,
gz::sim::EntityComponentManager &_ecm) override;
};
}
//! [header]
Expand Down
9 changes: 8 additions & 1 deletion examples/plugin/system_plugin/SampleSystem2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ GZ_ADD_PLUGIN(
gz::sim::System,
sample_system::SampleSystem2::ISystemPreUpdate,
sample_system::SampleSystem2::ISystemUpdate,
sample_system::SampleSystem2::ISystemPostUpdate)
sample_system::SampleSystem2::ISystemPostUpdate,
sample_system::SampleSystem2::ISystemReset)
//! [registerSampleSystem2]

using namespace sample_system;
Expand Down Expand Up @@ -38,3 +39,9 @@ void SampleSystem2::PostUpdate(const gz::sim::UpdateInfo &_info,
{
gzmsg << "SampleSystem2::PostUpdate" << std::endl;
}

void SampleSystem2::Reset(const gz::sim::UpdateInfo &_info,
gz::sim::EntityComponentManager &_ecm)
{
gzmsg << "SampleSystem2::Reset" << std::endl;
}
1 change: 1 addition & 0 deletions tutorials.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Gazebo @GZ_DESIGNATION_CAP@ library and how to use the library effectively.
* \subpage spherical_coordinates "Spherical coordinates": Working with latitude and longitude
* \subpage python_interfaces Python interfaces
* \subpage headless_rendering "Headless rendering": Access the GPU on a remote machine to produce sensor data without an X server.
* \subpage reset_simulation Reset simulation
* \subpage move_camera_to_model Move camera to model

**Migration from Gazebo classic**
Expand Down
11 changes: 7 additions & 4 deletions tutorials/create_system_plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ steps below.
## Decide on interfaces to implement

The first step of implementing a system plugin is to determine the subset of
available interfaces to implement. Aside from the base `System` object,
there are currently three additional available interfaces:
available interfaces to implement. Aside from the base `System` object,
there are currently four additional available interfaces:

1. ISystemConfigure
1. Has read-write access to world entities and components.
Expand All @@ -27,13 +27,16 @@ there are currently three additional available interfaces:
1. Has read-write access to world entities and components.
2. This is where systems say what they'd like to happen at time gz::sim::UpdateInfo::simTime.
3. Can be used to modify state before physics runs, for example for applying control signals or performing network synchronization.
2. ISystemUpdate
3. ISystemUpdate
1. Has read-write access to world entities and components.
2. Used for physics simulation step (i.e., simulates what happens at time gz::sim::UpdateInfo::simTime).
3. ISystemPostUpdate
4. ISystemPostUpdate
1. Has read-only access to world entities and components.
2. Captures everything that happened at time gz::sim::UpdateInfo::simTime.
3. Used to read out results at the end of a simulation step to be used for sensor or controller updates.
5. ISystemReset
1. Has read-write access to world entities and components.
2. Executed once the moment the plugin is reseted.

It's important to note that gz::sim::UpdateInfo::simTime does not refer to the current time, but the time reached after the `PreUpdate` and `Update` calls have finished.
So, if any of the `*Update` functions are called with simulation paused, time does not advance, which means the time reached after `PreUpdate` and `Update` is the same as the starting time.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions tutorials/reset_simulation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
\page reset_simulation Reset simulation

The Reset Gazebo transport API is exposed to allow resetting simulation to time zero.
It's possible to call this API using the command line or through the GUI.
In addition to the API, we have also expanded the simulation system API with a Reset interface.
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there limitations to the Reset system - are there known things that might have trouble being reset?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mjcarroll, when you implemented this feature, do you consider any limitations ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe that there are any major limitations with systems implementing the reset interface. They should have full access to the ECM functionality.

Systems that you may want to implement reset for are typically things that have a lot of loaded assets. This prevents having to reload those assets, and instead allow for a "warm" reboot of the system.


# Reset interface

System authors may now choose to implement the Reset interface to have a more intelligent
reset process (avoiding reloading assets or regenerating scene graphs being the motivating examples).
Since this interface is opt-in, systems that don't implement the API will still be reset via destruction and reconstruction.
The [physics](https://github.com/gazebosim/gz-sim/blob/gz-sim7/src/systems/physics/Physics.cc#L928-L937) and [rendering systems](https://github.com/gazebosim/gz-sim/blob/gz-sim7/src/systems/scene_broadcaster/SceneBroadcaster.cc#L452-L458) are the first two to implement this optimized reset functionality, with more to come as it makes sense to.

Following the tutorial \subpage createsystemplugins we should implement `ISystemReset` interface.

# Transport API

To call the reset transport API we should call the service `/world/default/control` and fill the request message type
`gz.msgs.WorldControl`, this service return a `gz.msgs.Boolean` with the status of the reset (true is everything was fine, false otherwise)

The `WorldControl` message now contains a reset field that we should filled if we want to reset the world:

```bash
gz service -s /world/default/control --reqtype gz.msgs.WorldControl --reptype gz.msgs.Boolean --timeout 3000 --req 'reset: {all: true}'
```

# GUI

We included a new button in the `World Control` plugin allowing to reset the simulation from the GUI

@image html files/reset_simulation/reset_button.gif