Gzsim simulation standalone single process

Hi,
I need to do some unit testing of robot controllers with simulator feedback.
I would like to directly use gzsim library in c++ to run a simple simulation. Something like this:

int main()
{
    gz::sim::ServerConfig serverConfig;
    gz::sim::Server server(serverConfig);
    
   // add a static plane
   // add a box over the plane, with gravity
   // add imu sensor to box 
 
   for (size_t i = 0; i < 100; i++) {
        server.RunOnce(false);
        // get box position
        // get imu output
    }
    return 0;
}

All should run in single process without sending data with gz-transport
Is it possible?

Edit:
Ok, i found TestFixture that seems helpful.

int main()
{
    using namespace gz;
    gz::common::Console::SetVerbosity(4);

    // Load a world with a fixture
    gz::sim::ServerConfig config;
    config.SetSdfFile("sim.sdf");

    gz::sim::TestFixture fixture(config);

    // Register callbacks, for example:
    fixture
      .OnPostUpdate([&](const sim::UpdateInfo& info, const sim::EntityComponentManager& _ecm) {
          //   std::cout << "it " << info.iterations << "\n";
          // Access the falling box entity by its name
          _ecm.Each<gz::sim::components::Model, gz::sim::components::Pose>(
            [&](const gz::sim::Entity& _entity,
                const gz::sim::components::Model*,
                const gz::sim::components::Pose* _pose) -> bool {
                // std::cout << "Entity: " << _entity << "\n";
                std::string name = _ecm.Component<gz::sim::components::Name>(_entity)->Data();
                if (name == "falling_box") {
                    std::cout << info.iterations
                              << "\tFalling box position: " << _pose->Data().Pos() << "\n";
                }
                return true;
            });
      })
      .Finalize();
    // Be sure to call finalize before running the server.

    // Run the server
    fixture.Server()->Run(true, 1000, false);
    return 0;
}

Now this is working but i have 2 issues:

  • gz is still exposing topics (with gz topic -l i see it)
  • i dont know how to get output of Imu sensor, is there a way to directly access gz::sim::ImuSensor ?

Thanks

Sensor data is currently routed through gz-transport. There’s an issue (https://github.com/gazebosim/gz-sim/issues/2367) that includes making sensor data available from the ECM, but it’s still in the works.

For the record, even with gz-transport, the code as you have it still runs in a single process (i.e, you wouldn’t pay for serialization or network transport), but gz-transport has a background thread that calls your sensor data callbacks, so your code would have to be written differently.