Index: mttroot/mtt/bin/mtt ================================================================== --- mttroot/mtt/bin/mtt +++ mttroot/mtt/bin/mtt @@ -15,10 +15,14 @@ ############################################################### ## Version control history ############################################################### ## $Header$ ## $Log$ +## Revision 1.350 2002/05/21 08:45:36 gawthrop +## DIY reps: no longer skips rep if file exists already. +## Avoids problems in rep.txt when options change +## ## Revision 1.349 2002/05/20 10:29:59 gawthrop ## -oct now forces an m target language to be oct ## (if the rep is an the list: "ae input logic numpar simpar state") ## ## Revision 1.348 2002/05/20 08:50:12 geraint @@ -1656,11 +1660,11 @@ rm -f *_csex.oct *_numpar.oct *_smxa.oct *_state.oct rm -f *_ode.oct *_odeo.oct rm -f *_cseo.mexglx *_input.mexglx *_simpar.mexglx *_smxax.mexglx rm -f *_csex.mexglx *_numpar.mexglx *_smxa.mexglx *_state.mexglx rm -f *_ode.mexglx *_odeo.mexglx - rm -f *_sfun.mexglx *_sfun_ae.mexglx + rm -f *_sfun.mexglx *_sfun_*.mexglx rm -f *_cseo.cc *_input.cc *_simpar.cc *_smxax.cc rm -f *_csex.cc *_numpar.cc *_smxa.cc *_state.cc rm -f *_ode.cc *_odeo.cc rm -f *_logic.m *_logic.cc *_logic.oct rm -f *_state.m *_state.cc *_state.oct @@ -1697,11 +1701,11 @@ rm -f $1_csex.oct $1_numpar.oct $1_smxa.oct $1_state.oct rm -f $1_ode.oct $1_odeo.oct rm -f $1_cseo.mexglx $1_input.mexglx $1_simpar.mexglx $1_smxax.mexglx rm -f $1_csex.mexglx $1_numpar.mexglx $1_smxa.mexglx $1_state.mexglx rm -f $1_ode.mexglx $1_odeo.mexglx - rm -f $1_sfun.mexglx $1_sfun_ae.mexglx + rm -f $1_sfun.mexglx $1_sfun_*.mexglx rm -f $1_cseo.cc $1_input.cc $1_simpar.cc $1_smxax.cc rm -f $1_csex.cc $1_numpar.cc $1_smxa.cc $1_state.cc rm -f $1_ode.cc $1_odeo.cc rm -f $1_logic.m $1_logic.cc $1_logic.oct rm -f $1_state.m $1_state.cc $1_state.oct 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 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 +$(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 $(SYS)_sfun_interface.mexglx echo Creating $@ mex $(SYS)_sfun.c cp *_sfun*mexglx .. $(SYS)_sfun.c:: ${MTT_REP}/sfun_rep/sfun.c.tmpl @@ -25,11 +25,24 @@ $(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 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 +$(SYS)_sfun_interface.mexglx: $(SYS)_sfun_interface.c sfun_debug.h $(SYS)_def.h $(SYS)_sympar.h $(SYS)_numpar.c + echo Creating $@ + mex $(SYS)_sfun_interface.c + +$(SYS)_sfun_interface.c: ${MTT_REP}/sfun_rep/sfun_interface.c.tmpl $(SYS)_struc.c + echo Creating $@ + cat ${MTT_REP}/sfun_rep/sfun_interface.c.tmpl |\ + sed 's//$(SYS)/g' |\ + ${MTT_REP}/sfun_rep/insert_file.sh > $@ + +$(SYS)_sfun.zip: $(SYS)_sfun.c sfun_debug.h $(SYS)_sfun_ae.c $(SYS)_sfun_input.c $(SYS)_sfun_interface.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 $@ @@ -60,12 +73,21 @@ ${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 + +$(SYS)_struc.c: $(SYS)_struc.txt + echo Creating $@ + gawk '\ +($$1 == "input") { printf " double *%s\t= &(mttu[%d]);\t/* input and output */\n", $$4, $$2-1 }\ +($$1 == "state") { printf " const double *%s\t= &(mttx[%d]);\t/* input */\n", $$4, $$2-1 }\ +($$1 == "output") { printf " const double *%s\t= &(mtty[%d]);\t/* input */\n", $$4, $$2-1 }\ +($$1 != "input" && $$1 != "state" && $$1 != "output") { }\ + ' $^ > $@ 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 @@ -8,5 +8,9 @@ mex _sfun_input.c If numerical solution of algebraic equations is also required: mex _sfun_ae.c + +The interface block can be created with + +mex _sfun_interface.c ADDED mttroot/mtt/lib/rep/sfun_rep/insert_file.sh Index: mttroot/mtt/lib/rep/sfun_rep/insert_file.sh ================================================================== --- mttroot/mtt/lib/rep/sfun_rep/insert_file.sh +++ mttroot/mtt/lib/rep/sfun_rep/insert_file.sh @@ -0,0 +1,4 @@ +#! /usr/bin/gawk -f +# script to replace the line: "/* insert filename */" with the contents of file "filename" +($1 != "/*" || $2 != "insert" || $4 != "*/") { print } +($1 == "/*" && $2 == "insert" && $4 == "*/") { cmd = "cat " $3 ; system(cmd) } Index: mttroot/mtt/lib/rep/sfun_rep/mdl.tmpl ================================================================== --- mttroot/mtt/lib/rep/sfun_rep/mdl.tmpl +++ mttroot/mtt/lib/rep/sfun_rep/mdl.tmpl @@ -1,12 +1,12 @@ Model { - Name "generic" + Name "" Version 4.00 SampleTimeColors off - LibraryLinkDisplay "none" + LibraryLinkDisplay "all" WideLines off - ShowLineDimensions off + ShowLineDimensions on ShowPortDataTypes off ShowStorageClass off ExecutionOrder off RecordCoverage off CovPath "/" @@ -25,12 +25,12 @@ Creator "geraint" UpdateHistory "UpdateHistoryNever" ModifiedByFormat "%" LastModifiedBy "geraint" ModifiedDateFormat "%" - LastModifiedDate "Mon May 20 15:53:00 2002" - ModelVersionFormat "1.%" + LastModifiedDate "Thu May 23 16:35:42 2002" + ModelVersionFormat "1.%" ConfigurationManager "None" SimParamPage "Solver" StartTime "0.0" StopTime "10.0" SolverMode "Auto" @@ -152,12 +152,12 @@ FontSize 9 FontWeight "normal" FontAngle "normal" } System { - Name "generic" - Location [40, 379, 681, 493] + Name "" + Location [51, 441, 346, 574] Open on ModelBrowserVisibility off ModelBrowserWidth 200 ScreenColor "white" PaperOrientation "landscape" @@ -165,111 +165,189 @@ 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" + BlockType Inport + Name "In1" + Position [25, 43, 55, 57] + Port "1" + LatchInput off + DataType "double" + SignalType "real" + Interpolate on + } + Block { + BlockType SubSystem + Name "MTT Model\n" + Ports [1, 1] + Position [100, 20, 140, 80] + ForegroundColor "blue" + BackgroundColor "lightBlue" + ShowPortLabels on + TreatAsAtomicUnit off + RTWSystemCode "Auto" + RTWFcnNameOpts "Auto" + RTWFileNameOpts "Auto" + System { + Name "MTT Model\n" + Location [45, 448, 1008, 720] + Open off + ModelBrowserVisibility off + ModelBrowserWidth 200 + ScreenColor "white" + PaperOrientation "landscape" + PaperPositionMode "auto" + PaperType "usletter" + PaperUnits "inches" + ZoomFactor "100" + Block { + BlockType Inport + Name "In1" + Position [590, 203, 620, 217] + Port "1" + LatchInput off + DataType "double" + SignalType "real" + Interpolate on + } + Block { + BlockType "S-Function" + Name "MTT Model Inputs" + Ports [1, 1] + Position [390, 59, 565, 91] + 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 [65, 61, 250, 199] + 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 "S-Function" + Name "S-Function" + Ports [4, 2] + Position [710, 47, 840, 238] + BackgroundColor "lightBlue" + DropShadow on + FunctionName "_sfun_interface" + PortCounts "[]" + SFunctionModules "''" + } + Block { + BlockType Outport + Name "Out1" + Position [905, 183, 935, 197] + Port "1" + OutputWhenDisabled "held" + InitialOutput "[]" + } + Line { + Name "MTT Model States: MTTX" + Labels [2, 0] + SrcBlock "MTT Plant Model" + SrcPort 1 + Points [0, 0; 105, 0] + Branch { + Points [0, -20] + DstBlock "MTT Model Inputs" + DstPort 1 + } + Branch { + Points [0, 25] + DstBlock "S-Function" + DstPort 2 + } + } + Line { + Name "MTT Model Outputs: MTTY" + Labels [1, 0] + SrcBlock "MTT Plant Model" + SrcPort 2 + DstBlock "S-Function" + DstPort 3 + } + Line { + Name "MTT Model Inputs: MTTU" + Labels [1, 0] + SrcBlock "MTT Model Inputs" + SrcPort 1 + DstBlock "S-Function" + DstPort 1 + } + Line { + SrcBlock "S-Function" + SrcPort 1 + Points [30, 0; 0, -70; -850, 0; 0, 105] + DstBlock "MTT Plant Model" + DstPort 1 + } + Line { + SrcBlock "In1" + SrcPort 1 + DstBlock "S-Function" + DstPort 4 + } + Line { + SrcBlock "S-Function" + SrcPort 2 + DstBlock "Out1" + DstPort 1 + } + } + } + Block { + BlockType Outport + Name "Out1" + Position [185, 43, 215, 57] + Port "1" + OutputWhenDisabled "held" + InitialOutput "[]" + } + Line { + SrcBlock "MTT Model\n" + SrcPort 1 + DstBlock "Out1" + DstPort 1 + } + Line { + SrcBlock "In1" + SrcPort 1 + DstBlock "MTT Model\n" DstPort 1 } } } ADDED mttroot/mtt/lib/rep/sfun_rep/sfun_interface.c.tmpl Index: mttroot/mtt/lib/rep/sfun_rep/sfun_interface.c.tmpl ================================================================== --- mttroot/mtt/lib/rep/sfun_rep/sfun_interface.c.tmpl +++ mttroot/mtt/lib/rep/sfun_rep/sfun_interface.c.tmpl @@ -0,0 +1,237 @@ +/* -*-c-*- Put emacs into c-mode + * _sfun_interface.c: + * Matlab S-function to process inputs and outputs of + */ + + +#define S_FUNCTION_NAME _sfun_interface +#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 double *controller_inputs; +static double *controller_outputs; + +static unsigned int i; /* loop counter */ + +/* Start EDIT */ +/* Edit this block to define the number of controller inputs, outputs and parameters */ +const int NumberOfControllerInputs = 1; /* inputs TO controller */ +const int NumberOfControllerOutputs = 1; /* outputs FROM controller */ +/* End EDIT */ + +static void +_process_inputs (void) +{ + /* insert _struc.c */ + + /* Start EDIT */ + /* Edit this block to process the model inputs and outputs */ + + /* Remove the following line */ + fprintf (stderr, "*** Error: _sfun_interface.c has not been customised!\n"); return; + + /* simple example follows */ + + /* Get total of all outputs and input to controller */ + controller_inputs[0] = 0.0; + for (i = 0; i < MTTNY; i++) { + controller_inputs[0] += mtty[i]; + } + + /* overwrite first model input with output from controller */ + mttu[0] = controller_outputs[0]; + + /* End EDIT */ +} + + +/****************************************************************************** + * DO NOT EDIT ANYTHING BELOW THIS LINE * + ******************************************************************************/ + +/* system equations */ + +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); + + controller_inputs = array_of_double (NumberOfControllerInputs); + controller_outputs = array_of_double (NumberOfControllerOutputs); + + PRINT_LEAVE; +} + +static void +update_inputs_from_simulink (SimStruct *S) +{ + PRINT_ENTER; + for (i = 0; i < MTTNU; i++) { + mttu[i] = *ssGetInputPortRealSignalPtrs (S, 0)[i]; + } + for (i = 0; i < MTTNX; i++) { + mttx[i] = *ssGetInputPortRealSignalPtrs (S, 1)[i]; + } + for (i = 0; i < MTTNY; i++) { + mtty[i] = *ssGetInputPortRealSignalPtrs (S, 2)[i]; + } + for (i = 0; i < NumberOfControllerOutputs; i++) { + controller_outputs[i] = *ssGetInputPortRealSignalPtrs (S, 3)[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, 4)) return; + ssSetInputPortWidth(S, 0, MTTNU); + ssSetInputPortWidth(S, 1, MTTNX); + ssSetInputPortWidth(S, 2, MTTNY); + ssSetInputPortWidth(S, 3, NumberOfControllerOutputs); /* inputs from controller */ + ssSetInputPortDirectFeedThrough(S, 0, 1); + ssSetInputPortDirectFeedThrough(S, 1, 1); + ssSetInputPortDirectFeedThrough(S, 2, 1); + ssSetInputPortDirectFeedThrough(S, 3, 1); + + if (!ssSetNumOutputPorts(S, 2)) return; + ssSetOutputPortWidth(S, 0, MTTNU); /* altered inputs */ + ssSetOutputPortWidth(S, 1, NumberOfControllerInputs); /* outputs to controller */ + + 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); + + _process_inputs (); + + UNUSED_ARG(tid); /* not used in single tasking mode */ + + for (i = 0; i < MTTNU; i++) { + ssGetOutputPortRealSignal (S, 0)[i] = mttu[i]; + } + + for (i = 0; i < NumberOfControllerInputs; i++) { + ssGetOutputPortRealSignal (S, 1)[i] = controller_inputs[i]; + } + + PRINT_LEAVE; +} + +#define MDL_DERIVATIVES +static void mdlDerivatives(SimStruct *S) +{ + PRINT_ENTER; + PRINT_LEAVE; +} + +static void mdlTerminate(SimStruct *S) +{ + PRINT_ENTER; + + UNUSED_ARG(S); + + free (mttpar); + free (mttu); + free (mttx); + free (mtty); + + free (controller_inputs); + free (controller_outputs); + + 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