Skip to content

Commit ca1bffb

Browse files
authored
Integrate clock synchronization across swarm agents (#81)
* Started integration of timesync * Added timesync thread * Added support for clock synchronization and ping measurement * Clean up comments and docstrings * Cleaned up implementation and examples for PR
1 parent 4d7582b commit ca1bffb

File tree

8 files changed

+284
-38
lines changed

8 files changed

+284
-38
lines changed

examples/arming.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ def print_message_response_cb(future: Future) -> None:
5555
f"({response.target_agent_id}): {response.code}"
5656
)
5757
else:
58-
for response in responses:
59-
print(
60-
f"Result of {response.message_type} message sent to "
61-
f"({response.target_agent_id}): {response.code}"
62-
)
58+
print(
59+
f"Result of {responses.message_type} message sent to "
60+
f"({responses.target_agent_id}): {responses.code}"
61+
)
6362

6463
return
6564

examples/check_state.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def main() -> None:
6969
agent = mavswarm.get_agent_by_id(agent_id)
7070

7171
if agent is not None:
72-
print(f"The current attitude of {agent} is: {agent.attitude}")
72+
# print(f"The current attitude of {agent} is: {agent.attitude}")
73+
pass
7374

7475
time.sleep(0.5)
7576

examples/companion_computer.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ def print_message_response_cb(future: Future) -> None:
6565
f"({response.target_agent_id}): {response.code}"
6666
)
6767
else:
68-
for response in responses:
69-
print(
70-
f"Result of {response.message_type} message sent to "
71-
f"({response.target_agent_id}): {response.code}"
72-
)
68+
print(
69+
f"Result of {responses.message_type} message sent to "
70+
f"({responses.target_agent_id}): {responses.code}"
71+
)
7372

7473
return
7574

examples/set_flight_mode.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ def print_message_response_cb(future: Future) -> None:
5757
f"({response.target_agent_id}): {response.code}"
5858
)
5959
else:
60-
for response in responses:
61-
print(
62-
f"Result of {response.message_type} message sent to "
63-
f"({response.target_agent_id}): {response.code}"
64-
)
60+
print(
61+
f"Result of {responses.message_type} message sent to "
62+
f"({responses.target_agent_id}): {responses.code}"
63+
)
6564

6665
return
6766

examples/set_home_position.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ def print_message_response_cb(future: Future) -> None:
5656
f"({response.target_agent_id}): {response.code}"
5757
)
5858
else:
59-
for response in responses:
60-
print(
61-
f"Result of {response.message_type} message sent to "
62-
f"({response.target_agent_id}): {response.code}"
63-
)
59+
print(
60+
f"Result of {responses.message_type} message sent to "
61+
f"({responses.target_agent_id}): {responses.code}"
62+
)
6463

6564
return
6665

pymavswarm/agent.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
from __future__ import annotations
1818

19+
from collections import deque
20+
from statistics import fmean
1921
from typing import Any
2022

2123
import monotonic
@@ -110,6 +112,10 @@ def __init__(
110112
self.__hrl_state = swarm_state.Generic(
111113
"hrl_state", None, optional_context_props=context_props
112114
)
115+
self.__ping = swarm_state.Generic(
116+
"ping", 0, optional_context_props=context_props
117+
)
118+
self.__clock_offset: deque[int] = deque(maxlen=5)
113119

114120
return
115121

@@ -328,10 +334,43 @@ def hrl_state(self) -> Generic:
328334
"""
329335
HRL state that the agent is in.
330336
337+
:return: hrl state
331338
:rtype: str
332339
"""
333340
return self.__hrl_state
334341

342+
@property
343+
def ping(self) -> Generic:
344+
"""
345+
Agent latency [ms].
346+
347+
:return: latency
348+
:rtype: Generic
349+
"""
350+
return self.__ping
351+
352+
def update_clock_offset(self, offset: int) -> None:
353+
"""
354+
Update the agent clock offset relative to the source clock.
355+
356+
:param offset: clock offset
357+
:type offset: int
358+
"""
359+
self.__clock_offset.append(offset)
360+
return
361+
362+
@property
363+
def clock_offset(self) -> int:
364+
"""
365+
Average offset between the source clock and agent clock.
366+
367+
This property does not implement a watcher interface.
368+
369+
:return: clock offset
370+
:rtype: int
371+
"""
372+
return int(fmean(self.__clock_offset))
373+
335374
def __str__(self) -> str:
336375
"""
337376
Print agent information in a human-readable format.

0 commit comments

Comments
 (0)