📄 animcpp1.m
字号:
set(signalH,'Tag','SourceSelect');
dispmodeH = upH(1);
% The value of signalH should match that of SL block
[winName] = bdroot(gcs);
signal_block = [winName, '/Target Position'];
signal_value = get_param(signal_block, 'waveform');
if strcmp(signal_value, 'sine'),
set(signalH, 'value', 1);
elseif strcmp(signal_value, 'square'),
set(signalH, 'value', 2);
elseif strcmp(signal_value, 'sawtooth'),
set(signalH, 'value', 3);
else
error('Unknown signal generator!');
end
constant_block = [winName, '/Constant'];
if str2double(get_param(constant_block, 'value')) < 0,
set(signalH, 'value', 4);
end
% ====== Appending handles as the second row of userdata
tmp = [signalH dispmodeH -1 -1 -1 -1 -1 -1 -1 -1];
set(AnimCppFigH, 'userdata', [get(AnimCppFigH, 'userdata'); tmp]);
% ###### animation objects ######
% ====== cart
cart_height = 0.2;
cart_length = 0.4;
cart = cart_length/2*[-1 1 1 -1 -1] + ...
j*(cart_height/2*[-1 -1 1 1 -1]-cart_height/2+.005);
cartH = patch(real(cart), imag(cart), 'm');
set(cartH, 'erase', 'xor');
set(cartH, 'userdata', cart);
% ====== pole 1
pole_length = 0.5;
pole_radius = 0.02;
pole1 = pole_radius*[-1 1 1 -1 -1] + ...
j*(pole_length/2*[-1 -1 1 1 -1]+pole_length/2+.003);
pole1H = patch(real(pole1), imag(pole1), 'r');
set(pole1H, 'erase', 'xor', 'clipping', 'off');
set(pole1H, 'userdata', pole1);
% ====== pole 2
pole_length = 1;
pole_radius = 0.02;
pole2 = pole_radius*[-1 1 1 -1 -1] + ...
j*(pole_length/2*[-1 -1 1 1 -1]+pole_length/2);
pole2H = patch(real(pole2), imag(pole2), 'y');
set(pole2H, 'erase', 'xor', 'clipping', 'off');
set(pole2H, 'userdata', pole2);
% ====== force arrow
force_x = [-1 0 nan -0.1 0 -0.1];
force_y = [0 0 nan 0.1 0 -0.1];
force = force_x + j*force_y;
forceH = line(real(force), imag(force), ...
'erase', 'xor', 'color', 'c', 'clip', 'off');
set(forceH, 'userdata', force, ...
'xdata', [0], 'ydata', [0]);
% ====== reference triangle
ref = cart_length/2*[-1 1 0 -1] + ...
j*cart_height*([0 0 1 0] - 1.1);
ref = ref - 2*j*cart_height;
refH = line(real(ref), imag(ref));
set(refH, 'color', 'g', 'linewidth', 2);
% refH = patch(real(ref), imag(ref), 'g');
set(refH, 'erase', 'background');
set(refH, 'userdata', ref);
% ====== axis settings
pos_range = [-2 2];
set(AnimCppAxisH, 'clim', [1 64], ...
'xlim', pos_range, ...
'ylim', [-cart_height 1*1.2], ...
'box', 'on');
axis equal;
set(AnimCppAxisH, 'visible', 'off');
% ====== colormap settings for floor
colormap(gray); % To assign correct patch color
patch([pos_range -pos_range], ...
cart_height*[-2 -2 -1 -1], [10 10 55 55]);
% ====== append the handles as third row of userdata
tmp = [cartH pole1H pole2H forceH refH -1 -1 -1 -1 -1];
set(AnimCppFigH, 'userdata', [get(AnimCppFigH, 'userdata'); tmp]);
% ====== change to normalized units
set(findobj(AnimCppFigH,'Units','pixels'), 'Units','normalized');
% ====== move the reference triangle by mouse
% action when button is first pushed down
action1 = [mfilename '([], [], [], [], ''mouse_action'')'];
% actions after the mouse is pushed down
action2 = action1;
% action when button is released
action3 = action1;
% temporary storage for the recall in the down_action
set(AnimCppAxisH,'UserData',action2);
% set action when the mouse is pushed down
down_action=[ ...
'set(AnimCppFigH,''WindowButtonMotionFcn'',get(AnimCppAxisH,''UserData''));' ...
action1];
set(AnimCppFigH,'WindowButtonDownFcn',down_action);
% set action when the mouse is released
up_action=[ ...
'set(AnimCppFigH,''WindowButtonMotionFcn'','' '');', action3];
set(AnimCppFigH,'WindowButtonUpFcn',up_action);
end
% ====== change labels of standard UI controls
tmp = get(AnimCppFigH, 'userdata');
set(tmp(1, 1), 'visible', 'off');
set(tmp(1, 2:3), 'visible', 'on');
closeHandle=findobj(AnimCppFigH, 'style', 'pushbutton', 'String', ' Close');
set(closeHandle, 'Enable', 'off');
sys = [0 0 0 6 0 0];
set(AnimCppFigH, 'HandleVisibility', 'on');
elseif nargin == 5, % for callbacks of GUI
% ###### standard UI controls
if strcmp(action, 'start_sl'),
tmp = get(AnimCppFigH, 'userdata');
set(tmp(1, 1), 'visible', 'off');
set(tmp(1, 2:3), 'visible', 'on');
closeHandle=findobj(AnimCppFigH, 'style', 'pushbutton', 'String', ' Close');
set(closeHandle, 'Enable', 'off');
[winName] = bdroot(gcs);
set_param(winName, 'SimulationCommand', 'start');
elseif strcmp(action, 'stop_sl'),
tmp = get(AnimCppFigH, 'userdata');
set(tmp(1, 1), 'visible', 'on');
set(tmp(1, 2:5), 'visible', 'off');
closeHandle=findobj(AnimCppFigH, 'style', 'pushbutton', 'String', ' Close');
set(closeHandle, 'Enable', 'on');
[winName] = bdroot(gcs);
set_param(winName, 'SimulationCommand', 'stop');
elseif strcmp(action, 'pause_sl'),
tmp = get(AnimCppFigH, 'userdata');
set(tmp(1, 3), 'visible', 'off');
set(tmp(1, 4:5), 'visible', 'on');
[winName] = bdroot(gcs);
set_param(winName, 'SimulationCommand', 'pause');
elseif strcmp(action, 'step_sl'),
[winName] = bdroot(gcs);
set_param(winName, 'SimulationCommand', 'step');
elseif strcmp(action, 'continue_sl'),
tmp = get(AnimCppFigH, 'userdata');
set(tmp(1, 3), 'visible', 'on');
set(tmp(1, 4:5), 'visible', 'off');
[winName] = bdroot(gcs);
set_param(winName, 'SimulationCommand', 'continue');
elseif strcmp(action, 'info'),
helpwin(mfilename);
% title = get(AnimCppFigH, 'Name');
% content = [];
% fhelpfun(title, content);
elseif strcmp(action, 'close'),
% [winName, sysName] = get_param;
% set_param(winName, 'Simulation running', 'stop');
delete(AnimCppFigH);
% ###### additional UI controls
elseif strcmp(action, 'target_pos'),
[winName] = bdroot(gcs);
signal_block = [winName, '/Target Position'];
constant_block = [winName, '/Constant'];
tmp = get(AnimCppFigH, 'userdata');
signalH = tmp(2, 1);
signal = get(signalH, 'value');
if signal == 1, % sinusoid wave
set_param(signal_block, 'wave', 'sine');
set_param(constant_block, 'value', '1');
elseif signal == 2, % square wave
set_param(signal_block, 'wave', 'square');
set_param(constant_block, 'value', '1');
elseif signal == 3, % saw wave
set_param(signal_block, 'wave', 'sawtooth');
set_param(constant_block, 'value', '1');
elseif signal == 4, % mouse-driven
set_param(constant_block, 'value', '-1');
else
error('Unknown wave option!');
end
elseif strcmp(action, 'show_trail'),
tmp = get(AnimCppFigH, 'userdata');
dispmodeH = tmp(2, 2);
objectH = tmp(3, 1:4);
dispmode = get(dispmodeH, 'value');
if dispmode == 0, % xor
set(objectH, 'erasemode', 'xor');
set(AnimCppFigH, 'color', get(AnimCppFigH, 'color'));
else
set(objectH, 'erasemode', 'none');
end
elseif strcmp(action, 'clear_trail'),
refresh(AnimCppFigH);
elseif strcmp(action, 'mouse_action')
[winName] = bdroot(gcs);
if str2double(get_param([winName, '/Constant'], 'value')) < 0,
curr_info = get(AnimCppAxisH, 'CurrentPoint');
tmp = get(AnimCppFigH, 'UserData');
refH = tmp(3, 5);
desired_pos = curr_info(1,1);
desired_pos = min(max(desired_pos, -1.5), 1.5);
ref = get(refH, 'userdata');
new_ref = ref + desired_pos;
set(refH, 'xdata', real(new_ref), 'ydata', imag(new_ref));
[windowname] = bdroot(gcs);
set_param([windowname, '/Target Position',13,'(Mouse-Driven)'],...
'value', num2str(desired_pos));
end
else
fprintf('Unknown action string: %s.\n', action);
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -