⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ballbeam.m

📁 MATLAB 球形摆控制源程序,实用广
💻 M
📖 第 1 页 / 共 2 页
字号:
function ballbeam(action)

% BALLBEAM demonsrates Proportional-Derivative (PD) control as applied to a
% ball and beam demonstration experiment. Execute by calling with no arguments.
%
% P and PD controllers are implemented which manipulates the beam angle in
% response to the position of the ball on the beam. The setpoint is indicated
% by the red marker, and is adjusted using the slider at the bottom of the figure.
% The control gain, Kc, and the Derivative Time constant, Td, are adjusted by
% a sliders located in the right hand control panel.
%
% This demo has been tested on Matlab 5.3 and Matlab 6.0. Some of the user
% interface code was shamelessly ripped out of the standard Matlab demos.
%
% Jeff Kantor
% Version 1.0
% March 24, 2001

if nargin < 1
    action = 'initialize';
end

persistent ballradius beamlength beamwidth
persistent xmin xmax umin umax g dt 
persistent t x v u done
persistent Kc Td N xsp xlast dterm Ad Bd
persistent ballHndl xball yball 
persistent beamHndl xbeam ybeam
persistent markerHndl xmarker ymarker
persistent setpointHndl
persistent runHndl resetHndl stopHndl closeHndl infoHndl
persistent gainHndl gaintxtHndl gainlblHndl
persistent dervHndl dervtxtHndl dervlblHndl
persistent manualHndl modeHndl mode
persistent bbAxes plotAxes plotHndl legHndl tdata xdata xspdata

