Code Monkey home page Code Monkey logo

modelica-3rdparty / modelica_devicedrivers Goto Github PK

View Code? Open in Web Editor NEW
75.0 18.0 30.0 23.56 MB

Free library for interfacing hardware drivers to Modelica models. There is support for joysticks, keyboards, UDP, TCP/IP, LCM, MQTT, shared memory, AD/DA converters, serial port and other devices.

License: BSD 3-Clause "New" or "Revised" License

Modelica 51.00% CMake 2.98% C 36.07% C++ 1.25% Makefile 1.42% Shell 0.08% Motoko 7.20%
modelica modelica-library mqtt mqtt-client iot hacktoberfest

modelica_devicedrivers's Issues

Support of SimulationX wanted?

Support of SimulationX built-in simulation requires a vendor specific annotation in case of a missing Library annotation for external functions.

This is OK:

  encapsulated function write
    import Modelica_DeviceDrivers.Communication.SharedMemory;
    input SharedMemory sm;
    input String data;
    input Integer len;
    external "C" MDD_SharedMemoryWrite(sm,data,len)
    annotation(IncludeDirectory="modelica://Modelica_DeviceDrivers/Resources/Include",
           Include = "#include \"MDDSharedMemory.h\" ",
           Library = "rt");
  end write;

But this needs to be changed from

  encapsulated function getDataSize
    import Modelica_DeviceDrivers.Communication.SharedMemory;
    input SharedMemory sm;
    output Integer length;
    external "C" length=  MDD_SharedMemoryGetDataSize(sm)
    annotation(IncludeDirectory="modelica://Modelica_DeviceDrivers/Resources/Include",
           Include = "#include \"MDDSharedMemory.h\" ");
  end getDataSize;

to either

  encapsulated function getDataSize
    import Modelica_DeviceDrivers.Communication.SharedMemory;
    input SharedMemory sm;
    output Integer length;
    external "C" length=  MDD_SharedMemoryGetDataSize(sm)
    annotation(IncludeDirectory="modelica://Modelica_DeviceDrivers/Resources/Include",
           Include = "#include \"MDDSharedMemory.h\" ",
           __iti_dll = "rt.dll");
  end getDataSize;

or

  encapsulated function getDataSize
    import Modelica_DeviceDrivers.Communication.SharedMemory;
    input SharedMemory sm;
    output Integer length;
    external "C" length=  MDD_SharedMemoryGetDataSize(sm)
    annotation(IncludeDirectory="modelica://Modelica_DeviceDrivers/Resources/Include",
           Include = "#include \"MDDSharedMemory.h\" ",
           Library = "rt");
  end getDataSize;

If wanted I can send pull request on it. Please comment.

Modelica License 2 for header files

Is it right to have the header files licensed under Modelica License 2? Usually only the Modelica files of a package use Modelica License 2 whereas C source and header files use some more appropriate license like BSD3.

Awkward I/O causality in communication examples

As written in c273fe6 I noticed that most of the communication example models are awkward due to the missing I/O causality of the communication source and sink block being both in the same model. Thus it is not defined if a source reads the data from the current or the previous sample step.

This can be solved if the source blocks get some kind of causality input (which would be the pkgIn.dummy of the corresponding sink block). Shall we do this? @bernhard-thiele Please advise if I can do it.

By the way, if I remember correctly TLC (code generation from Simulink models) has some strategy to send as early as possible and to read as late as possible.

Windows compilation

Windows compilation is restricted to MSVC. It should be straight forward to relax Windows compilation to also support MinGW and Cygwin.

MDD_socketCANRead not in compliance with MLS

There is a comment @TODO Not sure whether memcpy to data is possible in external Modelica functions. Check. on function MDD_socketCANRead in MDDSocketCAN.h. According to the Modelica Language Specification (MLS) input arguments of type String map to const char* C type. If doing so the memcpy operation is no longer legal. New memory for the return buffer needs to be allocated by the ModelicaAllocateString function. See also c94ae5c for #23 and #27 where it was fixed for MDDSoftingCAN.h.

TCP/IP support wanted?

My branch add-TCP/IP-support at tbeu/Modelica_DeviceDrivers contains a not yet final implementation of a TCP/IP socket client (for Windows).
@bernhard-thiele Can you please test and estimate if you want to have this in the library? In Incubate?

