matlab 有哪些类,Matlab有关S 函数与类

在robotics的工具包里有个叫quaternion的mfile.小弟想把它写成s-function.代码如下:

function [sys,x0,str,ts] = quaternion(t,x,u,flag)

%

% The following outlines the general structure of an S-function.

%

switch flag,

%%%%%%%%%%%%%%%%%%

% Initialization %

%%%%%%%%%%%%%%%%%%

case 0,

[sys,x0,str,ts]=mdlInitializeSizes;

%%%%%%%%%%%%%%%

% Derivatives %

%%%%%%%%%%%%%%%

case 1,

sys=mdlDerivatives(t,x,u);

%%%%%%%%%%

% Update %

%%%%%%%%%%

case 2,

sys=mdlUpdate(t,x,u);

%%%%%%%%%%%

% Outputs %

%%%%%%%%%%%

case 3,

sys=mdlOutputs(t,x,u);

%%%%%%%%%%%%%%%%%%%%%%%

% GetTimeOfNextVarHit %

%%%%%%%%%%%%%%%%%%%%%%%

case 4,

sys=mdlGetTimeOfNextVarHit(t,x,u);

%%%%%%%%%%%%%

% Terminate %

%%%%%%%%%%%%%

case 9,

sys=mdlTerminate(t,x,u);

%%%%%%%%%%%%%%%%%%%%

% Unexpected flags %

%%%%%%%%%%%%%%%%%%%%

otherwise

error(['Unhandled flag = ',num2str(flag)]);

end

% end sfuntmpl

%

%=============================================================================

% mdlInitializeSizes

% Return the sizes, initial conditions, and sample times for the S-function.

%=============================================================================

%

function [sys,x0,str,ts]=mdlInitializeSizes

%

% call simsizes for a sizes structure, fill it in and convert it to a

% sizes array.

%

% Note that in this example, the values are hard coded.  This is not a

% recommended practice as the characteristics of the block are typically

% defined by the S-function parameters.

%

sizes = simsizes;

sizes.NumContStates  = 0;

sizes.NumDiscStates  = 4;

sizes.NumOutputs     = 4;

sizes.NumInputs      = 9;

sizes.DirFeedthrough = 1;

sizes.NumSampleTimes = 1;   % at least one sample time is needed

sys = simsizes(sizes);

%

% initialize the initial conditions

%

x0  = zeros(sizes.NumDiscStates ,1);

%

% str is always an empty matrix

%

str = [];

%

% initialize the array of sample times

%

ts  = [0 0];

% end mdlInitializeSizes

%

%=============================================================================

% mdlDerivatives

% Return the derivatives for the continuous states.

%=============================================================================

%

function sys=mdlDerivatives(t,x,u)

R=[];

sys = [];

% end mdlDerivatives

%

%=============================================================================

% mdlUpdate

% Handle discrete state updates, sample time hits, and major time step

% requirements.

%=============================================================================

%

function sys=mdlUpdate(t,x,u)

u1=u';

R=[u1(1,1:3); u1(1,4:6);u1(1,7:9)];

q = quaternion(R);

x(1)=q.s;

x(2:4) = q.v;

sys = [x];

% end mdlUpdate

%

%=============================================================================

% mdlOutputs

% Return the block outputs.

%=============================================================================

%

function sys=mdlOutputs(t,x,u)

sys = [x];

% end mdlOutputs

%

%=============================================================================

% mdlGetTimeOfNextVarHit

% Return the time of the next hit for this block.  Note that the result is

% absolute time.  Note that this function is only used when you specify a

% variable discrete-time sample time [-2 0] in the sample time array in

% mdlInitializeSizes.

%=============================================================================

%

function sys=mdlGetTimeOfNextVarHit(t,x,u)

sampleTime = 1;    %  Example, set the next hit to be one second later.

sys = t + sampleTime;

% end mdlGetTimeOfNextVarHit

%

%=============================================================================

% mdlTerminate

% Perform any end of simulation tasks.

%=============================================================================

%

function sys=mdlTerminate(t,x,u)

sys = [];

% end mdlTerminate

function q = quaternion(a1)

if all(size(a1) == [3 3])

% Q = QUATERNION(R)  from a 3x3 or 4x4 matrix

q = quaternion( tr2q(a1) );

elseif all(size(a1) == [4 4])

q = quaternion( tr2q(a1(1:3,1:3)) );

elseif all(size(a1) == [1 4])

% Q = QUATERNION([s v1 v2 v3]) from 4 elements

q.s = a1(1);

q.v = a1(2:4);

q = class(q, 'quaternion');

else

error('unknown dimension of input');

end

%TR2Q Convert homogeneous transform to a unit-quaternion

%

% Q = tr2q(T)

%

% Return a unit quaternion corresponding to the rotational part of the

% homogeneous transform T.

%

% See also: Q2TR

% Copyright (C) 1993 Peter Corke

function q = tr2q(t)

qs = sqrt(trace(t)+1)/2.0;

kx = t(3,2) - t(2,3); % Oz - Ay

ky = t(1,3) - t(3,1); % Ax - Nz

kz = t(2,1) - t(1,2); % Ny - Ox

if (t(1,1) >= t(2,2)) & (t(1,1) >= t(3,3))

kx1 = t(1,1) - t(2,2) - t(3,3) + 1; % Nx - Oy - Az + 1

ky1 = t(2,1) + t(1,2);   % Ny + Ox

kz1 = t(3,1) + t(1,3);   % Nz + Ax

add = (kx >= 0);

elseif (t(2,2) >= t(3,3))

kx1 = t(2,1) + t(1,2);   % Ny + Ox

ky1 = t(2,2) - t(1,1) - t(3,3) + 1; % Oy - Nx - Az + 1

kz1 = t(3,2) + t(2,3);   % Oz + Ay

add = (ky >= 0);

else

kx1 = t(3,1) + t(1,3);   % Nz + Ax

ky1 = t(3,2) + t(2,3);   % Oz + Ay

kz1 = t(3,3) - t(1,1) - t(2,2) + 1; % Az - Nx - Oy + 1

add = (kz >= 0);

end

if add

kx = kx + kx1;

ky = ky + ky1;

kz = kz + kz1;

else

kx = kx - kx1;

ky = ky - ky1;

kz = kz - kz1;

end

nm = norm([kx ky kz]);

if nm == 0,

q = quaternion([1 0 0 0]);

else

s = sqrt(1 - qs^2) / nm;

qv = s*[kx ky kz];

q = quaternion([qs qv]);

end

执行到红色那句时报错了:

提示为:

Error in 'quaternion_test/S-Function' while executing M-File S-function 'quaternionsf', flag = 2 (update), at time 0. MATLAB error message:

Error using ==> class

The CLASS function must be called from a class constructor.

小弟就纳闷了,这个类明显是在这个文件里面定义的啊,还有什么其他的class constructor呢?

[本帖最后由 古月非文 于 2010-6-21 11:17 编辑]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值