📄 s_xmp5.c
字号:
/* *****************************************************//* s_xmp5 *//* This is a C version of the S-file subsystem that *//* models a cart with inverted pendulum. The cart *//* and pendulum masses and pendulum length are *//* parameters that must be set in the block dialog *//* box. *//* *//* Based on MathWorks template file *//* Copyright (c) 1990-96 by The MathWorks, Inc. *//* All Rights Reserved *//* *****************************************************/#define S_FUNCTION_NAME s_xmp5#include "simstruc.h"#include "math.h"#define M_macro *mxGetPr(ssGetArg(S,0))#define m_macro *mxGetPr(ssGetArg(S,1))#define l_macro *mxGetPr(ssGetArg(S,2))/* *****************************************************//* mdlInitializeSizes *//* *****************************************************/static void mdlInitializeSizes(SimStruct *S){ ssSetNumContStates( S, 4); ssSetNumDiscStates( S, 0); ssSetNumInputs( S, 1); ssSetNumOutputs( S, 4); ssSetDirectFeedThrough(S, 0); ssSetNumSampleTimes( S, 1); ssSetNumSFcnParams( S, 3); ssSetNumRWork( S, 0); ssSetNumIWork( S, 0); ssSetNumPWork( S, 0); }/* *****************************************************//* mdlInitializeSampleTimes *//* *****************************************************/static void mdlInitializeSampleTimes(SimStruct *S){ ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME); ssSetOffsetTime(S, 0, 0.0);}/* *****************************************************//* mdlInitializeConditions *//* *****************************************************/static void mdlInitializeConditions(double *x0, SimStruct *S){ int i ; for(i = 0 ; i < 4 ; i++) *(x0+i) = 0.0 ;}/* *****************************************************//* mdlOutputs *//* *****************************************************/static void mdlOutputs(double *y, double *x, double *u, SimStruct *S, int tid){ int i ; for(i = 0 ; i < 4 ; i++) *(y+i) = *(x+i) ; /* y = x */}/* *****************************************************//* mdlUpdate *//* *****************************************************/static void mdlUpdate(double *x, double *u, SimStruct *S, int tid){}/* *****************************************************//* mdlDerivatives *//* *****************************************************/static void mdlDerivatives(double *dx, double *x, double *u, SimStruct *S, int tid){ static double g = 9.8 ; /* Acceleration due to gravity */ double M, m, l ; /* Model parameters */ /* MATLAB matrix to contain the mass matrix */ mxArray *Mass ; /* pointer to the data part of the matrix */ double *Mass_v ; /* Store right hand side of dynamic equation */ mxArray *x_dot_dot_rhs ; double *x_dot_dot_rhs_v ; /* pointer to the data */ mxArray *input_array[2], *out_array[1] ; double *x_dot_dot ; M = M_macro ; /* Obtain the values of the parameters */ m = m_macro ; l = l_macro ; /* Create mass matrix */ Mass = mxCreateDoubleMatrix(2, 2, mxREAL) ; Mass_v = mxGetPr(Mass) ; /* Load the values in mass matrix */ *Mass_v = M + m ; *(Mass_v+1) = m*cos(*(x+2)) ; *(Mass_v+2) = m*l*cos(*(x+2)) ; *(Mass_v+3) = m*l ; /* Create rhs */ x_dot_dot_rhs = mxCreateDoubleMatrix(2, 1, mxREAL) ; x_dot_dot_rhs_v = mxGetPr(x_dot_dot_rhs) ; *x_dot_dot_rhs_v = m*l*pow(*(x+3),2.0)*sin(*(x+2)) + *u ; *(x_dot_dot_rhs_v+1) = m*g*sin(*(x+2)) ; /* Set up for call to MATLAB */ input_array[0] = Mass ; input_array[1] = x_dot_dot_rhs ; /* Solve system */ mexCallMATLAB(1, out_array, 2, input_array, "\\") ; x_dot_dot = mxGetPr(out_array[0]) ; /* Fill derivative vector */ *dx = *(x+1) ; *(dx+1) = *x_dot_dot ; *(dx+2) = *(x+3) ; *(dx+3) = *(x_dot_dot+1) ; /* Free the memory allocated by MATLAB */ mxDestroyArray(out_array[0]) ; /* Free the memory used for Mass and rhs */ mxDestroyArray(Mass) ; mxDestroyArray(x_dot_dot_rhs) ;}/* *****************************************************//* mdlTerminate *//* *****************************************************/static void mdlTerminate(SimStruct *S){}#ifdef MATLAB_MEX_FILE#include "simulink.c" #else#include "cg_sfun.h" #endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -