Skip to content

Commit

Permalink
fix: reduction of loop time
Browse files Browse the repository at this point in the history
  • Loading branch information
dixilo committed Feb 21, 2025
1 parent a9c5c31 commit a83db7b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
55 changes: 33 additions & 22 deletions socs/agents/stimulator_encoder/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ def acq(self, session, params):
{'timestamp_tai': 1736541833.679634,
'state': 1,
'freq': 10.1,
'timestamp': 1736541796.779634
}
"""
pace_maker = Pacemaker(100)
pace_maker = Pacemaker(0.5)

with self.lock.acquire_timeout(timeout=0, job='acq') as acquired:
if not acquired:
Expand All @@ -79,10 +80,14 @@ def acq(self, session, params):

self._dev.run()

downsample_time = time.time()
tai_latest = 0.
en_st_latest = 0
pulse_count = 0

time_prev = time.time()
while self.take_data:
# Data acquisition
current_time = time.time()
time_now = time.time()
data = {'block_name': 'stim_enc', 'data': {}}

tai_list = []
Expand All @@ -92,32 +97,38 @@ def acq(self, session, params):
while not self._dev.fifo.empty():
_d = self._dev.fifo.get()
tai_list.append(_d.time.tai)
ts_list.append(time.time())
ts_list.append(_d.utime)
en_st_list.append(_d.state)

if len(tai_list) != 0:
data['timestamps'] = ts_list
data['data']['timestamps_tai'] = tai_list
data['data']['state'] = en_st_list

field_dict = {'timestamp_tai': tai_list[-1],
'state': en_st_list[-1]}

session.data.update(field_dict)

self.agent.publish_to_feed('stim_enc', data)
session.data.update({'timestamp': current_time})

if current_time - downsample_time > 0.1:
data_downsampled = {'timestamp': current_time,
'block_name': 'stim_enc_downsampled',
'data': {
'timestamps_tai': tai_list[-1],
'state': en_st_list[-1]
}}
self.agent.publish_to_feed('stim_enc_downsampled',
data_downsampled)
downsample_time = current_time

tai_latest = tai_list[-1]
en_st_latest = en_st_list[-1]
pulse_count += len(en_st_list)

# Slow feed for InfluxDB and session.data
freq = pulse_count / (time_now - time_prev)
field_dict = {'timestamp_tai': tai_latest,
'state': en_st_latest,
'freq': freq,
'timestamp': time_now}
session.data.update(field_dict)

data_downsampled = {'timestamp': time_now,
'block_name': 'stim_enc_downsampled',
'data': {
'timestamps_tai': tai_latest,
'state': en_st_latest,
'freq': freq
}}
self.agent.publish_to_feed('stim_enc_downsampled',
data_downsampled)
time_prev = time_now
pulse_count = 0

pace_maker.sleep()

Expand Down
34 changes: 23 additions & 11 deletions socs/agents/stimulator_encoder/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pathlib import Path
from queue import Queue
from threading import Thread
from time import sleep
from time import sleep, time

import numpy as np

Expand All @@ -34,11 +34,11 @@ class StimEncTime:
[sec 48 bits][nsec 30 bits][sub-nsec 16 bits]
"""

def __init__(self, time_raw):
def __init__(self, time_raw: int):
self._time_raw = time_raw

@property
def sec(self):
def sec(self) -> int:
"""
Seconds part of timestamp.
Expand All @@ -50,7 +50,7 @@ def sec(self):
return self._time_raw >> 46

@property
def nsec(self):
def nsec(self) -> int:
"""
Nano second part of timestamp.
Expand All @@ -62,7 +62,7 @@ def nsec(self):
return (0x_00000000_00003fff_ffff0000 & self._time_raw) >> 16

@property
def tai(self):
def tai(self) -> float:
"""
Time in seconds from TAI epoch.
Expand All @@ -87,13 +87,14 @@ class StimEncData:
def __init__(self, data_bytes):
self._data_bytes = data_bytes
self._data_int = int(data_bytes[0]) + (int(data_bytes[1]) << 32) + (int(data_bytes[2]) << 64)
self._utime = time()

@property
def state(self):
def state(self) -> int:
return (self._data_int & 0xC0_00_00_00_00000000_00000000) >> 94

@property
def time_raw(self):
def time_raw(self) -> int:
"""94 bit TSU timestamp.
Returns
Expand All @@ -104,7 +105,7 @@ def time_raw(self):
return self._data_int & 0x3F_FF_FF_FF_FFFFFFFF_FFFFFFFF

@property
def time(self):
def time(self) -> StimEncTime:
"""
TSU timestamp.
Expand All @@ -115,11 +116,22 @@ def time(self):
"""
return StimEncTime(self.time_raw)

@property
def utime(self) -> float:
"""
Unix timestamp at class creation.
Returns
-------
utime : Unix timestamp when this object is created.
"""
return self._utime

def __str__(self):
return f'time={int(self.time.g3) / 1e8:.8f} data={self.state:02b}'


def get_path_dev():
def get_path_dev() -> Path:
"""
Acquire devicefile path for `str_rd` IP core.
Expand Down Expand Up @@ -165,7 +177,7 @@ def __init__(self, path_dev, path_lock=PATH_LOCK, verbose=True):
self._dev = mmap.mmap(self._dfile, 0x100, mmap.MAP_SHARED, mmap.PROT_READ, offset=0)

# Data FIFO
self.fifo = Queue()
self.fifo: "Queue[StimEncData]" = Queue()

# Runner
self._thread = None
Expand All @@ -192,7 +204,7 @@ def _get_info(self):

return r_len, w_len, residue

def _get_data(self):
def _get_data(self) -> StimEncData:
data = np.frombuffer(self._dev, np.uint32, 4, offset=16)

return StimEncData(data)
Expand Down

0 comments on commit a83db7b

Please sign in to comment.