Creating a simulink_hmi_blocks/Lamp programatically, bug?

4 views (last 30 days)
Hi,
I am creating a simple model including a step function (min = 0, max = 1) connected to a gain. This gain is then connected to a terminator. The main purpose is to try out the simulink_hmi_blocks/Lamp, and hopefully get it to show red for a value = 1, and green for a value of 0. This is tested by setting the gain to either 0 or 1.
The model runs, and the lamp is working. What is frustating though is the fact that there seems to be some kind of bug with the lamp. Even though it is connected, a text "Double-click to connect" is shown on the lamp as well as the lamp is grayed. Note that when I pick the gain block manually from the lamp properties the text is removed and the gray shading is lost? It would be nice to have this running 100% from the script without manually having to edit the block parameters to get the lamp to work. Anyone? The code is shown below
:
%------------------------------------------------------------------------------------------------------------------------
function build_lamp_model()
model_name = 'build_lamp';
open_system(new_system(model_name));
% Add step
step = add_block('simulink/Sources/Step',[model_name '/' 'step']);
h_step = get_param(step,'PortHandles');
dim = get_param(step,'Position'); % use these dimension to position the other blocks
% Add gain block
gain = add_block('simulink/Math Operations/Gain',[model_name '/' 'gain']);
h_gain = get_param(gain,'PortHandles');
lamp_param = Simulink.HMI.SignalSpecification;
lamp_param.BlockPath = Simulink.BlockPath([model_name '/' 'gain']);
set_param(gain,'Position',[dim(1)+100,dim(2),dim(3)+100,dim(4)])
% Connect line between step and gain
step_gain_line = add_line(model_name,h_step.Outport,h_gain.Inport);
% add terminator of gain block
terminator = add_block('simulink/Sinks/Terminator',[model_name '/' 'terminator']);
h_terminator = get_param(terminator,'PortHandles');
set_param(terminator,'Position',[dim(1)+200,dim(2),dim(3)+200,dim(4)])
% Connect line between gain and terminator
gain_terminator_line = add_line(model_name,h_gain.Outport,h_terminator.Inport);
% Add lamp
lamp = add_block('simulink_hmi_blocks/Lamp',[model_name '/' 'lamp']);
set_param([model_name '/' 'lamp'],'Binding',lamp_param)
lampState1.Value = 0;
lampState1.Color = [0 1 0];
lampState2.Value = 1;
lampState2.Color = [1 0 0];
lampStates = [lampState1 lampState2];
set_param([model_name '/' 'lamp'],'StateColors',lampStates);
set_param(lamp,'Position',[dim(1)+300,dim(2),dim(3)+300,dim(4)])
end
  2 Comments
Daniel Hansson
Daniel Hansson on 9 May 2021
I solved this by adding a short pause and then use set_param(gcb,'ShowInitialText','off'). This solved the issue.
Aditya Patil
Aditya Patil on 19 May 2021
Feel free to post your solution as answer, so that it is easier to find for people in future.

Sign in to comment.

Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!