4

This is about a typical problem in larger embedded system projects: Data from a text-file/database must be hard coded to C-code. The data will change the control flow, table dimension etc. What is the solution of choice?

Background: In our (large) embedded software we have to connect hundreds of signals (outputs of finite state machines) with busses (e.g. a CAN-bus). We are using Simulink/Stateflow as model-based development tool (state machines) and auto-coding.

The connection will have to scale, do dataype conversions etc. Usually all the information for the conversion/connection is stored in a database or file (e.g. dbc-text-file).

Apparently the usual dynamic way: reading the database and dynamically connect/convert accordingly is not indicated if hard deterministic realtime capability must be ensured. This data has to be hard coded.

We have not found a realistic and more practical way other than to use the Simulink API: write external m-code which reads the data from file and automates "drawing a picture" of the entangeld connections into the Simulink model! This is finally auto-coded to C. Needless to say, that this scripted "painting-code" - while effective - is not very reusable, maintainable etc. I can't find an effective solution even taking : compiler/code generation, model driven architecture, Autosar, model transformations, into consideration. Constructing for each external data-document an own compiler, which transforms the data to C-code, seems to be unrealistic ...

Is there a practical alternative? Is this a fundamental weakness of Simulink "language" (i.e. it has no underlying high level language like other model driven embedded-tools like modelica) ?

goofy
  • 549
  • 2
  • 7
  • Can you clarify the question a bit? For example: where are you today (DB-text-file -> Simulink model -> C) and where would you like to be ideally (DB -> C,...?) – pmb Jan 13 '14 at 16:34
  • Why did you discard all of those things you took into consideration? For example: Simulink -> DB? I'm using this approach with the data definitions generated with C API (ASAP2 is another option used for AUTOSAR). – pmb Jan 13 '14 at 16:36
  • Exactly, we are here: DB-text-file -> Matlab-Script -> Simulink model -> C And ideally it could be something like: DB-text-file -> embedded Simulink m-function -> C. (This is another approach I discarded: the embedded Simulink m-function is a second class citizen because it can't determine what signal names/number of signals/dimension/data types are entering and where the outputs are connected to. So it don't know how to interface the connection/conversion) – goofy Jan 13 '14 at 18:21
  • I did not fully get your final proposal: Is your way using C API directly this: DB-text-file -> C (maybe via Simulink Coder). If your answer is yes, I've to see how hard it will be, because I have also to write data back to the auto-code with the API. Hoped to stay on a higher level language. But thank you for this good idea. – goofy Jan 13 '14 at 18:22
  • "embedded Simulink m-function is a 2nd class citizen ... (because it can't do some key anlayses)". You make my point (elaborated in my answer) about strong translator machinery requiring strong analyzers, and that an off-the-shelf bit of machinery doesn't want to do that for you (the point in the link I provide). So you need machinery where you can provide the extra smarts, to do a good job. – Ira Baxter Jan 13 '14 at 18:44
  • It probably isn't enough for you, but nevertheless C API is a feature that is worth checking out: they are C structs that are generated when you build the model, describing the parameters, signals, inports, etc. http://www.mathworks.com/help/rtw/ug/data-exchange.html#f75428 – pmb Jan 14 '14 at 07:16

2 Answers2

4

Your actual problem isn't really well described by your question.

The way I read it is that you have a complex set of data, that requires a complex code generation process. Whenever one needs a complex translation, you'll end up building complicated analysis and code generation machinery. That's generally not an easy task.

You've done it in two stages: 1) read the database and make Simulink, 2) let MATLAB compile the Simulink to source code. Your "database" content is in effect a specification (e.g., a DSL). You have a front end that reads the "spec" and interprets it into a Simulink model; you seem to be complaining that there is no underlying semantics for Simulink. Well... is there an underlying robust semantics for your specification DSL? That is probably part of your problem. Simulink itself has poorly defined semantics, too. The combination means that whatever ad hoc transformations you are doing from your DSL to Simulink, and from Simulink to C, is in effect ad hoc and likely difficult to maintain. We can argue about whether C has cleanly defined semantics; it sort of does but it isn't easy to see in the standards documents.

In any case, you are building a staged translator. Your first stage probably needs more structure and better analyses. Ideally you'd transform to an intermediate represention that was more formal; I always thought that Colored Petri nets were much better than Simulink, partly because they do have clear, formal semantics. (Modelica is pretty nice, too). Ideally you'd transform the intermediate stage into C using transformation rules you can control, rather that what Simulink happens to do.

This is easier if you have good foundation machinery on which to build translators. The best class of machinery for this purpose IMHO are program transformation systems. (I happen to build one of them [see bio], that knows C and Simulink already, and could be taught Modelica). You can read a bit more about what is needed in this SO answer on what it takes to build translators.

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • Thank you for precisily defining my problem. Unfortunately your answer is raising my fear: with my constraints I'll not make it - already stuck in your links. Can not even give your answer a point, because I have no stackoverflow reputation ... – goofy Jan 13 '14 at 19:44
  • "already stuck in your links"? The links provided are valid; you mean the information to which the links point are making you nervous? They should, in one sense; these are complicated engines. OTOH, you have a big critical problem that you want solved well. You've said that a half-baked scheme isn't satisfying... then you get to move to a fully-baked scheme :-} – Ira Baxter Jan 13 '14 at 20:30
  • @user3174640 You need at least 15 rep to upvote, but if you feel that your question has been answered, please mark the answer as "accepted" by clicking the hollow check mark to the right of the question. One you have enough rep you can also upvote the accepted answer, and any others you found helpful. – Adi Inbar Jan 13 '14 at 23:50
  • Understanding: Simulink, C-API are solving the problem not effectively. Even modelica has maybe only some needed language features but not all to solve it directly (something like static reflection is missing?) So thank you a lot for this exact analysis! I need one of your tools, you must definitively join the language semantics definition group of Simulink, Modelica and finally realtime embedded code becomes more dynamic (PRET machines?) ... – goofy Jan 14 '14 at 08:00
  • @user3174640: My personal belief is you don't solve language analysis problems from "inside" the language (e.g., reflection is never adequate, because it doesn't provide access to the full details of the language); you have to solve it from outside, which is the point of program transformation systems, which can see the full details of the language from outside the language, do arbitrary analyses (in support of) doing arbitrary transformations/code generation tasks. – Ira Baxter Jan 14 '14 at 08:09
  • @user3174640: where is this "language semantics definition group"? – Ira Baxter Jan 14 '14 at 08:10
  • @Ira Baxter: For Modelica you can join the open modelica association [here](https://www.modelica.org/association). Mathworks is extremly closed up (maybe you know [Pieter](http://en.wikipedia.org/wiki/Pieter_Mosterman)). – goofy Jan 15 '14 at 18:54
-1

It's difficult to make out what your exact question is, but here are two suggestions:

If you're looking for a code generator that can generate code from your state diagrams which you can understand try fsmpro.io

For making individual logic, you'll have to paste code rather than using any inbuilt utilities to design units.

William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33