$darkmode
simulation_sync.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2024 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
22 #pragma once
23 
24 #include <cstdint>
25 
26 #include <cloe/core/duration.hpp>
27 #include <cloe/sync.hpp>
28 
29 namespace engine {
30 
34 class SimulationSync : public cloe::Sync {
35  public: // Overrides
36  SimulationSync() = default;
37  SimulationSync(const SimulationSync &) = default;
38  SimulationSync(SimulationSync &&) = delete;
39  SimulationSync &operator=(const SimulationSync &) = default;
40  SimulationSync &operator=(SimulationSync &&) = delete;
41  virtual ~SimulationSync() = default;
42 
43  explicit SimulationSync(const cloe::Duration &step_width) : step_width_(step_width) {}
44 
45  uint64_t step() const override { return step_; }
46  cloe::Duration step_width() const override { return step_width_; }
47  cloe::Duration time() const override { return time_; }
48  cloe::Duration eta() const override { return eta_; }
49 
56  double realtime_factor() const override { return realtime_factor_; }
57 
62  double achievable_realtime_factor() const override {
63  return static_cast<double>(step_width().count()) / static_cast<double>(cycle_time_.count());
64  }
65 
66  public: // Modification
74  void increment_step() {
75  step_ += 1;
76  time_ += step_width_;
77  }
78 
83  void set_realtime_factor(double s) { realtime_factor_ = s; }
84 
85  void set_eta(cloe::Duration d) { eta_ = d; }
86 
87  void reset() {
88  time_ = cloe::Duration(0);
89  step_ = 0;
90  }
91 
92  void set_cycle_time(cloe::Duration d) { cycle_time_ = d; }
93 
94  private:
95  // Simulation State
96  uint64_t step_{0};
97  cloe::Duration time_{0};
98  cloe::Duration eta_{0};
99  cloe::Duration cycle_time_{0};
100 
101  // Simulation Configuration
102  double realtime_factor_{1.0}; // realtime
103  cloe::Duration step_width_{20'000'000}; // should be 20ms
104 };
105 
106 } // namespace engine
Definition: sync.hpp:34
Definition: simulation_sync.hpp:34
double realtime_factor() const override
Definition: simulation_sync.hpp:56
double achievable_realtime_factor() const override
Definition: simulation_sync.hpp:62
cloe::Duration step_width() const override
Definition: simulation_sync.hpp:46
cloe::Duration time() const override
Definition: simulation_sync.hpp:47
cloe::Duration eta() const override
Definition: simulation_sync.hpp:48
void increment_step()
Definition: simulation_sync.hpp:74
void set_realtime_factor(double s)
Definition: simulation_sync.hpp:83
uint64_t step() const override
Definition: simulation_sync.hpp:45
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36