bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
synchroniser.h
Go to the documentation of this file.
1#ifndef BITSIM_DISTRIBUTED_SYNCHRONISER_H
2#define BITSIM_DISTRIBUTED_SYNCHRONISER_H
3
7
8#include <boost/noncopyable.hpp>
9
10#include <chrono>
11#include <map>
12#include <optional>
13#include <string>
14#include <vector>
15
16namespace bitsim
17{
18namespace distributed
19{
20
24enum class SyncMode
25{
32
39};
40
63
68{
69 std::string participant;
71 real_t time{0.0};
72 real_t age{0.0};
73 bool stale{false};
74 bool left{false};
75 std::vector<BodyState> bodies;
76 std::vector<real_t> values;
77};
78
98class Synchroniser : private boost::noncopyable
99{
100 public:
101 Synchroniser(Transport &transport, const std::string &participant,
103
108
118 const std::vector<PeerState> &exchange(uint_t step, const std::vector<BodyState> &bodies,
119 const std::vector<real_t> &values = {});
120
124 void leave();
125
126 real_t time_of(uint_t step) const noexcept { return step * config_.sync_period; }
127
128 const std::string &participant() const noexcept { return participant_; }
129
130 const SynchroniserConfig &config() const noexcept { return config_; }
131
132 uint_t n_sent() const noexcept { return n_sent_; }
133 uint_t n_received() const noexcept { return n_received_; }
134 uint_t n_resent() const noexcept { return n_resent_; }
135
139 uint_t n_overruns() const noexcept { return n_overruns_; }
140
144 real_t max_wait() const noexcept { return max_wait_; }
145
146 private:
147 using steady = std::chrono::steady_clock;
148
149 void send_(const StateMessage &message);
150 void handle_(const std::string &bytes);
151 void drain_();
152 const std::vector<PeerState> &lockstep_(uint_t step);
153 const std::vector<PeerState> &wall_clock_(uint_t step);
154
155 Transport &transport_;
156 std::string participant_;
157 SynchroniserConfig config_;
158
159 std::optional<StateMessage> current_;
160 std::optional<StateMessage> previous_;
161 bool has_left_{false};
162
163 // lockstep: the states received per peer and step
164 std::map<std::string, std::map<uint_t, StateMessage>> pending_;
165 // wall-clock: the newest state per peer
166 std::map<std::string, StateMessage> latest_;
167 // the last step of the peers that left
168 std::map<std::string, uint_t> left_at_;
169
170 std::optional<steady::time_point> start_;
171 std::vector<PeerState> peers_;
172
173 uint_t n_sent_{0};
174 uint_t n_received_{0};
175 uint_t n_resent_{0};
176 uint_t n_overruns_{0};
177 real_t max_wait_{0.0};
178};
179
180} // namespace distributed
181} // namespace bitsim
182
183#endif // BITSIM_DISTRIBUTED_SYNCHRONISER_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
uint_t n_sent() const noexcept
Definition synchroniser.h:132
Synchroniser(Transport &transport, const std::string &participant, const SynchroniserConfig &config)
Definition synchroniser.cpp:20
real_t max_wait() const noexcept
lockstep: the longest exchange() waited [s]; wall-clock: the latest a step started
Definition synchroniser.h:144
uint_t n_received() const noexcept
Definition synchroniser.h:133
const std::vector< PeerState > & exchange(uint_t step, const std::vector< BodyState > &bodies, const std::vector< real_t > &values={})
Send the state of this participant's bodies at step, and get the others'.
Definition synchroniser.cpp:102
~Synchroniser()
Leaves, if leave() was not called.
Definition synchroniser.cpp:35
const std::string & participant() const noexcept
Definition synchroniser.h:128
uint_t n_resent() const noexcept
Definition synchroniser.h:134
const SynchroniserConfig & config() const noexcept
Definition synchroniser.h:130
uint_t n_overruns() const noexcept
wall-clock: the steps that started more than a sync period late
Definition synchroniser.h:139
void leave()
Tell the others that this participant stops after the last step it exchanged.
Definition synchroniser.cpp:299
real_t time_of(uint_t step) const noexcept
Definition synchroniser.h:126
How the participants of a distributed simulation reach each other: everything one publishes,...
Definition transport.h:34
Definition chrono_ghosts.cpp:12
SyncMode
How the participants of a distributed simulation keep time.
Definition synchroniser.h:25
@ WALL_CLOCK
Definition synchroniser.h:38
@ LOCKSTEP
Definition synchroniser.h:31
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
What a participant knows of another at a step.
Definition synchroniser.h:68
std::string participant
Definition synchroniser.h:69
real_t age
wall-clock: how long ago it was sent [s]; 0 in lockstep
Definition synchroniser.h:72
real_t time
the simulated time of the state
Definition synchroniser.h:71
std::vector< real_t > values
the values it sent with the state, as they were
Definition synchroniser.h:76
uint_t step
Definition synchroniser.h:70
bool left
the participant has left; bodies is its last state
Definition synchroniser.h:74
bool stale
wall-clock: older than stale_after, or never received
Definition synchroniser.h:73
std::vector< BodyState > bodies
Definition synchroniser.h:75
What a participant of a distributed simulation sends: the state of its bodies at one step,...
Definition state_message.h:64
The settings of Synchroniser.
Definition synchroniser.h:45
SyncMode mode
Definition synchroniser.h:46
real_t sync_period
the simulated time between two exchanges [s]
Definition synchroniser.h:47
real_t real_time_factor
wall-clock: simulated seconds per wall-clock second
Definition synchroniser.h:59
real_t timeout
lockstep: the longest wait for a step [s]
Definition synchroniser.h:56
std::vector< std::string > peers
Definition synchroniser.h:54
real_t resend_period
lockstep: send the last steps again this often while waiting [s]
Definition synchroniser.h:57
real_t stale_after
wall-clock: a state older than this is stale [s]
Definition synchroniser.h:60
bool extrapolate
wall-clock: move the states forward by their age
Definition synchroniser.h:61