arflow.replay

A library for replaying ARFlow data.

 1"""A library for replaying ARFlow data."""
 2
 3import pickle
 4import threading
 5import time
 6from typing import List
 7
 8from arflow.core import ARFlowService
 9from arflow.service_pb2 import DataFrameRequest, RegisterRequest
10
11
12class ARFlowPlayer(threading.Thread):
13    """A class for replaying ARFlow data."""
14
15    service: ARFlowService
16    frame_data: List
17    n_frame: int
18
19    def __init__(self, service: ARFlowService, frame_data_path: str) -> None:
20        super().__init__()
21        self.service = service()
22        with open(frame_data_path, "rb") as f:
23            raw_data = pickle.load(f)
24
25        self.frame_data = []
26        start_delta = 0
27        for i, data in enumerate(raw_data):
28            if i == 0:
29                start_delta = data["time_stamp"] - 3
30                self.frame_data.append(
31                    {
32                        "time_stamp": data["time_stamp"] - start_delta,
33                        "data": RegisterRequest.FromString(data["data"]),
34                    }
35                )
36            else:
37                self.frame_data.append(
38                    {
39                        "time_stamp": data["time_stamp"] - start_delta,
40                        "data": DataFrameRequest.FromString(data["data"]),
41                    }
42                )
43
44        self.uid = self.frame_data[1]["data"].uid
45
46        self.period = 0.001  # Simulate a 1ms loop.
47        self.n_frame = 0
48
49        self.i = 0
50        self.t0 = time.time()
51        self.start()
52
53    def sleep(self):
54        self.i += 1
55        delta = self.t0 + self.period * self.i - time.time()
56        if delta > 0:
57            time.sleep(delta)
58
59    def run(self):
60        while True:
61            current_time = time.time() - self.t0
62
63            t = self.frame_data[self.n_frame]["time_stamp"]
64
65            if t - current_time < 0.001:
66                data = self.frame_data[self.n_frame]["data"]
67                if self.n_frame == 0:
68                    self.service.register(data, None, uid=self.uid)
69                else:
70                    self.service.data_frame(data, None)
71
72                self.n_frame += 1
73
74            if self.n_frame > len(self.frame_data) - 1:
75                break
76
77            self.sleep()
78
79        print("Reply finished.")
80        exit()
class ARFlowPlayer(threading.Thread):
13class ARFlowPlayer(threading.Thread):
14    """A class for replaying ARFlow data."""
15
16    service: ARFlowService
17    frame_data: List
18    n_frame: int
19
20    def __init__(self, service: ARFlowService, frame_data_path: str) -> None:
21        super().__init__()
22        self.service = service()
23        with open(frame_data_path, "rb") as f:
24            raw_data = pickle.load(f)
25
26        self.frame_data = []
27        start_delta = 0
28        for i, data in enumerate(raw_data):
29            if i == 0:
30                start_delta = data["time_stamp"] - 3
31                self.frame_data.append(
32                    {
33                        "time_stamp": data["time_stamp"] - start_delta,
34                        "data": RegisterRequest.FromString(data["data"]),
35                    }
36                )
37            else:
38                self.frame_data.append(
39                    {
40                        "time_stamp": data["time_stamp"] - start_delta,
41                        "data": DataFrameRequest.FromString(data["data"]),
42                    }
43                )
44
45        self.uid = self.frame_data[1]["data"].uid
46
47        self.period = 0.001  # Simulate a 1ms loop.
48        self.n_frame = 0
49
50        self.i = 0
51        self.t0 = time.time()
52        self.start()
53
54    def sleep(self):
55        self.i += 1
56        delta = self.t0 + self.period * self.i - time.time()
57        if delta > 0:
58            time.sleep(delta)
59
60    def run(self):
61        while True:
62            current_time = time.time() - self.t0
63
64            t = self.frame_data[self.n_frame]["time_stamp"]
65
66            if t - current_time < 0.001:
67                data = self.frame_data[self.n_frame]["data"]
68                if self.n_frame == 0:
69                    self.service.register(data, None, uid=self.uid)
70                else:
71                    self.service.data_frame(data, None)
72
73                self.n_frame += 1
74
75            if self.n_frame > len(self.frame_data) - 1:
76                break
77
78            self.sleep()
79
80        print("Reply finished.")
81        exit()

A class for replaying ARFlow data.

ARFlowPlayer(service: arflow.core.ARFlowService, frame_data_path: str)
20    def __init__(self, service: ARFlowService, frame_data_path: str) -> None:
21        super().__init__()
22        self.service = service()
23        with open(frame_data_path, "rb") as f:
24            raw_data = pickle.load(f)
25
26        self.frame_data = []
27        start_delta = 0
28        for i, data in enumerate(raw_data):
29            if i == 0:
30                start_delta = data["time_stamp"] - 3
31                self.frame_data.append(
32                    {
33                        "time_stamp": data["time_stamp"] - start_delta,
34                        "data": RegisterRequest.FromString(data["data"]),
35                    }
36                )
37            else:
38                self.frame_data.append(
39                    {
40                        "time_stamp": data["time_stamp"] - start_delta,
41                        "data": DataFrameRequest.FromString(data["data"]),
42                    }
43                )
44
45        self.uid = self.frame_data[1]["data"].uid
46
47        self.period = 0.001  # Simulate a 1ms loop.
48        self.n_frame = 0
49
50        self.i = 0
51        self.t0 = time.time()
52        self.start()

This constructor should always be called with keyword arguments. Arguments are:

group should be None; reserved for future extension when a ThreadGroup class is implemented.

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

name is the thread name. By default, a unique name is constructed of the form "Thread-N" where N is a small decimal number.

args is the argument tuple for the target invocation. Defaults to ().

kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.

frame_data: List
n_frame: int
uid
period
i
t0
def sleep(self):
54    def sleep(self):
55        self.i += 1
56        delta = self.t0 + self.period * self.i - time.time()
57        if delta > 0:
58            time.sleep(delta)
def run(self):
60    def run(self):
61        while True:
62            current_time = time.time() - self.t0
63
64            t = self.frame_data[self.n_frame]["time_stamp"]
65
66            if t - current_time < 0.001:
67                data = self.frame_data[self.n_frame]["data"]
68                if self.n_frame == 0:
69                    self.service.register(data, None, uid=self.uid)
70                else:
71                    self.service.data_frame(data, None)
72
73                self.n_frame += 1
74
75            if self.n_frame > len(self.frame_data) - 1:
76                break
77
78            self.sleep()
79
80        print("Reply finished.")
81        exit()

Method representing the thread's activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object's constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

Inherited Members
threading.Thread
start
join
name
ident
is_alive
daemon
isDaemon
setDaemon
getName
setName
native_id