Using Simulink Coder - atomic change of multidimensional parameters (matrix, vector)

446 views Asked by At

I am using Simulink and Simulink Coder to generate a dll of arbitrary Models. My C Application uses the mathworks CAPI. It runs arbitrary models (hard real time below 1 ms) and is able to modify any parameters of the model (by using the tunable parameters).

For simlpe scalar values I am obtaining the adress of the value.

Pseudocode:

void* simplegain = rtwCAPI_GetSignalAddrIdx()
*simplegain=42;

Everything runs fine. However, this approach can not be applied if I want an atomic change of complete vector and matrix.

For multidimensional Data I used memcopy to write all values from a destination to the result of GetSignalAddIdx(). Measurements have shown that using memcopy is to slow. Analysing the generated Code show various calls of rt_Lookup

real_T rt_Lookup(const real_T *x, int_T xlen, real_T u, const real_T *y)
// x is the pointer the matrix The Adress of the matrix is declared in a global structure  `rtDataAddrMap` statically. I can read it out, but do not know how to change.

What I like to achieve is:

  1. Define a second map in my application (same size).
  2. Write all new value the this second map.
  3. Change just the pointer in rtDataAddrMap to activate the second map.

The general question: How can I achieve to change multidimensional parameters atomically? What is the regular way to do this? (Code Generation Options etc..)

The specific question: (if my approach was right) What are reasonable solutions to change the data pointer of a matrix?

1

There are 1 answers

7
Henrik Carlqvist On

Atomic in the sense of calling an instruction which does its work in a single clock cycle (and thus not possible to interrupt) is not possible to achieve when it comes to this kind of multidimensional arrays. Instead you will need some kind of real time mechanism like a mutex or semaphore to protect your data. Mutexes and semaphores are built upon atomic operations which guarantees that two processes will not be able to consume the same resource at once.

Your approach with ping pong buffering of your data area will probably improve performance. Unfortunately I do not have enough experience from Mathworks generated code to tell how to implement that.