✅作者简介热爱科研的Matlab仿真开发者擅长毕业设计辅导、数学建模、数据处理、算法改进、程序设计科研仿真。完整代码获取 定制创新 论文复现私信个人信条做科研博学之、审问之、慎思之、明辨之、笃行之是为博学慎思明辨笃行。1. 相关介绍1.1 四旋翼飞行器建模思路对四旋翼飞行器建模其实就是在 Simulink 中模仿一架实际四旋翼的输入输出特性能够正确反映飞行器在给定转速指令如 PWM 波下的输出情况比如角速度、速度等如果给定初始状态还应能获得姿态、位置等信息。1.2 广义电机模型广义电机模型输入为使能转速输出为每个电机产生的力与力矩包括两大部分1电机动力学模型输入为转速指令实际系统中应为机载控制器发出的 PWM波或其他调节电调输入占空比的信号输出为推力与力矩。2使能转速-期望转速转换模块输入为“使能转速”输出为四个电机的期望转速。“使能转速”包括一个常量 与三个变量ி , ఏ , ట 使能转速的每个分量描述其使飞行器运动状态产生变化的效果下文将详细阐释。在整个四旋翼飞行器控制系统中使能转速是姿态控制器的输出。为使整个系统模块化程度更高将此转换模块与电机动力学模型整合为了广义电机模型。2. 运行效果展示3. 部分代码呈现function [sys,x0,str,ts,simStateCompliance] double_atti(t,x,u,flag,dt,J,K)%switch flag,%%%%%%%%%%%%%%%%%%% Initialization %%%%%%%%%%%%%%%%%%%case 0,[sys,x0,str,ts,simStateCompliance]mdlInitializeSizes(dt);%%%%%%%%%%%%%%%% Derivatives %%%%%%%%%%%%%%%%case 1,sysmdlDerivatives(t,x,u);%%%%%%%%%%% Update %%%%%%%%%%%case 2,sysmdlUpdate(t,x,u,dt);%%%%%%%%%%%% Outputs %%%%%%%%%%%%case 3,sysmdlOutputs(t,x,u,J,K);%%%%%%%%%%%%%%%%%%%%%%%% GetTimeOfNextVarHit %%%%%%%%%%%%%%%%%%%%%%%%case 4,sysmdlGetTimeOfNextVarHit(t,x,u);%%%%%%%%%%%%%% Terminate %%%%%%%%%%%%%%case 9,sysmdlTerminate(t,x,u);%%%%%%%%%%%%%%%%%%%%% Unexpected flags %%%%%%%%%%%%%%%%%%%%%otherwiseDAStudio.error(Simulink:blocks:unhandledFlag, num2str(flag));end% end sfuntmpl%%% mdlInitializeSizes% Return the sizes, initial conditions, and sample times for the S-function.%%function [sys,x0,str,ts,simStateCompliance]mdlInitializeSizes(dt)%% 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 15;%Rd wd dwdsizes.NumOutputs 7;% M notfullranksizes.NumInputs 21;%R w Rdsizes.DirFeedthrough 1;sizes.NumSampleTimes 1; % at least one sample time is neededsys simsizes(sizes);%% initialize the initial conditions%x0 [1 0 0 0 1 0 0 0 1 0 0 0 0 0 0];%% str is always an empty matrix%str [];%% initialize the array of sample times%ts [dt 0];% Specify the block simStateCompliance. The allowed values are:% UnknownSimState, The default setting; warn and assume DefaultSimState% DefaultSimState, Same sim state as a built-in block% HasNoSimState, No sim state% DisallowSimState Error out when saving or restoring the model sim statesimStateCompliance UnknownSimState;% end mdlInitializeSizes%%% mdlDerivatives% Return the derivatives for the continuous states.%%function sysmdlDerivatives(t,x,u)sys [];% end mdlDerivatives%%% mdlUpdate% Handle discrete state updates, sample time hits, and major time step% requirements.%%function sysmdlUpdate(t,x,u,dt)%内部状态读取pre_Rdreshape(x(1:9),[3,3]);pre_wdreshape(x(10:12),[3,1]);%反馈信息处理Rdreshape(u(13:21),[3,3]);if trace(pre_Rd)0wd[0;0;0];elseWdinv(Rd)*(Rd-pre_Rd)/dt;wdvee(Wd);endif norm(wd)0dwd[0;0;0];elsedwd(wd-pre_wd)/dt;end% wdreshape(u(16:18),[3,1]);% dwd[0;0;0];if norm(wd)20wdwd/norm(wd)*20;end%if norm(dwd)100dwddwd/norm(dwd)*100;endsys [reshape(Rd,[9,1]);wd;dwd];% end mdlUpdate%%% mdlOutputs% Return the block outputs.%%function sysmdlOutputs(t,x,u,J,K)%内部状态读取Rdreshape(x(1:9),[3,3]);wdreshape(x(10:12),[3,1]);dwdreshape(x(13:15),[3,1]);%反馈信息处理Rreshape(u(1:9),[3,3]);wreshape(u(10:12),[3,1]);RrRd*R;eR0.5*vee(Rr-Rr);eww-R*Rd*wd;hat_ewhat(ew);tmp1Rr*hat_ew*hat(w)-hat(wd)*Rr*hat_ew- hat(dwd)*Rr;Adtmp1-tmp1;RB[Rr(2,2)Rr(3,3) -Rr(2,1) -Rr(3,1);-Rr(1,2) Rr(1,1)Rr(3,3) -Rr(3,2);-Rr(1,3) -Rr(2,3) Rr(1,1)Rr(2,2)];evee(Rr-Rr);devee(Rr*hat_ewhat_ew*Rr);if rank(RB)3BRB*inv(J);Minv(B)*(-vee(Ad)-K*[e;de])cross(w,J*w);unfullrank0;elseM-8.81/10*eR-2.54/10*ewcross(w,J*w)-J*(hat(w)*Rr*wd-Rr*dwd);unfullrank1;endM(1:3)-8.81/10*eR-2.54/10*ewcross(w,J*w)-J*(hat(w)*Rr*wd-Rr*dwd);unfullrank1;% q dcm2quat(R);% qd dcm2quat(Rd);% q_inv quatinv(q); % 计算q的逆% qe quatmultiply(q_inv, qd); % 计算q的逆与qd的乘积% w_cmd 8 * sign(qe(1)) * qe(2:4);% we w_cmd - w;% sys [we;unfullrank];th10.3;th20.1;M(1)deadzone(-th1,M(1),th1);M(2)deadzone(-th1,M(2),th1);M(3)deadzone(-th2,M(3),th2);sys [M;unfullrank;wd];% 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 sysmdlGetTimeOfNextVarHit(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 sysmdlTerminate(t,x,u)sys [];% end mdlTerminate4. 参考文献更多免费数学建模和仿真教程关注领取如果觉得内容不错那就请分享和点个“在看”呗