Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Access derivatives from Embedded Coder

Asked by Etienne Coetzee on 10 Nov 2013

Hi Folks

I am trying to access the derivative values from code generated by embedded coder. I have a Fortran program that is linked to a c-function via the ISO_C_BINDING module (below). I am now trying to link this function to the generated code from embedded coder. When I define the derivative values in C in the below function, the program works correctly. I then generate code from a Simulink model that contains the same derivative definitions, but cannot figure out if I have implemented the code in the best way. I have included the code below. It seems to work, but I am having to initialise the model on each iteration, which does not seem correct. I cannot figure out why. I have initialised it before this function call as well, but this doe not work, hence I have to initialise on each iteration. The code also does not work when running with OpenMP, even though the original C-code does work with OpenMP. Any help or examples will be appreciated.

Cheers, Etienne

#include "fintrf.h"
#include "auto_f2c.h"
#include <stdio.h>                     /* This ert_main.c example uses printf/fflush */
#include "ab.h"                        /* Model's header file */
#include "rtwtypes.h"                  /* MathWorks types */
/*extern void ab_initialize(void);*/
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/*   ab :            The A --> B reaction */
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/* Subroutine */ int func(integer *ndim, doublereal *u, integer *icp, 
	doublereal *par, integer *ijac, doublereal *f, doublereal *dfdu, 
	doublereal *dfdp)
{
/*     ---------- ---- */
/* Evaluates the algebraic equations or ODE right hand side */
/* Input arguments : */
/*      NDIM   :   Dimension of the ODE system */
/*      U      :   State variables */
/*      ICP    :   Array indicating the free parameter(s) */
/*      PAR    :   Equation parameters */
/* Values to be returned : */
/*      F      :   ODE right hand side values */
/* Normally unused Jacobian arguments : IJAC, DFDU, DFDP (see manual)*/ 
	real_T *f0 = 0;
      ab_initialize();
	rtsiSetdX(&ab_M->solverInfo,f0);
      /* Function Body */
  	ab_U.P1=par[0];
  	ab_U.P2=par[1];
  	ab_U.P3=par[2];
    /* InitializeConditions for Integrator: '<Root>/U2' */
      ab_X.U1_CSTATE=u[0];
      ab_X.U2_CSTATE=u[1];
      ab_step();
      ab_derivatives();
	f[0] = ab_dX.U1_CSTATE;
    f[1] = ab_dX.U2_CSTATE;
      return 0;
  } /* func_ */

0 Comments

Etienne  Coetzee

0 Answers

Contact us