• 基于MATLAB的一级倒立摆控制仿真,带GUI界面操作显示倒立摆动画,控制器控制输出


    目录

    1.算法描述

    2.仿真效果预览

    3.MATLAB核心程序

    4.完整MATLAB


    1.算法描述

          一个可以活动的小车上立着一根不稳定随时会倒下的杆。小车的轮子由电机控制,可以控制小车电机的转动力矩M。同时,也可以获取小车轮子转动的圈数N(可以精确到小数)和杆相对于垂直位置的倾角α.

          不考虑车轮打滑, 小车所受力大小等于电机力矩乘车轮半径, 小车位置可以从转动圈数计算出, 小车可简化为最经典的一阶倒立摆:


     

     对小车水平方向:
    Mx¨+bx˙+N=F

    对摆水平方向:
    N=md2dt(x+lsin⁡θ)

    即:
    N=mx¨+mlθ¨cos⁡θ¨−mlθ˙2sin⁡θ

    对摆垂直方向:
    P=mg+md2dt(lcos⁡θ)
    即:

    P=mg−mlθ¨sin⁡θ¨−mlθ˙2cos⁡θ

    关节力矩:
    −Plsin⁡θ−Nlcos⁡θ=Iθ¨

    令 ϕ=π+θ
    在工作点 θ≈0 进行线性化: sin⁡θ=0,cos⁡θ=1,θ˙2=0 经过一顿操作化简之后能得到:
    (I+ml2)ϕ¨−mglϕ=mlx¨ (M+m)x¨+bx˙−mlϕ¨=F=u

    2.仿真效果预览

    matlab2022a仿真如下:

    3.MATLAB核心程序

    1. function varargout = pendulum(varargin)
    2. % PENDULUM M-file for pendulum.fig
    3. % PENDULUM, by itself, creates a new PENDULUM or raises the existing
    4. % singleton*.
    5. %
    6. % H = PENDULUM returns the handle to a new PENDULUM or the handle to
    7. % the existing singleton*.
    8. %
    9. % PENDULUM('CALLBACK',hObject,eventData,handles,...) calls the local
    10. % function named CALLBACK in PENDULUM.M with the given input arguments.
    11. %
    12. % PENDULUM('Property','Value',...) creates a new PENDULUM or raises the
    13. % existing singleton*. Starting from the left, property value pairs are
    14. % applied to the GUI before pendulum_OpeningFunction gets called. An
    15. % unrecognized property name or invalid value makes property application
    16. % stop. All inputs are passed to pendulum_OpeningFcn via varargin.
    17. %
    18. % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
    19. % instance to run (singleton)".
    20. %
    21. % See also: GUIDE, GUIDATA, GUIHANDLES
    22. % Copyright 2002-2003 The MathWorks, Inc.
    23. % Edit the above text to modify the response to help pendulum
    24. % Last Modified by SHE on 15-Dec-2008 22:08:58
    25. % Begin initialization code - DO NOT EDIT
    26. gui_Singleton = 1;
    27. gui_State = struct('gui_Name', mfilename, ...
    28. 'gui_Singleton', gui_Singleton, ...
    29. 'gui_OpeningFcn', @pendulum_OpeningFcn, ...
    30. 'gui_OutputFcn', @pendulum_OutputFcn, ...
    31. 'gui_LayoutFcn', [] , ...
    32. 'gui_Callback', []);
    33. if nargin && ischar(varargin{1})
    34. gui_State.gui_Callback = str2func(varargin{1});
    35. end
    36. if nargout
    37. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
    38. else
    39. gui_mainfcn(gui_State, varargin{:});
    40. end
    41. % End initialization code - DO NOT EDIT
    42. % --- Executes just before pendulum is made visible.
    43. function pendulum_OpeningFcn(hObject, eventdata, handles, varargin)
    44. % This function has no output args, see OutputFcn.
    45. % hObject handle to figure
    46. % eventdata reserved - to be defined in a future version of MATLAB
    47. % handles structure with handles and user data (see GUIDATA)
    48. % varargin command line arguments to pendulum (see VARARGIN)
    49. % Choose default command line output for pendulum
    50. handles.output = hObject;
    51. % Update handles structure
    52. guidata(hObject, handles);
    53. % UIWAIT makes pendulum wait for user response (see UIRESUME)
    54. % uiwait(handles.figure1);
    55. % --- Outputs from this function are returned to the command line.
    56. function varargout = pendulum_OutputFcn(hObject, eventdata, handles)
    57. % varargout cell array for returning output args (see VARARGOUT);
    58. % hObject handle to figure
    59. % eventdata reserved - to be defined in a future version of MATLAB
    60. % handles structure with handles and user data (see GUIDATA)
    61. % Get default command line output from handles structure
    62. varargout{1} = handles.output;
    63. function edit1_Callback(hObject, eventdata, handles)
    64. % hObject handle to edit1 (see GCBO)
    65. % eventdata reserved - to be defined in a future version of MATLAB
    66. % handles structure with handles and user data (see GUIDATA)
    67. % Hints: get(hObject,'String') returns contents of edit1 as text
    68. % str2double(get(hObject,'String')) returns contents of edit1 as a double
    69. %以字符串的形式来存储数据文本框1的内容。如果字符串不是数字,则现实空白内容
    70. input =str2double(get(hObject,'String'));
    71. %检查输入是否为空. 如果为空,则默认显示为0
    72. if (isempty(input))
    73. set(hObject,'String','0')
    74. end
    75. guidata(hObject, handles);
    76. % --- Executes during object creation, after setting all properties.
    77. function edit1_CreateFcn(hObject, eventdata, handles)
    78. % hObject handle to edit1 (see GCBO)
    79. % eventdata reserved - to be defined in a future version of MATLAB
    80. % handles empty - handles not created until after all CreateFcns called
    81. % Hint: edit controls usually have a white background on Windows.
    82. % See ISPC and COMPUTER.
    83. if ispc
    84. set(hObject,'BackgroundColor','white');
    85. else
    86. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    87. end
    88. function edit2_Callback(hObject, eventdata, handles)
    89. % hObject handle to edit2 (see GCBO)
    90. % eventdata reserved - to be defined in a future version of MATLAB
    91. % handles structure with handles and user data (see GUIDATA)
    92. % Hints: get(hObject,'String') returns contents of edit2 as text
    93. % str2double(get(hObject,'String')) returns contents of edit2 as a double
    94. %以字符串的形式来存储数据文本框2的内容。如果字符串不是数字,则现实空白内容
    95. input =str2double(get(hObject,'String'));
    96. %检查输入是否为空. 如果为空,则默认显示为0
    97. if (isempty(input))
    98. set(hObject,'String','0')
    99. end
    100. guidata(hObject, handles);
    101. % --- Executes during object creation, after setting all properties.
    102. function edit2_CreateFcn(hObject, eventdata, handles)
    103. % hObject handle to edit2 (see GCBO)
    104. % eventdata reserved - to be defined in a future version of MATLAB
    105. % handles empty - handles not created until after all CreateFcns called
    106. % Hint: edit controls usually have a white background on Windows.
    107. % See ISPC and COMPUTER.
    108. if ispc
    109. set(hObject,'BackgroundColor','white');
    110. else
    111. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    112. end
    113. function edit3_Callback(hObject, eventdata, handles)
    114. % hObject handle to edit3 (see GCBO)
    115. % eventdata reserved - to be defined in a future version of MATLAB
    116. % handles structure with handles and user data (see GUIDATA)
    117. % Hints: get(hObject,'String') returns contents of edit3 as text
    118. % str2double(get(hObject,'String')) returns contents of edit3 as a double
    119. %以字符串的形式来存储数据文本框3的内容。如果字符串不是数字,则现实空白内容
    120. input =str2double(get(hObject,'String'));
    121. %检查输入是否为空. 如果为空,则默认显示为0
    122. if (isempty(input))
    123. set(hObject,'String','0')
    124. end
    125. guidata(hObject, handles);
    126. % --- Executes during object creation, after setting all properties.
    127. function edit3_CreateFcn(hObject, eventdata, handles)
    128. % hObject handle to edit3 (see GCBO)
    129. % eventdata reserved - to be defined in a future version of MATLAB
    130. % handles empty - handles not created until after all CreateFcns called
    131. % Hint: edit controls usually have a white background on Windows.
    132. % See ISPC and COMPUTER.
    133. if ispc
    134. set(hObject,'BackgroundColor','white');
    135. else
    136. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    137. end
    138. function edit4_Callback(hObject, eventdata, handles)
    139. % hObject handle to edit4 (see GCBO)
    140. % eventdata reserved - to be defined in a future version of MATLAB
    141. % handles structure with handles and user data (see GUIDATA)
    142. % Hints: get(hObject,'String') returns contents of edit4 as text
    143. % str2double(get(hObject,'String')) returns contents of edit4 as a double
    144. %以字符串的形式来存储数据文本框4的内容。如果字符串不是数字,则现实空白内容
    145. input =str2double(get(hObject,'String'));
    146. %检查输入是否为空. 如果为空,则默认显示为0
    147. if (isempty(input))
    148. set(hObject,'String','0')
    149. end
    150. guidata(hObject, handles);
    151. % --- Executes during object creation, after setting all properties.
    152. function edit4_CreateFcn(hObject, eventdata, handles)
    153. % hObject handle to edit4 (see GCBO)
    154. % eventdata reserved - to be defined in a future version of MATLAB
    155. % handles empty - handles not created until after all CreateFcns called
    156. % Hint: edit controls usually have a white background on Windows.
    157. % See ISPC and COMPUTER.
    158. if ispc
    159. set(hObject,'BackgroundColor','white');
    160. else
    161. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    162. end
    163. function edit5_Callback(hObject, eventdata, handles)
    164. % hObject handle to edit5 (see GCBO)
    165. % eventdata reserved - to be defined in a future version of MATLAB
    166. % handles structure with handles and user data (see GUIDATA)
    167. % Hints: get(hObject,'String') returns contents of edit5 as text
    168. % str2double(get(hObject,'String')) returns contents of edit5 as a double
    169. %以字符串的形式来存储数据文本框5的内容。如果字符串不是数字,则现实空白内容
    170. input =str2double(get(hObject,'String'));
    171. %检查输入是否为空. 如果为空,则默认显示为0
    172. if (isempty(input))
    173. set(hObject,'String','0')
    174. end
    175. guidata(hObject, handles);
    176. % --- Executes during object creation, after setting all properties.
    177. function edit5_CreateFcn(hObject, eventdata, handles)
    178. % hObject handle to edit5 (see GCBO)
    179. % eventdata reserved - to be defined in a future version of MATLAB
    180. % handles empty - handles not created until after all CreateFcns called
    181. % Hint: edit controls usually have a white background on Windows.
    182. % See ISPC and COMPUTER.
    183. if ispc
    184. set(hObject,'BackgroundColor','white');
    185. else
    186. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    187. end
    188. % --- Executes on selection change in popupmenu1.
    189. function popupmenu1_Callback(hObject, eventdata, handles)
    190. % hObject handle to popupmenu1 (see GCBO)
    191. % eventdata reserved - to be defined in a future version of MATLAB
    192. % handles structure with handles and user data (see GUIDATA)
    193. % Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
    194. % contents{get(hObject,'Value')} returns selected item from popupmenu1
    195. popup_sel_index = get(handles.popupmenu1, 'Value');
    196. popup_sel_index = get(handles.popupmenu1, 'Value');
    197. switch popup_sel_index
    198. case 1
    199. set(handles.text10 ,'String','期望极点');
    200. set(handles.text11 ,'String',' -10 -10');
    201. set(handles.text12 ,'String','-2+2*sqrt(3)*i -2-2*sqrt(3)*i');
    202. case 2
    203. set(handles.text10 ,'String','加权矩阵');
    204. set(handles.text11 ,'String','Q=[1 0 0 0; 0 0 0 0; 0 0 1 0;0 0 0 0];');
    205. set(handles.text12 ,'String',' R=1');
    206. case 3
    207. set(handles.text10 ,'String','参考输入');
    208. set(handles.text11 ,'String',' y_r=0.2*U(t)');
    209. set(handles.text12 ,'String',' ');
    210. case 4
    211. set(handles.text10 ,'String','PID');
    212. set(handles.text11 ,'String',' P=5,I=0.001,D=1');
    213. set(handles.text12 ,'String',' ');
    214. end
    215. % --- Executes during object creation, after setting all properties.
    216. function popupmenu1_CreateFcn(hObject, eventdata, handles)
    217. % hObject handle to popupmenu1 (see GCBO)
    218. % eventdata reserved - to be defined in a future version of MATLAB
    219. % handles empty - handles not created until after all CreateFcns called
    220. % Hint: popupmenu controls usually have a white background on Windows.
    221. % See ISPC and COMPUTER.
    222. if ispc
    223. set(hObject,'BackgroundColor','white');
    224. else
    225. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    226. end
    227. % --- Executes on button press in pushbutton1.
    228. function pushbutton1_Callback(hObject, eventdata, handles)
    229. % hObject handle to pushbutton1 (see GCBO)
    230. % eventdata reserved - to be defined in a future version of MATLAB
    231. % handles structure with handles and user data (see GUIDATA)
    232. warning off
    233. M = str2double(get(handles.edit1, 'String'));%小车质量
    234. m = str2double(get(handles.edit2, 'String'));%摆杆质量
    235. b = str2double(get(handles.edit3, 'String'));%小车阻尼
    236. I = str2double(get(handles.edit4, 'String'));%摆杆转动惯量
    237. l = str2double(get(handles.edit5, 'String'));%摆杆长度
    238. g=9.8; %重力加速度
    239. %%%%%%%%%%%%%%%%%%%%%%%%计算系统状态矩阵%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    240. A=[ 0 1 0 0;...
    241. (M+m)*m*g*l/((M+m)*I+M*m*l^2) 0 0 m*l*b/((M+m)*I+M*m*l^2);...
    242. 0 0 0 1;...
    243. -m^2*l^2*g/((M+m)*I+M*m*l^2) 0 0 -(I+m*l^2)*b/((M+m)*I+M*m*l^2)];
    244. B=[0;-m*l/(((M+m)*I+M*m*l^2));0;(I+m*l^2)/((M+m)*I+M*m*l^2)];
    245. C=[0 0 1 0;1 0 0 0];
    246. D=zeros(2,1);
    247. E=zeros(4,1);
    248. %%%%%%%%%%%%%%%%%%%%将状态矩阵转存到状态空间%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    249. assignin('base','A',A);
    250. assignin('base','B',B);
    251. assignin('base','C',C);
    252. assignin('base','D',D);
    253. assignin('base','E',E);
    254. %%%%%%%%%%%%%%%%%%%%%%选择倒立摆的控制方式%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    255. popup_sel_index = get(handles.popupmenu1, 'Value');
    256. switch popup_sel_index
    257. case 1 %%极点配置法
    258. Qc=ctrb(A,B);
    259. EA=[-10 0 0 0;...
    260. 0 -10 0 0;...
    261. 0 0 -2-2*sqrt(3)*i 0;...
    262. 0 0 0 -2+2*sqrt(3)*i];
    263. PP=polyvalm(poly(EA),A);
    264. Ks=[0 0 0 1]*inv(Qc)*PP; %%计算反馈矩阵
    265. [t,x,y]=sim('jwgcqp.mdl'); %%调用降维观测器的simulink模块
    266. axes(handles.axes1);
    267. plot(t,y(:,1));
    268. axes(handles.axes2);
    269. plot(t,y(:,3));
    270. axes(handles.axes3)
    271. plot([-0.02;0.02],[-0.8;-0.8],'color','k','linestyle','-','linewidth',2);
    272. axis([-0.02 0.02 -1.2 1.2]);
    273. car1=line([-0.004;0.004],[-0.5;-0.5],'color','k','linestyle','-','erasemode','xor','linewidth',25);
    274. car21=line(-0.004,-0.72,'color','k','linestyle','-','erasemode','xor','markersize',40);
    275. car22=line( 0.004,-0.72,'color','k','linestyle','-','erasemode','xor','markersize',40);
    276. pendulum1=line([0;0],[-0.3;0.6],'color','r','linestyle','-','erasemode','xor','linewidth',10);
    277. pendulum2=line(0,-0.27,'color','g','linestyle','-','erasemode','xor','markersize',30);
    278. for s=1:length(t)
    279. set(car21,'xdata',-0.004+y(s,3),'ydata',-0.72);
    280. set(car22,'xdata', 0.004+y(s,3),'ydata',-0.72);
    281. set(car1,'xdata',[-0.004+y(s,3);0.004+y(s,3)],'ydata',[-0.5;-0.5]);
    282. set(pendulum2,'xdata',y(s,3),'ydata',-0.27);
    283. set(pendulum1,'xdata',[y(s,3);sin(y(s,1))+y(s,3)],'ydata',[-0.3;-0.3+cos(y(s,1))]);
    284. drawnow;
    285. end
    286. case 2 %%LQR最优控制器
    287. QQ=diag([1,0,1,0]);
    288. RR=1;
    289. Ks=lqr(A,B,QQ,RR); %%计算反馈矩阵
    290. [t,x,y]=sim('jwgcql.mdl'); %%调用降维观测器的simulink模块
    291. % figure(2)
    292. % subplot(4,2,1)
    293. % plot(t,y(:,1))
    294. % title('系统状态变化')
    295. % subplot(4,2,2)
    296. % plot(t,y(:,5))
    297. % title('降维观测器结果')
    298. % subplot(4,2,3)
    299. % plot(t,y(:,2))
    300. % subplot(4,2,4)
    301. % plot(t,y(:,6))
    302. % subplot(4,2,5)
    303. % plot(t,y(:,3))
    304. % subplot(4,2,6)
    305. % plot(t,y(:,7))
    306. % subplot(4,2,7)
    307. % plot(t,y(:,4))
    308. % subplot(4,2,8)
    309. % plot(t,y(:,8))
    310. axes(handles.axes1);
    311. plot(t,y(:,1));
    312. axes(handles.axes2);
    313. plot(t,y(:,3));
    314. axes(handles.axes3);
    315. plot([-0.3;0.3],[-0.8;-0.8],'color','k','linestyle','-','linewidth',2);
    316. axis([-0.3 0.3 -1.2 1.2]);
    317. car1=line([-0.06;0.06],[-0.5;-0.5],'color','k','linestyle','-','erasemode','xor','linewidth',25);
    318. car21=line(-0.06,-0.72,'color','k','linestyle','-','erasemode','xor','markersize',40);
    319. car22=line( 0.06,-0.72,'color','k','linestyle','-','erasemode','xor','markersize',40);
    320. pendulum1=line([0;0],[-0.3;0.6],'color','r','linestyle','-','erasemode','xor','linewidth',10);
    321. pendulum2=line(0,-0.27,'color','g','linestyle','-','erasemode','xor','markersize',30);
    322. for s=1:length(t)
    323. set(car21,'xdata',-0.06+y(s,3),'ydata',-0.72);
    324. set(car22,'xdata', 0.06+y(s,3),'ydata',-0.72);
    325. set(car1,'xdata',[-0.06+y(s,3);0.06+y(s,3)],'ydata',[-0.5;-0.5]);
    326. set(pendulum2,'xdata',y(s,3),'ydata',-0.27);
    327. set(pendulum1,'xdata',[y(s,3);sin(y(s,1))+y(s,3)],'ydata',[-0.3;-0.3+cos(y(s,1))]);
    328. drawnow;
    329. end
    330. case 3
    331. % A=[ 0 1 0 0;...
    332. % (M+m)*m*g*l/((M+m)*I+M*m*l^2) 0 0 m*l*b/((M+m)*I+M*m*l^2);...
    333. % 0 0 0 1;...
    334. % -m^2*l^2*g/((M+m)*I+M*m*l^2) 0 0 -(I+m*l^2)*b/((M+m)*I+M*m*l^2)];
    335. % B=[0;-m*l/(((M+m)*I+M*m*l^2));0;(I+m*l^2)/((M+m)*I+M*m*l^2)];
    336. % C=[0 0 1 0;1 0 0 0];
    337. % p=[-10,-7,-1.901,-1.9];
    338. % Kysw=place(A,B,p);
    339. [x,y]=sim('pedulumpid.mdl');
    340. axes(handles.axes1);
    341. plot(t,y(:,1));
    342. axes(handles.axes2);
    343. plot(t,y(:,3));
    344. axes(handles.axes3);
    345. plot([-0.3;0.3],[-0.8;-0.8],'color','k','linestyle','-','linewidth',2);
    346. axis([-0.3 0.3 -1.2 1.2]);
    347. car1=line([-0.06;0.06],[-0.5;-0.5],'color','k','linestyle','-','erasemode','xor','linewidth',25);
    348. car21=line(-0.06,-0.72,'color','k','linestyle','-','erasemode','xor','markersize',40);
    349. car22=line( 0.06,-0.72,'color','k','linestyle','-','erasemode','xor','markersize',40);
    350. pendulum1=line([0;0],[-0.3;0.6],'color','r','linestyle','-','erasemode','xor','linewidth',10);
    351. pendulum2=line(0,-0.27,'color','g','linestyle','-','erasemode','xor','markersize',30);
    352. for s=1:length(t)
    353. set(car21,'xdata',-0.06+y(s,3),'ydata',-0.72);
    354. set(car22,'xdata', 0.06+y(s,3),'ydata',-0.72);
    355. set(car1,'xdata',[-0.06+y(s,3);0.06+y(s,3)],'ydata',[-0.5;-0.5]);
    356. set(pendulum2,'xdata',y(s,3),'ydata',-0.27);
    357. set(pendulum1,'xdata',[y(s,3);sin(y(s,1))+y(s,3)],'ydata',[-0.3;-0.3+cos(y(s,1))]);
    358. drawnow;
    359. end
    360. end
    361. % --- Executes on button press in pushbutton4.
    362. function pushbutton4_Callback(hObject, eventdata, handles)
    363. % hObject handle to pushbutton4 (see GCBO)
    364. % eventdata reserved - to be defined in a future version of MATLAB
    365. % handles structure with handles and user data (see GUIDATA)
    366. pause;
    367. % --- Executes on button press in pushbutton5.
    368. function pushbutton5_Callback(hObject, eventdata, handles)
    369. % hObject handle to pushbutton5 (see GCBO)
    370. % eventdata reserved - to be defined in a future version of MATLAB
    371. % handles structure with handles and user data (see GUIDATA)
    372. close(gcbf);
    373. clc,clear,close all
    374. A87

    4.完整MATLAB

    V

  • 相关阅读:
    浅拷贝和深拷贝?仔细搞懂你
    [H5动画制作系列 ] Text及Button 的基础原理Demo
    如何在 Windows 上安装 ONLYOFFICE 协作空间社区版
    redis持久化
    海智算法训练营第三十一天 | 第八章 贪心算法 part02 | ● 122.买卖股票的最佳时机II ● 55. 跳跃游戏 ● 45.跳跃游戏II
    VLAN通讯
    【机器学习】阿里云天池竞赛——工业蒸汽量预测(5)
    设计模式:访问者模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)
    面对多种信号干扰,如何实现高效干扰测试?
    JavaSe-JAVA的多态
  • 原文地址:https://blog.csdn.net/hlayumi1234567/article/details/128177722