我试图通过simulink中的总线对象从我的c-mex s-function输出以下结构。以下代码片段来自我的主matlab文件,如果我运行此脚本,则创建slBus1
- simulink-bus-object并将其放置在matlab-workspace中。
a.b = 7;
a.c = 5;
a.e(1).d = 1;
a.e(2).d = 2;
a.e(3).d = 3;
bus1 = Simulink.Bus.createObject(a);
我使用simulink s-function-builder创建了以下代码,用于通过simulink-bus输出结构a
(或slBus1
)。 simulink模型看起来像这样:
如您所见,来自总线选择器块的所有输出端口都具有奇怪的维度[1x1],但子结构e
应该具有维度[3x1]。如果我在main-strucure中使用索引,也会发生这种情况,例如:
a(1).b = 1;
a(2).b = 2;
a(3).b = 3;
我只会从simulink总线输出中获得a(1).b
。
您有什么建议如何解决这个问题?我在文档或互联网的其他地方没有找到任何相关信息。谢谢!
/*
* File: test_build.c
*
*
*
* --- THIS FILE GENERATED BY S-FUNCTION BUILDER: 3.0 ---
*
* This file is an S-function produced by the S-Function
* Builder which only recognizes certain fields. Changes made
* outside these fields will be lost the next time the block is
* used to load, edit, and resave this file. This file will be overwritten
* by the S-function Builder block. If you want to edit this file by hand,
* you must change it only in the area defined as:
*
* %%%-SFUNWIZ_defines_Changes_BEGIN
* #define NAME 'replacement text'
* %%% SFUNWIZ_defines_Changes_END
*
* DO NOT change NAME--Change the 'replacement text' only.
*
* For better compatibility with the Simulink Coder, the
* "wrapper" S-function technique is used. This is discussed
* in the Simulink Coder's Manual in the Chapter titled,
* "Wrapper S-functions".
*
* -------------------------------------------------------------------------
* | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
* -------------------------------------------------------------------------
* Created: Sun Mar 5 17:15:44 2017
*
*
*/
#define S_FUNCTION_LEVEL 2
#define S_FUNCTION_NAME test_build
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/* %%%-SFUNWIZ_defines_Changes_BEGIN --- EDIT HERE TO _END */
#define NUM_INPUTS 0
#define NUM_OUTPUTS 1
/* Output Port 0 */
#define OUT_PORT_0_NAME y0
#define OUTPUT_0_WIDTH 1
#define OUTPUT_DIMS_0_COL 1
#define OUTPUT_0_DTYPE real_T
#define OUTPUT_0_COMPLEX COMPLEX_NO
#define OUT_0_FRAME_BASED FRAME_NO
#define OUT_0_BUS_BASED 1
#define OUT_0_BUS_NAME slBus1
#define OUT_0_DIMS 1-D
#define OUT_0_ISSIGNED 1
#define OUT_0_WORDLENGTH 8
#define OUT_0_FIXPOINTSCALING 1
#define OUT_0_FRACTIONLENGTH 3
#define OUT_0_BIAS 0
#define OUT_0_SLOPE 0.125
#define NPARAMS 0
#define SAMPLE_TIME_0 INHERITED_SAMPLE_TIME
#define NUM_DISC_STATES 0
#define DISC_STATES_IC [0]
#define NUM_CONT_STATES 0
#define CONT_STATES_IC [0]
#define SFUNWIZ_GENERATE_TLC 1
#define SOURCEFILES "__SFB__"
#define PANELINDEX 6
#define USE_SIMSTRUCT 0
#define SHOW_COMPILE_STEPS 0
#define CREATE_DEBUG_MEXFILE 0
#define SAVE_CODE_ONLY 0
#define SFUNWIZ_REVISION 3.0
/* %%%-SFUNWIZ_defines_Changes_END --- EDIT HERE TO _BEGIN */
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
#include "simstruc.h"
typedef struct {
real_T d;
} e;
typedef struct {
real_T b;
real_T c;
e e;
} slBus1;
/*
* Code Generation Environment flag (simulation or standalone target).
*/
static int_T isSimulationTarget;
/* Utility function prototypes. */
static int_T GetRTWEnvironmentMode(SimStruct *S);
/* Macro used to check if Simulation mode is set to accelerator */
#define isDWorkPresent !(ssRTWGenIsCodeGen(S) && !isSimulationTarget)
/*====================*
* S-function methods *
*====================*/
/* Function: mdlInitializeSizes ===============================================
* Abstract:
* Setup sizes of the various vectors.
*/
static void mdlInitializeSizes(SimStruct *S)
{
DECL_AND_INIT_DIMSINFO(outputDimsInfo);
ssSetNumSFcnParams(S, NPARAMS);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch will be reported by Simulink */
}
ssSetNumContStates(S, NUM_CONT_STATES);
ssSetNumDiscStates(S, NUM_DISC_STATES);
if (!ssSetNumInputPorts(S, NUM_INPUTS)) return;
if (!ssSetNumOutputPorts(S, NUM_OUTPUTS)) return;
/* Register slBus1 datatype for Output port 0 */
#if defined(MATLAB_MEX_FILE)
if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY)
{
DTypeId dataTypeIdReg;
ssRegisterTypeFromNamedObject(S, "slBus1", &dataTypeIdReg);
if(dataTypeIdReg == INVALID_DTYPE_ID) return;
ssSetOutputPortDataType(S,0, dataTypeIdReg);
}
#endif
ssSetBusOutputObjectName(S, 0, (void *) "slBus1");
ssSetOutputPortWidth(S, 0, OUTPUT_0_WIDTH);
ssSetOutputPortComplexSignal(S, 0, OUTPUT_0_COMPLEX);
ssSetBusOutputAsStruct(S, 0,OUT_0_BUS_BASED);
ssSetOutputPortBusMode(S, 0, SL_BUS_MODE);
if (ssRTWGenIsCodeGen(S)) {
isSimulationTarget = GetRTWEnvironmentMode(S);
if (isSimulationTarget==-1) {
ssSetErrorStatus(S, " Unable to determine a valid code generation environment mode");
return;
}
isSimulationTarget |= ssRTWGenIsModelReferenceSimTarget(S);
}
/* Set the number of dworks */
if (!isDWorkPresent) {
if (!ssSetNumDWork(S, 0)) return;
} else {
if (!ssSetNumDWork(S, 1)) return;
}
if (isDWorkPresent) {
/*
* Configure the dwork 0 (y0BUS)
*/
#if defined(MATLAB_MEX_FILE)
if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
DTypeId dataTypeIdReg;
ssRegisterTypeFromNamedObject(S, "slBus1", &dataTypeIdReg);
if (dataTypeIdReg == INVALID_DTYPE_ID) return;
ssSetDWorkDataType(S, 0, dataTypeIdReg);
}
#endif
ssSetDWorkUsageType(S, 0, SS_DWORK_USED_AS_DWORK);
ssSetDWorkName(S, 0, "y0BUS");
ssSetDWorkWidth(S, 0, DYNAMICALLY_SIZED);
ssSetDWorkComplexSignal(S, 0, COMPLEX_NO);
}
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
/* Take care when specifying exception free code - see sfuntmpl_doc.c */
ssSetOptions(S, (SS_OPTION_EXCEPTION_FREE_CODE |
SS_OPTION_USE_TLC_WITH_ACCELERATOR |
SS_OPTION_WORKS_WITH_CODE_REUSE));
}
/* Function: mdlInitializeSampleTimes =========================================
* Abstract:
* Specifiy the sample time.
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, SAMPLE_TIME_0);
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
/* Function: mdlStart =======================================================
* Abstract:
* This function is called once at start of model execution. If you
* have states that should be initialized once, this is the place
* to do it.
*/
static void mdlStart(SimStruct *S)
{
/* Bus Information */
slDataTypeAccess *dta = ssGetDataTypeAccess(S);
const char *bpath = ssGetPath(S);
DTypeId eId = ssGetDataTypeId(S, "e");
DTypeId slBus1Id = ssGetDataTypeId(S, "slBus1");
int_T *busInfo = (int_T *)malloc(6*sizeof(int_T));
if(busInfo==NULL) {
ssSetErrorStatus(S, "Memory allocation failure");
return;
}
/* Calculate offsets of all primitive elements of the bus */
busInfo[0] = dtaGetDataTypeElementOffset(dta, bpath,slBus1Id,0);
busInfo[1] = dtaGetDataTypeSize(dta, bpath, ssGetDataTypeId(S, "double"));
busInfo[2] = dtaGetDataTypeElementOffset(dta, bpath,slBus1Id,1);
busInfo[3] = dtaGetDataTypeSize(dta, bpath, ssGetDataTypeId(S, "double"));
busInfo[4] = dtaGetDataTypeElementOffset(dta, bpath,slBus1Id,2) +dtaGetDataTypeElementOffset(dta, bpath,eId,0);
busInfo[5] = dtaGetDataTypeSize(dta, bpath, ssGetDataTypeId(S, "double"));
ssSetUserData(S, busInfo);
}
#endif /* MDL_START */
#define MDL_SET_OUTPUT_PORT_DATA_TYPE
static void mdlSetOutputPortDataType(SimStruct *S, int port, DTypeId dType)
{
ssSetOutputPortDataType(S, 0, dType);
}
#define MDL_SET_DEFAULT_PORT_DATA_TYPES
static void mdlSetDefaultPortDataTypes(SimStruct *S)
{
ssSetOutputPortDataType(S, 0, SS_DOUBLE);
}
#define MDL_SET_WORK_WIDTHS
#if defined(MDL_SET_WORK_WIDTHS) && defined(MATLAB_MEX_FILE)
static void mdlSetWorkWidths(SimStruct *S)
{
/* Set the width of DWork(s) used for marshalling the IOs */
if (isDWorkPresent) {
/* Update dwork 0 */
ssSetDWorkWidth(S, 0, ssGetOutputPortWidth(S, 0));
}
}
#endif
void sete(e *out)
{
out[0].d=1;
out[1].d=2;
out[2].d=2;
}
void test_build_Outputs_wrapper(slBus1 *y0)
{
y0[0].b=3;
y0[0].c=4;
sete(&y0[0].e);
}
/* Function: mdlOutputs =======================================================
*
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
char *y0 = (char *) ssGetOutputPortSignal(S,0);
int_T* busInfo = (int_T *) ssGetUserData(S);
/* Temporary bus copy declarations */
slBus1 _y0BUS;
/*Copy input bus into temporary structure*/
test_build_Outputs_wrapper(&_y0BUS);
/*Copy temporary structure into output bus*/
(void) memcpy(y0+ busInfo[0], &_y0BUS.b, busInfo[1]);
(void) memcpy(y0+ busInfo[2], &_y0BUS.c, busInfo[3]);
(void) memcpy(y0+ busInfo[4], &_y0BUS.e.d, busInfo[5]);
}
/* Function: mdlTerminate =====================================================
* Abstract:
* In this function, you should perform any actions that are necessary
* at the termination of a simulation. For example, if memory was
* allocated in mdlStart, this is the place to free it.
*/
static void mdlTerminate(SimStruct *S)
{
/*Free stored bus information*/
int_T *busInfo = (int_T *) ssGetUserData(S);
if(busInfo!=NULL) {
free(busInfo);
}
}
static int_T GetRTWEnvironmentMode(SimStruct *S)
{
int_T status;
mxArray *plhs[1];
mxArray *prhs[1];
int_T err;
/*
* Get the name of the Simulink block diagram
*/
prhs[0] = mxCreateString(ssGetModelName(ssGetRootSS(S)));
plhs[0] = NULL;
/*
* Call "isSimulationTarget = rtwenvironmentmode(modelName)" in MATLAB
*/
mexSetTrapFlag(1);
err = mexCallMATLAB(1, plhs, 1, prhs, "rtwenvironmentmode");
mexSetTrapFlag(0);
mxDestroyArray(prhs[0]);
/*
* Set the error status if an error occurred
*/
if (err) {
if (plhs[0]) {
mxDestroyArray(plhs[0]);
plhs[0] = NULL;
}
ssSetErrorStatus(S, "Unknow error during call to 'rtwenvironmentmode'.");
return -1;
}
/*
* Get the value returned by rtwenvironmentmode(modelName)
*/
if (plhs[0]) {
status = (int_T) (mxGetScalar(plhs[0]) != 0);
mxDestroyArray(plhs[0]);
plhs[0] = NULL;
}
return (status);
}
#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