📄 pwm_cmd.m
字号:
function pwm_cmd
% Produces the command for use by the PWM generator
pwm_glbl % Global definitions
% The 'next-state'is maintained as a separate variable so that the
% initialization canbe handled correctly and so a test can be
% constructed to tell whether the entry function of a state
% should be run. The next-state variable must be static so it
% remembered across function invocations. The 'global' designation
% is the only way Matlab has of making a variable static.
% The default value of the next-state variable is -1, indicating
% that no state change has taken place.
global Spwm_cmd_next % Next state for this task (other local statics
% can be listed here.
% These aren't really global, but do have to be 'static'
% Matlab doesn't have a 'static' declaration so this will have to do!
if Spwm_cmd == 0
% Initialization section - occurs once only
Spwm_cmd = 1; % Make sure this section is not executed again!
Spwm_cmd_next = 1; % First state.
%The default is -1 which indicates stay in same state
return;
end
% This task runs at specific times. If current time has not reached
% the scheduled run time, just return.
if tstep < Tpwm_cmd
return;
end
Tpwm_cmd = Tpwm_cmd + Tcmd_delt; % Time for next run -- in this case
% the task runs at regular intervals, but that needn't
% be true all the time.
if Spwm_cmd_next ~= -1
% There has been a state change (including self-transition)
[i_audit,trans_trail] = audit(1,Spwm_cmd,Spwm_cmd_next,...
tstep,trans_trail,i_audit);
% Record this transition
Spwm_cmd = Spwm_cmd_next;
run_entry = 1;
Spwm_cmd_next = -1; % Default - stay in same state
else
run_entry = 0;
end
% This task has only one state -- it produces a command duty cycle as
% a function of time.
% Run the code associated with the current state
if Spwm_cmd == 1
% Entry section
% No entry section for pwm_cmd
% Action section
duty_cycle = 0.5 * sin(tstep * 2 * pi / cmd_period) + 0.5;
% Result is in range 0->1
%Test/exit section
% This task has only one state - no transitions!
else % Check for a state variable value that doesn't
% to a defined state.
error('Task: pwm_cmd -- unknown state encountered');
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -