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