1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/nodes/opal.c

325 lines
9.4 KiB
C
Raw Permalink Normal View History

/** Node type: OPAL (AsyncApi)
*
* This file implements the opal subtype for nodes.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
2017-04-27 12:56:43 +02:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2017-04-27 12:56:43 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <stdlib.h>
#include <math.h>
#include "nodes/opal.h"
#include "utils.h"
#include "plugin.h"
/* Private static storage */
static char *async_shmem_name; /**< Shared Memory identifiers and size, provided via argv. */
static char *print_shmem_name; /**< Shared Memory identifiers and size, provided via argv. */
static int async_shmem_size; /**< Shared Memory identifiers and size, provided via argv. */
static int send_icons, recv_icons; /** Number of send blocks used in the running OPAL model. */
static int *send_ids, *recv_ids; /** A dynamically allocated array of SendIDs. */
static Opal_GenAsyncParam_Ctrl params; /** String and Float parameters, provided by the OPAL AsyncProcess block. */
static pthread_mutex_t lock; /** Big Global Lock for libOpalAsync API */
2015-06-02 22:04:03 +02:00
int opal_register_region(int argc, char *argv[])
{
async_shmem_name = argv[1];
async_shmem_size = atoi(argv[2]);
print_shmem_name = argv[3];
}
int opal_init(struct super_node *sn)
2015-12-11 17:56:14 +01:00
{
int err;
2015-08-07 01:11:43 +02:00
if (sn->cli.argc != 4)
return -1;
pthread_mutex_init(&lock, NULL);
2015-08-07 01:11:43 +02:00
/* Enable the OpalPrint function. This prints to the OpalDisplay. */
err = OpalSystemCtrl_Register(print_shmem_name);
2015-09-19 15:28:28 +02:00
if (err != EOK)
error("OpalPrint() access not available (%d)", err);
2015-08-07 01:11:43 +02:00
/* Open Share Memory created by the model. */
err = OpalOpenAsyncMem(async_shmem_size, async_shmem_name);
2015-09-19 15:28:28 +02:00
if (err != EOK)
error("Model shared memory not available (%d)", err);
err = OpalGetAsyncCtrlParameters(&params, sizeof(Opal_GenAsyncParam_Ctrl));
2015-09-19 15:28:28 +02:00
if (err != EOK)
error("Could not get OPAL controller parameters (%d)", err);
2015-08-07 01:11:43 +02:00
/* Get list of Send and RecvIDs */
err = OpalGetNbAsyncSendIcon(&send_icons);
2015-09-19 15:28:28 +02:00
if (err != EOK)
error("Failed to get number of send blocks (%d)", err);
err = OpalGetNbAsyncRecvIcon(&recv_icons);
2015-09-19 15:28:28 +02:00
if (err != EOK)
error("Failed to get number of recv blocks (%d)", err);
2015-08-07 01:11:43 +02:00
send_ids = alloc(send_icons * sizeof(int));
recv_ids = alloc(recv_icons * sizeof(int));
2015-08-07 01:11:43 +02:00
err = OpalGetAsyncSendIDList(send_ids, send_icons * sizeof(int));
2015-09-19 15:28:28 +02:00
if (err != EOK)
error("Failed to get list of send ids (%d)", err);
err = OpalGetAsyncRecvIDList(recv_ids, recv_icons * sizeof(int));
2015-09-19 15:28:28 +02:00
if (err != EOK)
error("Failed to get list of recv ids (%d)", err);
2015-08-07 01:11:43 +02:00
info("Started as OPAL Asynchronous process");
2016-06-08 23:21:42 +02:00
info("This is VILLASnode %s (built on %s, %s)",
BUILDID, __DATE__, __TIME__);
2015-09-22 13:20:20 +02:00
opal_print_global();
return 0;
}
int opal_deinit()
2015-12-11 17:56:14 +01:00
{
2015-03-17 22:59:43 +01:00
int err;
err = OpalCloseAsyncMem(async_shmem_size, async_shmem_name);
2015-09-19 15:28:28 +02:00
if (err != EOK)
error("Failed to close shared memory area (%d)", err);
2015-08-07 01:11:43 +02:00
2017-02-12 14:12:35 -03:00
debug(LOG_OPAL | 4, "Closing OPAL shared memory mapping");
2015-08-07 01:11:43 +02:00
err = OpalSystemCtrl_UnRegister(print_shmem_name);
2015-09-19 15:28:28 +02:00
if (err != EOK)
error("Failed to close shared memory for system control (%d)", err);
2015-08-07 01:11:43 +02:00
pthread_mutex_destroy(&lock);
free(send_ids);
free(recv_ids);
2015-08-07 01:11:43 +02:00
return 0;
}
int opal_print_global()
2015-12-11 17:56:14 +01:00
{
2017-02-12 14:12:35 -03:00
debug(LOG_OPAL | 2, "Controller ID: %u", params.controllerID);
char *sbuf = alloc(send_icons * 5);
char *rbuf = alloc(recv_icons * 5);
2015-08-07 01:11:43 +02:00
for (int i = 0; i < send_icons; i++)
strcatf(&sbuf, "%u ", send_ids[i]);
for (int i = 0; i < recv_icons; i++)
strcatf(&rbuf, "%u ", recv_ids[i]);
2015-08-07 01:11:43 +02:00
2017-02-12 14:12:35 -03:00
debug(LOG_OPAL | 2, "Send Blocks: %s", sbuf);
debug(LOG_OPAL | 2, "Receive Blocks: %s", rbuf);
free(sbuf);
free(rbuf);
2017-02-12 14:12:35 -03:00
debug(LOG_OPAL | 2, "Control Block Parameters:");
2015-10-06 09:10:08 +02:00
for (int i = 0; i < GENASYNC_NB_FLOAT_PARAM; i++)
2017-02-12 14:12:35 -03:00
debug(LOG_OPAL | 2, "FloatParam[]%u] = %f", i, params.FloatParam[i]);
2015-10-06 09:10:08 +02:00
for (int i = 0; i < GENASYNC_NB_STRING_PARAM; i++)
2017-02-12 14:12:35 -03:00
debug(LOG_OPAL | 2, "StringParam[%u] = %s", i, params.StringParam[i]);
2015-08-07 01:11:43 +02:00
return 0;
}
int opal_parse(struct node *n, json_t *cfg)
{
2017-10-18 15:39:53 +02:00
struct opal *o = (struct opal *) n->_vd;
2015-08-07 01:11:43 +02:00
int ret;
json_error_t err;
ret = json_unpack_ex(cfg, &err, 0, "{ s: i, s: i, s: b }",
"send_id", &o->send_id,
"recv_id", &o->recv_id,
"reply", &o->reply
);
if (ret)
jerror(&err, "Failed to parse configuration of node %s", node_name(n));
return 0;
}
char * opal_print(struct node *n)
{
2017-10-18 15:39:53 +02:00
struct opal *o = (struct opal *) n->_vd;
2015-08-07 01:11:43 +02:00
/** @todo: Print send_params, recv_params */
2015-08-07 01:11:43 +02:00
return strf("send_id=%u, recv_id=%u, reply=%u",
o->send_id, o->recv_id, o->reply);
}
int opal_start(struct node *n)
{
2017-10-18 15:39:53 +02:00
struct opal *o = (struct opal *) n->_vd;
2015-08-07 01:11:43 +02:00
/* Search for valid send and recv ids */
int sfound = 0, rfound = 0;
for (int i = 0; i < send_icons; i++)
sfound += send_ids[i] == o->send_id;
for (int i = 0; i < send_icons; i++)
rfound += send_ids[i] == o->send_id;
2015-08-07 01:11:43 +02:00
if (!sfound)
error("Invalid send_id '%u' for node %s", o->send_id, node_name(n));
if (!rfound)
error("Invalid recv_id '%u' for node %s", o->recv_id, node_name(n));
2015-08-07 01:11:43 +02:00
/* Get some more informations and paramters from OPAL-RT */
OpalGetAsyncSendIconMode(&o->mode, o->send_id);
OpalGetAsyncSendParameters(&o->send_params, sizeof(Opal_SendAsyncParam), o->send_id);
OpalGetAsyncRecvParameters(&o->recv_params, sizeof(Opal_RecvAsyncParam), o->recv_id);
return 0;
}
int opal_stop(struct node *n)
{
return 0;
}
int opal_read(struct node *n, struct pool *pool, unsigned cnt)
{
2017-10-18 15:39:53 +02:00
struct opal *o = (struct opal *) n->_vd;
2015-08-07 01:11:43 +02:00
int state, len, ret;
unsigned id;
2015-08-07 01:11:43 +02:00
2015-05-07 13:57:43 +02:00
struct msg *m = &pool[first % poolsize];
2015-08-07 01:11:43 +02:00
double data[MSG_VALUES];
2015-10-13 16:14:01 +02:00
if (cnt != 1)
error("The OPAL-RT node type does not support combining!");
2015-08-07 01:11:43 +02:00
/* This call unblocks when the 'Data Ready' line of a send icon is asserted. */
do {
2015-09-19 15:28:28 +02:00
ret = OpalWaitForAsyncSendRequest(&id);
if (ret != EOK) {
state = OpalGetAsyncModelState();
2015-05-07 13:57:43 +02:00
if ((state == STATE_RESET) || (state == STATE_STOP))
error("OpalGetAsyncModelState(): Model stopped or resetted!");
return -1; // FIXME: correct return value
}
} while (id != o->send_id);
/* No errors encountered yet */
OpalSetAsyncSendIconError(0, o->send_id);
/* Get the size of the data being sent by the unblocking SendID */
OpalGetAsyncSendIconDataLength(&len, o->send_id);
if (len > sizeof(data)) {
warn("Ignoring the last %u of %u values for OPAL node %s (send_id=%u).",
len / sizeof(double) - MSG_VALUES, len / sizeof(double), node_name(n), o->send_id);
len = sizeof(data);
}
/* Read data from the model */
OpalGetAsyncSendIconData(data, len, o->send_id);
m->sequence = htons(o->seq_no++);
m->length = len / sizeof(double);
for (int i = 0; i < m->length; i++)
2015-05-07 13:57:43 +02:00
m->data[i].f = (float) data[i]; /* OPAL provides double precission */
/* This next call allows the execution of the "asynchronous" process
* to actually be synchronous with the model. To achieve this, you
* should set the "Sending Mode" in the Async_Send block to
* NEED_REPLY_BEFORE_NEXT_SEND or NEED_REPLY_NOW. This will force
* the model to wait for this process to call this
* OpalAsyncSendRequestDone function before continuing. */
if (o->reply)
OpalAsyncSendRequestDone(o->send_id);
/* Before continuing, we make sure that the real-time model
* has not been stopped. If it has, we quit. */
state = OpalGetAsyncModelState();
2015-05-07 13:57:43 +02:00
if ((state == STATE_RESET) || (state == STATE_STOP))
error("OpalGetAsyncModelState(): Model stopped or resetted!");
2015-05-07 13:57:43 +02:00
return 1;
}
int opal_write(struct node *n, struct pool *pool, unsigned cnt)
{
2017-10-18 15:39:53 +02:00
struct opal *o = (struct opal *) n->_vd;
2015-08-07 01:11:43 +02:00
2015-05-07 13:57:43 +02:00
struct msg *m = &pool[first % poolsize];
2015-08-07 01:11:43 +02:00
int state;
int len;
2015-05-07 13:57:43 +02:00
double data[m->length];
2015-10-13 16:14:01 +02:00
if (cnt != 1)
error("The OPAL-RT node type does not support combining!");
2015-08-07 01:11:43 +02:00
state = OpalGetAsyncModelState();
2015-05-07 13:57:43 +02:00
if ((state == STATE_RESET) || (state == STATE_STOP))
error("OpalGetAsyncModelState(): Model stopped or resetted!");
OpalSetAsyncRecvIconStatus(m->sequence, o->recv_id); /* Set the Status to the message ID */
OpalSetAsyncRecvIconError(0, o->recv_id); /* Set the Error to 0 */
/* Get the number of signals to send back to the model */
OpalGetAsyncRecvIconDataLength(&len, o->recv_id);
if (len > sizeof(data))
warn("Node %s is expecting more signals (%u) than values in message (%u)", node_name(n), len / sizeof(double), m->length);
2015-08-07 01:11:43 +02:00
for (int i = 0; i < m->length; i++)
2015-05-07 13:57:43 +02:00
data[i] = (double) m->data[i].f; /* OPAL expects double precission */
2015-05-07 13:57:43 +02:00
OpalSetAsyncRecvIconData(data, m->length * sizeof(double), o->recv_id);
2015-08-07 01:11:43 +02:00
2015-05-07 13:57:43 +02:00
return 1;
}
static struct plugin p = {
.name = "opal",
.description = "run as OPAL Asynchronous Process (libOpalAsyncApi)",
.type = PLUGIN_TYPE_NODE,
.node = {
.vectoroize = 1,
.size = sizeof(struct opal),
.type.start = opal_type_start,
.type.stop = opal_type_stop,
.parse = opal_parse,
.print = opal_print,
.start = opal_start,
.stop = opal_stop,
.read = opal_read,
.write = opal_write
}
};
2017-07-02 23:53:48 +02:00
REGISTER_PLUGIN(&p)
2017-07-24 19:33:35 +02:00
LIST_INIT_STATIC(&p.node.instances)