simscape做一个简单倒立摆

上周导师要求用滑模控制做一个倒立摆,细细了解了一下倒立摆这个模型,看了一些视频,激发了我的兴趣,在完成导师任务之后,想做一个可视化的倒立摆,发现了Simulink里的Simscape这个东西,于是就参考教程做了一个极其简单的PD控制的倒立摆。

Simscape官网介绍:

Simscape™ 可让您在 Simulink® 环境中迅速创建物理系统的模型。通过 Simscape,您可以基于物理连接直接相连模块框图建立物理组件模型。通过将基础组件依照原理图装配,为电机、桥式整流器、液压致动器和制冷系统等系统建模。Simscape 附加产品提供了更多复杂组件和分析功能。

Simscape 可帮助您开发控制系统并测试系统级性能。您可以利用基于 MATLAB® 的 Simscape 语言,使用文本定义物理建模组件、域和库,从而创建自定义组件模型。您可以利用 MATLAB 变量和表达式参数化您的模型,使用 Simulink 设计用于物理系统的控制系统。为了将模型部署到其他仿真环境,包括硬件在环 (HIL) 系统,Simscape 还支持生成 C 代码。

单摆

单摆的Simscape连接:

image-20211126163615481

仿真结果:

2021-11-26-16-39-23

单摆simulink程序

倒立摆:

加PD控制的倒立摆连接

image-20211126170348210

仿真结果
未加控制的倒立摆

2021-11-26-16-49-52

加入PD控制后

2021-11-26-16-55-58

位置输出和角度输出

image-20211126165431371

倒立摆simulink程序(适配matlab2021)

参考资料:

SimMechanics入门:做一个单摆

一步一步做一个倒立摆

Simscape模型装配坐标问题详解

  • 7
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Simscape是一款MATLAB工具箱,用于建模和仿真物理系统。倒立摆一个经典的控制问题,可以用Simscape来建模和仿真。以下是一个简单Simscape倒立摆模型的示例: 首先,我们需要定义系统的物理特性,包括摆杆的质量、长度、重心位置等参数。然后,我们可以使用Simscape中的旋转连接器来模拟摆杆的旋转运动,使用质量连接器来模拟摆杆的质量。我们还需要添加一个控制器来控制摆杆的运动。 下面是一个示例代码,实现了一个简单倒立摆控制器: ```matlab % Define system parameters m = 0.5; % mass of the pendulum l = 0.2; % length of the pendulum g = 9.81; % acceleration due to gravity % Create the Simscape model model = createpde(1); % Add the physical components pendulum = addComponent(model, 'Simscape/Mechanical/Rotational Elements/Rotational Spring-Damper', [1 1 0], 'pendulum'); mass = addComponent(model, 'Simscape/Mechanical/Translational Elements/Rigid Transform', [0 -l/2 0], 'mass'); gravity = addComponent(model, 'Simscape/Utilities/Physical Signal Sources/Constant', g, 'gravity'); % Connect the components connect(model, mass, pendulum.P, 'reference'); connect(model, gravity, pendulum.B, 'force'); % Add a controller controller = addComponent(model, 'Simscape/Electrical/Sources/Voltage Source', 0, 'controller'); connect(model, controller, pendulum.C, 'control signal'); % Set the parameters of the controller Kp = 100; Ki = 10; Kd = 1; setBlockParameter(model, 'controller/Kp', num2str(Kp)); setBlockParameter(model, 'controller/Ki', num2str(Ki)); setBlockParameter(model, 'controller/Kd', num2str(Kd)); % Simulate the system tspan = [0 10]; y0 = [0 pi/4 0 0]; options = odeset('RelTol',1e-3,'AbsTol',[1e-3 1e-3 1e-3 1e-3]); [t,y] = ode45(@(t,y)pendulumModel(t,y,m,l,g,Kp,Ki,Kd), tspan, y0, options); % Plot the results figure; plot(t, y(:,1), t, y(:,2)); xlabel('Time (s)'); ylabel('Angle (rad)'); legend('Pendulum Angle', 'Cart Position'); function dydt = pendulumModel(t, y, m, l, g, Kp, Ki, Kd) % Extract the state variables theta = y(1); phi = y(2); dtheta = y(3); dphi = y(4); % Compute the control signal e = -phi - Kd*dphi - Kp*theta - Ki*trapz(t,theta); u = max(min(e, 5), -5); % Compute the dynamics of the system ddtheta = (m*g*l*sin(phi) - m*l^2*dphi^2*sin(phi)*cos(phi) + u*cos(phi))/(m*l^2*(1 - cos(phi)^2)); ddphi = (-m*g*l*sin(phi)*cos(phi) + m*l^2*dphi^2*sin(phi) - u*sin(phi))/(m*l^2*(1 - cos(phi)^2)); % Return the state derivatives dydt = [dtheta; dphi; ddtheta; ddphi]; end ``` 该代码中,我们定义了倒立摆的物理参数,然后使用Simscape创建了倒立摆的模型。我们添加了一个控制器,用来控制摆杆的运动。控制器使用PD控制器,根据摆杆的角度和角速度计算出控制信号。最后,我们使用ODE45求解差分方程,得到系统的响应,并绘制了摆杆的角度和位置随时间的变化图像。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值