forked from rockin-robot-challenge/at_home_rsbb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_zone_base.h
389 lines (325 loc) · 8.47 KB
/
core_zone_base.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Copyright 2014 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
*
* This file is part of RoAH RSBB.
*
* RoAH RSBB is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RoAH RSBB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with RoAH RSBB. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __CORE_ZONE_BASE_H__
#define __CORE_ZONE_BASE_H__
#include "core_includes.h"
#include "core_shared_state.h"
struct Event {
string benchmark_code;
Benchmark benchmark;
string team;
string password;
unsigned round;
unsigned run;
Time scheduled_time;
// Duration interval_time;
Event (YAML::Node const& event_node)
{
benchmark_code = yamlschedget<string> (event_node, "benchmark");
team = yamlschedget<string> (event_node, "team");
round = yamlschedget<unsigned> (event_node, "round");
run = yamlschedget<unsigned> (event_node, "run");
scheduled_time = Time::fromBoost (boost::posix_time::time_from_string (yamlschedget<string> (event_node, "scheduled_time")));
// interval_time = Duration (yamlschedget<double> (event_node, "interval_time"));
}
};
class DisplayText
: boost::noncopyable
{
ostringstream text_;
string last_;
public:
DisplayText()
{
}
void
add (Time const& now,
string const& msg)
{
if (msg == last_) {
return;
}
last_ = msg;
text_ << endl;
text_ << " - " << to_string (now);
//text_ << endl;
text_ << " - ";
text_ << msg;
}
void
add (string const& msg)
{
add (Time::now(), msg);
}
string
str()
{
return text_.str();
}
string
last (size_t length = 1)
{
string ret = text_.str();
if (length >= ret.size()) {
return ret;
}
return ret.substr (ret.size() - length);
}
};
class RsbbLog
: boost::noncopyable
{
rosbag::Bag bag_;
DisplayText& display_text_;
public:
RsbbLog (string const& team,
unsigned round,
unsigned run,
string const& uuid,
DisplayText& display_text)
: display_text_ (display_text)
{
string log_dir = param_direct<string> ("~log_dir", ".");
system (string ("mkdir -p " + log_dir).c_str());
ostringstream o;
o << log_dir << "/online_log_";
o << to_string (Time::now());
o << "_" << team << "_round" << round << "_run" << run;
o << "_" << uuid << ".bag";
bag_.open (o.str(), rosbag::bagmode::Write);
}
~RsbbLog()
{
bag_.close();
}
void
log_empty (string const& topic,
Time const& time)
{
std_msgs::Empty msg;
bag_.write (topic, time, msg);
display_text_.add (time, topic);
}
void
log_uint8 (string const& topic,
Time const& time,
uint8_t i)
{
std_msgs::UInt8 msg;
msg.data = i;
bag_.write (topic, time, msg);
display_text_.add (time, topic + "\n" + to_string (i));
}
void
log_string (string const& topic,
Time const& time,
string const& s)
{
std_msgs::String msg;
msg.data = s;
bag_.write (topic, time, msg);
display_text_.add (time, topic + "\n" + s);
}
void
log_score (string const& topic,
Time const& time,
roah_rsbb::Score const& msg)
{
bag_.write (topic, time, msg);
display_text_.add (time, topic + "\n" + msg.group + ", " + msg.desc + " -> " + to_string (msg.value));
}
void
set_state (Time const& now,
roah_rsbb_msgs::BenchmarkState::State const& state,
string const& desc)
{
string state_name;
switch (state) {
case roah_rsbb_msgs::BenchmarkState_State_STOP:
state_name = "BenchmarkState_State_STOP";
break;
case roah_rsbb_msgs::BenchmarkState_State_PREPARE:
state_name = "BenchmarkState_State_PREPARE";
break;
case roah_rsbb_msgs::BenchmarkState_State_GOAL_TX:
state_name = "BenchmarkState_State_GOAL_TX";
break;
case roah_rsbb_msgs::BenchmarkState_State_WAITING_RESULT:
state_name = "BenchmarkState_State_WAITING_RESULT";
break;
}
log_uint8 ("/rsbb_log/rsbb_state", now, state);
log_string ("/rsbb_log/rsbb_state_str", now, state_name);
log_string ("/rsbb_log/rsbb_state_desc", now, desc);
}
void
end ()
{
log_empty ("/rsbb_log/end", Time::now());
}
#if 0
template<class T>
void write (std::string const& topic, ros::Time const& time, T const& msg,
boost::shared_ptr<ros::M_string> connection_header = boost::shared_ptr<ros::M_string>())
{
bag_.write<T> (topic, time, msg, connection_header);
}
#endif
};
class ReceiverRepeated
{
set <string> last_;
RsbbLog& log_;
string topic_;
DisplayText& display_text_;
public:
ReceiverRepeated (RsbbLog& log,
string const& topic,
DisplayText& display_text)
: log_ (log)
, topic_ (topic)
, display_text_ (display_text)
{
}
void
receive (Time const& now,
::google::protobuf::RepeatedPtrField<string> const& field)
{
set<string> this_set;
for (string const& s : field) {
this_set.insert (s);
if (0 == last_.count (s)) {
display_text_.add (now, topic_ + "\n" + s);
log_.log_string (topic_, now, s);
}
}
last_ = move (this_set);
}
};
class TimeControl
{
CoreSharedState& ss_;
Duration timeout_;
Time start_time_;
Duration delay_acc_;
bool paused_;
Time pause_start_;
Timer timeout_timer_;
const function<void (void) > timeout_2_;
void
timeout (const TimerEvent& timer_event)
{
if (paused_) {
return;
}
if (start_timer (timer_event.current_real)) {
return;
}
timeout_2_();
}
bool
start_timer (Time const& now)
{
Duration until_timeout = get_until_timeout (now);
timeout_timer_.stop();
if (until_timeout > Duration ()) {
timeout_timer_ = ss_.nh.createTimer (until_timeout, &TimeControl::timeout, this, true, true);
return true;
}
return false;
}
public:
TimeControl (CoreSharedState& ss,
Duration timeout,
function<void (void) > const& timeout_2)
: ss_ (ss)
, timeout_ (timeout)
, delay_acc_()
, paused_ (false)
, timeout_timer_ ()
, timeout_2_ (timeout_2)
{
}
~TimeControl()
{
timeout_timer_.stop();
}
void
start_reset (Time const& now)
{
start_time_ = now;
delay_acc_ = Duration();
paused_ = false;
start_timer (now);
}
void
start_reset (Time const& now,
Duration const& new_timeout)
{
timeout_ = new_timeout;
start_reset (now);
}
void
stop_pause (Time const& now)
{
if (paused_) {
return;
}
paused_ = true;
pause_start_ = now;
timeout_timer_.stop();
}
void
resume (Time const& now)
{
if (! paused_) {
return;
}
delay_acc_ += (now - pause_start_);
paused_ = false;
start_timer (now);
}
void
resume_hot (Time const& now)
{
if (! paused_) {
return;
}
// Does not accumulate, stopped time actually counts in resets
paused_ = false;
start_timer (now);
}
Duration
get_until_timeout (Time const& now)
{
return start_time_ + timeout_ + delay_acc_ - (paused_ ? pause_start_ : now);
}
Duration
get_until_timeout_for_timeout (Time const& now,
Duration const& timeout)
{
return start_time_ + timeout + delay_acc_ - (paused_ ? pause_start_ : now);
}
Duration
get_elapsed (Time const& now)
{
return (paused_ ? pause_start_ : now) - start_time_ - delay_acc_;
}
};
#endif