رابعا: نمذجة المتحكمات PI و PD باستخدام الـ C-Code


0022

0023 0024 0025 0026 0027 0028 0029

رابعا: نمذجة المتحكمات PI و PD باستخدام الـ C-Code

يمكننا أيضا استخدام بلوك الـ S-Function في بناء نظام يحاكي عمل كل من متحكمات الـ PI والـ PD حيث نطبق الدارة التالية

علما أن برمجة الـ PI هي:

/*

 * DC-Servo Motor Modelling

 * Copyright (c) 2006 by University of Aleppo

 *  All Rights Reserved

 */

#define S_FUNCTION_NAME PI1

#include <stddef.h>

#include <stdlib.h>

#include <math.h>

#include “simstruc.h”

#ifdef MATLAB_MEX_FILE

#include “mex.h”

#endif

#define SAMPLE_TIME_ARG         ssGetArg(S,0)

#define KI_ARG                  ssGetArg(S,1)

#define KP_ARG                  ssGetArg(S,2)

#define NUMBER_OF_ARGS          (3)

#define NSAMPLE_TIMES           (1)

#define NUMBER_OF_INPUTS        (1)  //U

#define NUMBER_OF_OUTPUTS       (1) // x=[] state vector

/*variables */

float tsam, e_K, e_K_1, UPI_K, UPI_K_1, KI, KP;

static void mdlInitializeSizes(SimStruct *S)

{

    if (ssGetNumArgs(S) != NUMBER_OF_ARGS) {

#ifdef MATLAB_MEX_FILE

    mexErrMsgTxt(“Wrong number of input arguments passed.\nThree arguments are expected\n”);

#endif

    }

    /* Set up size information */

    ssSetNumContStates(    S, 0);                       /* number of continuous states */

    ssSetNumDiscStates(    S, 5);                       /* number of discrete states */

    ssSetNumInputs(        S, NUMBER_OF_INPUTS);        /* number of inputs */

    ssSetNumOutputs(       S, NUMBER_OF_OUTPUTS);       /* number of outputs */

    ssSetDirectFeedThrough(S, 0);                       /* direct feedthrough flag */

    ssSetNumSampleTimes(   S, NSAMPLE_TIMES);           /* number of sample times */

    ssSetNumInputArgs(     S, NUMBER_OF_ARGS);          /* number of input arguments */

    ssSetNumRWork(         S, 0);                       /* number of real work vector elements */

    ssSetNumIWork(         S, 0);                       /* NUMBER_OF_IWORKS);      /* number of integer work vector elements */

    ssSetNumPWork(         S, 0);                       /* number of pointer work vector elements */

}

static void mdlInitializeSampleTimes(SimStruct *S)

{

    ssSetSampleTimeEvent(S, 0, mxGetPr(SAMPLE_TIME_ARG)[0]);

    ssSetOffsetTimeEvent(S, 0, 0.0);

}

static void mdlInitializeConditions(double *x0, SimStruct *S)

{

/*Sampling Time*/

    tsam  = mxGetPr(SAMPLE_TIME_ARG)[0];

    KI    = mxGetPr(KI_ARG)[0];

    KP    = mxGetPr(KP_ARG)[0];

/* States initialization*/

UPI_K_1=0;

e_K_1=0;

}

static void mdlOutputs(double *y, double *x, double *u, SimStruct *S, int tid)

{

/*Vo*/

y[0]=UPI_K;

}

static void mdlUpdate(double *x, double *u, SimStruct *S, int tid)

{

//Model Update

    e_K= u[0];

    UPI_K=UPI_K_1+(KI*tsam+KP)*e_K-KP*e_K_1;

//update value

    e_K_1=e_K;

    UPI_K_1=UPI_K;

/*

 * mdlDerivatives – compute the derivatives

 *

 * In this function, you compute the S-function block’s derivatives.

 * The derivatives are placed in the dx variable.

 */

}

static void mdlDerivatives(double *dx, double *x, double *u, SimStruct *S, int tid)

{

}

/*

 * mdlTerminate – called when the simulation is terminated.

 *

 * In this function, you should perform any actions that are necessary

 * at the termination of a simulation.  For example, if memory was allocated

 * in mdlInitializeConditions, 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

وبارامتراته هي:

وبرمجة الـ PD هي:

/*

 * DC-Servo Motor Modelling

 * Copyright (c) 2006 by University of Aleppo

 *  All Rights Reserved

 */

#define S_FUNCTION_NAME PD1

#include <stddef.h>

