0

I have an input port to my CMEX S-Function that I set to NOT have Direct Feedthrough. However, when I try to use the signal from the input port in the mdlUpdate function call, it crashes with Segmentation Violation and informs me that the most likely cause is an incorrectly set direct feedthrough status of an input port.

Does using the input signal in mdlUpdate ALSO count as Direct Feedthrough?

  • No it doesn't. mdlUpdate is used to update discrete states, which in general may be a function of the inputs, and has nothing to do with whether the output is a function of the inputs. – Phil Goddard Aug 21 '14 at 20:45
  • Thanks @PhilGoddard. Does that mean that I _should_ be able to use the mdlUpdate to update the PWork vector values as well? That is what I am trying to do, but getting the error. I could post both the code and the error if that would help. –  Aug 21 '14 at 20:50
  • Yes, PWork vectors can be updated in mdlUpdate. – Phil Goddard Aug 21 '14 at 23:27
  • Phil, perhaps I should have been clearer. I use an input signal, that I have set to NOT have Direct Feedthrough, and assign its value to the PWork vector in mdlUpdate. This causes the Segmentation Violation. But, from what you say, I should be able to do this? To clarify, I have neither continuous, nor discrete states in my model; just inputs and outputs. –  Aug 22 '14 at 13:37
  • Reading between the lines, I suspect that you are then getting the value of the PWork vector in mdlOutputs and trying to use it as a way to circumvent defining your block as having direct feedthrough. That won't work. Your block either has direct feedthrough or it doesn't. mdlOutput is called before you store anything in the work vector. See the [documented flow chart](http://www.mathworks.com/help/simulink/sfg/how-the-simulink-engine-interacts-with-c-s-functions.html) of how an S-function interacts with the Simulink Engine. – Phil Goddard Aug 22 '14 at 13:51
  • Hi Phil. Your comment is true, but in a circuitous manner. I DO call the PWork vector in mdlOutput. However, as I mentioned in my other question that you also commented on (thank you!), I don't have direct feedthrough because I save the current time step value of the input port in the PWork vector, and only use it in the _next_ time step. However, in your opinion, if the value is used in the next _minor_ time step, that still counts as direct feedthrough right? –  Aug 22 '14 at 13:56

1 Answers1

0

For those who may look into this question in the future, I found the answer. mdlUpdate doesn't count as Direct Feedthrough. However, if you try to access the input port signal at the first time step, that will lead to a Segmentation Violation (the Mathworks documentation suggests that the error might be different for different computers).

The trick is to use ssIsFirstInitCond(S) to acquire whether the compiler is at the first time step or not, and avoid doing any assignment in the first step.

My code looks like this:

#define MDL_UPDATE
#if defined(MDL_UPDATE)
static void mdlUpdate(SimStruct *S)
{UNUSED_ARG(tid);
  if(!ssIsFirstInitCond(S))
  {
    real_T *u1 = (real_T *) ssGetInputPortSignal(S)[0];
    double *P1 = (double *) ssGetPWork(S)[0];
    // assign values here
  }
}
#endif

Also, note that mdlUpdate is called only in the major time steps

  • Why are you using a PWork vector? What you describe is the standard use for either a discrete state or a DWork vector. – Phil Goddard Aug 22 '14 at 19:25
  • Hey Phil. No good reason. A lack of understanding of the intricacies of Simulink. I will look into having states in my sfunction –  Aug 22 '14 at 23:55