simulink简介
Simulink是美国Mathworks公司推出的MATLAB中的一种可视化仿真工具,用于多域仿真以及基于模型的设计,能够提供图形编辑器、可自定义的模块库以及求解器,能够进行动态系统建模和仿真。
梯形速度曲线规划算法
梯形速度曲线控制算法是工业控制领域应用最为广泛的加减速控制策略之一。所谓梯形速度曲线将整个运动过程分为匀加速、匀速和匀减速三个阶段,在变速过程中加速度保持不变。
仿真
MATLAB主页->Simulink
建立模块
库浏览器->Simulink->User-Defined Functions->S-Function Builder
S-Function名称
输入输出端口
先编译一次,生成模板代码
绘制模块
添加Constant
添加Clock
添加Scope
添加Scope端口
完成
代码
sfun_myc.c
/*
* File: sfun_myc.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: Wed Dec 14 11:34:47 2022
*/
#define S_FUNCTION_LEVEL 2
#define S_FUNCTION_NAME sfun_myc
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/* %%%-SFUNWIZ_defines_Changes_BEGIN --- EDIT HERE TO _END */
#define NUM_INPUTS 4
/* Input Port 0 */
#define IN_PORT_0_NAME u0
#define INPUT_0_DIMS_ND {1,1}
#define INPUT_0_NUM_ELEMS 1
#define INPUT_0_WIDTH 1
#define INPUT_DIMS_0_COL 1
#define INPUT_0_DTYPE real_T
#define INPUT_0_COMPLEX COMPLEX_NO
#define IN_0_BUS_BASED 0
#define IN_0_BUS_NAME
#define IN_0_DIMS 1-D
#define INPUT_0_FEEDTHROUGH 1
#define IN_0_ISSIGNED 0
#define IN_0_WORDLENGTH 8
#define IN_0_FIXPOINTSCALING 1
#define IN_0_FRACTIONLENGTH 9
#define IN_0_BIAS 0
#define IN_0_SLOPE 0.125
/* Input Port 1 */
#define IN_PORT_1_NAME u1
#define INPUT_1_DIMS_ND {1,1}
#define INPUT_1_NUM_ELEMS 1
#define INPUT_1_WIDTH 1
#define INPUT_DIMS_1_COL 1
#define INPUT_1_DTYPE real_T
#define INPUT_1_COMPLEX COMPLEX_NO
#define IN_1_BUS_BASED 0
#define IN_1_BUS_NAME
#define IN_1_DIMS 1-D
#define INPUT_1_FEEDTHROUGH 1
#define IN_1_ISSIGNED 0
#define IN_1_WORDLENGTH 8
#define IN_1_FIXPOINTSCALING 1
#define IN_1_FRACTIONLENGTH 9
#define IN_1_BIAS 0
#define IN_1_SLOPE 0.125
/* Input Port 2 */
#define IN_PORT_2_NAME u2
#define INPUT_2_DIMS_ND {1,1}
#define INPUT_2_NUM_ELEMS 1
#define INPUT_2_WIDTH 1
#define INPUT_DIMS_2_COL 1
#define INPUT_2_DTYPE real_T
#define INPUT_2_COMPLEX COMPLEX_NO
#define IN_2_BUS_BASED 0
#define IN_2_BUS_NAME
#define IN_2_DIMS 1-D
#define INPUT_2_FEEDTHROUGH 1
#define IN_2_ISSIGNED 0
#define IN_2_WORDLENGTH 8
#define IN_2_FIXPOINTSCALING 1
#define IN_2_FRACTIONLENGTH 9
#define IN_2_BIAS 0
#define IN_2_SLOPE 0.125
/* Input Port 3 */
#define IN_PORT_3_NAME t
#define INPUT_3_DIMS_ND {1,1}
#define INPUT_3_NUM_ELEMS 1
#define INPUT_3_WIDTH 1
#define INPUT_DIMS_3_COL 1
#define INPUT_3_DTYPE real_T
#define INPUT_3_COMPLEX COMPLEX_NO
#define IN_3_BUS_BASED 0
#define IN_3_BUS_NAME
#define IN_3_DIMS 1-D
#define INPUT_3_FEEDTHROUGH 1
#define IN_3_ISSIGNED 0
#define IN_3_WORDLENGTH 8
#define IN_3_FIXPOINTSCALING 1
#define IN_3_FRACTIONLENGTH 9
#define IN_3_BIAS 0
#define IN_3_SLOPE 0.125
#define NUM_OUTPUTS 3
/* Output Port 0 */
#define OUT_PORT_0_NAME y0
#define OUTPUT_0_DIMS_ND {1,1}
#define OUTPUT_0_NUM_ELEMS 1
#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_BUS_BASED 0
#define OUT_0_BUS_NAME
#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
/* Output Port 1 */
#define OUT_PORT_1_NAME y1
#define OUTPUT_1_DIMS_ND {1,1}
#define OUTPUT_1_NUM_ELEMS 1
#define OUTPUT_1_WIDTH 1
#define OUTPUT_DIMS_1_COL 1
#define OUTPUT_1_DTYPE real_T
#define OUTPUT_1_COMPLEX COMPLEX_NO
#define OUT_1_BUS_BASED 0
#define OUT_1_BUS_NAME
#define OUT_1_DIMS 1-D
#define OUT_1_ISSIGNED 1
#define OUT_1_WORDLENGTH 8
#define OUT_1_FIXPOINTSCALING 1
#define OUT_1_FRACTIONLENGTH 3
#define OUT_1_BIAS 0
#define OUT_1_SLOPE 0.125
/* Output Port 2 */
#define OUT_PORT_2_NAME y2
#define OUTPUT_2_DIMS_ND {1,1}
#define OUTPUT_2_NUM_ELEMS 1
#define OUTPUT_2_WIDTH 1
#define OUTPUT_DIMS_2_COL 1
#define OUTPUT_2_DTYPE real_T
#define OUTPUT_2_COMPLEX COMPLEX_NO
#define OUT_2_BUS_BASED 0
#define OUT_2_BUS_NAME
#define OUT_2_DIMS 1-D
#define OUT_2_ISSIGNED 1
#define OUT_2_WORDLENGTH 8
#define OUT_2_FIXPOINTSCALING 1
#define OUT_2_FRACTIONLENGTH 3
#define OUT_2_BIAS 0
#define OUT_2_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 N/A
#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"
extern void sfun_myc_Outputs_wrapper(const real_T *u0,
const real_T *u1,
const real_T *u2,
const real_T *t,
real_T *y0,
real_T *y1,
real_T *y2);
/*====================*
* S-function methods *
*====================*/
/* Function: mdlInitializeSizes ===============================================
* Abstract:
* Setup sizes of the various vectors.
*/
static void mdlInitializeSizes(SimStruct *S)
{
DECL_AND_INIT_DIMSINFO(inputDimsInfo);
DECL_AND_INIT_DIMSINFO(outputDimsInfo);
ssSetNumSFcnParams(S, NPARAMS);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch will be reported by Simulink */
}
ssSetArrayLayoutForCodeGen(S, SS_COLUMN_MAJOR);
ssSetOperatingPointCompliance(S, USE_DEFAULT_OPERATING_POINT);
ssSetNumContStates(S, NUM_CONT_STATES);
ssSetNumDiscStates(S, NUM_DISC_STATES);
if (!ssSetNumInputPorts(S, NUM_INPUTS))
return;
/* Input Port 0 */
ssSetInputPortWidth(S, 0, INPUT_0_NUM_ELEMS);
ssSetInputPortDataType(S, 0, SS_DOUBLE);
ssSetInputPortComplexSignal(S, 0, INPUT_0_COMPLEX);
ssSetInputPortDirectFeedThrough(S, 0, INPUT_0_FEEDTHROUGH);
ssSetInputPortRequiredContiguous(S, 0, 1);/*direct input signal access*/
/* Input Port 1 */
ssSetInputPortWidth(S, 1, INPUT_1_NUM_ELEMS);
ssSetInputPortDataType(S, 1, SS_DOUBLE);
ssSetInputPortComplexSignal(S, 1, INPUT_1_COMPLEX);
ssSetInputPortDirectFeedThrough(S, 1, INPUT_1_FEEDTHROUGH);
ssSetInputPortRequiredContiguous(S, 1, 1);/*direct input signal access*/
/* Input Port 2 */
ssSetInputPortWidth(S, 2, INPUT_2_NUM_ELEMS);
ssSetInputPortDataType(S, 2, SS_DOUBLE);
ssSetInputPortComplexSignal(S, 2, INPUT_2_COMPLEX);
ssSetInputPortDirectFeedThrough(S, 2, INPUT_2_FEEDTHROUGH);
ssSetInputPortRequiredContiguous(S, 2, 1);/*direct input signal access*/
/* Input Port 3 */
ssSetInputPortWidth(S, 3, INPUT_3_NUM_ELEMS);
ssSetInputPortDataType(S, 3, SS_DOUBLE);
ssSetInputPortComplexSignal(S, 3, INPUT_3_COMPLEX);
ssSetInputPortDirectFeedThrough(S, 3, INPUT_3_FEEDTHROUGH);
ssSetInputPortRequiredContiguous(S, 3, 1);/*direct input signal access*/
if (!ssSetNumOutputPorts(S, NUM_OUTPUTS))
return;
/* Output Port 0 */
ssSetOutputPortWidth(S, 0, OUTPUT_0_NUM_ELEMS);
ssSetOutputPortDataType(S, 0, SS_DOUBLE);
ssSetOutputPortComplexSignal(S, 0, OUTPUT_0_COMPLEX);
/* Output Port 1 */
ssSetOutputPortWidth(S, 1, OUTPUT_1_NUM_ELEMS);
ssSetOutputPortDataType(S, 1, SS_DOUBLE);
ssSetOutputPortComplexSignal(S, 1, OUTPUT_1_COMPLEX);
/* Output Port 2 */
ssSetOutputPortWidth(S, 2, OUTPUT_2_NUM_ELEMS);
ssSetOutputPortDataType(S, 2, SS_DOUBLE);
ssSetOutputPortComplexSignal(S, 2, OUTPUT_2_COMPLEX);
ssSetNumPWork(S, 0);
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
ssSetSimulinkVersionGeneratedIn(S, "10.6");
/* 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));
}
#if defined(MATLAB_MEX_FILE)
#define MDL_SET_INPUT_PORT_DIMENSION_INFO
static void mdlSetInputPortDimensionInfo(SimStruct *S,
int_T port,
const DimsInfo_T *dimsInfo)
{
if (!ssSetInputPortDimensionInfo(S, port, dimsInfo))
return;
}
#endif
#define MDL_SET_OUTPUT_PORT_DIMENSION_INFO
#if defined(MDL_SET_OUTPUT_PORT_DIMENSION_INFO)
static void mdlSetOutputPortDimensionInfo(SimStruct *S,
int_T port,
const DimsInfo_T *dimsInfo)
{
if (!ssSetOutputPortDimensionInfo(S, port, dimsInfo))
return;
}
#endif
/* Function: mdlInitializeSampleTimes =========================================
* Abstract:
* Specifiy the sample time.
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, SAMPLE_TIME_0);
ssSetModelReferenceSampleTimeDefaultInheritance(S);
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_SET_INPUT_PORT_DATA_TYPE
static void mdlSetInputPortDataType(SimStruct *S, int port, DTypeId dType)
{
ssSetInputPortDataType(S, 0, dType);
}
#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)
{
ssSetInputPortDataType(S, 0, SS_DOUBLE);
ssSetOutputPortDataType(S, 0, SS_DOUBLE);
}
#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)
{
}
#endif /* MDL_START */
/* Function: mdlOutputs =======================================================
*
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
const real_T *u0 = (real_T *) ssGetInputPortRealSignal(S, 0);
const real_T *u1 = (real_T *) ssGetInputPortRealSignal(S, 1);
const real_T *u2 = (real_T *) ssGetInputPortRealSignal(S, 2);
const real_T *t = (real_T *) ssGetInputPortRealSignal(S, 3);
real_T *y0 = (real_T *) ssGetOutputPortRealSignal(S, 0);
real_T *y1 = (real_T *) ssGetOutputPortRealSignal(S, 1);
real_T *y2 = (real_T *) ssGetOutputPortRealSignal(S, 2);
sfun_myc_Outputs_wrapper(u0, u1, u2, t, y0, y1, y2);
}
/* 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)
{
}
#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
sfun_myc_wrapper.c
/*
* Include Files
*
*/
#if defined(MATLAB_MEX_FILE)
#include "tmwtypes.h"
#include "simstruc_types.h"
#else
#define SIMPLIFIED_RTWTYPES_COMPATIBILITY
#include "rtwtypes.h"
#undef SIMPLIFIED_RTWTYPES_COMPATIBILITY
#endif
/* %%%-SFUNWIZ_wrapper_includes_Changes_BEGIN --- EDIT HERE TO _END */
#include <math.h>
/* %%%-SFUNWIZ_wrapper_includes_Changes_END --- EDIT HERE TO _BEGIN */
#define u_width 1
#define u_1_width 1
#define u_2_width 1
#define u_3_width 1
#define y_width 1
#define y_1_width 1
#define y_2_width 1
/*
* Create external references here.
*
*/
/* %%%-SFUNWIZ_wrapper_externs_Changes_BEGIN --- EDIT HERE TO _END */
/* extern double func(double a); */
/* %%%-SFUNWIZ_wrapper_externs_Changes_END --- EDIT HERE TO _BEGIN */
/*
* Output function
*
*/
void sfun_myc_Outputs_wrapper(const real_T *u0,
const real_T *u1,
const real_T *u2,
const real_T *t,
real_T *y0,
real_T *y1,
real_T *y2)
{
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_BEGIN --- EDIT HERE TO _END */
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_END --- EDIT HERE TO _BEGIN */
//梯形速度曲线
int Am = u0[0];
int Vm = u1[0];
int Pf = u2[0];
int T = t[0];
int Ta = Vm/Am;
int Tm = (Pf - Am*Ta*Ta)/Vm; int Tf = 2*Ta+Tm; printf("%d\r\n",Tf);
//梯形
if(Tm>0){
if(T <= Ta){
y0[0] = 0.5*Am*T*T;
y1[0] = Am*T;
y2[0] = Am;
}else if(T<=(Ta+Tm)){
y0[0] = 0.5*Am*Ta*Ta + Vm*(T-Ta);
y1[0] = Vm;
y2[0] = 0;
}else if(T<=(Ta+Tm+Ta)){
y0[0] = 0.5*Am*Ta*Ta + Vm*Tm + 0.5*Am*(T-Ta-Tm)*(T-Ta-Tm);
y1[0] = Vm-Am*(T-Ta-Tm);
y2[0] = -Am; }
}else{ //三⻆形
Ta = sqrt(Pf/Am);
if(T<Ta){
y0[0] = 0.5*Am*T*T;
y1[0] = Am*T;
y2[0] = Am;
}else{
y0[0] = 0.5*Am*Ta*Ta + 0.5*Am*(T-Ta)*(T-Ta);
y1[0] = Am*Ta - Am*(T-Ta);
y2[0] = -Am;
}
}
}
编译
安装MinGW编译器(Visual C++ 2022会发生编译错误)
[https://ww2.mathworks.cn/support/requirements/previous-releases.html]
[https://www.cnblogs.com/limanjihe/p/10025354.html]
如果出现以下提示:错误使用 mex 未找到支持的编译器或 SDK。您可以安装免费提供的 MinGW-w64 C/C++ 编译器;请参阅安装 MinGW-w64 编译器。如需更多选项,请参阅 [http://www.mathworks.com/support/compilers/R2015b/win64.html]。
然后重启MATLAB,终端中输入命令:
mex sfun_myc.c sfun_myc_wrapper.c
运行
效果
效果未达预期