From 24b9a24b04590978302d356d27831878be04afda Mon Sep 17 00:00:00 2001 From: "J.A. de Jong - Redu-Sone B.V., ASCEE V.O.F" Date: Wed, 4 Jan 2023 15:15:03 +0100 Subject: [PATCH] Bugfix and code formatting. --- src/lasp/device/lasp_daqdata.h | 2 +- src/lasp/device/lasp_device_common.py | 80 --------------------------- src/lasp/device/lasp_rtaudiodaq.cpp | 8 ++- src/lasp/device/lasp_uldaq.cpp | 12 ++-- src/lasp/device/lasp_uldaq.h | 8 +-- src/lasp/device/lasp_uldaq_impl.cpp | 29 +++++----- src/lasp/device/lasp_uldaq_impl.h | 30 +++++++++- 7 files changed, 55 insertions(+), 114 deletions(-) delete mode 100644 src/lasp/device/lasp_device_common.py diff --git a/src/lasp/device/lasp_daqdata.h b/src/lasp/device/lasp_daqdata.h index 45b0034..b9a71ee 100644 --- a/src/lasp/device/lasp_daqdata.h +++ b/src/lasp/device/lasp_daqdata.h @@ -67,7 +67,7 @@ public: DaqData(const DaqData &); DaqData(DaqData &&); DaqData &operator=(const DaqData &) = delete; - virtual ~DaqData(); + ~DaqData(); /** * @brief Return pointer to the raw data corresponding to a certain sample diff --git a/src/lasp/device/lasp_device_common.py b/src/lasp/device/lasp_device_common.py deleted file mode 100644 index 2606215..0000000 --- a/src/lasp/device/lasp_device_common.py +++ /dev/null @@ -1,80 +0,0 @@ -__all__ = ['DaqChannel'] -import json, logging -from dataclasses import dataclass, field -from typing import List -import numpy as np -from dataclasses_json import dataclass_json -from ..lasp_common import Qty, SIQtys - - -@dataclass_json -@dataclass(eq=False) -class DaqChannel: - channel_enabled: bool - channel_name: str = 'Unnamed channel' - sensitivity: float = 1.0 - range_index: int = 0 - ACCoupling_enabled: bool = False - IEPE_enabled: bool = False - channel_metadata: str = '' - - def __post_init__(self): - # logging.debug(f'__post_init__({self.channel_name})') - self._qty = SIQtys.default() - - # Whether a digital high-pass filter should be used on input data. - # Negative means disabled. A positive number corresponds to the cut-on - # frequency of the installed highpass filter. - self._highpass = -1.0 - try: - meta = json.loads(self.channel_metadata) - if 'qty' in meta: - # The quantity itself is stored as a JSON string, in the JSON - # object called channel_metadata. - self._qty = Qty.from_json(meta['qty']) - if 'highpass' in meta: - self._highpass = meta['highpass'] - except json.JSONDecodeError: - logging.debug(f'No JSON data found in DaqChannel {self.channel_name}') - - @property - def qty(self): - return self._qty - - @qty.setter - def qty(self, newqty): - self._qty = newqty - self._store('qty', newqty.to_json()) - - @property - def highpass(self): - return self._highpass - - @highpass.setter - def highpass(self, newvalue: float): - newvalue = float(newvalue) - self._highpass = newvalue - self._store('highpass', newvalue) - - def _store(self, name, val): - try: - meta = json.loads(self.channel_metadata) - except json.JSONDecodeError: - meta = {} - - meta[name] = val - self.channel_metadata = json.dumps(meta) - - def __eq__(self, other): - """ - We overwrite the default equal-check as the floating point values - cannot be exactly checked due to conversion from/to JSON - """ - return (self.channel_enabled == other.channel_enabled and - self.channel_name == other.channel_name and - self.range_index == other.range_index and - self.ACCoupling_enabled == other.ACCoupling_enabled and - self.IEPE_enabled == other.IEPE_enabled and - self.channel_metadata == other.channel_metadata and - np.isclose(self.highpass,other.highpass)) - diff --git a/src/lasp/device/lasp_rtaudiodaq.cpp b/src/lasp/device/lasp_rtaudiodaq.cpp index 8c7db53..30a77af 100644 --- a/src/lasp/device/lasp_rtaudiodaq.cpp +++ b/src/lasp/device/lasp_rtaudiodaq.cpp @@ -231,7 +231,7 @@ public: if (nFramesPerBlock_copy != nFramesPerBlock) { throw rte("Got different number of frames per block back from RtAudio " - "backend. Do not know what to do"); + "backend. I do not know what to do."); } } @@ -410,8 +410,10 @@ std::unique_ptr createRtAudioDevice(const DeviceInfo &devinfo, void myerrorcallback(RtAudioError::Type, const string &errorText) { cerr << "RtAudio backend stream error: " << errorText << endl; } -int mycallback(void *outputBuffer, void *inputBuffer, unsigned int nFrames, - double streamTime, RtAudioStreamStatus status, void *userData) { +int mycallback( + void *outputBuffer, void *inputBuffer, unsigned int nFrames, + __attribute__((unused)) double streamTime, // Not used parameter streamTime + RtAudioStreamStatus status, void *userData) { return static_cast(userData)->streamCallback( outputBuffer, inputBuffer, nFrames, status); diff --git a/src/lasp/device/lasp_uldaq.cpp b/src/lasp/device/lasp_uldaq.cpp index 5e63376..cad6db3 100644 --- a/src/lasp/device/lasp_uldaq.cpp +++ b/src/lasp/device/lasp_uldaq.cpp @@ -2,16 +2,17 @@ #include "debugtrace.hpp" #include "lasp_config.h" #if LASP_HAS_ULDAQ == 1 -#include #include "lasp_uldaq.h" #include "lasp_uldaq_impl.h" +#include - -void fillUlDaqDeviceInfo(std::vector &devinfolist,void* vDescriptors) { +void fillUlDaqDeviceInfo(std::vector &devinfolist, + void *vDescriptors) { DEBUGTRACE_ENTER; - DaqDeviceDescriptor* descriptors_copy = static_cast(vDescriptors); + DaqDeviceDescriptor *descriptors_copy = + static_cast(vDescriptors); UlError err; unsigned int numdevs = MAX_ULDAQ_DEV_COUNT_PER_API; @@ -32,7 +33,7 @@ void fillUlDaqDeviceInfo(std::vector &devinfolist,void* vDescriptors descriptor = devdescriptors[i]; // Copy structure over, if given as not nullptr - if(descriptors_copy) { + if (descriptors_copy) { descriptors_copy[i] = descriptor; } @@ -100,5 +101,4 @@ std::unique_ptr createUlDaqDevice(const DeviceInfo &devinfo, return std::make_unique(devinfo, config); } - #endif // LASP_HAS_ULDAQ diff --git a/src/lasp/device/lasp_uldaq.h b/src/lasp/device/lasp_uldaq.h index 4d9ea19..c5593e9 100644 --- a/src/lasp/device/lasp_uldaq.h +++ b/src/lasp/device/lasp_uldaq.h @@ -1,15 +1,14 @@ #pragma once #include "lasp_daq.h" - /** * @brief The maximum number of devices that can be enumerated when calling * ulGetDaqDeviceInventory() */ const us MAX_ULDAQ_DEV_COUNT_PER_API = 100; -std::unique_ptr createUlDaqDevice(const DeviceInfo& devinfo, - const DaqConfiguration& config); +std::unique_ptr createUlDaqDevice(const DeviceInfo &devinfo, + const DaqConfiguration &config); /** * @brief Fill device info list with UlDaq specific devices, if any. @@ -20,5 +19,4 @@ std::unique_ptr createUlDaqDevice(const DeviceInfo& devinfo, * copy of the device descriptors is set to the memory of this pointer. We use * a void* pointer here to not expose the implementation of UlDaq. */ -void fillUlDaqDeviceInfo(std::vector, void* descriptors=nullptr); - +void fillUlDaqDeviceInfo(std::vector&, void *descriptors = nullptr); diff --git a/src/lasp/device/lasp_uldaq_impl.cpp b/src/lasp/device/lasp_uldaq_impl.cpp index 955d645..8e9c35f 100644 --- a/src/lasp/device/lasp_uldaq_impl.cpp +++ b/src/lasp/device/lasp_uldaq_impl.cpp @@ -80,7 +80,7 @@ DT9837A::DT9837A(const DeviceInfo &devinfo, const DaqConfiguration &config) } if (devinfo.api_specific_devindex < 0 || - devinfo.api_specific_devindex >= (int) MAX_ULDAQ_DEV_COUNT_PER_API) { + devinfo.api_specific_devindex >= (int)MAX_ULDAQ_DEV_COUNT_PER_API) { throw rte("Invalid device index"); } @@ -149,8 +149,10 @@ void DT9837A::stop() { } /** - * @brief Throws an exception in case it happens. Does nothing in case of no - * error. + * @brief Throws an appropriate stream exception based on the UlError number. + * The mapping is based on the error numbers as given in uldaq.h. There are a + * log of errors definded here (109 in total). Except for some, we will map + * most of them to a driver error. * * @param e */ @@ -161,13 +163,7 @@ inline void throwUlException(UlError err) { string errstr = getErrMsg(err); showErr(errstr); Daq::StreamStatus::StreamError serr; - if ((int)err < 5) { - serr = Daq::StreamStatus::StreamError::logicError; - } else if ((int)err < 9) { - serr = Daq::StreamStatus::StreamError::systemError; - } else if ((int)err < 18) { - serr = Daq::StreamStatus::StreamError::logicError; - } else if ((int)err == 18) { + if ((int)err == 18) { serr = Daq::StreamStatus::StreamError::inputXRun; } else if ((int)err == 19) { serr = Daq::StreamStatus::StreamError::outputXRun; @@ -276,8 +272,7 @@ bool InBufHandler::operator()() { auto runCallback = ([&](us totalOffset) { /* DEBUGTRACE_ENTER; */ - DaqData data(nFramesPerBlock, nchannels, - DataTypeDescriptor::DataType::dtype_fl64); + DaqData data(nFramesPerBlock, nchannels, dtype_descr.dtype); us monitorOffset = monitorOutput ? 1 : 0; /* /// Put the output monitor in front */ @@ -304,7 +299,7 @@ bool InBufHandler::operator()() { UlError err = ulDaqInScanStatus(daq.getHandle(), &status, &transferStatus); throwUlException(err); - increment = transferStatus.currentTotalCount - totalFramesCount; + us increment = transferStatus.currentTotalCount - totalFramesCount; totalFramesCount += increment; if (increment > nFramesPerBlock) { @@ -382,7 +377,7 @@ bool OutBufHandler::operator()() { if (status != SS_RUNNING) { return false; } - increment = transferStatus.currentTotalCount - totalFramesCount; + us increment = transferStatus.currentTotalCount - totalFramesCount; totalFramesCount += increment; if (increment > nFramesPerBlock) { @@ -392,7 +387,7 @@ bool OutBufHandler::operator()() { if (transferStatus.currentIndex < buffer_mid_idx) { topenqueued = false; if (!botenqueued) { - DaqData d(nFramesPerBlock, 1, DataTypeDescriptor::DataType::dtype_fl64); + DaqData d(nFramesPerBlock, 1, dtype_descr.dtype); res = cb(d); d.copyToRaw(0, reinterpret_cast(&(buf[buffer_mid_idx]))); @@ -401,7 +396,7 @@ bool OutBufHandler::operator()() { } else { botenqueued = false; if (!topenqueued) { - DaqData d(nFramesPerBlock, 1, DataTypeDescriptor::DataType::dtype_fl64); + DaqData d(nFramesPerBlock, 1, dtype_descr.dtype); res = cb(d); d.copyToRaw(0, reinterpret_cast(&(buf[0]))); @@ -453,11 +448,13 @@ void DT9837A::threadFcn(InDaqCallback inCallback, OutDaqCallback outCallback) { if (ibh) { if (!(*ibh)()) { _stopThread = true; + break; } } if (obh) { if (!(*obh)()) { _stopThread = true; + break; } } else { std::this_thread::sleep_for(std::chrono::microseconds(sleeptime_us)); diff --git a/src/lasp/device/lasp_uldaq_impl.h b/src/lasp/device/lasp_uldaq_impl.h index 4fea809..3cc588a 100644 --- a/src/lasp/device/lasp_uldaq_impl.h +++ b/src/lasp/device/lasp_uldaq_impl.h @@ -59,19 +59,43 @@ public: */ class BufHandler { protected: + /** + * @brief Reference to underlying Daq + */ DT9837A &daq; - const DataTypeDescriptor dtype_descr; + /** + * @brief The type of data, in this case always double precision floats + */ + const DataTypeDescriptor dtype_descr = dtype_desc_fl64; + /** + * @brief The number of channels, number of frames per callback (block). + */ us nchannels, nFramesPerBlock; + /** + * @brief Sampling frequency in Hz + */ double samplerate; std::vector buf; + /** + * @brief Whether the top / bottom part of the buffer are ready to be + * enqueued + */ bool topenqueued, botenqueued; - us increment = 0; - + /** + * @brief Counter for the total number of frames acquired / sent since the + * start of the stream. + */ us totalFramesCount = 0; long long buffer_mid_idx; public: + /** + * @brief Initialize bufhandler + * + * @param daq + * @param nchannels + */ BufHandler(DT9837A &daq, const us nchannels) : daq(daq), dtype_descr(daq.dtypeDescr()), nchannels(nchannels), nFramesPerBlock(daq.framesPerBlock()), samplerate(daq.samplerate()),