Example

 

The example using U Function is modeled by the equation of PMDC motor. (It is compared the result of PMDC in Actuator of CoLink.) The related data is UFunction_Sample.clk in <Install Dir>\Help\U_Function\UFunction_Sample. There is UFunction_Sample.dll in the file path and it is made by the file of <Install Dir>\Help\U_Function\UFunction_Sample in Visual studio 2005.

The below PMDC block is made by the PMDC block in Actuator and the below UFunction_PMDC is used by the DLL of U Function. Also, UFunction_Sample is made by the motor equation and the rotor equation.

 

Image_U-function.png

Figure 1  PMDC Block & UFunction_Sample block

 

 

 

 

 

 

Figure 2  PMDC dialog box

 

Figure 3  U Function dialog box

 

PMDC Equations

The voltage equations of PMDC are as follows.

 

 

where,  is the input voltage,  is the armature resistance,  is the armature inductance,  is the armature current, and E is the back electromotive force.

The armature winding is connected to the outside terminal. This block is very similar to a shunt connected DC motor. A back electromotive force (BEMF) by magnet, which is generated between the armature terminals, is proportional to the machine speed.

 

 

where,  is the voltage (BEMF) constant,  is the mechanical angular speed.

The electrical torque, which is developed by PMDC motor, is proportional to the armature current.

 

.

 

The torque constant  is equal to the voltage (BEMF) constant.

When the machine is in generator mode, the sign of torque is positive. In motor mode, the sign of torque is negative.

 

Rotor Equations

 

where, J is the inertia of rotor, θ is the rotational angle, ω is the rotational velocity of rotor.

 

Example

extern "C" __declspec(dllexport) void simInitializes( CSys *CS )

{

     // Port Numer and Size

     // No. eqn

           // Sample time

           CS->SetNumInputPort(1); The number of input port is 1.

           CS->SetInputPortSize(0,1); The data size to the first input port is 1.

     CS->SetNumOutputPort(1); The number of out port is 1.

     CS->SetOutputPortSize(0,1); The data size to the first output port is 1.

     CS->SetNumDiffStates(3); The number of different equation is 3.

     CS->SetSampleTime(-1.0); Do not use the sample time.

}

extern "C" __declspec(dllexport) void simInitialConditions( CSys *CS )

{

     double* Beta;

     int nDiff;

     nDiff = CS->GetNumDiffStates(); Get the number of different equation.

     Beta = CS->GetDiffStates();Get the data pointer of differential state.

     for( int i=0 ; i<nDiff ; i++ ) {

                Beta[i] = 0.0; Set the initial value of differential state.

     }

     // 1:FDM 2:Anlaytical

     CS->SetJacobianMethod(2); Input the analytical Jacobian.

     // For jacobian matrix

     CS->SetNumRWork(nDiff*nDiff); Define the array of Jacobian matrix.

     CS->SetNumIWork(0); Do not use IWork.

     CS->WriteToMessageFile("U Function Sample Code.\n"); Output the string to the message file.

 

}

 

// Optional

extern "C" __declspec(dllexport) void simUpdateParameters( CSys *CS )

{

     double simtime;

     simtime = CS->GetSimTime(); Get the current simulation time.

 

//if( simtime > 1.0 ) CS->SetParameter(0,0.2); Change the first parameter after 1 second in the simulation.

}

 

extern "C" __declspec(dllexport) void simJacobians( CSys *CS )

{

     Size

     // Sample time

     double Ra,La,Ke,Inertia;

     double *Jac;

     int nDiff;

 

           Ke = CS->GetParameter(0); Get the value of first parameter.

           Ra = CS->GetParameter(2); Get the value of third parameter.

           La = CS->GetParameter(3); Get the value of fourth parameter.

           Inertia = CS->GetParameter(4); Get the value of fifth parameter.

 

     Jac = CS->GetRWorkPtr();Get the pointer of RWork.

 

           Jac[0] = -Ra/La; Calculate the Jacobian matrix.

           Jac[1] = 0.0; Calculate the Jacobian matrix.

     Jac[2] = -Ke/La; Calculate the Jacobian matrix.

 

           Jac[3] = 0.0; Calculate the Jacobian matrix.

Jac[4] = 0.0; Calculate the Jacobian matrix.

           Jac[5] = 1.0; Calculate the Jacobian matrix.

 

     Jac[6] = Ke/Inertia; Calculate the Jacobian matrix.

           Jac[7] = 0.0; Calculate the Jacobian matrix.

           Jac[8] = 0.0; Calculate the Jacobian matrix.

 

     CS->SetJacobian(Jac); Defines the Jacobian matrix.

 

}

 

// Optional

extern "C" __declspec(dllexport) void simDerivatives( CSys *CS )

{

     double Ra,La,Ke,Kt,angle,omega,Ia,Va,E,Te,Tfric,Td;

     double Inertia,Bm,Tf,TL;

     double simtime;       

     int npole;

 

     double *Uin;

     double *dBeta,*Beta;

 

     // Parameter

     Ke = CS->GetParameter(0); Get the value of first parameter.

 

     npole = (int) CS->GetParameter(1); Get the value of second parameter.

 

     Ra = CS->GetParameter(2); Get the value of third parameter.

 

     La = CS->GetParameter(3); Get the value of fourth parameter.

 

     Inertia = CS->GetParameter(4); Get the value of fifth parameter.

 

     Bm = CS->GetParameter(5); Get the value of sixth parameter.

 

     Tf = CS->GetParameter(6); Get the value of seventh parameter.

 

     // Input

     Uin = CS->GetInputPortData(0); Get the value of input port.

 

     // Diff eqn.

     Beta = CS->GetDiffStates(); Get the pointer of differential state.

 

     dBeta = CS->GetDotDiffStates(); Get the pointer to the differential value of differential state.

 

     TL = 0.0;

     Va = Uin[0];

 

     Ia = Beta[0];

     angle = Beta[1];

     omega = Beta[2];

 

     // Motor eqn.

     E = Ke*omega;

     Kt = Ke;

     Te = Kt*Ia;

     Tfric = (omega>=0?1:-1)*(Bm*fabs(omega)+Tf);

     Td = Te-Tfric;

 

     // Result

           dBeta[0] = (Va-Ra*Ia-E)/La; Set the differential value of differential state by the equation.

           dBeta[1] = omega; Set the differential value of differential state by the equation.

           dBeta[2] = 1/Inertia*(Td - TL); Set the differential value of differential state by the equation.

 

     Output_Te = Te; Set the differential value of differential state by the equation.

 

}

 

extern "C" __declspec(dllexport) void simOutputs( CSys *CS )

{

     double *Yout;

    

     Yout = CS->GetOutputPortData(0); Get the pointer of output.

    

     Yout[0] = Output_Te; Output Te.

}

 

extern "C" __declspec(dllexport) void simTerminates( CSys *CS )

{

    

}