سؤال

i have a problem with arrays or something missed in these text.. my program works every 500ms and i want to read first 4 double values and take average of these values and then get next 4 double values and so on... i write something about this and can you pls look on this??

if (u_dcbus_pv_act[i] > 0 && i != 0)
{
    u_dcbus_pv = u_dcbus_pv_act[i];
    p_dcbus_pv = p_dcbus_pv_act[i];
}
if (i >= 3)
{
    for (int j = 0; j < 4; j++)
    {
        total_u += u_dcbus_pv;
        total_p += p_dcbus_pv;
    }

    average_u = total_u / 4;
    average_p = total_p / 4;
    u_dcbus_target = average_u;
    p_dcbus_pv_avg = average_p;
}
هل كانت مفيدة؟

المحلول

from what I understand of your description, I would do it something like this:

 /* add current samples to totals */
 total_u += u_dcbus_pv_act[i];
 total_p += p_dcbus_pv_act[i];

 /* every fourth tick, calc average and reset totals */
 if (i % 4 == 0)
 {
     average_u = total_u / 4;
     average_p = total_p / 4;
     total_u = 0;
     total_p = 0;
 }
 u_dcbus_target = average_u;
 p_dcbus_pv_avg = average_p;
 i++;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top