Index: mttroot/mtt/lib/rep/sfun_rep/Makefile ================================================================== --- mttroot/mtt/lib/rep/sfun_rep/Makefile +++ mttroot/mtt/lib/rep/sfun_rep/Makefile @@ -1,10 +1,10 @@ #! /usr/bin/make -f all: $(SYS)_sfun.mexglx -$(SYS)_sfun.mexglx: $(SYS)_sfun.c $(SYS)_def.h $(SYS)_sympar.h $(SYS)_numpar.c $(SYS)_ode.c $(SYS)_odeo.c $(SYS)_state.c $(SYS)_sfun_ae.mexglx +$(SYS)_sfun.mexglx: $(SYS)_sfun.c sfun_debug.h $(SYS)_def.h $(SYS)_sympar.h $(SYS)_numpar.c $(SYS)_ode.c $(SYS)_odeo.c $(SYS)_state.c $(SYS)_sfun_ae.mexglx $(SYS)_sfun_input.mexglx echo Creating $@ mex $(SYS)_sfun.c cp *_sfun*mexglx .. $(SYS)_sfun.c:: ${MTT_REP}/sfun_rep/sfun.c.tmpl @@ -13,26 +13,42 @@ $(SYS)_sfun_ae.mexglx: $(SYS)_sfun_ae.c $(SYS)_def.h $(SYS)_ae.c echo Creating $@ mex $(SYS)_sfun_ae.c -$(SYS)_sfun_ae.c:: ${MTT_REP}/sfun_rep/ae.c.tmpl +$(SYS)_sfun_ae.c:: ${MTT_REP}/sfun_rep/mex_ae.c.tmpl + echo Creating $@ + cat $^ | sed 's//$(SYS)/g' > $@ + +$(SYS)_sfun_input.mexglx: $(SYS)_sfun_input.c sfun_debug.h $(SYS)_def.h $(SYS)_sympar.h $(SYS)_input.c $(SYS)_numpar.c + echo Creating $@ + mex $(SYS)_sfun_input.c + +$(SYS)_sfun_input.c:: ${MTT_REP}/sfun_rep/sfun_input.c.tmpl echo Creating $@ cat $^ | sed 's//$(SYS)/g' > $@ -$(SYS)_sfun.zip: $(SYS)_sfun.c $(SYS)_sfun_ae.c $(SYS)_def.h $(SYS)_sympar.h $(SYS)_ae.c $(SYS)_numpar.c $(SYS)_ode.c $(SYS)_odeo.c $(SYS)_state.c README +$(SYS)_sfun.zip: $(SYS)_sfun.c sfun_debug.h $(SYS)_sfun_ae.c $(SYS)_sfun_input.c $(SYS)_def.h $(SYS)_sympar.h $(SYS)_ae.c $(SYS)_input.c $(SYS)_numpar.c $(SYS)_ode.c $(SYS)_odeo.c $(SYS)_state.c $(SYS).mdl README echo Creating $@ zip $@ $^ +$(SYS).mdl: ${MTT_REP}/sfun_rep/mdl.tmpl + echo Creating $@ + cat $^ | sed 's//$(SYS)/g' > $@ + README:: ${MTT_REP}/sfun_rep/README.tmpl echo Creating $@ cat $^ | sed 's//$(SYS)/g' > $@ $(SYS)_ae.c: $(SYS)_ae.m echo Creating $@ ${MTT_CC}/mtt_m2cc.sh $(SYS) ae c cat +$(SYS)_input.c: $(SYS)_input.m + echo Creating $@ + ${MTT_CC}/mtt_m2cc.sh $(SYS) input c cat + $(SYS)_numpar.c: $(SYS)_numpar.m echo Creating $@ ${MTT_CC}/mtt_m2cc.sh $(SYS) numpar c cat $(SYS)_ode.c: $(SYS)_ode.m @@ -44,8 +60,12 @@ ${MTT_CC}/mtt_m2cc.sh $(SYS) odeo c cat $(SYS)_state.c: $(SYS)_state.m echo Creating $@ ${MTT_CC}/mtt_m2cc.sh $(SYS) state c cat + +sfun_debug.h:: ${MTT_REP}/sfun_rep/sfun_debug.h + echo Copying $@ + cp $^ $@ %:: mtt -q $(OPTS) `echo $* | sed 's/\(.*\)_\(.*\)\.\(.*\)/\1 \2 \3/'` Index: mttroot/mtt/lib/rep/sfun_rep/README.tmpl ================================================================== --- mttroot/mtt/lib/rep/sfun_rep/README.tmpl +++ mttroot/mtt/lib/rep/sfun_rep/README.tmpl @@ -1,8 +1,12 @@ To build a Simulink funtion of the model without using MTT: mex _sfun.c + +The input block can be created with: + +mex _sfun_input.c If numerical solution of algebraic equations is also required: mex _sfun_ae.c DELETED mttroot/mtt/lib/rep/sfun_rep/ae.c.tmpl Index: mttroot/mtt/lib/rep/sfun_rep/ae.c.tmpl ================================================================== --- mttroot/mtt/lib/rep/sfun_rep/ae.c.tmpl +++ /dev/null @@ -1,106 +0,0 @@ -/* -*-c-*- Put emacs into c-mode */ - -#include -#include - -#include -#include "_def.h" - -/* utility procedures */ - -double * -array_of_double (size_t n) -{ - void *p = calloc (n, sizeof (double)); - if (! p) { - fprintf (stderr, "*** Error: failed to allocate memory\n"); - } - return (double *) p; -} - -/* system equations */ - -static double * -_ae (double *mttyz, - const double *mttx, - const double *mttu, - const double mttt, - const double *mttpar) -{ -#include "_ae.c" -} - -/* generic mex function */ - -#ifdef __cplusplus -extern "C" { -#endif - -void -mexFunction (int nlhs, mxArray *plhs[], - int nrhs, const mxArray *prhs[]) -{ - double *mttyz; /* residuals */ - - double *mttx; /* states */ - double *mttu; /* known + unknown inputs */ - double mttt; /* time */ - double *mttpar; /* parameters */ - - unsigned int i; - double *p; - - mttyz = array_of_double (MTTNYZ); - - mttx = array_of_double (MTTNX); - mttu = array_of_double (MTTNU + MTTNYZ); - mttpar = array_of_double (MTTNPAR); - - /* get trial values */ - p = mxGetPr (prhs[0]); - for (i = 0; i < MTTNYZ; i++) { - mttu[MTTNU + i] = p[i]; - } - - /* get states */ - p = mxGetPr (prhs[1]); - for (i = 0; i < MTTNX; i++) { - mttx[i] = p[i]; - } - - /* get known inputs */ - p = mxGetPr (prhs[2]); - for (i = 0; i < MTTNU; i++) { - mttu[i] = p[i]; - } - - /* get time */ - p = mxGetPr (prhs[3]); - mttt = *p; - - /* get parameters */ - p = mxGetPr (prhs[4]); - for (i = 0; i < MTTNPAR; i++) { - mttpar[i] = p[i]; - } - - /* evaluate residuals */ - _ae (mttyz, mttx, mttu, mttt, mttpar); - - /* return residuals */ - plhs[0] = mxCreateDoubleMatrix (MTTNYZ, 1, mxREAL); - p = mxGetPr (plhs[0]); - for (i = 0; i < MTTNYZ; i++) { - p[i] = mttyz[i]; - } - - /* release memory */ - free (mttx); - free (mttu); - free (mttpar); - free (mttyz); -} - -#ifdef __cplusplus -} -#endif ADDED mttroot/mtt/lib/rep/sfun_rep/mdl.tmpl Index: mttroot/mtt/lib/rep/sfun_rep/mdl.tmpl ================================================================== --- /dev/null +++ mttroot/mtt/lib/rep/sfun_rep/mdl.tmpl @@ -0,0 +1,275 @@ +Model { + Name "generic" + Version 4.00 + SampleTimeColors off + LibraryLinkDisplay "none" + WideLines off + ShowLineDimensions off + ShowPortDataTypes off + ShowStorageClass off + ExecutionOrder off + RecordCoverage off + CovPath "/" + CovSaveName "covdata" + CovMetricSettings "dw" + CovNameIncrementing off + CovHtmlReporting on + BlockNameDataTip off + BlockParametersDataTip off + BlockDescriptionStringDataTip off + ToolBar on + StatusBar on + BrowserShowLibraryLinks off + BrowserLookUnderMasks off + Created "Mon May 20 13:21:21 2002" + Creator "geraint" + UpdateHistory "UpdateHistoryNever" + ModifiedByFormat "%" + LastModifiedBy "geraint" + ModifiedDateFormat "%" + LastModifiedDate "Mon May 20 15:53:00 2002" + ModelVersionFormat "1.%" + ConfigurationManager "None" + SimParamPage "Solver" + StartTime "0.0" + StopTime "10.0" + SolverMode "Auto" + Solver "ode45" + RelTol "1e-3" + AbsTol "auto" + Refine "1" + MaxStep "auto" + MinStep "auto" + MaxNumMinSteps "-1" + InitialStep "auto" + FixedStep "auto" + MaxOrder 5 + OutputOption "RefineOutputTimes" + OutputTimes "[]" + LoadExternalInput off + ExternalInput "[t, u]" + SaveTime on + TimeSaveName "tout" + SaveState off + StateSaveName "xout" + SaveOutput on + OutputSaveName "yout" + LoadInitialState off + InitialState "xInitial" + SaveFinalState off + FinalStateName "xFinal" + SaveFormat "Array" + LimitDataPoints on + MaxDataPoints "1000" + Decimation "1" + AlgebraicLoopMsg "warning" + MinStepSizeMsg "warning" + UnconnectedInputMsg "warning" + UnconnectedOutputMsg "warning" + UnconnectedLineMsg "warning" + InheritedTsInSrcMsg "warning" + SingleTaskRateTransMsg "none" + MultiTaskRateTransMsg "error" + IntegerOverflowMsg "warning" + CheckForMatrixSingularity "none" + UnnecessaryDatatypeConvMsg "none" + Int32ToFloatConvMsg "warning" + InvalidFcnCallConnMsg "error" + SignalLabelMismatchMsg "none" + LinearizationMsg "none" + VectorMatrixConversionMsg "none" + SfunCompatibilityCheckMsg "none" + BlockPriorityViolationMsg "warning" + ArrayBoundsChecking "none" + ConsistencyChecking "none" + ZeroCross on + Profile off + SimulationMode "normal" + RTWSystemTargetFile "grt.tlc" + RTWInlineParameters off + RTWRetainRTWFile off + RTWTemplateMakefile "grt_default_tmf" + RTWMakeCommand "make_rtw" + RTWGenerateCodeOnly off + TLCProfiler off + TLCDebug off + TLCCoverage off + AccelSystemTargetFile "accel.tlc" + AccelTemplateMakefile "accel_default_tmf" + AccelMakeCommand "make_rtw" + TryForcingSFcnDF off + ExtModeMexFile "ext_comm" + ExtModeBatchMode off + ExtModeTrigType "manual" + ExtModeTrigMode "normal" + ExtModeTrigPort "1" + ExtModeTrigElement "any" + ExtModeTrigDuration 1000 + ExtModeTrigHoldOff 0 + ExtModeTrigDelay 0 + ExtModeTrigDirection "rising" + ExtModeTrigLevel 0 + ExtModeArchiveMode "off" + ExtModeAutoIncOneShot off + ExtModeIncDirWhenArm off + ExtModeAddSuffixToVar off + ExtModeWriteAllDataToWs off + ExtModeArmWhenConnect on + ExtModeSkipDownloadWhenConnect off + ExtModeLogAll on + ExtModeAutoUpdateStatusClock off + OptimizeBlockIOStorage on + BufferReuse on + ParameterPooling on + BlockReductionOpt on + RTWExpressionDepthLimit 5 + BooleanDataType off + BlockDefaults { + Orientation "right" + ForegroundColor "black" + BackgroundColor "white" + DropShadow off + NamePlacement "normal" + FontName "Helvetica" + FontSize 10 + FontWeight "normal" + FontAngle "normal" + ShowName on + } + AnnotationDefaults { + HorizontalAlignment "center" + VerticalAlignment "middle" + ForegroundColor "black" + BackgroundColor "white" + DropShadow off + FontName "Helvetica" + FontSize 10 + FontWeight "normal" + FontAngle "normal" + } + LineDefaults { + FontName "Helvetica" + FontSize 9 + FontWeight "normal" + FontAngle "normal" + } + System { + Name "generic" + Location [40, 379, 681, 493] + Open on + ModelBrowserVisibility off + ModelBrowserWidth 200 + ScreenColor "white" + PaperOrientation "landscape" + PaperPositionMode "auto" + PaperType "usletter" + PaperUnits "inches" + ZoomFactor "100" + ReportName "simulink-default.rpt" + Block { + BlockType "S-Function" + Name "MTT Model Inputs" + Ports [1, 1] + Position [30, 39, 205, 71] + BackgroundColor "lightBlue" + DropShadow on + FunctionName "_sfun_input" + PortCounts "[]" + SFunctionModules "''" + Port { + PortNumber 1 + Name "MTT Model Inputs: MTTU" + TestPoint off + LinearAnalysisOutput off + LinearAnalysisInput off + RTWStorageClass "Auto" + } + } + Block { + BlockType "S-Function" + Name "MTT Plant Model" + Ports [1, 2] + Position [320, 36, 465, 74] + BackgroundColor "lightBlue" + DropShadow on + FunctionName "_sfun" + PortCounts "[]" + SFunctionModules "''" + Port { + PortNumber 1 + Name "MTT Model States: MTTX" + TestPoint off + LinearAnalysisOutput off + LinearAnalysisInput off + RTWStorageClass "Auto" + } + Port { + PortNumber 2 + Name "MTT Model Outputs: MTTY" + TestPoint off + LinearAnalysisOutput off + LinearAnalysisInput off + RTWStorageClass "Auto" + } + } + Block { + BlockType Scope + Name "Scope" + Ports [1] + Position [595, 49, 625, 81] + BackgroundColor "lightBlue" + DropShadow on + Floating off + Location [604, 136, 1156, 874] + Open on + NumInputPorts "1" + TickLabels "OneTimeTick" + ZoomMode "on" + List { + ListType AxesTitles + axes1 "%" + } + List { + ListType SelectedSignals + axes1 "" + } + Grid "on" + TimeRange "auto" + YMin "0" + YMax "1" + SaveToWorkspace off + SaveName "ScopeData" + DataFormat "StructureWithTime" + LimitDataPoints on + MaxDataPoints "5000" + Decimation "1" + SampleInput off + SampleTime "0" + } + Line { + Name "MTT Model Inputs: MTTU" + Labels [0, 0] + SrcBlock "MTT Model Inputs" + SrcPort 1 + DstBlock "MTT Plant Model" + DstPort 1 + } + Line { + Name "MTT Model States: MTTX" + Labels [2, 0] + SrcBlock "MTT Plant Model" + SrcPort 1 + Points [0, -30; -455, 0] + DstBlock "MTT Model Inputs" + DstPort 1 + } + Line { + Name "MTT Model Outputs: MTTY" + Labels [-1, 0] + SrcBlock "MTT Plant Model" + SrcPort 2 + DstBlock "Scope" + DstPort 1 + } + } +} ADDED mttroot/mtt/lib/rep/sfun_rep/mex_ae.c.tmpl Index: mttroot/mtt/lib/rep/sfun_rep/mex_ae.c.tmpl ================================================================== --- /dev/null +++ mttroot/mtt/lib/rep/sfun_rep/mex_ae.c.tmpl @@ -0,0 +1,114 @@ +/* -*-c-*- Put emacs into c-mode + * _sfun_ae.c: + * Matlab mex algebraic equations for + */ + +#include +#include +#include +#include "sfun_debug.h" +#include "_def.h" + +/* utility procedures */ + +double * +array_of_double (size_t n) +{ + void *p = calloc (n, sizeof (double)); + if (! p) { + fprintf (stderr, "*** Error: failed to allocate memory\n"); + } + return (double *) p; +} + +/* system equations */ + +static double * +_ae (double *mttyz, + const double *mttx, + const double *mttu, + const double mttt, + const double *mttpar) +{ +#include "_ae.c" + PRINT_LEAVE; +} + +/* generic mex function */ + +#ifdef __cplusplus +extern "C" { +#endif + +void +mexFunction (int nlhs, mxArray *plhs[], + int nrhs, const mxArray *prhs[]) +{ + double *mttyz; /* residuals */ + + double *mttx; /* states */ + double *mttu; /* known + unknown inputs */ + double mttt; /* time */ + double *mttpar; /* parameters */ + + unsigned int i; + double *p; + + PRINT_ENTER; + + mttyz = array_of_double (MTTNYZ); + + mttx = array_of_double (MTTNX); + mttu = array_of_double (MTTNU + MTTNYZ); + mttpar = array_of_double (MTTNPAR); + + /* get trial values */ + p = mxGetPr (prhs[0]); + for (i = 0; i < MTTNYZ; i++) { + mttu[MTTNU + i] = p[i]; + } + + /* get states */ + p = mxGetPr (prhs[1]); + for (i = 0; i < MTTNX; i++) { + mttx[i] = p[i]; + } + + /* get known inputs */ + p = mxGetPr (prhs[2]); + for (i = 0; i < MTTNU; i++) { + mttu[i] = p[i]; + } + + /* get time */ + p = mxGetPr (prhs[3]); + mttt = *p; + + /* get parameters */ + p = mxGetPr (prhs[4]); + for (i = 0; i < MTTNPAR; i++) { + mttpar[i] = p[i]; + } + + /* evaluate residuals */ + _ae (mttyz, mttx, mttu, mttt, mttpar); + + /* return residuals */ + plhs[0] = mxCreateDoubleMatrix (MTTNYZ, 1, mxREAL); + p = mxGetPr (plhs[0]); + for (i = 0; i < MTTNYZ; i++) { + p[i] = mttyz[i]; + } + + /* release memory */ + free (mttx); + free (mttu); + free (mttpar); + free (mttyz); + + PRINT_LEAVE; +} + +#ifdef __cplusplus +} +#endif Index: mttroot/mtt/lib/rep/sfun_rep/sfun.c.tmpl ================================================================== --- mttroot/mtt/lib/rep/sfun_rep/sfun.c.tmpl +++ mttroot/mtt/lib/rep/sfun_rep/sfun.c.tmpl @@ -1,25 +1,17 @@ -/* -*-c-*- +/* -*-c-*- Put emacs into c-mode * _sfun.c: * Matlab S-function simulation of */ #define S_FUNCTION_NAME _sfun #define S_FUNCTION_LEVEL 2 -#if (0) /* DEBUG? */ -#define PRINT_ENTER(f) fprintf (stderr, "Entered %s\n", f) -#define PRINT_LEAVE(f) fprintf (stderr, "Leaving %s\n", f) -#else -#define PRINT_ENTER(f) -#define PRINT_LEAVE(f) -#endif /* DEBUG? */ - #include #include - #include "simstruc.h" +#include "sfun_debug.h" #include "_def.h" static double *mttdx; /* pointer to rates */ static double *mttu; /* pointer to inputs */ static double *mttpar; /* pointer to parameters */ @@ -34,35 +26,40 @@ static void _ae (void) { #include "_ae.c" + PRINT_LEAVE; } static void _numpar (void) { #include "_sympar.h" #include "_numpar.c" + PRINT_LEAVE; } static void _ode (void) { #include "_ode.c" + PRINT_LEAVE; } static void _odeo (void) { #include "_odeo.c" + PRINT_LEAVE; } static void _state (void) { #include "_state.c" + PRINT_LEAVE; } /* utility procedures */ static double * @@ -76,40 +73,40 @@ } static void initialise_arrays (void) { - PRINT_ENTER("initialise_arrays"); + PRINT_ENTER; mttdx = array_of_double (MTTNX); mttpar = array_of_double (MTTNPAR); mttu = array_of_double (MTTNU + MTTNYZ); mttx = array_of_double (MTTNX); mtty = array_of_double (MTTNY); mttyz = array_of_double (MTTNYZ); - PRINT_LEAVE("initialise_arrays"); + PRINT_LEAVE; } static void update_states_from_simulink (SimStruct *S) { - PRINT_ENTER("update_states_from_simulink"); + PRINT_ENTER; for (i = 0; i < MTTNX; i++) { mttx[i] = ssGetContStates (S)[i]; } - PRINT_LEAVE("update_states_from_simulink"); + PRINT_LEAVE; } static void update_inputs_from_simulink (SimStruct *S) { - PRINT_ENTER("update_inputs_from_simulink"); + PRINT_ENTER; for (i = 0; i < MTTNU; i++) { - mttu[i] = *ssGetInputPortRealSignalPtrs (S, i)[0]; + mttu[i] = *ssGetInputPortRealSignalPtrs (S, 0)[i]; } - PRINT_LEAVE("update_inputs_from_simulink"); + PRINT_LEAVE; } static void update_inputs_from_solver (void) { @@ -119,11 +116,11 @@ mxArray *MTT_MATLAB_Ui; mxArray *MTT_MATLAB_X; double *p; - PRINT_ENTER ("update_inputs_from_solver"); + PRINT_ENTER; /* starting value for solver - start with zero */ MTT_MATLAB_Ui = mxCreateDoubleMatrix (MTTNYZ, 1, mxREAL); mxSetName (MTT_MATLAB_Ui, "MTT_Ui"); p = mxGetPr (MTT_MATLAB_Ui); @@ -180,85 +177,86 @@ mxDestroyArray (MTT_MATLAB_T); mxDestroyArray (MTT_MATLAB_U); mxDestroyArray (MTT_MATLAB_Ui); mxDestroyArray (MTT_MATLAB_X); - PRINT_LEAVE ("update_inputs_from_solver"); + PRINT_LEAVE; } static void update_simtime_from_simulink (SimStruct *S) { - PRINT_ENTER("update_simtime_from_simulink"); + PRINT_ENTER; mttt = ssGetT (S); - PRINT_LEAVE("update_simtime_from_simulink"); + PRINT_LEAVE; } /* S-function methods */ static void mdlInitializeSizes(SimStruct *S) { - PRINT_ENTER("mdlInitializeSizes"); + PRINT_ENTER; + ssSetNumSFcnParams(S, 0); if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) { - PRINT_LEAVE("mdlInitializeSizes"); + PRINT_LEAVE; return; } ssSetNumContStates(S, MTTNX); ssSetNumDiscStates(S, 0); - if (!ssSetNumInputPorts(S, MTTNU)) return; - for (i = 0; i < MTTNU; i++) { - ssSetInputPortWidth(S, i, 1); - ssSetInputPortDirectFeedThrough(S, i, 1); - } - - if (!ssSetNumOutputPorts(S, MTTNY)) return; - for (i = 0; i < MTTNY; i++) { - ssSetOutputPortWidth(S, i, 1); - } - - ssSetNumSampleTimes(S, 1); - ssSetNumRWork(S, 0); - ssSetNumIWork(S, 0); - ssSetNumPWork(S, 0); - ssSetNumModes(S, 0); - ssSetNumNonsampledZCs(S, 0); - - ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE); - - initialise_arrays (); - PRINT_LEAVE("mdlInitializeSizes"); + if (!ssSetNumInputPorts(S, 1)) return; + ssSetInputPortWidth(S, 0, MTTNU); + ssSetInputPortDirectFeedThrough(S, 0, 1); + + if (!ssSetNumOutputPorts(S, 2)) return; + ssSetOutputPortWidth(S, 0, MTTNX); + ssSetOutputPortWidth(S, 1, MTTNY); + + ssSetNumSampleTimes(S, 1); + ssSetNumRWork(S, 0); + ssSetNumIWork(S, 0); + ssSetNumPWork(S, 0); + ssSetNumModes(S, 0); + ssSetNumNonsampledZCs(S, 0); + + ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE); + + initialise_arrays (); + + PRINT_LEAVE; } static void mdlInitializeSampleTimes(SimStruct *S) { - PRINT_ENTER("mdlInitializeSampleTimes"); + PRINT_ENTER; + ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME); ssSetOffsetTime(S, 0, 0.0); - PRINT_LEAVE("mdlInitializeSampleTimes"); + + PRINT_LEAVE; } #define MDL_INITIALIZE_CONDITIONS static void mdlInitializeConditions(SimStruct *S) { - PRINT_ENTER("mdlInitializeConditions"); + PRINT_ENTER; _numpar (); _state (); for (i = 0; i < MTTNX; i++) { ssGetContStates (S)[i] = mttx[i]; } - PRINT_LEAVE("leaving mdlInitializeConditions"); + PRINT_LEAVE; } static void mdlOutputs(SimStruct *S, int_T tid) { - PRINT_ENTER("entered mdlOutputs"); + PRINT_ENTER; update_states_from_simulink (S); update_inputs_from_simulink (S); if (MTTNYZ > 0) { update_inputs_from_solver (); @@ -266,22 +264,26 @@ update_simtime_from_simulink (S); UNUSED_ARG(tid); /* not used in single tasking mode */ _odeo (); + + for (i = 0; i < MTTNX; i++) { + ssGetOutputPortRealSignal (S, 0)[i] = mttx[i]; + } for (i = 0; i < MTTNY; i++) { - ssGetOutputPortRealSignal (S,i)[0] = mtty[i]; + ssGetOutputPortRealSignal (S, 1)[i] = mtty[i]; } - PRINT_LEAVE("leaving mdlOutputs"); + PRINT_LEAVE; } #define MDL_DERIVATIVES static void mdlDerivatives(SimStruct *S) { - PRINT_ENTER("entered mdlDerivatives\n"); + PRINT_ENTER; update_states_from_simulink (S); update_inputs_from_simulink (S); if (MTTNYZ > 0) { update_inputs_from_solver (); @@ -292,28 +294,28 @@ for (i = 0; i < MTTNX; i++) { ssGetdX (S)[i] = mttdx[i]; } - PRINT_LEAVE("leaving mdlDerivatives"); + PRINT_LEAVE; } static void mdlTerminate(SimStruct *S) { - PRINT_ENTER("entered mdlTerminate"); + PRINT_ENTER; UNUSED_ARG(S); free (mttdx); free (mttpar); free (mttu); free (mttx); free (mtty); free (mttyz); - PRINT_LEAVE("leaving mdlTerminate"); + PRINT_LEAVE; } #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */ #include "simulink.c" /* MEX-file interface mechanism */ #else #include "cg_sfun.h" /* Code generation registration function */ #endif ADDED mttroot/mtt/lib/rep/sfun_rep/sfun_debug.h Index: mttroot/mtt/lib/rep/sfun_rep/sfun_debug.h ================================================================== --- /dev/null +++ mttroot/mtt/lib/rep/sfun_rep/sfun_debug.h @@ -0,0 +1,30 @@ + +#ifndef HAVE_SFUN_DEBUG_H +#define HAVE_SFUN_DEBUG_H + + +#if defined DEBUG && defined __GNUC__ + +#include +#define PRINT_ENTER fprintf (stderr, "debug> Entered '%s'\n", __FUNCTION__); +#define PRINT_LEAVE fprintf (stderr, "debug> Leaving '%s'\n", __FUNCTION__); + +#elif defined DEBUG && ! defined __GNUC__ + +#include +#define PRINT_ENTER fprintf (stderr, "debug> Entered a function\n"); +#define PRINT_LEAVE fprintf (stderr, "debug> Leaving a function\n"); + +#elif ! defined DEBUG + +#define PRINT_ENTER +#define PRINT_LEAVE + +#else + +#error "Momentary lapse of logic : unreachable statement reached" + +#endif + + +#endif /* HAVE_SFUN_DEBUG_H */ ADDED mttroot/mtt/lib/rep/sfun_rep/sfun_input.c.tmpl Index: mttroot/mtt/lib/rep/sfun_rep/sfun_input.c.tmpl ================================================================== --- /dev/null +++ mttroot/mtt/lib/rep/sfun_rep/sfun_input.c.tmpl @@ -0,0 +1,182 @@ +/* -*-c-*- Put emacs into c-mode + * _sfun_input.c: + * Matlab S-function inputs for + */ + +#define S_FUNCTION_NAME _sfun_input +#define S_FUNCTION_LEVEL 2 + +#include +#include +#include "simstruc.h" +#include "sfun_debug.h" +#include "_def.h" + +static double *mttu; /* pointer to inputs */ +static double *mttpar; /* pointer to parameters */ +static double *mttx; /* pointer to states */ +static double *mtty; /* pointer to outputs */ +static double mttt; /* time */ + +static unsigned int i; /* loop counter */ + +/* system equations */ + +static void +_input (void) +{ +#include "_input.c" + PRINT_LEAVE; +} + +static void +_numpar (void) +{ +#include "_sympar.h" +#include "_numpar.c" + PRINT_LEAVE; +} + +/* utility procedures */ + +static double * +array_of_double (size_t n) +{ + void *p = calloc (n, sizeof (double)); + if (! p) { + fprintf (stderr, "*** Error: failed to allocate memory\n"); + } + return (double *) p; +} + +static void +initialise_arrays (void) +{ + PRINT_ENTER; + + mttpar = array_of_double (MTTNPAR); + mttu = array_of_double (MTTNU + MTTNYZ); + mttx = array_of_double (MTTNX); + mtty = array_of_double (MTTNY); + + PRINT_LEAVE; +} + +static void +update_inputs_from_simulink (SimStruct *S) +{ + PRINT_ENTER; + for (i = 0; i < MTTNX; i++) { + mttx[i] = *ssGetInputPortRealSignalPtrs (S, 0)[i]; + } + PRINT_LEAVE; +} + +static void +update_simtime_from_simulink (SimStruct *S) +{ + PRINT_ENTER; + mttt = ssGetT (S); + PRINT_LEAVE; +} + +/* S-function methods */ + +static void mdlInitializeSizes(SimStruct *S) +{ + PRINT_ENTER; + + ssSetNumSFcnParams(S, 0); + if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) { + PRINT_LEAVE; + return; + } + + ssSetNumContStates(S, 0); + ssSetNumDiscStates(S, 0); + + if (!ssSetNumInputPorts(S, 1)) return; + ssSetInputPortWidth(S, 0, MTTNX); + ssSetInputPortDirectFeedThrough(S, 0, 1); + + if (!ssSetNumOutputPorts(S, 1)) return; + ssSetOutputPortWidth(S, 0, MTTNU); + + ssSetNumSampleTimes(S, 1); + ssSetNumRWork(S, 0); + ssSetNumIWork(S, 0); + ssSetNumPWork(S, 0); + ssSetNumModes(S, 0); + ssSetNumNonsampledZCs(S, 0); + + ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE); + + initialise_arrays (); + + PRINT_LEAVE; +} + +static void mdlInitializeSampleTimes(SimStruct *S) +{ + PRINT_ENTER; + ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME); + ssSetOffsetTime(S, 0, 0.0); + PRINT_LEAVE; +} + +#define MDL_INITIALIZE_CONDITIONS +static void mdlInitializeConditions(SimStruct *S) +{ + PRINT_ENTER; + _numpar (); + PRINT_LEAVE; +} + +static void mdlOutputs(SimStruct *S, int_T tid) +{ + PRINT_ENTER; + + update_inputs_from_simulink (S); + update_simtime_from_simulink (S); + + UNUSED_ARG(tid); /* not used in single tasking mode */ + + _input (); + + for (i = 0; i < MTTNU; i++) { + ssGetOutputPortRealSignal (S, 0)[i] = mttu[i]; + } + + PRINT_LEAVE; +} + +#define MDL_DERIVATIVES +static void mdlDerivatives(SimStruct *S) +{ + PRINT_ENTER; + + update_inputs_from_simulink (S); + update_simtime_from_simulink (S); + + PRINT_LEAVE; +} + +static void mdlTerminate(SimStruct *S) +{ + PRINT_ENTER; + + UNUSED_ARG(S); + + free (mttpar); + free (mttu); + free (mttx); + free (mtty); + + PRINT_LEAVE; +} + +#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */ +#include "simulink.c" /* MEX-file interface mechanism */ +#else +#include "cg_sfun.h" /* Code generation registration function */ +#endif