Error in S-function MDL_Derivatives

3 visualizzazioni (ultimi 30 giorni)
Brian Tremaine
Brian Tremaine il 19 Nov 2020
Commentato: Brian Tremaine il 20 Nov 2020
I'm creating a model using example mex file csfunc.c as a starting example. When I compile & execute I get a fatal error in MDL_Derivatives at time= 0. For this code as a test I'm using the input U(0) in one of the derivatives. This throws an error. If I delete the reference to U(0) and just leave the additions of 1E-3 the model works.
At the beginning of the S-Functiion I do have the define: #define U(element) (*uPtrs[element])
What could cause this fatal fault?
I also have the input port delclared as a feedthrough, with
if (!ssSetNumInputPorts(S, 1)) return;
end
ssSetInputPortWidth(S, 0, 3);
ssSetInputPortDirectFeedThrough(S, 0, 1);
#define MDL_DERIVATIVES /* Change to #undef to remove function */
#if defined(MDL_DERIVATIVES)
/* Function: mdlDerivatives =================================================
* Abstract:
* In this function, you compute the S-function block's derivatives.
* The derivatives are placed in the derivative vector, ssGetdX(S).
*/
static void mdlDerivatives(SimStruct *S)
{
real_T *dx = ssGetdX(S);
real_T *x = ssGetContStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
/* xdot= f(x,u) */
dx[0]= 1E-3 + U(0);
dx[1]= 1E-3;
dx[2]= 1E-3;
dx[3]= 1E-3;
dx[4]= 1E-3;
}
#endif /* MDL_DERIVATIVES *
Any help on this is greatly appreciated!
  1 Commento
Brian Tremaine
Brian Tremaine il 20 Nov 2020
I seemed to have fixed it by adding the line ssSetInputPortRequiredContiguous(S, 0, 0) to mdlInitializeSizes. The line was added after the setting up the inputs,
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 5); /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
/* Return if number of expected != number of actual parameters */
return;
}
ssSetNumContStates(S, NUMSTATES); // ia,ib,ic,wrad,theta
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, 3);
ssSetInputPortDirectFeedThrough(S, 0, 1);
/*
* Set direct feedthrough flag (1=yes, 0=no).
* A port has direct feedthrough if the input is used in either
* the mdlOutputs or mdlGetTimeOfNextVarHit functions.
*/
ssSetInputPortRequiredContiguous(S, 0, 0);

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Event Functions in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by