0
Below is the code in my matlab function

I am trying to get the minimum state of charge among the 4 battery cell and trying to balance the battery in order to get all the battery in the similar range of state of charge but cell battery 4 is not working fine, whereas the cell battery 1-3 is working fine.

function [y1, y2, y3, y4] = fcn(s1,s2,s3,s4)

s1 = int16(s1);
s2 = int16(s2);
s3 = int16(s3);
s4 = int16(s4);

a = min([s1 s2 s3 s4]); 

if(s1 == a) 

    if(s1 == a && s4 == a) 

        y1 = 0;
        y2 = 1;
        y3 = 1;
        y4 = 0;
        
    elseif(s1 == a && s3 == a)

        y1 = 0;
        y2 = 1;
        y3 = 0;
        y4 = 1;
        
    elseif(s1 == a && s2 == a)
        
        y1 = 0;
        y2 = 0;
        y3 = 1;       
        y4 = 1;
        
    elseif(s1 == a)

        y1 = 0;
        y2 = 1;
        y3 = 1;       
        y4 = 1;

    else

        y1 = 0;
        y2 = 0;
        y3 = 0;       
        y4 = 0;

    end
    
elseif(s2 == a)

    if(s2 == a && s4 == a)

        y1 = 1;
        y2 = 0;
        y3 = 1;        
        y4 = 0;

    elseif(s2 == a && s3 == a)

        y1 = 1;
        y2 = 0;
        y3 = 0;       
        y4 = 1;
        
    elseif(s2 == a && s1 == a)

        y1 = 0;
        y2 = 0;
        y3 = 1;       
        y4 = 1;

    elseif(s2 == a)

        y1 = 1;
        y2 = 0;
        y3 = 1;       
        y4 = 1;

    else

        y1 = 0;
        y2 = 0;
        y3 = 0;        
        y4 = 0;

    end

elseif(s3 == a)

    if(s3 == a && s4 == a)

        y1 = 1;
        y2 = 1;
        y3 = 0;        
        y4 = 0;

    elseif(s3 == a && s2 == a)

        y1 = 1;
        y2 = 0;
        y3 = 0;       
        y4 = 1;
        
    elseif(s3 == a && s1 == a)

        y1 = 0;
        y2 = 1;
        y3 = 0;       
        y4 = 1;

    elseif(s3 == a)

        y1 = 1;
        y2 = 1;
        y3 = 0;        
        y4 = 1;

    else

        y1 = 0;
        y2 = 0;
        y3 = 0;        
        y4 = 0;

    end 
    
elseif(s4 == a)

    if(s4 == a && s3 == a)

        y1 = 1;
        y2 = 1;
        y3 = 0;       
        y4 = 0;

    elseif(s4 == a && s2 == a)

        y1 = 1;
        y2 = 0;
        y3 = 1;        
        y4 = 0;
        
    elseif(s4 == a && s1 == a)

        y1 = 0;
        y2 = 1;
        y3 = 1;        
        y4 = 0;

    elseif(s4 == a)

        y1 = 1;
        y2 = 1;
        y3 = 1;        
        y4 = 0;

    else

        y1 = 0;
        y2 = 0;
        y3 = 0;
        y4 = 0;

    end 

else

    y1 = 0;
    y2 = 0;
    y3 = 0;
    y4 = 0;

Below is the Connection of passive cell balancing in Simulink

  • Where is the part that is related to C or C++? Also as C and C++ are very different languages, please do not add both of them unless you need to combine them. Normaly only one language tag is applicable. – Gerhardh Mar 04 '21 at 12:12

1 Answers1

1

min() has an optional second output which returns the position of the minimum in your initial array. So you can remove all those if statements and replace them with

[a, idx] = min([s1, s2, s3, s4]);
y = ones(1, 4);
y(idx) = 0;

Then your y1, y2, ... are just the first, second, ... element in that vector. This will surely help finding any other problem you have.

Matteo V
  • 1,163
  • 1
  • 8
  • 17