Fix of #10 introduced new bug on my machine

I just noticed that the changes of #10 result in error when trying to compile on my windows machine:
16>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ctype.h(41): error C2485: '__declspec': nicht erkanntes erweitertes Attribut
16>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ctype.h(41): error C2059: Syntaxfehler: '('

Reverting to the version before the fix, fixes the bug for me.
@tbeu Since I don't work so much with Windows and Visual Studio I wonder if you have any comments on that. For the moment, I'm going to revert this change.

MDD_udpReceivingThread

Two questions on MDD_udpReceivingThread of MDDUDPSocket.h.

  1. Why are there two different functions MDD_udpReceivingThread?
  2. Is it really necessary to have DllExport on first function MDD_udpReceivingThread? It is not referenced from Modelica files.

Only clocked/sampled RandomRealSource

There actually is no need to have sampled RNG in Blocks.OperatingSystem.RandomRealSource. I propose to add a conditional external trigger port for the block. In case this trigger is off the RNG should happen in every step (which matches also the block icon). Do you agree?

Dimension conflict in SerialPackager

I stumbled accross the following lines in the add and get functions of the SerialPackager, e.g.

    when pkgIn.trigger then
      pkgOut.dummy =
        Modelica_DeviceDrivers.Blocks.Packaging.SerialPackager.Internal.DummyFunctions.addRealAsFloat(
          pkgOut.pkg,
          u,
          pkgIn.dummy,
          byteOrder);
    end when;

addRealAsFloat takes a scalar SerialPackager as first argument and returns a scalar Real variable. However pkgOut is not a scalar PackageOut connector but of dimension nu.

What is the reason of having nu in PartialSerialPackager?

Different directory structure between branches

Branch release has different directory structure (with top level directory Modelica_DeviceDrivers 1.2) than branches master or dllwrapper. This is not compatible when switching branches.

I propose to also add this top level directory (available with release branch) to other branches.

Support of big endian

Customer uses UDPSend to send data (double, float, int32) to external tool. He noticed that only little endian is supported whereas external tool expects big endian.
It would be nice if byte order layout could be chosen in UDP (and similar) blocks. Or should it go to Packager or Add/Get blocks? I can do it but would like to know where it belongs best?
@bernhard-thiele Please advice quickly since customer askes for it.

Issues on SynchronizeRealtime

MDD_realtimeSynchronize uses static variables which is no good programming since it is not thread-safe. Therefore it would be better to implement Modelica_DeviceDrivers.Blocks.OperatingSystem.SynchronizeRealtime as an external object.

Old-style icon

The package icon of Modelica_DeviceDrivers.Blocks looks old-style compared to all other icons visible in the library browser. Should the icon of Modelica.Blocks be reused here?

x64 binary of ITI_MDDSoftingCAN.dll

Modelica_DeviceDrivers\Resources\Library\win64\ITI_MDDSoftingCAN.dll links against Debug Version of MSVC runtime (MSVCR100D.dll). Please adapt to use release version.

CANL2_CH_STRUCT not defined in CAN Layer2 V5.17

I almost forgot this one to report: I installed CAN Layer2 V5.17 where in CANL2.H the struct CANL2_CH_STRUCT is commented out:

/*
typedef struct canl2_ch_s {
    CAN_HANDLE     ulChannelHandle;
    unsigned char  sChannelName[80];
} CANL2_CH_STRUCT;
*/

However, CANL2_CH_STRUCT is referenced by MDDSoftingCAN.h.

I propose to define and use an own struct based on the commented type.

Improve documentation regarding 32-bit vs 64-bit pitfalls of Linux libraries

Some commercial tool are only available as 32-bit application for Linux. Also the compilation of the simulation binary is done for 32-bit. Consequently, system libraries required by some devices need to be available as 32-bit libraries in these cases. This is sometimes a difficulty for users to figure out.

Notably, the keyboard interface needs the X11 library, and it must be available as 32-bit version if compilation is done for 32-bit.

Might think of writing a guide for a specific combination of tool and linux distribution (e.g., Dymola + Ubuntu).

libraryinfo.mos

