Skip to content

Commit

Permalink
Added samplingrate getter so that one can retrieve the actual
Browse files Browse the repository at this point in the history
sampling rate.
  • Loading branch information
berndporr committed Dec 6, 2019
1 parent fe5d452 commit f272943
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ and the analogue outputs, digital input and outputs synchronously::
# are measured. Blocking call if no samples are available.
getSampleFromBuffer()

# gets the actual sampling rate of the running acquisition
getSamplingRate()

# stops the background acquisition
stop()

Expand Down
1 change: 1 addition & 0 deletions examples/realtime_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def data_gen():
c.open()
print("ADC board:",c.get_board_name())
c.start(8,250)
print("Actual samplingrate =",c.getSamplingRate(),"Hz")

# now let's plot the data
fig, ax = plt.subplots()
Expand Down
5 changes: 5 additions & 0 deletions examples/realtime_two_channel_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ def getDataThread(qtPanningPlot1,qtPanningPlot2):
# start data acquisition
c.start(8,250)

# gets the actual samplingrate from the board which
# might be different to the requested because the
# board is allowed to change it to a rate it can do.
print("Actual samplingrate =",c.getSamplingRate(),"Hz")

# start the thread getting the data
t.start()

Expand Down
23 changes: 21 additions & 2 deletions pyusbdux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* pyusbdux
*
* Copyright (c) 1999,2000,2001 David A. Schleef <ds@schleef.org>
* 2018 Bernd Porr <mail@berndporr.me.uk>
* 2019 Bernd Porr <mail@berndporr.me.uk>
*
* This file may be freely modified, distributed, and combined with
* other software, as long as proper attribution is given in the
Expand Down Expand Up @@ -38,6 +38,7 @@ float samples[N_CHANS];
unsigned int chanlist[N_CHANS];
comedi_range* range_info[N_CHANS];
lsampl_t maxdata[N_CHANS];
comedi_cmd cmd;

static const char errorDevNotOpen[] = "Comedi device not open. Use open() first.";
static const char errorDisconnect[] = "Device error. Possible disconnect.";
Expand All @@ -54,6 +55,7 @@ void open(int comediDeviceNumber) {
if (strstr(comedi_get_board_name(dev),"usbdux") == NULL) {
throw "Not a USBDUX board.";
}
memset(&cmd,0,sizeof(comedi_cmd));
}


Expand Down Expand Up @@ -99,7 +101,6 @@ void start(int n_channels, double fs) {
bytes_per_sample = sizeof(sampl_t);
}

comedi_cmd cmd;
memset(&cmd,0,sizeof(comedi_cmd));
int ret = comedi_get_cmd_generic_timed(dev, subdevice, &cmd, n_chan, 1e9 / freq);
if(ret<0){
Expand Down Expand Up @@ -169,10 +170,27 @@ int hasSampleAvailable() {
return ret > 0;
}

float getSamplingRate() {
float sampling_rate = 0;
// the timing is done channel by channel
// this means that the actual sampling rate is divided by
// number of channels
if ((cmd.convert_src == TRIG_TIMER)&&(cmd.convert_arg)) {
sampling_rate=((1E9 / cmd.convert_arg)/n_chan);
}

// the timing is done scan by scan (all channels at once)
// the sampling rate is equivalent of the scan_begin_arg
if ((cmd.scan_begin_src == TRIG_TIMER)&&(cmd.scan_begin_arg)) {
sampling_rate=1E9 / cmd.scan_begin_arg;
}
return sampling_rate;
}

void stop() {
if (dev == NULL) throw errorDevNotOpen;
comedi_cancel(dev,subdevice);
memset(&cmd,0,sizeof(comedi_cmd));
}


Expand Down Expand Up @@ -232,4 +250,5 @@ void close() {
if (dev == NULL) throw errorDevNotOpen;
comedi_close(dev);
dev = NULL;
memset(&cmd,0,sizeof(comedi_cmd));
}
2 changes: 2 additions & 0 deletions pyusbdux.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ void start(int nChan, double fs);

int hasSampleAvailable();

float getSamplingRate();

sample_p getSampleFromBuffer();

void stop();
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def read(fname):


setup (name = 'pyusbdux',
version = '1.5.7',
version = '1.6.0',
author = "Bernd Porr",
author_email = "mail@berndporr.me.uk",
url = "https://github.com/berndporr/pyusbdux",
Expand Down

0 comments on commit f272943

Please sign in to comment.