2018-09-24 21:30:57 +02:00
|
|
|
/** Node-type for uldaq connections.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Manuel Pitz <manuel.pitz@eonerc.rwth-aachen.de>
|
2022-12-14 17:41:58 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-07-04 18:20:03 +02:00
|
|
|
* @license Apache 2.0
|
2018-09-24 21:30:57 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2019-01-13 00:02:34 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/config.hpp>
|
|
|
|
#include <villas/node_compat.hpp>
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/uldaq.hpp>
|
2020-07-04 22:03:56 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node/memory.hpp>
|
2021-06-21 16:11:42 -04:00
|
|
|
#include <villas/utils.hpp>
|
2018-09-24 21:30:57 +02:00
|
|
|
|
2020-07-04 22:03:56 +02:00
|
|
|
using namespace villas;
|
2021-05-10 00:12:30 +02:00
|
|
|
using namespace villas::node;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2018-10-04 03:00:14 +02:00
|
|
|
static unsigned num_devs = ULDAQ_MAX_DEV_COUNT;
|
|
|
|
static DaqDeviceDescriptor descriptors[ULDAQ_MAX_DEV_COUNT];
|
|
|
|
|
2018-09-25 12:56:23 +02:00
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
AiInputMode mode;
|
|
|
|
} input_modes[] = {
|
|
|
|
{ "differential", AI_DIFFERENTIAL },
|
|
|
|
{ "single-ended", AI_SINGLE_ENDED },
|
|
|
|
{ "pseudo-differential", AI_PSEUDO_DIFFERENTIAL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
DaqDeviceInterface interface;
|
|
|
|
} interface_types[] = {
|
|
|
|
{ "usb", USB_IFC },
|
|
|
|
{ "bluetooth", BLUETOOTH_IFC },
|
|
|
|
{ "ethernet", ETHERNET_IFC },
|
|
|
|
{ "any", ANY_IFC }
|
|
|
|
};
|
|
|
|
|
2018-09-24 22:28:51 +02:00
|
|
|
static const struct {
|
2018-09-25 10:04:25 +02:00
|
|
|
const char *name;
|
|
|
|
Range range;
|
2018-09-25 12:55:38 +02:00
|
|
|
float min, max;
|
2018-09-25 10:04:25 +02:00
|
|
|
} ranges[] = {
|
2018-09-25 12:55:38 +02:00
|
|
|
{ "bipolar-60", BIP60VOLTS, -60.0, +60.0 },
|
|
|
|
{ "bipolar-60", BIP60VOLTS, -60.0, +60.0 },
|
|
|
|
{ "bipolar-30", BIP30VOLTS, -30.0, +30.0 },
|
|
|
|
{ "bipolar-15", BIP15VOLTS, -15.0, +15.0 },
|
|
|
|
{ "bipolar-20", BIP20VOLTS, -20.0, +20.0 },
|
|
|
|
{ "bipolar-10", BIP10VOLTS, -10.0, +10.0 },
|
|
|
|
{ "bipolar-5", BIP5VOLTS, -5.0, +5.0 },
|
|
|
|
{ "bipolar-4", BIP4VOLTS, -4.0, +4.0 },
|
|
|
|
{ "bipolar-2.5", BIP2PT5VOLTS, -2.5, +2.5 },
|
|
|
|
{ "bipolar-2", BIP2VOLTS, -2.0, +2.0 },
|
|
|
|
{ "bipolar-1.25", BIP1PT25VOLTS, -1.25, +1.25 },
|
|
|
|
{ "bipolar-1", BIP1VOLTS, -1.0, +1.0 },
|
|
|
|
{ "bipolar-0.625", BIPPT625VOLTS, -0.625, +0.625 },
|
|
|
|
{ "bipolar-0.5", BIPPT5VOLTS, -0.5, +0.5 },
|
|
|
|
{ "bipolar-0.25", BIPPT25VOLTS, -0.25, +0.25 },
|
|
|
|
{ "bipolar-0.125", BIPPT125VOLTS, -0.125, +0.125 },
|
|
|
|
{ "bipolar-0.2", BIPPT2VOLTS, -0.2, +0.2 },
|
|
|
|
{ "bipolar-0.1", BIPPT1VOLTS, -0.1, +0.1 },
|
|
|
|
{ "bipolar-0.078", BIPPT078VOLTS, -0.078, +0.078 },
|
|
|
|
{ "bipolar-0.05", BIPPT05VOLTS, -0.05, +0.05 },
|
|
|
|
{ "bipolar-0.01", BIPPT01VOLTS, -0.01, +0.01 },
|
|
|
|
{ "bipolar-0.005", BIPPT005VOLTS, -0.005, +0.005 },
|
|
|
|
{ "unipolar-60", UNI60VOLTS , 0.0, +60.0 },
|
|
|
|
{ "unipolar-30", UNI30VOLTS , 0.0, +30.0 },
|
|
|
|
{ "unipolar-15", UNI15VOLTS , 0.0, +15.0 },
|
|
|
|
{ "unipolar-20", UNI20VOLTS , 0.0, +20.0 },
|
|
|
|
{ "unipolar-10", UNI10VOLTS , 0.0, +10.0 },
|
|
|
|
{ "unipolar-5", UNI5VOLTS , 0.0, +5.0 },
|
|
|
|
{ "unipolar-4", UNI4VOLTS , 0.0, +4.0 },
|
|
|
|
{ "unipolar-2.5", UNI2PT5VOLTS, 0.0, +2.5 },
|
|
|
|
{ "unipolar-2", UNI2VOLTS , 0.0, +2.0 },
|
|
|
|
{ "unipolar-1.25", UNI1PT25VOLTS, 0.0, +1.25 },
|
|
|
|
{ "unipolar-1", UNI1VOLTS , 0.0, +1.0 },
|
|
|
|
{ "unipolar-0.625", UNIPT625VOLTS, 0.0, +0.625 },
|
|
|
|
{ "unipolar-0.5", UNIPT5VOLTS, 0.0, +0.5 },
|
|
|
|
{ "unipolar-0.25", UNIPT25VOLTS, 0.0, +0.25 },
|
|
|
|
{ "unipolar-0.125", UNIPT125VOLTS, 0.0, +0.125 },
|
|
|
|
{ "unipolar-0.2", UNIPT2VOLTS, 0.0, +0.2 },
|
|
|
|
{ "unipolar-0.1", UNIPT1VOLTS, 0.0, +0.1 },
|
|
|
|
{ "unipolar-0.078", UNIPT078VOLTS, 0.0, +0.078 },
|
|
|
|
{ "unipolar-0.05", UNIPT05VOLTS, 0.0, +0.05 },
|
|
|
|
{ "unipolar-0.01", UNIPT01VOLTS, 0.0, +0.01 },
|
|
|
|
{ "unipolar-0.005", UNIPT005VOLTS, 0.0, +0.005 }
|
2018-09-24 22:28:51 +02:00
|
|
|
};
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static
|
|
|
|
AiInputMode uldaq_parse_input_mode(const char *str)
|
2018-09-25 12:56:23 +02:00
|
|
|
{
|
2019-04-22 23:43:46 +02:00
|
|
|
for (unsigned i = 0; i < ARRAY_LEN(input_modes); i++) {
|
2018-09-25 12:56:23 +02:00
|
|
|
if (!strcmp(input_modes[i].name, str))
|
|
|
|
return input_modes[i].mode;
|
2018-09-25 10:24:47 +02:00
|
|
|
}
|
|
|
|
|
2019-04-22 23:43:46 +02:00
|
|
|
return (AiInputMode) -1;
|
2018-09-25 12:56:23 +02:00
|
|
|
}
|
2018-09-25 10:24:47 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static
|
|
|
|
DaqDeviceInterface uldaq_parse_interface_type(const char *str)
|
2018-09-25 12:56:23 +02:00
|
|
|
{
|
2019-04-22 23:43:46 +02:00
|
|
|
for (unsigned i = 0; i < ARRAY_LEN(interface_types); i++) {
|
2018-09-25 12:56:23 +02:00
|
|
|
if (!strcmp(interface_types[i].name, str))
|
|
|
|
return interface_types[i].interface;
|
2018-09-25 10:24:47 +02:00
|
|
|
}
|
|
|
|
|
2019-04-22 23:43:46 +02:00
|
|
|
return (DaqDeviceInterface) -1;
|
2018-09-25 12:56:23 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static
|
|
|
|
const char * uldaq_print_interface_type(DaqDeviceInterface iftype)
|
2018-09-25 12:56:23 +02:00
|
|
|
{
|
2019-04-22 23:43:46 +02:00
|
|
|
for (unsigned i = 0; i < ARRAY_LEN(interface_types); i++) {
|
2018-09-25 12:56:23 +02:00
|
|
|
if (interface_types[i].interface == iftype)
|
|
|
|
return interface_types[i].name;
|
|
|
|
}
|
2018-09-25 10:24:47 +02:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
return nullptr;
|
2018-09-25 10:24:47 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static
|
|
|
|
Range uldaq_parse_range(const char *str)
|
2018-09-24 22:28:51 +02:00
|
|
|
{
|
2019-04-22 23:43:46 +02:00
|
|
|
for (unsigned i = 0; i < ARRAY_LEN(ranges); i++) {
|
2018-09-24 22:47:18 +02:00
|
|
|
if (!strcmp(ranges[i].name, str))
|
|
|
|
return ranges[i].range;
|
|
|
|
}
|
|
|
|
|
2019-04-22 23:43:46 +02:00
|
|
|
return (Range) -1;
|
2018-09-24 22:28:51 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static
|
|
|
|
DaqDeviceDescriptor * uldaq_find_device(struct uldaq *u) {
|
2019-04-22 23:45:38 +02:00
|
|
|
DaqDeviceDescriptor *d = nullptr;
|
2018-10-05 15:24:48 +02:00
|
|
|
|
|
|
|
if (num_devs == 0)
|
2019-04-22 23:45:38 +02:00
|
|
|
return nullptr;
|
2018-10-05 15:24:48 +02:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
if (u->device_interface_type == ANY_IFC && u->device_id == nullptr)
|
2018-10-05 15:24:48 +02:00
|
|
|
return &descriptors[0];
|
|
|
|
|
2019-04-22 23:43:46 +02:00
|
|
|
for (unsigned i = 0; i < num_devs; i++) {
|
2019-02-09 21:21:12 +00:00
|
|
|
d = &descriptors[i];
|
|
|
|
|
2018-10-05 15:24:48 +02:00
|
|
|
if (u->device_id) {
|
|
|
|
if (strcmp(u->device_id, d->uniqueId))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u->device_interface_type != ANY_IFC) {
|
|
|
|
if (u->device_interface_type != d->devInterface)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
return nullptr;
|
2018-10-05 15:24:48 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static
|
|
|
|
int uldaq_connect(NodeCompat *n)
|
2018-10-04 03:00:14 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *u = n->getData<struct uldaq>();
|
2018-10-04 03:00:14 +02:00
|
|
|
UlError err;
|
|
|
|
|
2018-10-05 15:24:48 +02:00
|
|
|
/* Find Matching device */
|
2018-10-04 03:00:14 +02:00
|
|
|
if (!u->device_descriptor) {
|
2018-10-05 15:24:48 +02:00
|
|
|
u->device_descriptor = uldaq_find_device(u);
|
2021-02-16 14:15:14 +01:00
|
|
|
if (!u->device_descriptor)
|
|
|
|
throw RuntimeError("Unable to find a matching device");
|
2018-10-04 03:00:14 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 15:24:48 +02:00
|
|
|
/* Get a handle to the DAQ device associated with the first descriptor */
|
2018-10-04 03:00:14 +02:00
|
|
|
if (!u->device_handle) {
|
2020-10-08 12:30:20 +02:00
|
|
|
u->device_handle = ulCreateDaqDevice(*u->device_descriptor);
|
2021-02-16 14:15:14 +01:00
|
|
|
if (!u->device_handle)
|
|
|
|
throw RuntimeError("Unable to create handle for DAQ device");
|
2018-10-04 03:00:14 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 15:24:48 +02:00
|
|
|
/* Check if device is already connected */
|
2018-10-04 03:00:14 +02:00
|
|
|
int connected;
|
|
|
|
err = ulIsDaqDeviceConnected(u->device_handle, &connected);
|
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
|
|
|
|
2018-10-05 15:24:48 +02:00
|
|
|
/* Connect to device */
|
2018-10-04 03:00:14 +02:00
|
|
|
if (!connected) {
|
|
|
|
err = ulConnectDaqDevice(u->device_handle);
|
|
|
|
if (err != ERR_NO_ERROR) {
|
2021-02-16 13:30:26 +01:00
|
|
|
char buf[ERR_MSG_LEN];
|
|
|
|
ulGetErrMsg(err, buf);
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Failed to connect to DAQ device: {}", buf);
|
2018-10-04 03:00:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::uldaq_type_start(villas::node::SuperNode *sn)
|
2018-10-04 03:00:14 +02:00
|
|
|
{
|
|
|
|
UlError err;
|
|
|
|
|
|
|
|
/* Get descriptors for all of the available DAQ devices */
|
|
|
|
err = ulGetDaqDeviceInventory(ANY_IFC, descriptors, &num_devs);
|
2021-02-16 14:15:14 +01:00
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
throw RuntimeError("Failed to retrieve DAQ device list");
|
2018-10-04 03:00:14 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
auto logger = logging.get("node:uldaq");
|
|
|
|
logger->info("Found {} DAQ devices", num_devs);
|
2019-04-22 23:43:46 +02:00
|
|
|
for (unsigned i = 0; i < num_devs; i++) {
|
2018-10-04 03:00:14 +02:00
|
|
|
DaqDeviceDescriptor *desc = &descriptors[i];
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->info(" {}: {} {} ({})", i, desc->uniqueId, desc->devString, uldaq_print_interface_type(desc->devInterface));
|
2018-10-04 03:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::uldaq_init(NodeCompat *n)
|
2018-09-24 22:28:51 +02:00
|
|
|
{
|
2018-10-04 03:00:14 +02:00
|
|
|
int ret;
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *u = n->getData<struct uldaq>();
|
2018-09-24 22:28:51 +02:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
u->device_id = nullptr;
|
2018-09-25 12:56:23 +02:00
|
|
|
u->device_interface_type = ANY_IFC;
|
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
u->in.queues = nullptr;
|
2018-09-24 22:47:18 +02:00
|
|
|
u->in.sample_rate = 1000;
|
2020-10-07 15:25:47 +02:00
|
|
|
u->in.scan_options = (ScanOption) (SO_DEFAULTIO | SO_CONTINUOUS);
|
2018-09-24 22:47:18 +02:00
|
|
|
u->in.flags = AINSCAN_FF_DEFAULT;
|
2018-09-25 10:10:30 +02:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
ret = pthread_mutex_init(&u->in.mutex, nullptr);
|
2018-10-04 03:00:14 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
ret = pthread_cond_init(&u->in.cv, nullptr);
|
2018-10-04 03:00:14 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-09-25 20:27:01 +02:00
|
|
|
|
2018-09-25 10:10:30 +02:00
|
|
|
return 0;
|
2018-09-24 22:47:18 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::uldaq_destroy(NodeCompat *n)
|
2018-09-25 10:04:42 +02:00
|
|
|
{
|
2018-10-04 03:00:14 +02:00
|
|
|
int ret;
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *u = n->getData<struct uldaq>();
|
2018-09-25 10:04:42 +02:00
|
|
|
|
|
|
|
if (u->in.queues)
|
2020-01-21 16:26:51 +01:00
|
|
|
delete[] u->in.queues;
|
2018-09-25 10:04:42 +02:00
|
|
|
|
2018-10-04 03:00:14 +02:00
|
|
|
ret = pthread_mutex_destroy(&u->in.mutex);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = pthread_cond_destroy(&u->in.cv);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-09-25 20:27:01 +02:00
|
|
|
|
2018-09-25 10:04:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::uldaq_parse(NodeCompat *n, json_t *json)
|
2018-09-24 22:47:18 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *u = n->getData<struct uldaq>();
|
2018-09-24 22:28:51 +02:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
const char *default_range_str = nullptr;
|
|
|
|
const char *default_input_mode_str = nullptr;
|
|
|
|
const char *interface_type = nullptr;
|
2018-09-24 22:47:18 +02:00
|
|
|
|
|
|
|
size_t i;
|
|
|
|
json_t *json_signals;
|
|
|
|
json_t *json_signal;
|
2018-09-25 10:04:25 +02:00
|
|
|
json_error_t err;
|
2018-09-24 22:47:18 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s?: s, s: { s: o, s: F, s?: s, s?: s } }",
|
2018-09-25 12:56:23 +02:00
|
|
|
"interface_type", &interface_type,
|
2018-10-04 03:00:14 +02:00
|
|
|
"device_id", &u->device_id,
|
2018-09-24 22:47:18 +02:00
|
|
|
"in",
|
|
|
|
"signals", &json_signals,
|
|
|
|
"sample_rate", &u->in.sample_rate,
|
2018-09-25 12:56:23 +02:00
|
|
|
"range", &default_range_str,
|
|
|
|
"input_mode", &default_input_mode_str
|
2018-09-24 22:47:18 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-node-uldaq");
|
2018-09-24 22:47:18 +02:00
|
|
|
|
2018-09-25 12:56:23 +02:00
|
|
|
if (interface_type) {
|
|
|
|
int iftype = uldaq_parse_interface_type(interface_type);
|
|
|
|
if (iftype < 0)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, "node-config-node-uldaq-interface-type", "Invalid interface type: {}", interface_type);
|
2018-09-25 12:56:23 +02:00
|
|
|
|
2019-04-22 23:43:46 +02:00
|
|
|
u->device_interface_type = (DaqDeviceInterface) iftype;
|
2018-09-25 12:56:23 +02:00
|
|
|
}
|
|
|
|
|
2020-01-21 16:26:51 +01:00
|
|
|
if (u->in.queues)
|
|
|
|
delete[] u->in.queues;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
u->in.channel_count = n->getInputSignals(false)->size();
|
2020-01-21 16:26:51 +01:00
|
|
|
u->in.queues = new struct AiQueueElement[u->in.channel_count];
|
2020-07-04 16:22:10 +02:00
|
|
|
if (!u->in.queues)
|
|
|
|
throw MemoryAllocationError();
|
2018-09-24 22:47:18 +02:00
|
|
|
|
|
|
|
json_array_foreach(json_signals, i, json_signal) {
|
2019-04-22 23:45:38 +02:00
|
|
|
const char *range_str = nullptr, *input_mode_str = nullptr;
|
2018-09-25 12:56:23 +02:00
|
|
|
int channel = -1, input_mode, range;
|
|
|
|
|
2018-09-25 13:01:58 +02:00
|
|
|
ret = json_unpack_ex(json_signal, &err, 0, "{ s?: s, s?: s, s?: i }",
|
2018-09-25 13:02:51 +02:00
|
|
|
"range", &range_str,
|
|
|
|
"input_mode", &input_mode_str,
|
2018-09-25 12:56:23 +02:00
|
|
|
"channel", &channel
|
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json_signal, err, "node-config-node-uldaq-signal", "Failed to parse signal configuration");
|
2018-09-25 12:56:23 +02:00
|
|
|
|
|
|
|
if (!range_str)
|
|
|
|
range_str = default_range_str;
|
|
|
|
|
|
|
|
if (!input_mode_str)
|
|
|
|
input_mode_str = default_input_mode_str;
|
|
|
|
|
|
|
|
if (channel < 0)
|
|
|
|
channel = i;
|
|
|
|
|
|
|
|
if (!range_str)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json_signal, err, "node-config-node-uldaq-signal", "No input range specified for signal {}.", i);
|
2018-09-24 22:47:18 +02:00
|
|
|
|
2018-09-25 12:56:23 +02:00
|
|
|
if (!input_mode_str)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json_signal, err, "node-config-node-uldaq-signal", "No input mode specified for signal {}.", i);
|
2018-09-25 12:56:23 +02:00
|
|
|
|
|
|
|
range = uldaq_parse_range(range_str);
|
|
|
|
if (range < 0)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json_signal, err, "node-config-node-uldaq-signal", "Invalid input range specified for signal {}.", i);
|
2018-09-25 12:56:23 +02:00
|
|
|
|
|
|
|
input_mode = uldaq_parse_input_mode(input_mode_str);
|
|
|
|
if (input_mode < 0)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json_signal, err, "node-config-node-uldaq-signal", "Invalid input mode specified for signal {}.", i);
|
2018-09-25 12:56:23 +02:00
|
|
|
|
2019-04-22 23:43:46 +02:00
|
|
|
u->in.queues[i].range = (Range) range;
|
|
|
|
u->in.queues[i].inputMode = (AiInputMode) input_mode;
|
2018-09-25 12:56:23 +02:00
|
|
|
u->in.queues[i].channel = channel;
|
2018-09-24 22:47:18 +02:00
|
|
|
}
|
2018-09-25 10:04:25 +02:00
|
|
|
|
2018-09-25 09:54:07 +02:00
|
|
|
return ret;
|
2018-09-24 22:28:51 +02:00
|
|
|
}
|
2020-10-08 12:30:20 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
char * villas::node::uldaq_print(NodeCompat *n)
|
2018-09-25 11:59:38 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *u = n->getData<struct uldaq>();
|
2018-09-25 12:56:23 +02:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
char *buf = nullptr;
|
2018-09-25 12:56:23 +02:00
|
|
|
|
2019-02-09 21:21:12 +00:00
|
|
|
if (u->device_descriptor) {
|
|
|
|
char *uid = u->device_descriptor->uniqueId;
|
|
|
|
char *name = u->device_descriptor->productName;
|
|
|
|
const char *iftype = uldaq_print_interface_type(u->device_descriptor->devInterface);
|
|
|
|
|
|
|
|
buf = strcatf(&buf, "device=%s (%s), interface=%s", uid, name, iftype);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const char *uid = u->device_id;
|
|
|
|
const char *iftype = uldaq_print_interface_type(u->device_interface_type);
|
|
|
|
|
|
|
|
buf = strcatf(&buf, "device=%s, interface=%s", uid, iftype);
|
|
|
|
}
|
|
|
|
|
2018-09-25 12:56:23 +02:00
|
|
|
buf = strcatf(&buf, ", in.sample_rate=%f", u->in.sample_rate);
|
|
|
|
|
|
|
|
return buf;
|
2018-09-25 11:59:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::uldaq_check(NodeCompat *n)
|
2018-09-24 21:30:57 +02:00
|
|
|
{
|
2018-10-04 03:00:14 +02:00
|
|
|
int ret;
|
|
|
|
long long has_ai, event_types, max_channel, scan_options, num_ranges_se, num_ranges_diff;
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *u = n->getData<struct uldaq>();
|
2018-09-24 22:43:44 +02:00
|
|
|
|
2018-10-04 03:00:14 +02:00
|
|
|
UlError err;
|
2018-09-25 09:55:51 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
if (n->in.vectorize < 100)
|
2021-08-10 10:12:48 -04:00
|
|
|
throw ConfigError(n->getConfig(), "node-config-node-vectorize", "Setting 'vectorize' must be larger than 100");
|
2018-09-25 09:55:51 +02:00
|
|
|
|
2018-10-04 03:00:14 +02:00
|
|
|
ret = uldaq_connect(n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
err = ulDevGetInfo(u->device_handle, DEV_INFO_HAS_AI_DEV, 0, &has_ai);
|
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
err = ulDevGetInfo(u->device_handle, DEV_INFO_DAQ_EVENT_TYPES, 0, &event_types);
|
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
err = ulAIGetInfo(u->device_handle, AI_INFO_NUM_CHANS, 0, &max_channel);
|
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
err = ulAIGetInfo(u->device_handle, AI_INFO_SCAN_OPTIONS, 0, &scan_options);
|
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
err = ulAIGetInfo(u->device_handle, AI_INFO_NUM_DIFF_RANGES, 0, &num_ranges_diff);
|
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
err = ulAIGetInfo(u->device_handle, AI_INFO_NUM_SE_RANGES, 0, &num_ranges_se);
|
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
Range ranges_diff[num_ranges_diff];
|
|
|
|
Range ranges_se[num_ranges_se];
|
|
|
|
|
|
|
|
for (int i = 0; i < num_ranges_diff; i++) {
|
2019-02-09 20:11:21 +00:00
|
|
|
err = ulAIGetInfo(u->device_handle, AI_INFO_DIFF_RANGE, i, (long long *) &ranges_diff[i]);
|
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
2018-10-04 03:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < num_ranges_se; i++) {
|
2019-02-09 20:11:21 +00:00
|
|
|
err = ulAIGetInfo(u->device_handle, AI_INFO_SE_RANGE, i, (long long *) &ranges_se[i]);
|
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
2018-10-04 03:00:14 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
if (!has_ai)
|
|
|
|
throw RuntimeError("DAQ device has no analog input channels");
|
2018-10-04 03:00:14 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
if (!(event_types & DE_ON_DATA_AVAILABLE))
|
|
|
|
throw RuntimeError("DAQ device does not support events");
|
2018-10-04 03:00:14 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
if ((scan_options & u->in.scan_options) != u->in.scan_options)
|
|
|
|
throw RuntimeError("DAQ device does not support required scan options");
|
2018-10-04 03:00:14 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
for (size_t i = 0; i < n->getInputSignals(false)->size(); i++) {
|
|
|
|
auto sig = n->getInputSignals(false)->getByIndex(i);
|
2018-10-04 03:00:14 +02:00
|
|
|
AiQueueElement *q = &u->in.queues[i];
|
2018-09-25 12:09:04 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (sig->type != SignalType::FLOAT)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Node supports only signals of type = float!");
|
2018-10-04 03:00:14 +02:00
|
|
|
|
|
|
|
switch (q->inputMode) {
|
2018-10-05 15:24:48 +02:00
|
|
|
case AI_PSEUDO_DIFFERENTIAL:
|
2018-10-04 03:00:14 +02:00
|
|
|
case AI_DIFFERENTIAL:
|
2018-10-05 15:24:48 +02:00
|
|
|
for (int j = 0; j < num_ranges_diff; j++) {
|
|
|
|
if (q->range == ranges_diff[j])
|
|
|
|
goto found;
|
2018-10-04 03:00:14 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AI_SINGLE_ENDED:
|
2018-10-05 15:24:48 +02:00
|
|
|
for (int j = 0; j < num_ranges_se; j++) {
|
|
|
|
if (q->range == ranges_se[j])
|
|
|
|
goto found;
|
2018-10-04 03:00:14 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Unsupported range for signal {}", i);
|
2018-10-05 15:24:48 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
found: if (q->channel > max_channel)
|
|
|
|
throw RuntimeError("DAQ device does not support more than {} channels", max_channel);
|
2018-09-25 12:09:04 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 09:55:51 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2018-09-24 22:43:44 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static
|
2018-09-25 20:27:01 +02:00
|
|
|
void uldaq_data_available(DaqDeviceHandle device_handle, DaqEventType event_type, unsigned long long event_data, void *ctx)
|
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *n = (NodeCompat *) ctx;
|
|
|
|
auto *u = n->getData<struct uldaq>();
|
2018-09-25 20:27:01 +02:00
|
|
|
|
|
|
|
pthread_mutex_lock(&u->in.mutex);
|
|
|
|
|
2018-09-25 22:41:27 +02:00
|
|
|
UlError err;
|
2018-09-25 20:27:01 +02:00
|
|
|
err = ulAInScanStatus(device_handle, &u->in.status, &u->in.transfer_status);
|
|
|
|
if (err != ERR_NO_ERROR)
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Failed to retrieve scan status in event callback");
|
2018-09-25 20:27:01 +02:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&u->in.mutex);
|
|
|
|
|
|
|
|
/* Signal uldaq_read() about new data */
|
|
|
|
pthread_cond_signal(&u->in.cv);
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::uldaq_start(NodeCompat *n)
|
2018-09-25 09:55:51 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *u = n->getData<struct uldaq>();
|
2018-09-24 22:43:44 +02:00
|
|
|
|
2018-09-25 21:46:42 +02:00
|
|
|
u->sequence = 0;
|
2018-10-04 03:00:14 +02:00
|
|
|
u->in.buffer_pos = 0;
|
2018-09-25 21:46:42 +02:00
|
|
|
|
2018-10-04 03:00:14 +02:00
|
|
|
int ret;
|
2018-09-25 10:25:05 +02:00
|
|
|
UlError err;
|
2018-09-24 22:30:40 +02:00
|
|
|
|
2018-09-25 20:27:01 +02:00
|
|
|
/* Allocate a buffer to receive the data */
|
2018-09-25 21:46:42 +02:00
|
|
|
u->in.buffer_len = u->in.channel_count * n->in.vectorize * 50;
|
2020-01-21 16:26:51 +01:00
|
|
|
u->in.buffer = new double[u->in.buffer_len];
|
2020-07-04 16:22:10 +02:00
|
|
|
if (!u->in.buffer)
|
|
|
|
throw MemoryAllocationError();
|
2018-09-24 22:30:40 +02:00
|
|
|
|
2018-10-04 03:00:14 +02:00
|
|
|
ret = uldaq_connect(n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-09-24 21:30:57 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
err = ulAInLoadQueue(u->device_handle, u->in.queues, n->getInputSignals(false)->size());
|
2021-02-16 14:15:14 +01:00
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
throw RuntimeError("Failed to load input queue to DAQ device");
|
2018-09-24 22:47:18 +02:00
|
|
|
|
2018-09-25 20:27:01 +02:00
|
|
|
/* Enable the event to be notified every time samples are available */
|
2018-09-25 20:38:34 +02:00
|
|
|
err = ulEnableEvent(u->device_handle, DE_ON_DATA_AVAILABLE, n->in.vectorize, uldaq_data_available, n);
|
2018-09-25 20:27:01 +02:00
|
|
|
|
2018-09-25 12:56:23 +02:00
|
|
|
/* Start the acquisition */
|
2019-04-22 23:43:46 +02:00
|
|
|
err = ulAInScan(u->device_handle, 0, 0, (AiInputMode) 0, (Range) 0, u->in.buffer_len / u->in.channel_count, &u->in.sample_rate, u->in.scan_options, u->in.flags, u->in.buffer);
|
2018-09-25 13:09:17 +02:00
|
|
|
if (err != ERR_NO_ERROR) {
|
2021-02-16 13:30:26 +01:00
|
|
|
char buf[ERR_MSG_LEN];
|
|
|
|
ulGetErrMsg(err, buf);
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Failed to start acquisition on DAQ device: {}", buf);
|
2018-09-25 13:09:17 +02:00
|
|
|
}
|
2018-09-25 12:56:23 +02:00
|
|
|
|
|
|
|
/* Get the initial status of the acquisition */
|
2018-09-25 20:27:01 +02:00
|
|
|
err = ulAInScanStatus(u->device_handle, &u->in.status, &u->in.transfer_status);
|
2018-09-25 13:09:17 +02:00
|
|
|
if (err != ERR_NO_ERROR) {
|
2021-02-16 13:30:26 +01:00
|
|
|
char buf[ERR_MSG_LEN];
|
|
|
|
ulGetErrMsg(err, buf);
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Failed to retrieve scan status on DAQ device: {}", buf);
|
2018-09-25 13:09:17 +02:00
|
|
|
}
|
2018-09-25 12:56:23 +02:00
|
|
|
|
2018-09-25 20:27:01 +02:00
|
|
|
if (u->in.status != SS_RUNNING) {
|
2021-02-16 13:30:26 +01:00
|
|
|
char buf[ERR_MSG_LEN];
|
|
|
|
ulGetErrMsg(err, buf);
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Acquisition did not start on DAQ device: {}", buf);
|
2018-09-25 13:09:17 +02:00
|
|
|
}
|
2018-09-24 21:30:57 +02:00
|
|
|
|
2018-09-25 10:25:05 +02:00
|
|
|
return 0;
|
2018-09-24 21:30:57 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::uldaq_stop(NodeCompat *n)
|
2018-09-24 21:30:57 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *u = n->getData<struct uldaq>();
|
2018-09-24 21:30:57 +02:00
|
|
|
|
2018-09-25 12:56:23 +02:00
|
|
|
UlError err;
|
2018-09-25 20:27:01 +02:00
|
|
|
|
2021-07-09 19:04:46 +02:00
|
|
|
/* @todo Fix deadlock */
|
2018-09-25 21:09:36 +02:00
|
|
|
//pthread_mutex_lock(&u->in.mutex);
|
2018-09-25 12:56:23 +02:00
|
|
|
|
|
|
|
/* Get the current status of the acquisition */
|
2018-09-25 20:27:01 +02:00
|
|
|
err = ulAInScanStatus(u->device_handle, &u->in.status, &u->in.transfer_status);
|
2018-09-25 12:56:23 +02:00
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
2018-09-25 09:54:07 +02:00
|
|
|
|
2018-09-25 12:56:23 +02:00
|
|
|
/* Stop the acquisition if it is still running */
|
2018-09-25 20:27:01 +02:00
|
|
|
if (u->in.status == SS_RUNNING) {
|
2018-09-25 12:56:23 +02:00
|
|
|
err = ulAInScanStop(u->device_handle);
|
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
|
|
|
}
|
2018-09-24 21:30:57 +02:00
|
|
|
|
2018-09-25 21:09:36 +02:00
|
|
|
//pthread_mutex_unlock(&u->in.mutex);
|
2018-09-25 20:27:01 +02:00
|
|
|
|
2018-09-25 12:56:23 +02:00
|
|
|
err = ulDisconnectDaqDevice(u->device_handle);
|
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
err = ulReleaseDaqDevice(u->device_handle);
|
|
|
|
if (err != ERR_NO_ERROR)
|
|
|
|
return -1;
|
2018-09-24 21:30:57 +02:00
|
|
|
|
2018-09-25 10:25:05 +02:00
|
|
|
return 0;
|
2018-09-24 21:30:57 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::uldaq_read(NodeCompat *n, struct Sample * const smps[], unsigned cnt)
|
2018-09-24 21:30:57 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *u = n->getData<struct uldaq>();
|
2018-09-24 21:30:57 +02:00
|
|
|
|
2018-09-25 20:27:01 +02:00
|
|
|
pthread_mutex_lock(&u->in.mutex);
|
2018-10-04 03:00:14 +02:00
|
|
|
|
2018-09-25 20:27:01 +02:00
|
|
|
if (u->in.status != SS_RUNNING)
|
|
|
|
return -1;
|
|
|
|
|
2019-06-27 01:51:03 +02:00
|
|
|
size_t start_index = u->in.buffer_pos;
|
2018-09-25 22:41:27 +02:00
|
|
|
|
2019-02-20 18:59:23 +00:00
|
|
|
/* Wait for data available condition triggered by event callback */
|
2020-10-12 17:43:30 +02:00
|
|
|
if (start_index + n->in.vectorize * u->in.channel_count > u->in.transfer_status.currentTotalCount)
|
2019-02-20 18:59:23 +00:00
|
|
|
pthread_cond_wait(&u->in.cv, &u->in.mutex);
|
|
|
|
|
2019-04-22 23:43:46 +02:00
|
|
|
for (unsigned j = 0; j < cnt; j++) {
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Sample *smp = smps[j];
|
2018-09-25 12:56:23 +02:00
|
|
|
|
2018-09-25 20:52:55 +02:00
|
|
|
long long scan_index = start_index + j * u->in.channel_count;
|
2018-09-25 12:56:23 +02:00
|
|
|
|
2019-04-22 23:43:46 +02:00
|
|
|
for (unsigned i = 0; i < u->in.channel_count; i++) {
|
2018-09-25 20:27:01 +02:00
|
|
|
long long channel_index = (scan_index + i) % u->in.buffer_len;
|
|
|
|
|
|
|
|
smp->data[i].f = u->in.buffer[channel_index];
|
2018-09-24 22:43:44 +02:00
|
|
|
}
|
2018-09-25 20:38:34 +02:00
|
|
|
|
|
|
|
smp->length = u->in.channel_count;
|
2021-08-10 10:12:48 -04:00
|
|
|
smp->signals = n->getInputSignals(false);
|
2018-09-25 21:46:42 +02:00
|
|
|
smp->sequence = u->sequence++;
|
2019-06-23 16:13:23 +02:00
|
|
|
smp->flags = (int) SampleFlags::HAS_SEQUENCE | (int) SampleFlags::HAS_DATA;
|
2018-09-24 22:43:44 +02:00
|
|
|
}
|
2018-10-04 03:00:14 +02:00
|
|
|
|
2019-02-09 21:21:12 +00:00
|
|
|
u->in.buffer_pos += u->in.channel_count * cnt;
|
2018-10-04 03:00:14 +02:00
|
|
|
|
2018-09-25 20:27:01 +02:00
|
|
|
pthread_mutex_unlock(&u->in.mutex);
|
2018-09-24 21:30:57 +02:00
|
|
|
|
2018-09-25 20:27:01 +02:00
|
|
|
return cnt;
|
|
|
|
}
|
2018-09-24 21:30:57 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static NodeCompatType p;
|
2019-04-23 00:36:06 +02:00
|
|
|
|
|
|
|
__attribute__((constructor(110)))
|
|
|
|
static void register_plugin() {
|
2021-06-21 16:11:42 -04:00
|
|
|
p.name = "uldaq";
|
|
|
|
p.description = "Measurement Computing DAQ devices like UL201 (libuldaq)";
|
|
|
|
p.vectorize = 0;
|
|
|
|
p.flags = 0;
|
|
|
|
p.size = sizeof(struct uldaq);
|
|
|
|
p.type.start = uldaq_type_start;
|
|
|
|
p.init = uldaq_init;
|
|
|
|
p.destroy = uldaq_destroy;
|
|
|
|
p.parse = uldaq_parse;
|
|
|
|
p.print = uldaq_print;
|
|
|
|
p.start = uldaq_start;
|
|
|
|
p.stop = uldaq_stop;
|
|
|
|
p.read = uldaq_read;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static NodeCompatFactory ncp(&p);
|
2019-04-23 13:14:47 +02:00
|
|
|
}
|