The libraryinfo.mos file says version 1.2 and MSL version 3.2. Should this be updated?

Release plan

Any plans to release a new version? v1.3 was released one year ago and many fixes where added since then.

I also propose to switch to semantic versioning, i.e. 1.4.0 would be a proper name according to https://github.com/mojombo/semver.

TestSerialPackager_UDP: Initial conditions are not fully specified

Translation of Modelica_DeviceDrivers.Blocks.Examples.TestSerialPackager_UDP:

The DAE has 102 scalar unknowns and 102 scalar equations.

The initial conditions for variables of type Real are not fully specified.
Dymola has selected default initial conditions.

Assuming fixed default start value for the discrete states:
getFloat.y[1](start = 0.0)
getFloat.y[2](start = 0.0)
getReal.y[1](start = 0.0)
getReal.y[2](start = 0.0)
getReal.y[3](start = 0.0)

The initial conditions for variables of type Integer are not fully specified.
Dymola has selected default initial conditions.

Assuming fixed default start value for the discrete states:
getInteger.y[1](start = 0)

Statistics

Original Model
Number of components: 33
Variables: 137
Parameters: 31 (31 scalars)
Unknowns: 106 (112 scalars)
Equations: 47
Nontrivial: 47

Translated Model
Constants: 30 scalars
Free parameters: 18 scalars
Parameter depending: 23 scalars
Time-varying variables: 26 scalars
Alias variables: 46 scalars
Assumed default initial conditions: 6
Number of mixed real/discrete systems of equations: 0
Sizes of linear systems of equations: { }
Sizes after manipulation of the linear systems: { }
Sizes of nonlinear systems of equations: { }
Sizes after manipulation of the nonlinear systems: { }
Number of numerical Jacobians: 0

Selected continuous time states

Finished

// experiment StopTime=5 StartTime=0

WARNING: 2 warnings were issued

Finished

Should this be fixed in the Get blocks or in the example model?

Annotation for External Libraries and Include Files

According to the Modelica Language Specification chapter 12.9.4 the annotations for external libraries and include files should be placed on the external function declaration (and not on the Modelica function).
So instead of

encapsulated function constructor "Claim the memory"
    import Modelica_DeviceDrivers.Packaging.SerialPackager;
  input Integer bufferSize = 16 * 1024;
  output SerialPackager pkg;
  external "C" pkg = MDD_SerialPackagerConstructor(bufferSize);
  annotation(IncludeDirectory="modelica://Modelica_DeviceDrivers/Resources/Include",
             Include = "#include \"MDDSerialPackager.h\" ");
end constructor;

better have

encapsulated function constructor "Claim the memory"
  import Modelica_DeviceDrivers.Packaging.SerialPackager;
  input Integer bufferSize = 16 * 1024;
  output SerialPackager pkg;
  external "C" pkg = MDD_SerialPackagerConstructor(bufferSize)
    annotation(IncludeDirectory="modelica://Modelica_DeviceDrivers/Resources/Include",
               Include = "#include \"MDDSerialPackager.h\" ");
end constructor;

Note the missing semicolon after the external function declaration.

Inconsitency of inclusion of ModelicaUtilities.h

In the Library include files we find includes like this

#include "ModelicaUtilities.h"
#include "../src/include/CompatibilityDefs.h"

in case of MSVC. But ModelicaUtilities.h is located in same directory as CompatibilityDefs.h is. Thus ModelicaUtilities.h is not found without explicitly adding src/include to the include path. Thus I propose to get either rid of the extra src/include directory by adding all files to the main Include directory. Or alternetively the include directives should be made consistent.

Only clocked/sampled communication

Currently the communication is cyclic only.

Proposal: This is taken from a trigger type implementation we once did for CAN and where more trigger options are available.

type trigger_type=enumeration(
  event_trig "Event",
  cyclic_trig "Cyclic",
  cyclic_event_trig "Cyclic + Event",
  initial_trig "At Start only",
  terminal_trig "At Stop only") "Use as trigger";

How to use Arduino Uno

I tried to connect arduino uno board with Openmodelica and Modelica_DeviceDrivers ,but couldn't succeed.Could you plz how to perform the communication between Openmodelica and arduino by using Modelica_DeviceDrivers package.

