Average value from 5 last values

Forum Home Forums Understanding the Software Using Formulas Average value from 5 last values

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #16202
    BamKR
    Participant

    Hello,

    I am trying to make formula for calculate average value from last 5 values.
    In firts steps formula update values every time when trigger state change from 1 to 0. State change of this trigger also mean that value is changed.I want to use automatic module for triggering formula in state changing moment, val=0 send command to channel with formula on output to calculate average and update values. I make formula but it don’t update values. Thanks for help.

    public double TimeUpdate()
    { 
        double time1 = Val(145); 
        double time2 = Val(146); 
        double time3 = Val(147); 
        double time4 = Val(148); 
        double time5 = Val(149); 
        double newTime = Val(144); 
    
        
        SetVal(149, time4); 
        SetVal(148, time3); 
        SetVal(147, time22); 
        SetVal(146, time11); 
        SetVal(145, newTime); 
    
        double average = (time1 + time2 + time3 + time4 + time5) / 5.0;
        SetVal(151, average);
        return average; 
    }
    #16205
    Mikhail
    Moderator

    Hello,
    Formulas for rolling average are here.

    #16209
    BamKR
    Participant

    Hello,

    I think I solved problem, values in formula wasn’t displayed till i put in input formula Val(cnl). For example, on channel [115] Value 1 I put input formula Val(115), and for other channel also like on img.

    https://imgur.com/a/tmvtCvz

    #16211
    Mikhail
    Moderator

    Hello,
    It seems that using the Val(145) input formula in the channel 145 is excessive.

    #16217
    BamKR
    Participant

    Yes, but when I don’t make self-reference, value is not displayed on channels. Maybe you know what I am doing wrong ? I am using setvalue function in formula but it is not enough.

    #16218
    Mikhail
    Moderator

    If you remove Val(145) in that channel, where do you set the channel’s 145 value? Which place the TimeUpdate() function is called?

    #16219
    BamKR
    Participant

    I am setting 145 value using value from plc stopwatch from value 144. TimeUpdate() is called on output formula on channel 150 from automatic module when input state is changing on plc. Everytime when input state is changing, I replace value on channels by 1 moving up.

    
    public double UpdateTimes()
    { 
        double time1 = Val(145); 
        double time2 = Val(146); 
        double time3 = Val(147); 
        double time4 = Val(148); 
        double time5 = Val(149); 
        double newTime = Val(144); 
    
        SetVal(149, time4); 
        SetVal(148, time3); 
        SetVal(147, time2); 
        SetVal(146, time1); 
        SetVal(145, newTime); 
    
        double average = (time1 + time2 + time3 + time4 + time5) / 5.0;
        SetVal(151, average);
    
        double average_time = (Val(103) * 60) / Val(143);
        SetVal(153, average_time);
    
        return average;
    }
    
    • This reply was modified 1 month, 2 weeks ago by BamKR.
    #16222
    Mikhail
    Moderator

    May be you should set an input formula of the channel 145 to blank, and try again.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.