S-function builder output: type mismatch error

3 次查看(过去 30 天)
Hi everyone,
I am creating a discrete S-function that has 4 inputs, 12 outputs and 12 discrete states, with sample time of 0.02s.
Each output and input has a name associated to it and therefore I can use those name directly in my computation. The Output depends on a rotation matrix which takes value from the states in the s-function. In the mdlOutput section, the outputs have 'real_T *' type but the previous state has 'real_T' type. Therefore there is a clash of type.
In the mdlUpdate section, the results from the outputs would be stored as the next state. Again, there is a data type clash. The codes are as follows:
mdlOutput
// Angle
real_T phi = xD[3];
real_T the = xD[4];
real_T psi = xD[5];
// Rotation matrix
real_T R[3][3] = {{cos(the)*cos(psi), sin(phi)*sin(the)*cos(psi)-cos(phi)*sin(psi), cos(phi)*sin(the)*cos(psi)+sin(phi)*sin(psi)}, ...;
real_T Rt[3][3] = {{cos(the)*cos(psi), cos(the)*sin(psi), -sin(the)}, ...;
// Inertia
real_T Ixx = 0.0258;
real_T Iyy = 0.0258;
real_T Izz = 0.0502;
// Gravity
real_T g = 9.81;
// Quadrotor
real_T d = 0.29; // Distance of rotor hub from CoG in x/y direction (m)
real_T rho = 1.225; // Density of air (kg/m^3)
real_T M = 1; // Mass of quadrotor (kg)
real_T r = 0.127; // Radius of prop (m)
real_T A = 3.14159*pow(r,2); // Area of prop spinning (m^2)
real_T Ct = 0.001269; // Thrust coefficient
real_T Cq = Ct * sqrt(Ct/2); // Drag coefficient
real_T b = Ct * rho * A * pow(r,2);
real_T k = -Cq*rho*A*pow(r,3);
// Force matrix
real_T dyn[4][4] = {{-b/M, -b/M, -b/M, -b/M}, ...;
// States
z1 = R[0][0]*xD[7] + R[0][1]*xD[8] + R[0][2]*xD[9];
z2 = R[1][0]*xD[7] + R[1][1]*xD[8] + R[1][2]*xD[9];
z3 = R[2][0]*xD[7] + R[2][1]*xD[8] + R[2][2]*xD[9];
n1 = xD[9];
n2 = xD[10];
n3 = xD[11];
v1 = R[0][2]*g;
v2 = R[1][2]*g;
v3 = R[2][2]*g + dyn[0][0]*pow(u[0],2) + dyn[0][1]*u[1] + dyn[0][2]*u[2] + dyn[0][3]*u[3];
o1 = 1/Ixx * dyn[1][0]*pow(u[0],2) + dyn[1][1]*u[1] + dyn[1][2]*u[2] + dyn[1][3]*u[3];
o2 = 1/Iyy * dyn[2][0]*pow(u[0],2) + dyn[2][1]*u[1] + dyn[2][2]*u[2] + dyn[2][3]*u[3];
o3 = 1/Izz * dyn[3][0]*pow(u[0],2) + dyn[3][1]*u[1] + dyn[3][2]*u[2] + dyn[3][3]*u[3];
mdlUpdate:
xD[0] = z1;
xD[1] = z2;
xD[2] = z3;
xD[3] = n1;
xD[4] = n2;
xD[5] = n3;
xD[6] = v1;
xD[7] = v2;
xD[8] = v3;
xD[9] = o1;
xD[10] = o2;
xD[11] = o3;
Any help on how to overcome the datatype clashes is greatly appreciated.

采纳的回答

Wern-Juin Choy
Wern-Juin Choy 2014-8-5
Update: I have solved the problem. It is pretty simple actually. The inputs were reduced to a 4x1 vector. The output is reduced to a 12x1 vector output. Using xD[i] = X[i], the mdlUpdate works and w[i] on the equations in mdlOutput.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Prepare Model Inputs and Outputs 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by