Swarm-SLAM  1.0.0
C-SLAM Framework
neighbor_monitor.py
Go to the documentation of this file.
1 from std_msgs.msg import UInt32
2 from time import time
3 
5  """Monitors if a neighboring robot is in range
6  """
7 
8  def __init__(self, node, rid, is_enabled, init_delay_sec, max_delay_sec):
9  """Initialization
10  Args:
11  id (int): Robot ID
12  """
13  self.nodenode = node
14  self.robot_idrobot_id = rid
15  self.is_enabledis_enabled = is_enabled
16  self.origin_robot_idorigin_robot_id = self.robot_idrobot_id
17 
18  self.init_delay_secinit_delay_sec = init_delay_sec
19  self.max_delay_secmax_delay_sec = max_delay_sec
20  self.first_heartbeat_receivedfirst_heartbeat_received = False
21  self.init_timeinit_time = time()
22  self.latest_time_stamplatest_time_stamp = self.init_timeinit_time
23  self.last_keyframe_receivedlast_keyframe_received = -1
24  self.last_keyframe_sentlast_keyframe_sent = -1
25  self.last_match_sentlast_match_sent = -1
26 
27  self.heartbeat_subscriberheartbeat_subscriber = self.nodenode.create_subscription(
28  UInt32, '/r' + str(rid) + '/' + 'cslam/heartbeat',
29  self.heartbeat_callbackheartbeat_callback, 10)
30 
31  def heartbeat_callback(self, msg):
32  """Callback to indicate that it is alive
33 
34  Args:
35  msg (UInt32):
36  """
37  self.origin_robot_idorigin_robot_id = msg.data
38  self.latest_time_stamplatest_time_stamp = time()
39  if not self.first_heartbeat_receivedfirst_heartbeat_received:
40  self.first_heartbeat_receivedfirst_heartbeat_received = True
41  self.init_timeinit_time = time()
42 
43  def is_alive(self):
44  """Check if it recently received a heartbeat signal
45 
46  Returns:
47  bool: liveliness indicator
48  """
49  if self.is_enabledis_enabled:
50  now = time()
51  return self.first_heartbeat_receivedfirst_heartbeat_received and now - self.init_timeinit_time > self.init_delay_secinit_delay_sec and now - self.latest_time_stamplatest_time_stamp < self.max_delay_secmax_delay_sec
52  else:
53  True
def __init__(self, node, rid, is_enabled, init_delay_sec, max_delay_sec)