/*
 * overheat_limit.c
 *
 *  Created on: 17 δεκ. 2020 γ.
 *      Author: star
 */

#include <edrk_main.h>
#include <overheat_limit.h>
#include <params_temper_p.h>
#include <protect_levels.h>

#include "IQmathLib.h"
#include "math_pi.h"
#include "limit_lib.h"


TEMPERATURE_LIMIT_KOEFFS temper_limit_koeffs = TEMPERATURE_LIMIT_KOEFFS_DEFAULTS;

void calc_limit_overheat()
{
    int *p_alarm, *p_abnormal;
    _iq sum_limit = CONST_IQ_1;
    _iq val;
    int i = 0;

    p_alarm = &protect_levels.alarm_temper_u_01;
    p_abnormal = &protect_levels.abnormal_temper_u_01;
    edrk.temper_limit_koeffs.power_units = CONST_IQ_1;
    for (i = 0; i < 7; i++) {
        val = linear_decrease(edrk.temper_edrk.real_int_temper_u[i],
                                             *(p_alarm + i), *(p_abnormal + i));
        edrk.temper_limit_koeffs.power_units = _IQmpy(val, edrk.temper_limit_koeffs.power_units);
    }
    sum_limit = _IQmpy(sum_limit,edrk.temper_limit_koeffs.power_units);

    p_alarm = &protect_levels.alarm_temper_air_int_01;
    p_abnormal = &protect_levels.abnormal_temper_air_int_01;
    edrk.temper_limit_koeffs.area = CONST_IQ_1;
    for (i = 0; i < 4; i++) {
        val = linear_decrease(edrk.temper_edrk.real_int_temper_air[i],
                              *(p_alarm + i), *(p_abnormal + i));
        edrk.temper_limit_koeffs.area = _IQmpy(val, edrk.temper_limit_koeffs.area);
    }
    sum_limit = _IQmpy(sum_limit,edrk.temper_limit_koeffs.area);

    edrk.temper_limit_koeffs.water_int = linear_decrease(edrk.temper_edrk.real_temper_water[0] * 10.0,
                        protect_levels.alarm_temper_water_int, protect_levels.abnormal_temper_water_int);
    sum_limit = _IQmpy(sum_limit,edrk.temper_limit_koeffs.water_int);

    edrk.temper_limit_koeffs.water_ext = linear_decrease(edrk.temper_edrk.real_temper_water[1] * 10.0,
                        protect_levels.alarm_temper_water_ext, protect_levels.abnormal_temper_water_ext);
    sum_limit = _IQmpy(sum_limit,edrk.temper_limit_koeffs.water_ext);


    p_alarm = &protect_levels.alarm_temper_acdrive_winding_U1;
    p_abnormal = &protect_levels.abnormal_temper_acdrive_winding_U1;
    edrk.temper_limit_koeffs.acdrive_windings = CONST_IQ_1;
    for (i = 0; i < 6; i++) {
        val = linear_decrease(edrk.temper_acdrive.winding.real_int_temper[i],
                              *(p_alarm + i), *(p_abnormal + i));
        edrk.temper_limit_koeffs.acdrive_windings = _IQmpy(val, edrk.temper_limit_koeffs.acdrive_windings);
    }
    sum_limit = _IQmpy(sum_limit,edrk.temper_limit_koeffs.acdrive_windings);

    edrk.temper_limit_koeffs.acdrive_bears = linear_decrease(edrk.temper_acdrive.bear.real_int_temper[0],
                protect_levels.alarm_temper_acdrive_bear_DNE, protect_levels.abnormal_temper_acdrive_bear_DNE);
    sum_limit = _IQmpy(sum_limit,edrk.temper_limit_koeffs.acdrive_bears);
    edrk.temper_limit_koeffs.acdrive_bears = linear_decrease(edrk.temper_acdrive.bear.real_int_temper[1],
                    protect_levels.alarm_temper_acdrive_bear_NE, protect_levels.abnormal_temper_acdrive_bear_NE);
    sum_limit = _IQmpy(sum_limit,edrk.temper_limit_koeffs.acdrive_bears);

    edrk.temper_limit_koeffs.sum_limit = sum_limit;

    if (edrk.temper_limit_koeffs.sum_limit < (CONST_IQ_1 - 1000))
        edrk.temper_limit_koeffs.code_status = 1;
    else
        edrk.temper_limit_koeffs.code_status = 0;

}
