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

📄 piha.m

📁 一个matlab的将军模型
💻 M
📖 第 1 页 / 共 4 页
字号:
  parent_chart = sf('get',q(k),'.chart');
  transitionsAll = sf('get',parent_chart,'.transitions');
  qk_transitions = sf('find',transitionsAll,'.src.id',q(k));
  if ~isempty(qk_transitions)
    result = 0;
    break;
  end
end
return

%------------------------------------------------------------------------------
function [total_expression,event_expression,condition_expression] = find_cond_expr(sfdata,cond_expr,event_expr,sf_machine)

%This function will take the condition expression and represent it as
%a logical function in terms of the PTHB's

data=sfdata{sf_machine}.InputData;
condition_expression=cond_expr;
if ~isempty(cond_expr)
    if cond_expr(1)==' '|cond_expr(length(cond_expr))==' '
        error('Stateflow condition xpressions cannot have leading or trailing spaces.')    
    end
end
total_expression=cond_expr;

ok=0;
for i=1:length(data)
   name=data{i}.Name;
   where=findstr(name,cond_expr);
   if (~isempty(where))&length(cond_expr)>=length(name)
   replace_at=[];
for n=where
      
      %The following code only checks to see if the character before and after the 
      %matched strings are non-characters.  This protects against the following case:
      %cond_expr='region1==1'  and sfdata.name='region'
      %******************************************************************************
   if n>1&(n+length(name))<=length(cond_expr)
      if ~(isletter(cond_expr(n-1))|isletter(cond_expr(n+length(name)))|...
               ~isempty(findstr(cond_expr(n-1),'0123456789'))|...
               ~isempty(findstr(cond_expr(n+length(name)),'0123456789')))
         replace_at=[replace_at n];
      end
   elseif (n+length(name))<=length(cond_expr)
		   if ~(isletter(cond_expr(n+length(name)))|~isempty(findstr(cond_expr(n+length(name)),'0123456789')))
            replace_at=[replace_at n];
         end
   elseif n>1
      if ~(isletter(cond_expr(n-1))|~isempty(findstr(cond_expr(n-1),'0123456789')))
         replace_at=[replace_at n];
      end
      
   end %end if where(...
		%******************************************************************************   
        
end %end for n=where

    %Perform the all replacements necessary for the 'name' condition
    index_old=1;
    condition_expression_new=[];
    for j=1:length(replace_at)
            condition_expression_new = [condition_expression_new condition_expression(index_old:replace_at(j)-1)];                
            condition_expression_new = [condition_expression_new data{i}.Expression];
            index_old=replace_at(j)+length(name);
    end
    condition_expression_new = [condition_expression_new condition_expression(index_old:length(condition_expression))];
    condition_expression = condition_expression_new;
    cond_expr=condition_expression;

end %end if (~isempty...
   
  end %end for i=1:...
  
  event_expression=[];
  %Now concatenate the event expression onto the condition expression:
  for i=1:length(sfdata{sf_machine}.InputEvent)
     input_expression=sfdata{sf_machine}.InputEvent{i}.Expression;
     input_event_name=sfdata{sf_machine}.InputEvent{i}.Name;
     if strcmp(deblank(input_event_name),deblank(event_expr))
        if ~isempty(condition_expression)
        	total_expression=strcat('(',input_expression,')&(',condition_expression,')');
        else
         total_expression=input_expression;
        end   
        event_expression=input_expression;
         break;
     end
     
  end
  



return

% -----------------------------------------------------------------------------

function [event_expr,cond_expr,clock,reset_flag] = process_label_string(labelString,sfdata,sf_machine,clockHandle)

% Assume that the condition expression lies between brackets
% (this should have been checked previously).
%Also assume that if there is a clock associated with this transition, it
%appears as the first element in the expression and that it is 'and'ed 
%with the rest of the expression.  
%Example: [(clock1==1)&((A==1)&B==1))]
clock=[];
no_condition=0;
start = findstr(labelString,'[');

%Find where the reset event starts (if there is one)
reset_start=findstr(labelString,'/');
reset_flag=0;

if ~isempty(start)
   event_expr = labelString(1:start-1);
elseif ~isempty(reset_start)
   event_expr = labelString(1:reset_start-1);
else 
   event_expr= labelString;   
end

% Test to see if the SF machine has event inputs but this transition
% does not.  Added by JPK (11/2002).
if isempty(event_expr)&~isempty(sfdata{sf_machine}.InputEvent)
   error(['Stateflow machine has event inputs, but at least one transition '...
   'does not have an event associated with it.']);
end

if ~isempty(reset_start)
if ~isempty(findstr('reset',labelString(reset_start+1:length(labelString))))
         reset_flag=1;
else
         error('Invalid reset event.')
end
end


  stop = findstr(labelString,']');
  is_clock=0;
  
  %Test for a clocked event in the event section of the labelstring
  for i=1:length(sfdata{sf_machine}.Clock)
     name=sfdata{sf_machine}.Clock{i}.Name;
     name=deblank(name);
     is_clock=strcmp(name,event_expr);
     if is_clock
        event_expr=[];
        zoh_block=sfdata{sf_machine}.Clock{i}.ClockHandle;           
        [dum1,dum2,clock]=intersect(zoh_block,clockHandle);
        break;
     end
  end
 
 cond_expr = labelString(start+1:stop-1);

 
  
return

% ----------------------------------------------------------------------------

function machine_id = get_machine_id(sys)

% the Stateflow function returns all block names whose begining substring
% matches with the name given in sys. thus, we have to make sure that we get
% an exact match.

possible_id = sf('find','all','machine.name',sys);
machine_id = [];
for k = 1:length(possible_id)
  name = sf('get',possible_id(k),'.name');
  if strcmp(name,sys)
    machine_id = possible_id(k);
    break;
  end
end
return

% ----------------------------------------------------------------------------

function chart_id = find_chart_id(machine_id,block_name)

possible_id = sf('find','all','chart.machine',machine_id, ...
    'chart.name',block_name);
chart_id = [];
for l = 1:length(possible_id)
  name = sf('get',possible_id(l),'.name');
  if strcmp(name,block_name)
    chart_id = possible_id(l);
    break;
  end
end
return

% ----------------------------------------------------------------------------

function X0 = get_initial_continuous_set(scsbH)

% Get initial continuous set (ICS) for each SCS block. Each ICS is a cell
% array of @linearcon objects.
ICS = {};
for k = 1:length(scsbH)
  ICS{k} = evalin('base',get_param(scsbH(k),'ICS'));
  if (length(ICS{k}) < 1)
    blockname = get_param(scsbH(k),'name');
    error(['Invalid initial continuous set specified for block ''' ...
	blockname '''.'])
  end
end

X0 = {};
idx = ones(1,length(scsbH));
stop = 0;
while ~stop
  % Compose combinations of overall ICS from ICS for each SCS block.
  ICS_CE = []; ICS_dE = [];
  ICS_CI = []; ICS_dI = [];
  for k = 1:length(scsbH)
    [CEk,dEk,CIk,dIk] = linearcon_data(ICS{k}{idx(k)});
    ICS_CE = [           ICS_CE            zeros(size(ICS_CE,1),size(CEk,2))
      zeros(size(CEk,1),size(ICS_CE,2))           CEk               ];
    ICS_dE = [ICS_dE; dEk];		 
    ICS_CI = [           ICS_CI            zeros(size(ICS_CI,1),size(CIk,2))
      zeros(size(CIk,1),size(ICS_CI,2))           CIk               ];
    ICS_dI = [ICS_dI; dIk];
  end
  % Put each combination into the cell array
  X0{length(X0)+1} = linearcon(ICS_CE,ICS_dE,ICS_CI,ICS_dI);

  % Increment the combination index
  k = length(scsbH);
  while (k >= 1)
    idx(k) = idx(k) + 1;
    if (idx(k) > length(ICS{k}))
      idx(k) = 1;
      k = k - 1;
    else
      k = -1;
    end
  end
  stop = (k == 0);
end

return

% ----------------------------------------------------------------------------

function AR = get_analysis_region(scsbH)

% Compose overall analysis region from analysis region for each SCS block.
CAR = [];
dAR = [];
for k = 1:length(scsbH)
  ARk = evalin('base',get_param(scsbH(k),'AR'));
  [CE,dE,Ck,dk] = linearcon_data(ARk);
  if ~isempty(CE) | ~isempty(dE)
    blockname = get_param(scsbH(k),'name');
    error(['Invalid analysis region for block ''' ...
	  blockname '''.'])
  end
  CAR = [          CAR                zeros(size(CAR,1),size(Ck,2))
    zeros(size(Ck,1),size(CAR,2))           Ck                ];
  dAR = [dAR; dk];		 
end
AR = linearcon([],[],CAR,dAR);
return

% ----------------------------------------------------------------------------

function state_number = get_state_number(state_id)

% We assume the label strings in the Stateflow diagram have the following 
% format.
%
%      <state_name>
%      entry: u = <state_number>;
%

labelString = sf('get',state_id,'.labelString');
% Skip all the white spaces at the end.
k = length(labelString);
while (k >= 1) & (isspace(labelString(k)) | (labelString(k) == ';'))
  k = k - 1;
end
% Search backwards until a non-numeric character is found.
labelString = labelString(1:k);
while (k >= 1) & (labelString(k) >= '0') & (labelString(k) <= '9')
  k = k - 1;
end
labelString = labelString(k+1:length(labelString));
state_number = str2num(labelString);
return

% ----------------------------------------------------------------------------

function state_number = get_labeled_state(state_id)

state_number = [];
for k = 1:length(state_id)
  state_number(k) = get_state_number(state_id(k));
end
return

% ---------------------------------------------------------------------------

function icell = find_initial_cells(X0,locations,AR)

%This function intersects linearcon object representing one piece of ICS with
%each CELL in invariant location

global CELLS

icell=[];
initial_invariant=locations.interior_cells;

if ~issubset(X0,AR)
    fprintf(['\nSome part of the initial continuous set is outside of the analysis region.  \nPress any key to continue anyhow.\n'])
    pause;
end

for i=1:length(initial_invariant)
	[C,d] = cell_ineq(CELLS{initial_invariant(i)}.boundary,CELLS{initial_invariant(i)}.hpflags);
	INV = linearcon([],[],C,d);
	if isfeasible(X0,INV)
   	icell=[icell initial_invariant(i)];
	end
end

%Test to see if any of the initial continuous set either a.) satifies a guard or b.) is outside of the 
%analysis region.
transitions=locations.transitions;
guards=[];
for k=1:length(transitions)
   guards=union(guards,transitions{k}.guard);
end

for i=1:length(guards)
	[C,d] = cell_ineq(CELLS{guards(i)}.boundary,CELLS{guards(i)}.hpflags);
	GUARD = linearcon([],[],C,d);
	if isfeasible(X0,GUARD)
      fprintf(['Some section of the initial continuous set satisfies' ... 
               'the guard \nof the initial location.'...
      	' This section will be neglected.\n\n  Press any key to continue.'])
    pause;

⌨️ 快捷键说明

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