Closest Array Element To Target Calculation???

spence144db

Member
Join Date
May 2011
Location
Lansing MI
Posts
34
I need help coming up with some code...I have have an array Part_Number.PreForge_Temp[220]. With a pyrometer I scan the part for 2 seconds and fill the array with a FIFO every at a 10ms interval till its full. I need help coming up with some math or code to search the array and find the element that is closest to my desired temp setpoint.

With the ex. below Part_Number.PreForge_Temp[5] would be the closest to my target temp. I need a way to return this result. Google has only returned ways to do this is a PC environment like Java.

Part_Number.Target_Temp = 1085 C

Part_Number.PreForge_Temp[0] = 1098 C
Part_Number.PreForge_Temp[1] = 1112 C
Part_Number.PreForge_Temp[3] = 1034 C
Part_Number.PreForge_Temp[4] = 1058 C
Part_Number.PreForge_Temp[5] = 1074 C
.
.
.
.
.
Part_Number.PreForge_Temp[220] = 1100 C

Edit: Sorry I assumed most would know by tag structure but just incase..1756-L73S (AB ControLogix PLC)
 
Last edited:
I am thinking out loud here:
I am thinking that you will have to run through the array looking for temps that are say +/- 10 degrees from target. Store the positions of the elements that meet that test.
Then run the ones that were with in 10 through again at say +/- 5 degrees and see how many meet that standard. Then maybe 2 or 1 and keep going until you have only 1 element that is remaining.

You didnt say what PLC you are using but if it is a Logix5000 unit you should be able to use the FSC Instruction to do most of that.

Hope it helps get you going.
 
Try this with your data (I did a quick test with 5 elements):
Code:
result := 0;
last_difference := 9999;
FOR x := 0 TO 119 DO
  difference := ABS(Part_Number.PreForge_Temp[x] -  Part_Number.Target_Temp);
  IF difference < last_difference THEN
    result := x;
    last_difference := difference;
  END_IF;
END_FOR;
The variable result stores the position you are looking for. You need to put the code in a subroutine and call it just once each time.
 
result := 0;
last_difference := ABS(Part_Number.PreForge_Temp[x] - Part_Number.Target_Temp);
FOR x := 1 TO 199 DO
difference := ABS(Part_Number.PreForge_Temp[x] - Part_Number.Target_Temp);
IF difference < last_difference THEN
result := x;
last_difference := difference;
END_IF;
END_FOR;

How would you do it yourself using pencil and paper? Describe the steps that you are taking and then write code to match.
 
FOR x := 0 TO 119 DO

Of course what is shown as '119' here should actually be the highest index of the array - '219' in your case.

I use essentially this algorithm to match cases scanned by height against an ideal array of case heights to identify each for further processing. (Height is the key identifying dimension in my process.)
 
result := 0;
last_difference := ABS(Part_Number.PreForge_Temp[x] - Part_Number.Target_Temp);
FOR x := 1 TO 199 DO
difference := ABS(Part_Number.PreForge_Temp[x] - Part_Number.Target_Temp);
IF difference < last_difference THEN
result := x;
last_difference := difference;
END_IF;
END_FOR;

How would you do it yourself using pencil and paper? Describe the steps that you are taking and then write code to match.
First time you run your code, you can assume that x = 0, and last_difference receives the value of the first element. But the next time you run the code, x will equal to 199. So, the first element is never compared inside the FOR loop.

BTW, I used 199 in the high limit of the FOR loop because I misread the size of the array. In fact, I'm not sure if the array length is 220 or 221.
 
Try this with your data (I did a quick test with 5 elements):
Code:
result := 0;
last_difference := 9999;
FOR x := 0 TO 119 DO
  difference := ABS(Part_Number.PreForge_Temp[x] -  Part_Number.Target_Temp);
  IF difference < last_difference THEN
    result := x;
    last_difference := difference;
  END_IF;
END_FOR;
The variable result stores the position you are looking for. You need to put the code in a subroutine and call it just once each time.

Thank you for this...I just dropped this into my own little aoi and it works perfect.

I changed around the tag names and added my notes but this gave me that spark that I needed to get it...thanks again
 
Last edited:

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,666
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...
Replies
9
Views
2,733
Hi, I'm having an issue in crimson 3.0 when I create a programme using a case statement referencing a fault word that each bit needs to change the...
Replies
4
Views
194
I am trying to copy an array of real numbers into a UDT with a real data type element. I have attached a snip below showing my COP instruction...
Replies
4
Views
203
I have an array of 55 REAL values. Is there a way to multiply based on the array location ? I have 55 transfer belts that are equally spaced...
Replies
3
Views
153
Back
Top Bottom