Author Topic: Applying calculations on large arrays  (Read 8209 times)

Offline spittet

  • PLUS+1 Guru
  • *****
  • Posts: 118
  • Karma: +6/-1
Applying calculations on large arrays
« on: April 10, 2020, 01:21:47 AM »
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!

Offline Marbek_Elektronik

  • PLUS+1 Guru
  • *****
  • Posts: 352
  • Karma: +8/-0
    • Marbek Elektronik
Re: Applying calculations on large arrays
« Reply #1 on: April 12, 2020, 09:21:18 AM »
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.

Please describe your application a littel more exactly.

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


Marbek Elektronik, Dipl.-Ing. Bernd Konrad
Dienstleistung, Entwicklung, Herstellung

Offline spittet

  • PLUS+1 Guru
  • *****
  • Posts: 118
  • Karma: +6/-1
Re: Applying calculations on large arrays
« Reply #2 on: April 13, 2020, 09:26:36 PM »
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.

Let me know if you think I'm on the right track about this.

Have a good day!

Sam

Offline FStelzer

  • PLUS+1 Expert
  • ****
  • Posts: 50
  • Karma: +8/-0
Re: Applying calculations on large arrays
« Reply #3 on: April 15, 2020, 01:17:52 PM »
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: [Select]
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


Coding:

Code: [Select]
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


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

Offline spittet

  • PLUS+1 Guru
  • *****
  • Posts: 118
  • Karma: +6/-1
Re: Applying calculations on large arrays
« Reply #4 on: April 18, 2020, 10:02:20 PM »
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