How to get Average of data ?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to get Average of data ?

Jump to solution
1,516 Views
omerkaanbasakin
Contributor IV

Hello ı am trying to get average of data x and data y. Set 2ms periodic interupt. Stroge and get average of datas. When interupt comes uart_send average of data. wrote something but not sure.

Main:

DATA_queue = xQueueCreate(13,sizeof(message));

*first get datas from spi  in data task uint16_t x , y and msg struct datax,datay. put queue .

uint16_t x,y ;

 

uint16_t msg.datax = x;

uint16_t msg.datay =y;

xQueueSendToBack(DATA_queue, &msg, portMAX_DELAY);

*second data storage task receive queue and get average of datas between two interupt and send .

 

xQueueReceive(DATA_queue, &uart_send_buffer,portMAX_DELAY);

LPUART_RTOS_Send(&uart_handle,(uint8_t *)uart_send_buffer,sizeof(uart_send_buffer));

How get average of datas in this task ?

0 Kudos
1 Solution
1,270 Views
omerkaanbasakin
Contributor IV

I got the solution.

Get data in in buffers and count how many data you get by another counter [buff_counter (the number of the elements in the buffer)].

When PIT occurs get sum of the values and divide buff_counter. Tried and approved.

Best Regrads,

Ömer.

View solution in original post

0 Kudos
7 Replies
1,271 Views
omerkaanbasakin
Contributor IV

I got the solution.

Get data in in buffers and count how many data you get by another counter [buff_counter (the number of the elements in the buffer)].

When PIT occurs get sum of the values and divide buff_counter. Tried and approved.

Best Regrads,

Ömer.

0 Kudos
1,270 Views
Sabina_Bruce
NXP Employee
NXP Employee

Hello Omer,

My name is Sabina and I'll be happy to help you out. Could you please tell me what microcontroller you are using.

Also regarding the code snippet provided above, could you please clarify are you wanting to receive the data and calculate the avarage between x and y and store the averaged value. Or are you storing all the data received and then doing the average every 2ms.

If you are working with an example from the SDK please let me know.

Best Regards,

Sabina

0 Kudos
1,270 Views
omerkaanbasakin
Contributor IV

Hello nxf51209‌ ,

Thanks for your reply. I am using  imxrt 1050 and ı want to receive data and calculate average x values and y values seperately. When 2ms interrupt occurs , average of x and average of y should sended by uart. I tried to storage data and get average same time and just send average of axis. I based myself on the examlpe of sdk. Example name is Ecompass.

Here is my last code about average: 

pastedImage_1.png

0 Kudos
1,270 Views
converse
Senior Contributor V

There seems to be a fundamental problem with your code.

Your first loop is  copying x_buffer[n+1] to x_buffer [n] (and the same for y_buffer). The means the first data pair at x_buffer[0] and y_buffer[0] is destroyed as you have overwritten x_buffer[0] with x_buffer[1] . Note that this also leaves you with 2 copies of x_buffer[11] (located at x_buffer[10] and x_buffer[11]) - and ditto for y_buffer.

Your second loop is correctly averaging the values in x_buffer and y_buffer - but your data is not what was originally in those buffers, and so will not be correct.

0 Kudos
1,270 Views
omerkaanbasakin
Contributor IV

Hello Con ,

 Thanks for your reply. As you said , not worked as expected. If any suggestions please let me know.

Best Regards,

Ömer 

0 Kudos
1,270 Views
Sabina_Bruce
NXP Employee
NXP Employee

Hello Omer,

From the above code snippet that you have provided it looks like you are missing the value for the position 0 on both x and y. 

x_buffer[0]

y_buffer[0]

Are you not able to receive the values you are expecting?

Please let me know if the average you are reading is not as expected.

Best Regards,

Sabina

0 Kudos
1,270 Views
omerkaanbasakin
Contributor IV

Hello Sabina, 

The code not worked as expected. Average is senseless. I am trying to fix .

Best regards,

Ömer

0 Kudos