主要内容

How to Use DWork Vectors

Using DWork Vectors in C MEX S-Functions

The following steps show how to initialize and use DWork vectors in a C MEX S-function. For a full list ofSimStruct与dwork矢量有关的宏,请参阅dwork矢量c mex宏.

  1. mdlinitialsizes, specify the number of DWork vectors using thessSetNumDWorkmacro. For example, to specify that the S-function contains two DWork vectors, use the command

    ssSetNumDWork(S, 2);

    虽然mdlinitialsizes方法告诉Simulink金宝app®engine how many DWork vectors the S-function will use, the engine does not allocate memory for the DWork vectors, at this time.

    S功能可以推迟指定DWORK向量的数量,直到可以通过传递值来获得有关S功能输入的所有信息动态式尺寸ssSetNumDWorkmacro. If an S-function defers specifying the number of DWork vectors inmdlinitialsizes, it must provide amdlsetworkwidthsmethod to set up the DWork vectors.

  2. If the S-function does not provide anmdlsetworkwidths方法,mdlinitialsizesmethod sets any applicable attributes for each DWork vector. For example, the following lines initialize the widths and data types of the DWork vectors initialized in the previous step.

    sssetDworkWidth(s,0,2);sssetDworkWidth(S,1,1);sssetDworkDatatype(s,0,ss_double);sssetDworkDatatype(S,1,ss_boolean);

    The following table lists attributes you can set for a DWork vector and shows an example of the macro that sets it. Seesssetdworkrtwstorageclassfor a list of supported storage classes.

    Attribute
    数据类型 ssSetDWorkDataType(S, 0, SS_DOUBLE);
    大小 ssSetDWorkWidth(S, 0, 2);
    Name ssSetDWorkName(S, 0, "sfcnState");
    Usage type sssetDworkUsageType(S, 0, SS_DWORK_USED_AS_DSTATE);
    Numeric type, either real or complex sssetDworkComplexSignal(S, 0, COMPLEX_NO);
    金宝appSimulink Coder™标识符 ssSetDWorkRTWIdentifier(s,0,“增益”);
    金宝appSimulink编码器storage class sssetdworkrtwstorageclass(S, 0, 2);
    金宝appSimulink编码器C类型预选赛 ssSetDWorkRTWTypeQualifier(S, 0, "volatile");
  3. mdlStart,初始化应仅在仿真开始时设置的任何DWORD向量的值。使用ssgetDWorkmacro to retrieve a pointer to each DWork vector and initialize the values. For example, the followingmdlStartmethod initializes the first DWork vector.

    静态void mdlStart(simStruct *s){real_t *x =(real_t *)ssgetDwork(s,0);/ *初始化第一个dwork vector */ x [0] = 0;x [1] = 2;}

    The Simulink engine allocates memory for the DWork vector before calling themdlStartmethod. Because themdlStart方法在模拟开头仅调用一次,请勿将其用于数据或需要重新初始化的状态,例如,在重新确定包含S函数的残疾子系统时。

  4. mdlInitializeConditions,初始化需要在仿真中某些点重新初始化的任何DWORD向量的值。引擎执行mdlInitializeConditions在模拟的开头以及任何启用包含S函数的子系统的启用子系统。看到mdlStart在上一个步骤中的示例用于初始化DWork Vector值的命令。

  5. mdlOutputs,mdlupdate等等,使用ssgetDWorkmacro to retrieve a pointer to the DWork vector and use or update the DWork vector values. For example, for a DWork vector storing two discrete states, the followingmdlOutputsmdlupdate方法计算输出并更新离散状态值。

    先前定义的S功能U(element)as(*uptrs [element]),A,B,C, 和D作为离散状态空间系统的状态空间矩阵。

    /*功能:mdlOutputs =================================================== *摘要: *y = cx + du */ static void mdlOutputs(simStruct *s,int_t tid){if(ssgetDworkUsageType(s,0)== ss_dwork_used_used_as_as_dstate){real_t *y = y = ssgetOutputputputportorportrealSignal(s,0);real_t *x =(real_t *)ssgetDwork(s,0);InputRealPtrStype uptrs = ssgetInputporterSignalPtrs(s,0);Unused_arg(tid);/*未在单个任务模式中使用*//*y = cx+du*/ y [0] = c [0] [0]*x [0]+c [0] [1] [1]*x [1]+d [0] [0]*u(0)+d [0] [1]*u(1);y [1] = c [1] [0]*x [0]+c [1] [1]*x [1]+d [1] [0]*u(0)*u(0)+d [1] [1] [1]]*u(1);}} #define mdl_update /*函数:mdlupdate ======================================================== *摘要: * xdot = ax + bu */ static void mdlupdate(simStruct * s,int_t tid){real_t tempx [2] = {0.0,0.0,0.0};real_t *x =(real_t *)ssgetDwork(s,0); InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0); UNUSED_ARG(tid); /* not used in single tasking mode */ /* xdot=Ax+Bu */ tempX[0]=A[0][0]*x[0]+A[0][1]*x[1]+B[0][0]*U(0)+B[0][1]*U(1); tempX[1]=A[1][0]*x[0]+A[1][1]*x[1]+B[1][0]*U(0)+B[1][1]*U(1); x[0]=tempX[0]; x[1]=tempX[1]; }

