PLUS+1 Software > General controls

Applying calculations on large arrays

(1/1)

spittet:
Hi,

What are your tricks to play with large arrays?

I have a program that will have an array of 96x U16 elements. On this array, I will need to do some calculations to find values like these :

- the highest value (and it's position inside the array)
- the lowest value (and it's position inside the array)
- the sum of all values of all elements

Because of the large size of the array, I'm questionning what is the best way to do these kind of calculations on an array.

Please share our experiences. Some code examples would be great.

Have a good day!

Marbek_Elektronik:
Hi,
what is the source of your array?
For example: I have typed arrays manually or I have used text-arrays and edit them with text editor. Is it a array constant?
Or have you create this array by software? Is it a value?

Ok, I think it is a value.
Now, is it possible to analyse the elements by creating the array value?
But , if you overwrite elements, there will be a problem.

I think, you  could do it manually by building sub-array, perhaps 12x Array8(U16).

spittet:
Hi,

My array has a fixed length of 96 U16 values.
All these values comes from CAN messages received by the controller. There are 4 different CAN Messages. I receive 3 different U16 values in each CAN message (on CAN_DATA_0 to CAN_DATA_5 inclusively). CAN_DATA_6 serves as a multiplexor (it can have 8 possible values). So, 4 CAN messages x 3 U16 values per message x 8 possible values of the multiplexor = 96 different U16 values.

I want to place all of these in an array for the ease of transportability between different pages of my program.

Also, as I said, I will need to do some calculations on this array like : maximum value of the array (and it's position), minimum value of the array (and it's position), sum of all the values inside the array.

I really don't feel like doing a subpage and copying it 96x to do these calculations.

I have some experience on C programming and I definately think that this could be easily done in C code within a "for loop" that is executed each time a new CAN message is received (and the values of the elements inside the array has changed).

Now, I'm looking at the "POU with C-code" function of PLUS1. I've only used the graphical code in PLUS1, so I need to experiment with POU.

Have a good day!

Sam

FStelzer:
Hi,

I think a POU with structured text is the thing you need.

Create a POU function block with following code inside:

define variables:

--- Code: ---FUNCTION_BLOCK SearchArrays
VAR_INPUT
(* UINT = U16, 0...95 = 96 array places *)
Input_Array : ARRAY[0..95] OF UINT;
END_VAR
VAR_OUTPUT
(* DINT = S32 *)
sum : DINT;
max : DINT;
min : DINT;
place_min : DINT;
place_max : DINT;
END_VAR
VAR
i : DINT;
place_min_temp : DINT;
place_max_temp : DINT;
sum_temp : DINT;
min_temp : DINT;
max_temp : DINT;
END_VAR
--- End code ---

Coding:

--- Code: ---max_temp := 0;
min_temp := 100000; //Value must be higher than max value of array data type
place_min_temp:=0;
place_max_temp:=0;
sum_temp:=0;

FOR i := 0 to 95 by 1 do //search all array places

IF Input_Array[i] > max_temp then //get max value

max_temp := Input_Array[i];
place_max_temp := i;

END_IF;

IF Input_Array[i] < min_temp then // get min value

min_temp := Input_Array[i];
place_min_temp := i;

END_IF;

sum_temp := sum_temp+Input_Array[i];

END_FOR;

//get output values after going through the whole array
place_min := place_min_temp; //place where min value is located
place_max := place_max_temp; // place where max value is located
min := min_temp; //min value
max := max_temp; //max value
sum := sum_temp; //sum value

--- End code ---

I didn't test the code but the compiler says that everything should be OK  ;D

After insert the POU in your GUIDE program, you can connect your array to the input bus and get out the data on the output bus.

Regards,
FStelzer

spittet:
Thanks a lot FStelzer!

Finnaly I used a POU (exactly as you suggested), but I used the C-code one because I'm more familiar with this programming langage.

Everything is working as it should.

It was my first use of POU and I will definately use them a lot more in the future! It was a good learning curve.

Thanks again for the support

Sam