Find closest value in several values in SCL...

userxyz

Member
Join Date
May 2002
Location
any
Posts
2,768
Hi,

Anyone had to program something to find the nearest ?

I have a list of the folowing X position values:

0
133
213
293
373
453
533
1093
1653

The middle of a tube needs to find best X position to pick.
So Middle Tube = Length / 2.

And this value has to find the closest value in that list...

Can I do this with... hmm substract the Middle of the Tube form each value and see wich one has the smallest value... or how would you guys write this >

Thanks,
Kind regards,
Combo
 
pseudocode:

Code:
Set smallest_difference := 9999 ;
loop through all value[x]
  calculate difference := ABS(middle_value - value[x])
  if difference < smallest_difference THEN
    smallest_difference := difference 
    no_found := x
 
Thanks

Thanks JesperMP !!!!!!!!!🍺🍺🍺🍺🍺🍺


pseudocode:

Code:
Set smallest_difference := 9999 ;
loop through all value[x]
  calculate difference := ABS(middle_value - value[x])
  if difference < smallest_difference THEN
    smallest_difference := difference 
    no_found := x
 
For next

I made this from the Pseudo, still need to test it:

Code:
    REGION X      
        #"Tube Middle Position" := "DB130 UNIT SPECIFIC"."PART READY FOR PICKUP".Length / 2.0;
        #"Found Nearest" := FALSE;
        FOR #i := 1 TO 8 DO
            #"Difference with Positions" := ABS(#"Tube Middle Position" - "DB220 CALC".OPNAME."UNIT 100 POSITIONS"[#i]);
            IF #"Difference with Positions" < #"Smallest Difference" THEN
                #"Smallest Difference" := #"Difference with Positions";
                #"Found Nearest" := TRUE;
            END_IF;
        END_FOR;
    END_REGION

Thanks JesperMP !!!!!!!!!🍺🍺🍺🍺🍺🍺
 
Tested

TESTED, this one works:

Code:
    REGION X      
        #"Tube Middle Position" := "DB130 UNIT SPECIFIC"."PART READY FOR PICKUP".Length / 2.0;
        #"Smallest Difference" := 9999.9;
        FOR #i := 1 TO 8 DO
            #"Difference with Positions" := ABS(#"Tube Middle Position" - "DB220 CALC".OPNAME."UNIT 100 POSITIONS"[#i]);
            IF #"Difference with Positions" < #"Smallest Difference" THEN
                #"Smallest Difference" := #"Difference with Positions";
                "DB220 CALC".OPNAME.X := "DB220 CALC".OPNAME."UNIT 100 POSITIONS"[#i];
            END_IF;
        END_FOR;
    END_REGION

Another thanks JesperMP
 

Similar Topics

Hello! I am in the process of converting a Horner program into Studio 5000 for a customers gasifier. The Oxygen sensors are +/- 1000 mV. The...
Replies
3
Views
2,665
Hi , Where i can find Mitsubishi PLC Card end of line & replacement model details. i am looking for Q02CPU replacement model. Please advice. thanks
Replies
2
Views
126
I have tested every screen and no single screen individually has this fault pop up, but when I compile and send to the PanelView it comes up. If I...
Replies
4
Views
172
Hi, One of my customers has an old fabric tensile testing machine. The IC # AD7501KN of its controller has malfunctioned. This IC is related to...
Replies
1
Views
80
Hello everyone, I am a student and would like to take the next step and learn FactoryTalk (Batch preferably) and how to create HMIs etc. Would...
Replies
4
Views
490
Back
Top Bottom