Skip to content

Commit

Permalink
measured packet lag for buffered AIN data (18ms)
Browse files Browse the repository at this point in the history
  • Loading branch information
mazerj committed Mar 30, 2018
1 parent d040cd1 commit 8a4d96f
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions labjack.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import u3
import os, threading, signal
import numpy as np
from monoclock import monotonic_time
import monotonic

# U3 internal clock speed (used for streaming timestamps)
U3CLOCK = 4000000.
Expand Down Expand Up @@ -159,22 +159,28 @@ def _start_ad(self):
self.errorcount = 0

self.d.streamStart()
self.clockoffset_host = monotonic_time()

if 0:
# read clock directly
lo = self.d.getFeedback(u3.Timer0(),)[0] # timer LSB
hi = self.d.getFeedback(u3.Timer1(),)[0] # timer MSB
u3_clock = (hi<<16) + lo

self.clockoffset_host = monotonic.monotonic()


# The buffered stream data lags about 18ms.
# That is -- each fragment of 200 samples) is received
# by the host 18ms after the last sample was digitized.
# This is NOT samplingRate dependent!
#
# You can see this lab if you plot the ts values (hostclock
# times of the actual samples) vs the rects values (hostclock
# at the time data was received by the host)

try:
for r in self.d.streamData():
if r is not None:
self.nsamps_ad += len(r['AIN0'])
hi = np.bitwise_and(np.array(r['AIN224']), 0xffff)
lo = np.bitwise_and(np.array(r['AIN200']), 0xffff)
ts = (hi<<16) + lo
rects = np.zeros_like(ts) + monotonic.monotonic()
self.frags.append((ts,
rects,
np.array(r['AIN0']),
np.array(r['AIN1']),
np.array(r['AIN2']),
Expand Down Expand Up @@ -212,7 +218,7 @@ def _start_dig(self):
# if the state of either line has changed, interrupt
# the main thread will os.kill(). self.ievent is used
# to pass info about the event.
self.ievent = ('din', state, monotonic_time())
self.ievent = ('din', state, monotonic.monotonic())
if self.dig_callback:
self.dig_callback(self, self.ievent)
else:
Expand Down Expand Up @@ -246,14 +252,16 @@ def get(self, t0=0.0):
# reassemble analog datastream from fragment pool
rawts = np.array([])
ts = np.array([])
rects = np.array([]) # receipt time.. only for testing
a0 = np.array([])
a1 = np.array([])
a2 = np.array([])
a3 = np.array([])

for (ts_, a0_, a1_, a2_, a3_) in self.frags:
for (ts_, rects_, a0_, a1_, a2_, a3_,) in self.frags:
rawts = np.concatenate((rawts, ts_,))
ts = np.concatenate((ts, ts_,))
rects = np.concatenate((rects, rects_,))
a0 = np.concatenate((a0, a0_,))
a1 = np.concatenate((a1, a1_,))
a2 = np.concatenate((a2, a2_,))
Expand All @@ -277,7 +285,7 @@ def get(self, t0=0.0):
# the clock_monotonic time to get real system time back
ts = ((ts - ts[0]) / U3CLOCK) + self.clockoffset_host - t0

return (rawts, ts, a0, a1, a2, a3)
return (rawts, ts, rects, a0, a1, a2, a3)

def digout(self, line, state):
if line in [0, 1]:
Expand All @@ -303,12 +311,17 @@ def digin(self, line):
time.sleep(t)
s.stop(wait=1)

(rawts, ts, a0, a1, a2, a3) = s.get()
(rawts, ts, rect, a0, a1, a2, a3,) = s.get()

print '# u3clk hostclk a0 a1 a2 a3'
print 'rects,u3clk,hostclk,a0,a1,a2,a3'
for n in range(len(rawts)):
print '%.0f\t%.0f\t%f\t%f\t%f\t%f' % \
(rawts[n], ts[n], a0[n], a1[n], a2[n], a3[n],)
print '%.2f,%.2f,%.2f,%f,%f,%f,%f' % \
(1000*rect[n], 1000*rawts[n], 1000*ts[n], \
a0[n], a1[n], a2[n], a3[n],)

if s.errorcount:
sys.stderr.write('%d errors!\n' % s.errorcount)


except KeyboardInterrupt:
print 'shutting down'
Expand Down

0 comments on commit 8a4d96f

Please sign in to comment.