Skip to content

Commit bf8955c

Browse files
authored
Merge pull request #106 from resibots/status_bar
fix the real time factor + improve scheduler + improve status bar
2 parents ffa3579 + 475db8a commit bf8955c

File tree

7 files changed

+46
-24
lines changed

7 files changed

+46
-24
lines changed

src/examples/iiwa.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ int main()
3939
simu.add_checkerboard_floor();
4040
simu.add_robot(global_robot);
4141
simu.add_robot(ghost);
42+
simu.set_text_panel("IIWA simulation");
4243
simu.run(20.);
4344

4445
global_robot.reset();

src/python/utils.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,27 @@ namespace robot_dart {
1313
py::arg("dt"),
1414
py::arg("sync") = false)
1515

16-
.def("__call__", &Scheduler::operator())
17-
.def("schedule", &Scheduler::schedule)
16+
.def("__call__", &Scheduler::operator(),
17+
py::arg("frequency"))
18+
.def("schedule", &Scheduler::schedule,
19+
py::arg("frequency"))
1820

1921
.def("step", &Scheduler::step)
2022

2123
.def("reset", &Scheduler::reset,
2224
py::arg("dt"),
2325
py::arg("sync") = false,
24-
py::arg("current_time") = 0.)
26+
py::arg("current_time") = 0.,
27+
py::arg("real_time") = 0.)
2528

2629
.def("set_sync", &Scheduler::set_sync)
2730
.def("sync", &Scheduler::sync)
2831

2932
.def("current_time", &Scheduler::current_time)
3033
.def("next_time", &Scheduler::next_time)
34+
.def("real_time", &Scheduler::real_time)
35+
.def("real_time_factor", &Scheduler::real_time_factor)
36+
.def("it_duration", &Scheduler::it_duration)
3137
.def("dt", &Scheduler::dt);
3238
}
3339
} // namespace python

src/robot_dart/gui/magnum/glfw_application.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ namespace robot_dart {
3333
/* Initialize DART world */
3434
init(simu, Magnum::GL::defaultFramebuffer.viewport().size()[0], Magnum::GL::defaultFramebuffer.viewport().size()[1]);
3535

36-
/* Loop at 60 Hz max */
37-
setSwapInterval(1);
36+
/* No VSync */
37+
setSwapInterval(0);
3838

3939
redraw();
4040
}

