programing in Simulink a lamp

조회 수: 5 (최근 30일)
ali
ali 2021년 9월 4일
댓글: Walter Roberson 2025년 1월 28일
i need the lamp to display different color when it gets certain value from the matlab function. i add another Matlab Function to program the lamp but i am not sure if that such a thing to exist.
this is simulink. Here is my code that is in the Matlab Function to program the lamp as you see how i connect the lamp to the function. the code below is the code inside the matlab function. i am not even sure if my code is right at all.
function lampStates = caution(distance)
lampState1.Value = ( distance >= 0.0) && ( distance <= 0.5);
lampState2.Value = ( distance == 0.5);
lampState3.Value = ( distance == 1.5);
lampState4.Value = ( distance == 2.5);
lampState5.Value = ( distance >= 2.5);
if ( distance >= 0.0) && ( distance <= 0.5)
lampState1.Color = [254 51 10];
elseif ( distance == 0.5)
lampState2.Color = [237 177 32];
elseif ( distance == 1.5)
lampState3.Color = [100 212 19];
elseif ( distance == 2.5)
lampState4.Color = [237 177 32];
elseif ( distance >= 2.5)
lampState5.Color = [254 51 10];
end
lampStates = [lampState1 lampState2 lampState3 lampState4 lampState5];
  댓글 수: 1
Walter Roberson
Walter Roberson 2025년 1월 28일
lampState2.Value = ( distance == 0.5);
0.5 (and 1.5, and 2.5) are values that can be exactly represented in double precision. It is not uncommon that floating point calculations might end up with exact 0.5 or 1.5 or 2.5.
However... it is also not uncommon for floating point calcuations to end up with (say) 1.4999999999999966693309261245303787291049957275390625 instead of 1.5 exactly . So it is often not a good idea to test for 0.5, 1.5, 2.5 exactly but to instead test for ranges, or test for values within a tolerance. For example,
if distance > 1.25 & distance <= 1.75
or
if abs(distance - 1.5) < 1e-7
or (as of R2024b)
if isapprox(distance, 1.5)

댓글을 달려면 로그인하십시오.

답변 (1개)

Kanishk
Kanishk 2025년 1월 28일
Hi @ali
I understand you want to program the states and color of the Simulink lamp to visualize "distance" signal in Simulink dashboard itself. You can indeed use Simulink Lamp for this purpose but you do not need "MATLAB Function Block". You can write a script to define the "States" and "colors" of the lamp block.
To define the states for the Lamp block, you need set the ranges of values you want a color to be displayed. Here is a simple code to set the "States" and "Colors" of the Lamp block programmatically.
lampState1.Value = [0.0 0.5];
lampState2.Value = [0.5 1.5];
lampState3.Value = [1.5 2.5];
lampState4.Value = [2.5 inf];
lampState1.Color = [254 51 10]/256;
lampState2.Color = [237 177 32]/256;
lampState3.Color = [100 212 19]/256;
lampState4.Color = [237 177 32]/256;
lampStates = [lampState1 lampState2 lampState3 lampState4];
set_param(gcs+"/Lamp1", "StateValueType", "Range")
set_param(gcs+"/Lamp1", "StateColors", lampStates)
You can execute this code in MATLAB after opening the Simulink model or add this code in the "InitFcn" model callback using the Property Inspector.
You can learn more how to add code to model callbacks in Simulink following this official MathWorks documentation.

카테고리

Help CenterFile Exchange에서 Interactive Model Editing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by