Initialize buildable Matlab Simulink Model with parameters from SQLite Database

huangapple go评论60阅读模式
英文:

Initialize buildable Matlab Simulink Model with parameters from SQLite Database

问题

Context: 我有一个庞大的Simulink模型,将用于在Debian 10上进行自动化模拟。因此,必须将其构建为独立的C代码,使用Matlab Coder。然后,调用此代码以启动模拟。

What I need: 我需要找到一种初始化我构建的模型的方式,该模型有大约500个参数。这些参数在每次模拟运行时都会更改,并存储在SQLite文件中。目标是将参数写入数据库,然后启动模型,模型在初始化期间从SQLite中读取参数(可能使用InitFcn模型回调,尽管我愿意考虑其他方法)。

What I have tried:

  1. 直接SQL接口:我尝试使用直接的Matlab-SQL接口,例如JDBC(因为我无法访问Database-toolbox),但这些接口不支持代码生成。

  2. 编写一个读取SQLite文件的C函数,然后在InitFcn回调中使用coder.ceval调用该函数,像这样:

   data = 0;
   err = coder.ceval('read_function', 4, 2, 12, coder.wref(data));
   parameter = data;
  • 这里的问题是coder.wref在Matlab中不受支持,因此在InitFcn中无法工作。(如果我错误,请纠正我)
    这似乎只在Matlab-Function-Block中起作用:
   Error evaluating 'InitFcn' callback of block_diagram 'Model'.
   Caused by:
   The coder.wref function is not supported in MATLAB.

因此,我对第二种方法的问题是,我无法在初始化期间调用C函数。

  1. 使用Matlab函数块读取参数不是一个真正的选项,因为我将不得不导出所有信号,这使得模型的维护和进一步开发变得非常困难。此外,我的建议是,模型可能根本无法运行,因为需要参数来初始化模型。

Questions:

  1. 是否有办法使上述方法之一起作用?如果是,如何操作?我哪里错了?
  2. 是否有另一种(更简单的)选项将数据作为数组或结构传递给我的模型?
英文:

Context: I have a huge Simulink Model that is going to be used for automated simulations on a Debian 10. Therefore it has to be built as standalone C-Code using the Matlab Coder. This code is then called to start the simulation.

What I need: I need to find a way to initialize my built model with ~500 parameters. These change with each simulation run and are stored in a SQLite file. The goal is to have parameters written to the database, then start the Model which reads the parameters from SQLite during initialization (presumably using the InitFcn Model Callback, although I'm open to alternatives).

What I have tried:

  1. Direct SQL interface: I tried to use a direct Matlab-SQL interface such as JDBC (since I don't have access to the Database-toolbox) but those are not supported for Code generation.

  2. Write a C-function that reads the SQLite file, then call the function during initialization in the InitFcn Callback using coder.ceval like this:

   data = 0;
   err = coder.ceval('read_function',4, 2, 12, coder.wref(data));
   parameter = data;
  • Problem here is that coder.wref is not supported in Matlab and therefore doesn't work in the InitFcn. (Please correct me if I'm wrong)
    This only seems to work inside a Matlab-Function-Block:
   Error evaluating 'InitFcn' callback of block_diagram 'Model'.
   Caused by:
   The coder.wref function is not supported in MATLAB.

So my problem with the second approach is, that I can't call the C-function during initialization.

  1. Using a Matlab-function-Block to read the parameters isn't really an option, since I would have to route all the signals out which makes maintaining and further development of the model really hard. Also my suggestion is, that the model would not even run because the parameters are needed to initialize the model.

Questions:

  1. Is there a way to make one of the above approaches work? If yes, how? Where is my mistake?
  2. Is there another (simpler) option to pass the data as an array or struct to my model?

Database looks like this:

Identifier               Default         
latitude                    52.5
longitude                   13.4
electricity_consumption   4000.0
ventilation_stream          50.0
PV_peak                     30.0
PV_orientation               0.0
no_vessels                  28.0
heatpump_exists              1.0
hotwater_consumption      1000.0
.
.
.

答案1

得分: 0

SQLite: 这种方法对我来说不起作用,因为直接的SQL-Matlab接口不支持代码生成。实际上,可以编写一个C函数,从SQLite读取数据,并在Matlab函数块中通过coder.ceval调用该函数,在模拟期间读取信号。这对于代码生成(Simulink coder)也有效。然而,这不适用于初始化(请参阅问题)。

所以我的原始方法都没有奏效。

解决方法: 最终,我转向了基于Simulink RSIM目标的方法,它生成代码(也适用于Linux),并可以通过包含所有参数的.mat文件进行参数化。.mat文件可以进行修改以更新参数。这需要一些额外的代码来自动化这一步骤。此外,RSIM的模型配置有点复杂。

英文:

After having spent so much time on this issue, I would like to share my experience on this problem:

SQLite: This approach did not work out for me because the direct SQL-Matlab interfaces are not supported for code generation.
It is in fact possible to write a C-function, that reads from SQLite and call that function in a Matlab-function-block via coder.ceval wich allows to read in a signal during simulation. This works for code generation (Simulink coder) as well. However this will not work for initialization (see question).

So none of my original approaches ended up working.

Workaround: I ended up switching to an approach based on the Simulink RSIM-target wich generates code (also for Linux) and can be parametrized via a .mat file wich contains all the parameters. The .mat file can be modified to update parameters. This required some additional code wich automates this step. Also the model configuration for RSIM is a bit tricky.

huangapple
  • 本文由 发表于 2020年1月6日 22:11:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定