Swarm-SLAM  1.0.0
C-SLAM Framework
simulated_rendezvous.cpp
Go to the documentation of this file.
2 
3 using namespace cslam;
4 
5 SimulatedRendezVous::SimulatedRendezVous(std::shared_ptr<rclcpp::Node> &node,
6  const std::string &schedule_file,
7  const unsigned int &robot_id) : node_(node), robot_id_(robot_id), enabled_(true)
8 {
9  try
10  {
11  int64_t initial_time = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
12  std::ifstream schedule(schedule_file);
13  if (schedule.is_open())
14  {
15  for (std::string line; std::getline(schedule, line);)
16  {
17  auto delim0 = line.find(",");
18  if (robot_id_ == std::stoul(line.substr(0, delim0)))
19  {
20  RCLCPP_INFO(node_->get_logger(), "Simulated rendezvous schedule of robot %s", line.c_str());
21  while (delim0 != std::string::npos)
22  {
23  auto delim1 = line.find(",", delim0 + 1);
24 
25  auto start = std::stoull(line.substr(delim0 + 1, delim1)) + initial_time;
26 
27  delim0 = delim1;
28  delim1 = line.find(",", delim0);
29 
30  auto end = std::stoull(line.substr(delim0 + 1, delim1)) + initial_time;
31 
32  rendezvous_ranges_.push_back(std::make_pair(start, end));
33 
34  delim0 = line.find(",", delim1 +1);
35  }
36  }
37  }
38  schedule.close();
39  } else {
40  RCLCPP_ERROR(node_->get_logger(), "Unable to open rendezvous schedule file");
41  enabled_ = false;
42  }
43  }
44  catch (const std::exception &e)
45  {
46  RCLCPP_ERROR(node_->get_logger(), "Reading simulated rendezvous schedule failed: %s", e.what());
47  enabled_ = false;
48  }
49 }
50 
52 {
53  if (enabled_)
54  {
55  bool is_alive = false;
56  uint64_t current_time = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
57 
58  for (const auto &range : rendezvous_ranges_)
59  {
60  //RCLCPP_INFO(node_->get_logger(), "Time until rendezvous (%d,%d) = %d | Time until rendezvous end = %d", ((int) range.first), ((int) range.second), range.first - ((int)current_time), range.second - ((int)current_time));
61  if (current_time >= range.first && current_time <= range.second)
62  {
63  is_alive = true;
64  }
65  }
66  return is_alive;
67  }
68  return true;
69 }
bool is_alive()
Check if the robot is alive (aka in the rendez-vous schedule)
SimulatedRendezVous(std::shared_ptr< rclcpp::Node > &node, const std::string &schedule_file, const unsigned int &robot_id)
Rendez-vous simulation. Reads a config file indicating when the robot should be considered alive.
Definition: __init__.py:1