-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.py
63 lines (46 loc) · 1.71 KB
/
connection.py
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
import time
from networktables import NetworkTablesInstance
from typing import Tuple
NetworkTables = NetworkTablesInstance.getDefault()
RIO_IP = "10.47.74.2"
UDP_RECV_PORT = 5005
UDP_SEND_PORT = 5006
Results = Tuple[float, float, float]
class NTConnection:
def __init__(self, inst: NetworkTablesInstance = NetworkTables) -> None:
inst.initialize(server=RIO_IP)
self.inst = inst
nt = inst.getTable("/vision")
self.entry = nt.getEntry("data")
self.ping = nt.getEntry("ping")
self.raspi_pong = nt.getEntry("raspi_pong")
self.rio_pong = nt.getEntry("rio_pong")
self.fps_entry = nt.getEntry("fps")
self.old_fps_time = 0.0
self.last_ping_time = 0.0
self.time_to_pong = 0.00000001
self._get_time = time.monotonic
def send_results(self, results: Results) -> None:
self.entry.setDoubleArray(results)
self.inst.flush()
def pong(self) -> None:
self.ping_time = self.ping.getNumber(0)
if abs(self.ping_time - self.last_ping_time) > self.time_to_pong:
self.rio_pong.setNumber(self.ping_time)
self.raspi_pong.setNumber(self._get_time())
self.last_ping_time = self.ping_time
def set_fps(self) -> None:
current_time = time.monotonic()
fps = 1 / (current_time - self.old_fps_time)
self.old_fps_time = current_time
self.fps_entry.setDouble(fps)
class DummyConnection:
def __init__(self):
self.results = None
def send_results(self, results: Results) -> None:
print("results being sent", results)
self.results = results
def pong(self) -> None:
pass
def set_fps(self) -> None:
pass