ثانيا: نمذجة اللحرك باستخدام الـ C-Code


0013

0014001500160017001800190020

ثانيا: نمذجة المحرك باستخدام الـ C-Code

الدارة على الماتلاب

يمكن استخدام الـ C-Code لنمذجة جميع الأنظمة التي لا يقدمها برنامج MATLAB® جاهزة في بيئة Simulink، وهنا سنقوم بإنشاء نموذج عن محرك التيار المستمر باستخدام هذه الطريقة:

يمكن تقسيم كتابة البرنامج إلى ثلاثة مراحل:

  1. مرحلة التهيئة: يتم في هذه المرحلة تضمين المكتبات اللازمة، والتصريح عن عدد المداخل والمخارج والـ Aeguments، والتصريح عن المتحولات، وتحديد عدد المداخل وعدد المخارج وعدد الـ Aeguments وعدد أزمنة التقطيع وعدد الحالات المستمرة أو المتقطعة، وتحديد قيمة زمن التقطيع المستخدم في البلوك، وإعطاء قيم بدائية للمتحولات.
  2. مرحلة الـ Update: في هذه المرحلة يتم تحديد المخارج، وتحديد المداخل، وبرمجة المعادلات الممثلة للنظام، وتحديث المخارج.
  3. إيقاف المحاكاة.

/*

* DC-Servo Motor Modelling

* Copyright (c) 2006 by University of Aleppo

*  All Rights Reserved

*/

 

 

#define S_FUNCTION_NAME DC_Motor

 

 

#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 Ra_ARG                  ssGetArg(S,1)

#define La_ARG                  ssGetArg(S,2)

#define J_ARG                   ssGetArg(S,3)

#define B_ARG                   ssGetArg(S,4)

#define Cepsi_ARG               ssGetArg(S,5)

 

#define NUMBER_OF_ARGS          (6)

#define NSAMPLE_TIMES           (1)

#define NUMBER_OF_INPUTS        (2)  //U

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

 

/*variables */

 

float Ts, Ra, La, J, B, Cepsi;

float tsam, U_K, TL_K, ia_K, ia_K_1, W_K, W_K_1, Theta_K, Theta_K_1;

 

 

 

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];

Ra    = mxGetPr(Ra_ARG)[0];

La    = mxGetPr(La_ARG)[0];

J     = mxGetPr(J_ARG)[0];

B     = mxGetPr(B_ARG)[0];

Cepsi = mxGetPr(Cepsi_ARG)[0];

 

/* States initialization*/

 

ia_K_1=0;

W_K_1=0;

Theta_K_1=0;

 

Ra=11.8;

La=0.2;

Cepsi=0.945;

B=0.0006;

J=0.009;

}

 

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

{

/*Vo*/

y[0]= ia_K;

y[1]=W_K;

y[2]=Theta_K;

 

}

 

 

 

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

{

 

//Model Update

U_K= u[0];

TL_K= u[1];

 

ia_K=(1-(Ra/La)*tsam)*ia_K_1 + (-(Cepsi/La)*tsam)*W_K_1 + (tsam/La)*U_K;

 

W_K=((Cepsi/J)*tsam)*ia_K_1 + (1-(B/J)*tsam)*W_K_1 + (-tsam/J)*TL_K;

 

Theta_K = (tsam)*W_K_1 + Theta_K_1;

 

 

 

//update value

ia_K_1=ia_K;

W_K_1=W_K;

Theta_K_1=Theta_K;

 

if(Theta_K_1 >= 6.283185307179586)

{

Theta_K_1 = Theta_K_1 – 6.283185307179586;

Theta_K = Theta_K – 6.283185307179586;

}

else if (Theta_K_1 < 0)

{

Theta_K_1 = Theta_K_1 + 6.283185307179586;

Theta_K = Theta_K + 6.283185307179586;

}

 

 

/*

* 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

 

وعند تنفيذ الدارة العملية التالية

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

  • التجربة الأولى:
Final value Initial value Step time Final value Initial value Step time
6 0 3 150 0 1 1
  • التجربة الثانية:
Final value Initial value Step time Final value Initial value Step time
6 0 3 -150 150 5 2
  • التجربة الثالثة:
Final value Initial value Step time Final value Initial value Step time
6 0 3 50 150 4 3
  • التجربة الرابعة
Final value Initial value Step time Final value Initial value Step time
6 0 3 20 0 1 4
  • التجربة الخامسة (j=0.09 , Ra=5 ohm):
Final value Initial value Step time Final value Initial value Step time
6 0 3 150 0 1 1

أضف تعليق