Skip to content

FLEXLAB Input Output

Michael Wetter edited this page Feb 17, 2014 · 4 revisions

Summary

This feature and requirement specification describes how to exchange data with actuators, sensors and the data acquisition system for FLEXLAB to enable hardware-in-the-loop simulations. The feature specification and requirements are separated into sections that are specific to Modelica and to the C-functions that implement the communication with FLEXLAB. This is done to allow using the C functions for other simulators.

Modelica

In Modelica, a user must be able to instantiate a Modelica block that sends actuation signals to an actuator. The block must have the following parameters

  1. the sample period in seconds,
  2. the time out in seconds,
  3. a data base querry, and
  4. the verbosity level using an enumeration.

The same procedure, with a different block, must be used to read data from FLEXLAB.

C

The C-functions must be non-blocking, e.g., they time out and return if the communication fails. They must return information messages which a tool may display in a verbose mode, warning messages, and error messages. In case of an error, the C functions must return a descriptive string that indicates the error. A tool that implements these C functions can then propagate the error message to its output device (such as a log file or a graphical user interface). The C functions must not call exit(), as a tool that calls the C functions may have to call its own exit routines.

Remark about reading values

A read command sends a pair (time, sensor_address) to retrieve the value of the sensor at the specified time. However, there is no guarantee that a value exists at the specified time: It may be missing, or the time may not coincide with a sampling time. In addition, some (continuous) data may be interpolated, while this does not make sense for discrete values. How this should be addressed still needs to be specified.

Possible Implementation

Modelica

In Modelica, the sampling can be implemented using a similar construct as is used in the Python block. However, separate blocks must be used to separate the write and read commands.

C

In C, there must be separate functions for writing data, and for reading data. Both functions must take as a minimum the following input arguments

  1. A function pointer to write information messages.
  2. A function pointer to write warning messages.
  3. A function pointer to write error messages.
  4. A time out in seconds.

The C function pointers are used to propagate messages to the calling program. A tool that calls these messages can then propagate the message to its output device (such as a graphical user interface). These function pointers must have the signature

void (*informationMessage)(const char *string,...)

The C functions must write the information (or warning or error) message to this function pointer using, for example,

(*informationMessage)("Wrote to the sensor '%s'.\n", sensorName);

For an example implementation, see the function pythonExchangeValuesNoModelica.c.

In case of communication failure, the functions must return after the time out period elapsed.

The read function must take as an input argument the data base querry that includes the address of the sensor and the address of its value in the data base. The read function must then figure out whether the value can be obtained from the database or the sensor. (This must be hidden in the C function as the caller cannot know whether the value has already been flushed from the data acquisition to the long-term data base.) The read function also takes as an argument pointers to arrays, the maximum number of elements, and a pointer to an integer that declares how many element were returned. A possible signature is

void FLEXLABReadValues(const char * querry,
                       double * dbl, size_t maxDbl, size_t * nDblRet,
                       double * int, size_t maxInt, size_t * nIntRet,
                       void (*info)(const char *string,...),
                       void (*info)(const char *warning,...),
                       void (*info)(const char *error,...));

The argument querry is a database querry. The C function must automatically get the data eiter from the data base or from a sensor. maxTYPE is the size of the array, and the arguments nTYPERet is used to return the actual number of values written into dbl (or int), or zero if no data is available.

Question: do we need to read strings? If yes, how do we deal with variable string lenghts if we want to return an array of strings?

Question: How do we know the time stamps of the returned data if they don't coincide with the querry? Do we need another array for each data type that contains the associated time stamp?

The write function must take as an input argument the address of the actuator.

A possible signature is

void FLEXLABWriteValues(const char * command,
                        void (*info)(const char *string,...),
                        void (*info)(const char *warning,...),
                        void (*info)(const char *error,...));

where command is a FLEXLab write command.

The C functions must be developed so that they can be compiled on Windows, Linux and Mac OS X (all 32/64 bit, except for Mac which will only be 64 bit). The source code must be released under the BSD license.

It is recommended to use the following construct in the Makefile. The recommendation below is adapted from the Modelica Specification 3.3.

// Header file for the function in the dynamic link/shared
// library ExternalLib2 so that the desired functions
// are defined to be exported for Microsoft VisualStudio
// and for GNU C-compiler (note, for Linux it is recommended
// to use the compiler option “-fPIC” to build shared libraries
// or object libraries that are later transformed to
// a shared library):
// File ExternalFunc2.h
#ifdef __cplusplus extern "C" {
#endif
#ifdef _MSC_VER
#ifdef EXTERNAL_FUNCTION_EXPORT
# define EXTLIB2_EXPORT __declspec( dllexport ) #else
# define EXTLIB2_EXPORT __declspec( dllimport ) #endif
#elif __GNUC__ >= 4
     /* In gnuc, all symbols are by default exported. It is still often useful,
        to not export all symbols but only the needed ones */
# define EXTLIB2_EXPORT __attribute__ ((visibility("default"))) #else
# define EXTLIB2_EXPORT
#endif
 EXTLIB2_EXPORT void ExternalFunc2(<function arguments>);
#ifdef __cplusplus }
#endif

Discussion

It is not clear how the authentication with user name and password should be handled. This data must not be part of the model parameters, as the model is likely to be on an open shared drive.

Actual Implementation

n/a