您不必在mdlterminatemethod to deallocate the memory used to store the DWork vector. Similarly, if you are generating inlined code for the S-function, you do not have to write anmdlrtw访问TLC文件中DWork Vector的方法。Simu金宝applink软件为您处理了DWork矢量的这些方面。

dwork矢量c mex宏

The following table lists the C MEX macros pertaining to DWork vectors.

描述
ssSetNumDWork 指定dwork向量的数量。
ssGetNumDWork 查询dwork矢量的数量。
ssgetDWork 获取指向特定DWORD向量的指针。
ssgetDWorkComplexSignal 确定特定的DWork矢量是真实的还是复杂的。
SSGETDWORKDATATYPE 获取DWORD向量的数据类型。
ssgetDWorkName Get the name of a DWork vector.
ssgetDworkrtwidentifier Get the identifier used to declare a DWork vector in the generated code.
ssgetDworkrtwIdentifiermustresolvetosignalObject 指出DWORD向量是否必须解析为金宝appsimulink.signalobject in the MATLAB®workspace.
ssgetDWorkRTWStorageClass Get the storage class of a DWork vector.
ssgetDworkrtwTyPequalifier 获取使用C类型预选赛来声明生成代码中的DWork向量。
SSGETDWORKUSAGETYPE 确定在s功能中如何使用DWORD矢量。
SSGETDWORKUSDASDSTATE 确定DWORD矢量是否存储离散状态。
ssgetDWorkWidth Get the size of a DWork vector.
sssetDworkComplexSignal 如果用油钻井的元素指定rk vector are real or complex.
ssSetDWorkDataType 指定DWORD向量的数据类型。
ssSetDWorkName 指定DWORD矢量的名称。
ssSetDWorkRTWIdentifier Specify the identifier used to declare a DWork vector in the generated code.
sssetdworkrtwidentifiermustresolvetosignalObject Specify if a DWork vector must resolve to a金宝appsimulink.signal目的。
sssetdworkrtwstorageclass Specify the storage class for a DWork vector.
ssSetDWorkRTWTypeQualifier 指定用于在生成代码中声明DWork向量的C类型预选赛。
sssetDworkUsageType 指定在s函数中如何使用DWORD矢量。
ssSetDWorkUsedAsDState 指定DWork Vector存储离散状态值。
ssSetDWorkWidth Specify the width of a DWork vector.

使用带有旧版代码的dwork向量

您可以使用DWork向量与旧版代码进行通信。如果您有将数据结构分配在内存中的现有代码,请在DWork向量中存储指向这些数据结构的指针。然后,您的S功能可以通过指针与旧版代码进行通信。另外,为了简单地设置S功能,您可以使用指针工作向量存储指针。看Elementary Work Vectors有关指针工作向量的描述。

You can also use DWork vectors to store the state of legacy code. The template filesfuntmpl_gate_fortran.cshows how to use DWork vectors to interact with legacy Fortran code. The Legacy Code Tool uses DWork vectors to maintain the states of legacy C or C++ code incorporated through the tool. See使用旧代码工具集成C函数for more information on the Legacy Code Tool.