Process priority not reset

Function setProcessPriority is only called at the start of the simulation but not reset at the end of the simulation.

Also it would be nice if there would be an option to not set the process priority in Modelica_DeviceDrivers.Blocks.OperatingSystem.SynchronizeRealtime.

discrete input/output variables of connector

Apply the following patch to the master branch

 Modelica_DeviceDrivers/Blocks/Interfaces.mo | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Modelica_DeviceDrivers/Blocks/Interfaces.mo b/Modelica_DeviceDrivers/Blocks/Interfaces.mo
index 76705ef..d667751 100644
--- a/Modelica_DeviceDrivers/Blocks/Interfaces.mo
+++ b/Modelica_DeviceDrivers/Blocks/Interfaces.mo
@@ -5,7 +5,7 @@ package Interfaces
   connector PackageIn "Packager input connector"
     input Modelica_DeviceDrivers.Packaging.SerialPackager pkg;
     input Boolean trigger;
-    input Real dummy;
+    discrete input Real dummy;

     output Boolean backwardTrigger;
     output Integer userPkgBitSize;
@@ -27,7 +27,7 @@ package Interfaces
   connector PackageOut "Packager output connector"
     output Modelica_DeviceDrivers.Packaging.SerialPackager pkg;
     output Boolean trigger;
-    output Real dummy;
+    discrete output Real dummy;

     input Boolean backwardTrigger;
     input Integer userPkgBitSize;

Now Dymola 2016 raises the following translation error on Modelica_DeviceDrivers.Blocks.Examples.TestSerialPackager_UDP which I do not understand.

The following real variables are declared with the prefix discrete,
but are not assigned in a when-clause, either by an assignment or an equation:
addFloat.pkgIn.dummy
addInteger.pkgIn.dummy
addReal.pkgIn.dummy
getFloat.pkgIn.dummy
getInteger.pkgIn.dummy
getReal.pkgIn.dummy
uDPSend.pkgIn.dummy

HTML errors

The released version v1.1 contains lots of HTML errors.
https://gist.github.com/dietmarw/c4dc5629e581702eb2d1

In addition are links to images or other packages are pointing to the wrong place (looks like the URL was typed incorrectly):

[Checking file /home/dietmarw/.workspace/gh-modelica/Modelica_DeviceDrivers/Resources/help/Modelica_DeviceDrivers.Blocks.Packaging.SerialPackager.UnpackUnsignedInteger.html]:
Not found: Modelica_DeviceDrivers.Blocks.Examples.TestSerialPackagerBitUnpack_UDP.html
[Checking file /home/dietmarw/.workspace/gh-modelica/Modelica_DeviceDrivers/Resources/help/Modelica_DeviceDrivers.UsersGuide.html]:
Not found: Modelica_DeviceDrivers.UsersGuide.Literature.html
[Checking file /home/dietmarw/.workspace/gh-modelica/Modelica_DeviceDrivers/Resources/help/Modelica_DeviceDrivers.Utilities.Icons.InternalPackage.html]:
Not found: ../Extras/Images/dlr_logo.png
[Checking file /home/dietmarw/.workspace/gh-modelica/Modelica_DeviceDrivers/Resources/help/Modelica_DeviceDrivers.Utilities.Icons.html]:
Not found: ../Extras/Images/dlr_logo.png

Github project description

I edited the description from

Free (standard conform) library for interfacing hardware drivers to Modelica models. There is support for joysticks, keyboards, UDP, shared memory, AD/DA converters and other devices.

to

Free library for interfacing hardware drivers to Modelica models. There is support for joysticks, keyboards, UDP, shared memory, AD/DA converters and other devices.

thus deleting the "standard conform" which is misleading and simply does not hold as long as #1669 (= first issue of #54) and #71 aren't resolved. Once they are properly fixed it could be reverted.

MSVC build (CompatibilityDefs.h)

CompatibilityDefs.h includes winsock2.h for MSVC build. This breaks the compilation.

TestInputKeyboardKey.c
C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(91) : warning C4005: 'AF_IPX' : macro redefinition
        C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(460) : see previous definition of 'AF_IPX'
