Event on change while using formula of stat

Forum Home Forums Understanding the Software Using Formulas Event on change while using formula of stat

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #9112
    JW
    Participant

    Hi,

    I added some custom “Events Types” with color indication.

    for a channel, I enable event on change and use formulas.
    CalVal();CalStat()

    While the value changes, 2 event log appears, screen capture in link.
    – the 1st event only show the new value (“Normal”) with old stat (Leak/Cyan);
    – while the 2nd event show the new value (“Normal”) with new stat (Normal/Green).
    https://drive.google.com/file/d/1jCBetZss3Pp4XCYk24hN8rjNJMAyVp-1/view?usp=sharing

    Is there any way to show 1 event only, with new Val and Stat at the same time?

    #9117
    Mikhail
    Moderator

    Hi,

    Likely, this is caused by formulas. Please provide screenshots of the input channel properties and code of the used formulas.

    #9122
    JW
    Participant

    Channel settings:
    the value can be 0,1,2,3
    the stat can be 0,121,122,123
    https://drive.google.com/file/d/1IXNixhZteaGPdD4zrm4qe3Q6wMCXM3lJ/view?usp=sharing

    ———————
    Formulas:

    
    int LeakValList(int [] lst) {
    int na = 0;
    int active = 0;
    int inactive = 0;
    int normal = 0;
    for (int i = 0; i < lst.Length; i++)
    {
    if (Val(lst[i]) == 1) {active ++;}
    else if (Val(lst[i]) == 2) {inactive ++;}
    else if (Val(lst[i]) == 3) {normal ++;}
    else {na++;}
    }
    if (active > 0) {return 1;}
    else if (inactive > 0) {return 2;}
    else if (normal > 0) {return 3;}
    else {return 0;}
    }
    
    int LeakStat() {
    int s = (int) Val();
    if (s > 0) {s = s + 120;}
    return s;
    }
    

    I was using Cnl, now replaced with Val() for stat calculation, but seems the same.

    • This reply was modified 3 years, 5 months ago by JW.
    #9126
    JW
    Participant

    Q1:
    for real/discrete channel, Cnl and Val() are value before and after formula calculation;
    but for calculated real/discrete, what’s the Cnl? last value or same as Val()?

    Q2:
    Is it possible to return Val and Stat at the same time using 1 formula?

    #9130
    Mikhail
    Moderator

    I suppose, events created twice, because LeakStat returns different statuses.
    The idea is the formulas like that:

    int LeakStat(int leak)
    {
    // your calculations
    }

    In the channel: LeakValList(210, 220); LeakStat(LeakValList(210, 220))
    Note: now you create a new array (formula argument) on every loop. It’s not efficient, because memory is allocated and cleaned too active.

    #9131
    Mikhail
    Moderator

    Q1:
    Cnl is come from the PLC.
    Val() is stored in the channel.
    For calculated channels Cnl is meaningless.

    Q2:
    It will be included in v6.

    #9135
    JW
    Participant

    Thanks Mikhail.

    what’s the proper way to pass an array to a formula as input, while the size of array is not constant.

    the input is usually multiple channel numbers. e.g. it can be {11,12} or {11,12,13}

    #9143
    JW
    Participant

    I came up with the following solution.
    One downside is that I can’t see which channels are called in the channel formula, but not a big deal.

    in dictionaries/formulas, declare the arrays I need to use,

    public static int[] lst_1 = new int[]{1};
    public static int[] lst_2 = new int[]{1,2};
    public static int[] lst_3 = new int[]{1,2,3};
    public static int[] lst_4 = new int[]{1,2,3,4};

    then in channels,

    LeakValList(lst_1)
    LeakValList(lst_2)
    #9144
    Romiros
    Participant

    public double LeakValList(params int[] nums)
    {
    for (int i = 0; i < nums.Length; i++)
    {

    }

    }

    In formula LeakValList(101,102,103)

    #9145
    JW
    Participant

    thanks Romiros, works perfectly!

    #9147
    Mikhail
    Moderator

    what’s the proper way to pass an array to a formula as input, while the size of array is not constant

    In addition to the good approach that @Romiros provided, you can create similar formulas with different number of parameters. Sometimes Microsoft does like that.

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