#include <stdlib.h>

#include <math.h>

#include “simstruc.h”

#ifdef MATLAB_MEX_FILE

#include “mex.h”

#endif

#define SAMPLE_TIME_ARG         ssGetArg(S,0)

#define KD_ARG                  ssGetArg(S,1)

#define KP_ARG                  ssGetArg(S,2)

#define NUMBER_OF_ARGS          (3)

#define NSAMPLE_TIMES           (1)

#define NUMBER_OF_INPUTS        (1)  //U

#define NUMBER_OF_OUTPUTS       (1) // x=[] state vector

/*variables */

float tsam, e_K, e_K_1, UPD_K, UPD_K_1, KD, KP;

static void mdlInitializeSizes(SimStruct *S)

{

    if (ssGetNumArgs(S) != NUMBER_OF_ARGS) {

#ifdef MATLAB_MEX_FILE

    mexErrMsgTxt(“Wrong number of input arguments passed.\nThree arguments are expected\n”);

#endif

    }

    /* Set up size information */

    ssSetNumContStates(    S, 0);                       /* number of continuous states */

    ssSetNumDiscStates(    S, 5);                       /* number of discrete states */

    ssSetNumInputs(        S, NUMBER_OF_INPUTS);        /* number of inputs */

    ssSetNumOutputs(       S, NUMBER_OF_OUTPUTS);       /* number of outputs */

    ssSetDirectFeedThrough(S, 0);                       /* direct feedthrough flag */

    ssSetNumSampleTimes(   S, NSAMPLE_TIMES);           /* number of sample times */

    ssSetNumInputArgs(     S, NUMBER_OF_ARGS);          /* number of input arguments */

    ssSetNumRWork(         S, 0);                       /* number of real work vector elements */

    ssSetNumIWork(         S, 0);                       /* NUMBER_OF_IWORKS);      /* number of integer work vector elements */

    ssSetNumPWork(         S, 0);                       /* number of pointer work vector elements */

}

static void mdlInitializeSampleTimes(SimStruct *S)

{

    ssSetSampleTimeEvent(S, 0, mxGetPr(SAMPLE_TIME_ARG)[0]);

    ssSetOffsetTimeEvent(S, 0, 0.0);

}

static void mdlInitializeConditions(double *x0, SimStruct *S)

{

/*Sampling Time*/

    tsam  = mxGetPr(SAMPLE_TIME_ARG)[0];

    KD    = mxGetPr(KD_ARG)[0];

    KP    = mxGetPr(KP_ARG)[0];

/* States initialization*/

UPD_K_1=0;

e_K_1=0;

}

static void mdlOutputs(double *y, double *x, double *u, SimStruct *S, int tid)

{

/*Vo*/

y[0]=UPD_K_1;

}

static void mdlUpdate(double *x, double *u, SimStruct *S, int tid)

{

//Model Update

    e_K= u[0];

UPD_K=(KP+KD/tsam)*e_K-(KD/tsam)*e_K_1;

//update value

    e_K_1=e_K;

    UPD_K_1=UPD_K;

/*

 * mdlDerivatives – compute the derivatives

 *

 * In this function, you compute the S-function block’s derivatives.

 * The derivatives are placed in the dx variable.

 */

}

static void mdlDerivatives(double *dx, double *x, double *u, SimStruct *S, int tid)

{

}

/*

 * mdlTerminate – called when the simulation is terminated.

 *

 * In this function, you should perform any actions that are necessary

 * at the termination of a simulation.  For example, if memory was allocated

 * in mdlInitializeConditions, 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

وبارامتراته هي:

بتشغيل الدارة السابقة نحصل على النتائج التالية التي تظهر أولا الموضع المرغوب، ثم موضع المحرك، ثم التيار المسحوب، ثم سرعة المحرك. وذلك في الحالة التي يتم فيها تطبيق حمل TL على المحرك عند الثانية 3:

Advertisements

اترك رد

إملأ الحقول أدناه بالمعلومات المناسبة أو إضغط على إحدى الأيقونات لتسجيل الدخول:

WordPress.com Logo

أنت تعلق بإستخدام حساب WordPress.com. تسجيل خروج   / تغيير )

صورة تويتر

أنت تعلق بإستخدام حساب Twitter. تسجيل خروج   / تغيير )

Facebook photo

أنت تعلق بإستخدام حساب Facebook. تسجيل خروج   / تغيير )

Google+ photo

أنت تعلق بإستخدام حساب Google+. تسجيل خروج   / تغيير )

Connecting to %s