C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(131) : warning C4005: 'AF_MAX' : macro redefinition
        C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(479) : see previous definition of 'AF_MAX'
C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(168) : warning C4005: 'SO_DONTLINGER' : macro redefinition
        C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\winsock.h(402) : see previous definition of 'SO_DONTLINGER'
C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\ws2def.h(212) : error C2011: 'sockaddr' : 'struct' type redefinition
...
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\cl.EXE"' : return code '0x2'

The issue can be resolved if

  • winsock2.h is not included in CompatibilityDefs.h
  • MDDSharedMemory.h also includes Windows.h
  • MDDUDPSocket.h includes Windows.h instead of winsock2.h

I recommend to remove the whole MSVC compatibility block as it is actually not needed.

Communication.SoftingCAN not supported on 64-bit machine

There is Library={"canL2"} set to the annotation of the external functions of the Communication.SoftingCAN. However, canL2.lib and canL2.dll are not available for 64-bit simulation tools where the appropriate Softing lib is named canL2_64.

Assignment of external objects not conform with MLS

Customer asked for UDP support in SimulationX. Thus I tried to figure out why SerialPackager fails in SimulationX. The reason is that the external objects, i.e. there constructors are used in assignments which is illegal according to the MLS 3.3 (rev 1) section 12.9.7: "Only the constructor may return external objects and external object can only be bound in component declarations and neither modified later nor assigned to."
Thus the current design of the SerialPackager and the Communication package is bad in two ways ๐Ÿ˜’

  1. External objects in connectors are not legal since the connect statement is equivalent to an assignment equation.
  2. Constructing the external object in when initial() is not legal at all.

Whereas SimulationX is tolerant with the first problem the second problem is a really bad design failure. I changed the construction of the external object from something like

model Packager
  Interfaces.PackageOut pkgOut;
  protected
    Integer bufferSize;
  equation
    when initial() then
      bufferSize = something;
      pkgOut.pkg = SerialPackager(bufferSize); // create external object here
    end when;
end Packager;

to

model Packager
  Interfaces.PackageOut pkgOut(pkg = SerialPackager(bufferSize));
  protected
    Integer bufferSize;
  equation
    when initial() then
      bufferSize = something;
    end when;
end Packager;

which still violates the first problem but could fix the second one. However, this now reveals the bad design of the auto propagation of the buffer size. It is not clear which external object defines the auto buffer size and which external object uses the derived buffer size.

In the need for a quick fix for the second problem I would consider to either use a fixed buffer size or refer to some global parameters like e.g. Modelica.Mechanics.MultiBody.World of the MSL holds.

@bernhard-thiele, @MartinOtter What do you think? Can we find a workaround in order to get the customer project running soon? Do you have other ideas to fix the second problem (or even the first problem to make the library really standard conform)? I am willing to contribute of course.

Linux support of SoftingCAN

In Softing's download center I can see a "CAN-AC PCI V5.18 Driver for Linux Kernel 3.2 (Kubuntu 12.04 LTS)". Does this mean that SoftingCAN could be ported to Linux?

Improve JoystickInput

Linux implementation of MDD_joystickGetData uses static variables and never frees allocated memory for axis and button arrays. It should be turned into an external object.

DllExport in CompatibilityDefs.h

DllExport in CompatibilityDefs.h is always defined no matter if the project is compiled as DLL or EXE. I propose to add a new macro MDD_EXPORTS that needs to be actively set if MDD is compiled as dynamic library, i.e. MDD_EXPORTS has to be set in the cmake build script / VS project.

MDDSpaceMouse.lib

It looks like MDDSpaceMouse.lib is (and also previously was) compiled with /MD. If I am not wrong Dymola takes /MT as default. Please check and test.

TestInputSpaceMouse.c
MSVCRT.lib(MSVCR90.dll) : error LNK2005: _free already defined in LIBCMT.lib(free.obj)
MSVCRT.lib(MSVCR90.dll) : error LNK2005: _malloc already defined in LIBCMT.lib(malloc.obj)
   Creating library TestInputSpaceMouse.lib and object TestInputSpaceMouse.exp
LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
TestInputSpaceMouse.exe : fatal error LNK1169: one or more multiply defined symbols found
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\link.EXE"' : return code '0x491'
Stop.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.