Simulink: Matlab function block for Charge controller of a solar home system.
2 次查看(过去 30 天)
显示 更早的评论
I am modelling a solar home system, with a battery protection system or charge controller, which controls 2 switches depending on the voltage of the battery.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/233081/image.jpeg)
% This is the code for the functional block, kindly read the comments
function [Sw_A, Sw_B] = fcn(v1,v2,v3,v4,v_bat,i_bat)% sw A is PV sw and SW_B is load switch
while i_bat < 0 % battery is discharging hence curent is negative
if v_bat < v1 % 1. if battery voltage is too low, load is cut off
Sw_B = 0;
elseif v_bat < v3 % 4. Battery is discharged into load and voltage is safely low, reconnect pv
Sw_A = 1;
end
while i_bat > 0 % Battery is charging
if v_bat > v2 % 2. if voltage is raised high enough, the load is reconnected
Sw_B = 1;
elseif v_bat > v4 % 3. Battery is being overcherged, cut off PV switch
Sw_A = 0;
end
end
This function is showing multiple errors, I can only assume I have done some silly rookie mistakes. Any help will be greartly appreciated. if you require any more data please ask.
![jh.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/233082/jh.jpeg)
0 个评论
采纳的回答
Fangjun Jiang
2019-8-7
Keep in mind that the MATLAB Function block executes once at every simulation step. So I don't think you should use the while-loop. It will cause the block thus the whole Simulink model runs for ever. Use if-else statement instead. Also, make sure "end" statement is provided to match the "if" statement.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Sources 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!