Declaring a FB within a struct and calling it in ST

martinll
2021-09-24
2021-09-30
  • martinll - 2021-09-24

    Hi!

    2 h google gave me nothing, here I go.

    In short to explain the problem i have a bunch of Real and Bool in a struct that i want to be able to individually change depending on struct-internal parameters (using index in the array) and with some inputs from sensors.

    To do so i have learned (is this true?) that you need a 2d array.

    I have an 2d array
    ArrayName:Array[0..1,0..6] of Struct

    As i can´t get the update of variables explained above working i have created a FB to hopefully solve this problem.
    How do i declare a FB in a Struct that is going into an array? Is it just LocalFBname:TheNameOfTheFuntionBlock?
    I want individual copies of my FB in every part of the array (total of 24).

    When i have tried this i can´t seem to get it working, i get the "start"-input true but then the EN on the first block stays false (with a nice blue line coming right to it). I´m guessing Cycle problems, or could it be anything else?

    I could be overseeing something very obvious as i´m fairly new to Codesys so any ideas are appreciated.

    Best,

    M

     
  • martinll - 2021-09-25

    So for one of the problems i had been writing to instances of the FB but not calling the actuall FB in the ST-code. To do this simply write
    Arrayname[index].theinstanceyouwanttocall();

    then its done.

    M

     
  • ojz0r - 2021-09-25

    Can you please describe what you want to do in more detail. There might be a risk of xy problem

     
  • martinll - 2021-09-28

    Hi!

    I think the 2d array was unnecessary, it seems to work fine with a 1d array.
    But i´m still having some trouble with the FB in-bedded in the struct.

    The FB consists (in big parts) of a blink that adds a Real number to a Real variable every time it is high (a bit more often than the cycle time).

    I want it in a for loop as i want to cycle through the array to register what happens with the inputs that is connected to the array.

    But if i write array[index].MyInbeddedFB.CAR_Moving>=5 [index].MyInbeddedFB.CAR_Moving<=15 and Ice_On_Road=true then Car_Crash:=True else car_crash:=false; nothing happens as the car_crash:=true is made False at the next cycle.

    If i remove the Else car_crash:=false; the car_crash does not turn false when CAR_Moving<=15 and that is something i want it to do.

    So, does anyone have any idea of how this is done?

    I´m still a rookie in Codesys so please keep an open mind on what could be the problem, this could for sure be an easy one.

    Best,

    M

     
  • ojz0r - 2021-09-29

    Ah so the FB instance is embedded in the array, it seems clear now that i re-read everything.
    It is very difficult to say without seeing the actual code.
    Have you tried splitting it up so you keep your values in one array and the FB instances in another? Simply call the FB and inserting the values from the first array.
    If you want everything packaged then maybe look into making a custom STRUCT (user defined type record) that has one setup of what you need, including FB type, and then make an array of that.

     
  • dFx

    dFx - 2021-09-29

    I feel like your actual problem has nothing to do with your topic title. It may be better for search purpose to split the topic.

    Anyway, your problem is more some generic code thinking related issue.
    If your car is crashed because it went too fast on ice, it shouldn't be "not crashed" because it lowers its speed (feels like it usually happens when you crash your car).

    So you must decide what is the behavior of the crashed property of your car (does it stays at TRUE forever once a crash occured ? Does the "repair_car" bit resets the crash state ? Or is it only a warning that only tells the driver a crash is imminent, and thus, should be reset when the speed lowers or the ice isn't here ?)

     
  • martinll - 2021-09-30

    Hi!

    Thank you for all your answers! I know, this post is going further and further from the topic. I´ll try to concertize my questions and start a new post as i´m having some trouble pinpointing what i´m actually searching the answer for.

    Once again, thanks for all your reply, I´ll be back with a more precise question in a new post shortly.

    M

     

Log in to post a comment.