|
|
@@ -1,4 +1,5 @@
|
|
|
from CBD.CBD import BaseBlock
|
|
|
+from threading import Lock
|
|
|
|
|
|
|
|
|
class CollectorBlock(BaseBlock):
|
|
|
@@ -16,7 +17,8 @@ class CollectorBlock(BaseBlock):
|
|
|
"""
|
|
|
def __init__(self, block_name, input_ports, data=None):
|
|
|
BaseBlock.__init__(self, block_name, input_ports, [])
|
|
|
- self.data = data
|
|
|
+ self._data = data
|
|
|
+ self.lock = Lock()
|
|
|
|
|
|
def compute(self, curIteration):
|
|
|
raise NotImplementedError()
|
|
|
@@ -29,6 +31,12 @@ class CollectorBlock(BaseBlock):
|
|
|
"""
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
+ @property
|
|
|
+ def data(self):
|
|
|
+ with self.lock:
|
|
|
+ res = self._data
|
|
|
+ return res
|
|
|
+
|
|
|
|
|
|
class SignalCollectorBlock(CollectorBlock):
|
|
|
"""
|
|
|
@@ -45,11 +53,12 @@ class SignalCollectorBlock(CollectorBlock):
|
|
|
self.buffer_size = buffer_size
|
|
|
|
|
|
def compute(self, curIteration):
|
|
|
- time = self.getClock().getTime()
|
|
|
- value = self.getInputSignal(curIteration, "IN1").value
|
|
|
- self.data.append((time, value))
|
|
|
- if self.buffer_size > 0:
|
|
|
- self.data = self.data[-self.buffer_size:]
|
|
|
+ with self.lock:
|
|
|
+ time = self.getClock().getTime()
|
|
|
+ value = self.getInputSignal(curIteration, "IN1").value
|
|
|
+ self._data.append((time, value))
|
|
|
+ if self.buffer_size > 0:
|
|
|
+ self._data = self._data[-self.buffer_size:]
|
|
|
|
|
|
def clear(self):
|
|
|
self.data.clear()
|
|
|
@@ -80,11 +89,12 @@ class PositionCollectorBlock(CollectorBlock):
|
|
|
self.buffer_size = buffer_size
|
|
|
|
|
|
def compute(self, curIteration):
|
|
|
- x, y = self.getInputSignal(curIteration, "X").value, self.getInputSignal(curIteration, "Y").value
|
|
|
- self.distance_travelled += self.distance_from_last(x, y)
|
|
|
- self.data.append((x, y))
|
|
|
- if self.buffer_size > 0:
|
|
|
- self.data = self.data[-self.buffer_size:]
|
|
|
+ with self.lock:
|
|
|
+ x, y = self.getInputSignal(curIteration, "X").value, self.getInputSignal(curIteration, "Y").value
|
|
|
+ self.distance_travelled += self.distance_from_last(x, y)
|
|
|
+ self._data.append((x, y))
|
|
|
+ if self.buffer_size > 0:
|
|
|
+ self._data = self._data[-self.buffer_size:]
|
|
|
|
|
|
def clear(self):
|
|
|
self.data.clear()
|
|
|
@@ -105,7 +115,6 @@ class PositionCollectorBlock(CollectorBlock):
|
|
|
|
|
|
return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
|
|
|
|
|
|
-
|
|
|
@property
|
|
|
def data_xy(self):
|
|
|
"""
|
|
|
@@ -133,12 +142,13 @@ class StatisticsCollectorBlock(CollectorBlock):
|
|
|
self.clear()
|
|
|
|
|
|
def compute(self, curIteration):
|
|
|
- data = self.getInputSignal(curIteration, "IN1").value
|
|
|
- self.data["count"] += 1
|
|
|
- self.data["sum"] += data
|
|
|
- self.data["sumOfSquares"] += data ** 2.0
|
|
|
- self.data["min"] = min(self.data["min"], data)
|
|
|
- self.data["max"] = max(self.data["max"], data)
|
|
|
+ with self.lock:
|
|
|
+ data = self.getInputSignal(curIteration, "IN1").value
|
|
|
+ self.data["count"] += 1
|
|
|
+ self.data["sum"] += data
|
|
|
+ self.data["sumOfSquares"] += data ** 2.0
|
|
|
+ self.data["min"] = min(self.data["min"], data)
|
|
|
+ self.data["max"] = max(self.data["max"], data)
|
|
|
|
|
|
def clear(self):
|
|
|
"""
|