Recently came across a simple issues that I could not solve on my own. I have a Simulink model that uses a matlab function for some calculations inside the model. The idea is that at some specified moment of time I need to change the voltage of an electric drive. And I need to change it until the rotor’s position reaches another specified value. For instance:
If control_signal == 1; (command to start the execution);
While Angle ~= 180 \\ desired angle is 180;
Control voltage = 5 - 0.1 (5V is the initial value, while the increment of
the voltage change is 0.1)
End
end
So technically what I was thinking will happen, is that the cycle will be executed until the angle of 180 is reached, at some value of the control voltage (for instance 4.6). But when I am running this code, Simulink can’t execute the model. So without any warning or errors, simulation freezes at some stage (when the main condition kicks in). So looks like it can’t process further when the cycle’s execution starts. Can somebody help me with the code? Because the described behaviour of the model during simulation is definitely caused by the above mentioned code.
Thank you in advance.
My guess is you convert the value of the angle expressed in radians to degrees, and you calculate the angle beforehand using trigonometry functions - it will never reach precisely 180 degrees. You should choose some epsilon such that 180 - epsilon
is close enough to 180 for your application and use that value as your loop condition.
Also, based on your description of the problem you are trying to solve, sounds like you actually want to check whether the angle is not greater than 180, not if it equals 180.
Also, you are incrementing your voltage incorrectly. Everytime you enter the loop, your control_voltage variable equals 5 - 0.1, which means it's constant and will equal 4.9 no matter how many times you run that loop. This is the correct way:
control_voltage = 5; % initial value
if control_signal == 1; (command to start the execution);
while Angle ~= (180 - epsilon) % some epsilon of your choice
control_voltage = control_voltage - 0.1; % now it's smaller by 0.1 every time the loop is run
end
end