src/robot_dart/gui/magnum/gs/camera.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,9 @@ namespace robot_dart {
304304
if (text->text.empty()) // ignore empty strings
305305
continue;
306306

307-
Magnum::GL::Mesh mesh;
307+
Magnum::GL::Mesh mesh{Magnum::NoCreate};
308308
Magnum::Range2D rectangle;
309-
std::tie(mesh, rectangle) = Magnum::Text::Renderer2D::render(*debug_data.font, *debug_data.cache, 28.f, text->text, *debug_data.text_vertices, *debug_data.text_indices, Magnum::GL::BufferUsage::StaticDraw, Magnum::Text::Alignment(text->alignment));
309+
std::tie(mesh, rectangle) = Magnum::Text::Renderer2D::render(*debug_data.font, *debug_data.cache, 28.f, text->text, *debug_data.text_vertices, *debug_data.text_indices, Magnum::GL::BufferUsage::DynamicDraw, Magnum::Text::Alignment(text->alignment));
310310

311311
auto viewport = Magnum::Vector2{_camera->viewport()};
312312
auto sc = Magnum::Vector2{viewport.max() / 1024.f};

src/robot_dart/robot_dart_simu.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ namespace robot_dart {
282282
if (update_control_freq)
283283
_control_freq = _physics_freq;
284284

285-
_scheduler.reset(timestep, _scheduler.sync(), _scheduler.current_time());
285+
_scheduler.reset(timestep, _scheduler.sync(), _scheduler.current_time(), _scheduler.real_time());
286286
}
287287

288288
Eigen::Vector3d RobotDARTSimu::gravity() const
@@ -456,11 +456,13 @@ namespace robot_dart {
456456
out.precision(3);
457457
double rt = _scheduler.real_time();
458458

459-
out << std::fixed << "[Simulation: " << _world->getTime()
459+
out << std::fixed << "[simulation time: " << _world->getTime()
460460
<< "s ] ["
461-
<< "Real: " << rt << "s] [";
461+
<< "wall time: " << rt << "s] [";
462462
out.precision(1);
463-
out << _world->getTime() / rt << "x]";
463+
out << _scheduler.real_time_factor() << "x]";
464+
out << " [it: " << _scheduler.it_duration() * 1e3 << " ms]";
465+
out << (_scheduler.sync() ? " [sync]" : " [no-sync]");
464466

465467
return out.str();
466468
}

src/robot_dart/scheduler.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace robot_dart {
44
bool Scheduler::schedule(int frequency)
55
{
6-
if (_max_frequency == -1)
7-
_start = clock_t::now();
6+
if (_max_frequency == -1) {
7+
_start_time = clock_t::now();
8+
_last_iteration_time = _start_time;
9+
}
810

911
_max_frequency = std::max(_max_frequency, frequency);
1012
double period = std::round((1. / frequency) / _dt);
@@ -18,13 +20,14 @@ namespace robot_dart {
1820
return false;
1921
}
2022

21-
void Scheduler::reset(double dt, bool sync, double current_time)
23+
void Scheduler::reset(double dt, bool sync, double current_time, double real_time)
2224
{
2325
ROBOT_DART_EXCEPTION_INTERNAL_ASSERT(dt > 0. && "Time-step needs to be bigger than zero.");
2426

2527
_current_time = 0.;
2628
_real_time = 0.;
2729
_simu_start_time = current_time;
30+
_real_start_time = real_time;
2831
_current_step = 0;
2932
_max_frequency = -1;
3033

@@ -38,7 +41,9 @@ namespace robot_dart {
3841
_current_step += 1;
3942

4043
auto end = clock_t::now();
41-
std::chrono::duration<double, std::micro> real = end - _start;
44+
_it_duration = std::chrono::duration<double, std::micro>(end - _last_iteration_time).count();
45+
_last_iteration_time = end;
46+
std::chrono::duration<double, std::micro> real = end - _start_time;
4247
if (_sync) {
4348
auto expected = std::chrono::microseconds(int(_current_time * 1e6));
4449
std::chrono::duration<double, std::micro> adjust = expected - real;
@@ -47,7 +52,7 @@ namespace robot_dart {
4752
}
4853

4954
_real_time = real.count() * 1e-6;
50-
return _real_time;
55+
return _real_start_time + _real_time;
5156
}
5257

5358
} // namespace robot_dart

src/robot_dart/scheduler.hpp

+16-8
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,34 @@ namespace robot_dart {
2727
/// returns the real-time (in seconds)
2828
double step();
2929

30-
void reset(double dt, bool sync = false, double current_time = 0.);
30+
void reset(double dt, bool sync = false, double current_time = 0., double real_time = 0.);
3131

32+
/// synchronize the simulation clock with the wall clock
33+
/// (when possible, i.e. when the simulation is faster than real time)
3234
void set_sync(bool enable) { _sync = enable; }
33-
bool sync() { return _sync; }
35+
bool sync() const { return _sync; }
3436

35-
/// current time according to the simulation
37+
/// current time according to the simulation (simulation clock)
3638
double current_time() const { return _simu_start_time + _current_time; }
37-
/// next time according to the simulation
39+
/// next time according to the simulation (simulation clock)
3840
double next_time() const { return _simu_start_time + _current_time + _dt; }
39-
/// time according to the clock's computer
40-
double real_time() const { return _real_time; }
41+
/// time according to the clock's computer (wall clock)
42+
double real_time() const { return _real_start_time + _real_time; }
43+
/// dt used by the simulation (simulation clock)
4144
double dt() const { return _dt; }
45+
// 0.8x => we are simulating at 80% of real time
46+
double real_time_factor() const { return _dt / it_duration(); }
47+
// time for a single iteration (wall-clock)
48+
double it_duration() const { return _it_duration * 1e-6; }
4249

4350
protected:
44-
double _current_time = 0., _simu_start_time = 0., _real_time = 0;
51+
double _current_time = 0., _simu_start_time = 0., _real_time = 0., _real_start_time = 0., _it_duration = 0.;
4552
double _dt;
4653
int _current_step = 0;
4754
bool _sync;
4855
int _max_frequency = -1;
49-
clock_t::time_point _start;
56+
clock_t::time_point _start_time;
57+
clock_t::time_point _last_iteration_time;
5058
};
5159
} // namespace robot_dart
5260

0 commit comments

Comments
 (0)