switch lower(action)
case {'initialize'}
    
    % Basic dimensions.
    
    ballradius = 1;
    beamlength = 20;
    beamwidth = 0.3;
    
    % System parameters
    
    xmin = 0;                   % Left limit
    xmax = beamlength;          % Right limit
    umin = -0.25;               % Minimum beam angle
    umax = 0.25;                % Maximum beam angle
    g = 0.05;                   % Gravitational Constant
    dt = 0.2;                   % Time Step

    % Set initial conditions for ball and beam position

    t = 0;                      % Initial time
    x = beamlength/2;           % Initial Ball position
    v = 0;                      % Initial Velocity
    u = 0;                      % Initial beam angle
    done = 0;                   % Done Flag

    % Setup controller with initial parameters

    mode = 1;                   % Flag, start in manual mode
                                %   Mode == 1 => Manual
                                %   Mode == 2 => Proportional
                                %   Mode == 3 => PD
    Kc = 0.1;                   % Proportional Gain
    Td = 20;                    % Derivative time constant
    N = 10;                     % Maximum derivative gain
    xsp = x;                    % Initial setpoint
    xlast = x;                  % Last measured value
    dterm = 0;                  % Derivative term
    Ad = Td/(Td+N*dt);          % Derivative filter constant
    Bd = Kc*Td*N/(Td+N*dt);     % Derivative gain
    
    % Create figure

    figNumber = figure( ...
        'Name','Ball & Beam Demo', ...
        'NumberTitle','off', ...
        'BackingStore','off');
    
    % Create axes to hold ball & beam animation

    bbAxes = axes(...
        'Units','normalized', ...
        'Position',[0.05 0.05 0.75 0.70],...
        'Visible','off',...
        'NextPlot','add');
    
    % Create axes to hold the plotting window
    
    plotAxes = axes(...
        'Units','normalized',...
        'Position',[0.10 0.70 0.65 0.25],...
        'Visible','off');
    
    % Information for all buttons
    labelColor=[0.8 0.8 0.8];
    yInitPos=0.90;
    xPos=0.85;
    btnWid=0.10;
    btnHt=0.08;
    % Spacing between the button and the next command's label
    spacing=0.025;
   
    %====================================
    % The CONSOLE frame
    frmBorder=0.02;
    yPos=0.05-frmBorder;
    frmPos=[xPos-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
    h=uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
        'BackgroundColor',[0.5 0.5 0.5]);

    %====================================
    % The START button
    btnNumber=1;
    yPos=0.90-(btnNumber-1)*(btnHt+spacing);

    runHndl=uicontrol( ...
      'Style','pushbutton', ...
      'Units','normalized', ...
      'Position',[xPos yPos-spacing btnWid btnHt], ...
      'String','Run', ...
      'Interruptible','on', ...
      'Callback','ballbeam(''run'');');
  
    %====================================
    % The RESET button
    btnNumber=2;
    yPos=0.90-(btnNumber-1)*(btnHt+spacing);
    
    resetHndl=uicontrol( ...
      'Style','pushbutton', ...
      'Units','normalized', ...
      'Position',[xPos yPos-spacing btnWid btnHt], ...
      'String','Reset', ...
      'Interruptible','on', ...
      'Callback','ballbeam(''reset'');');
  
    %====================================
    % The STOP button
    btnNumber=3;
    yPos=0.90-(btnNumber-1)*(btnHt+spacing);
   
    stopHndl=uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',[xPos yPos-spacing btnWid btnHt], ...
        'Enable','off', ...
        'String','Stop', ...
        'Callback','ballbeam(''stop'');');

    %====================================
    % The MODE popup button
    btnNumber=4;
    yPos=0.90-(btnNumber-1)*(btnHt+spacing);
    textStr='Mode';
    popupStr=['Manual';' P    ';' PD   '];
   
    % Generic button information
    btnPos1=[xPos yPos-spacing+btnHt/2 btnWid btnHt/2];
    btnPos2=[xPos yPos-spacing btnWid btnHt/2];
    popupHndl=uicontrol( ...
       'Style','text', ...
       'Units','normalized', ...
       'Position',btnPos1, ...
       'String',textStr);
    btnPos=[xPos yPos-spacing btnWid btnHt/2];
    modeHndl=uicontrol( ...
       'Style','popup', ...
       'Units','normalized', ...
       'Position',btnPos2, ...
       'String',popupStr,...
       'Value',mode,...
       'Callback','ballbeam(''mode'')');
   
    %====================================
    % The first slider -- Kc, the control gain
    btnNumber=5;
    yPos=0.90-(btnNumber-1)*(btnHt+spacing);

    gainHndl = uicontrol( ...
        'Style','slider', ...
        'Units','normalized', ...
        'Position',[xPos yPos-btnHt btnWid btnHt/2], ...
        'Min',0, ...
        'Max', 0.5, ...
        'Value', Kc, ...
        'Visible','off',...
        'Callback','ballbeam(''control'');');
    gainlblHndl = uicontrol( ...
        'Style','text', ...
        'String','Kc', ...
        'Units','normalized', ...
        'Visible','off',...
        'Position',[xPos yPos btnWid btnHt/2]);
    gaintxtHndl = uicontrol( ...
        'Style','text', ...
        'String',num2str(Kc), ...
        'Units','normalized', ...
        'Visible','off',...
        'Position',[xPos yPos-btnHt/2 btnWid btnHt/2]);
    if mode >= 2,
        set(gainlblHndl,'Visible','on');
        set(gaintxtHndl,'Visible','on');
    end

    %====================================
    % The second slider -- Td, the Derivative Time Constant
    btnNumber=6;
    yPos=0.90-(btnNumber-1)*(btnHt+spacing);

    dervHndl = uicontrol( ...
        'Style','slider', ...
        'Units','normalized', ...
        'Position',[xPos yPos-1.5*btnHt btnWid btnHt/2], ...
        'Min',0, ...
        'Max',50, ...
        'Value',Td,...
        'Visible','off',...
        'Callback','ballbeam(''control'')');
    dervlblHndl = uicontrol( ...
        'Style','text', ...
        'String','Td', ...
        'Units','normalized', ...
        'Visible','off',...
        'Position',[xPos yPos-btnHt/2 btnWid btnHt/2]);
    dervtxtHndl = uicontrol( ...
        'Style','text', ...
        'String',num2str(Td), ...
        'Units','normalized', ...
        'Visible','off',...
        'Position',[xPos yPos-btnHt btnWid btnHt/2]);
    if mode == 3,
        set(dervlblHndl,'Visible','on');
        set(dervtxtHndl,'Visible','on');
    end

   %====================================
   % The INFO button

    infoHndl=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'Position',[xPos 0.16 btnWid btnHt], ...
        'String','Info', ...
        'Callback','ballbeam(''info'')');

    %====================================
    % The CLOSE button
   
    closeHndl=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'Position',[xPos 0.05 btnWid btnHt], ...
        'String','Close', ...

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -