/* ÖÍÈÈ ÑÝÒ (ñ) 2002 ã. Processor: TMS320C32 Filename: vector_troll.h ÑÈÑÒÅÌÀ ÂÅÊÒÎÐÍÎÃÎ ÓÏÐÀÂËÅÍÈy Edit date: 04-12-02 Function: Revisions: */ #include "IQmathLib.h" #include "DSP281x_Device.h" // DSP281x Headerfile Include File #include "DSP281x_Examples.h" // DSP281x Examples Include File #include #include "mathlib.h" #include "params_norma.h" #include "math_pi.h" #include "params_pwm24.h" #include #include "params_norma.h" _iq SQRT_32 = _IQ(0.8660254037844); _iq CONST_23 = _IQ(2.0/3.0); _iq CONST_15 = _IQ(1.5); #define real float float my_satur_float(float Input, float Positive, float Negative, float DeadZone) { if (fabs(DeadZone)>0.000001 && Input>-DeadZone && Input=Positive) Input=Positive; else if (Input<=Negative) Input=Negative; return Input; } int my_satur_int(int Input, int Positive, int Negative, int DeadZone) { if (DeadZone!=0 && Input>-DeadZone && Input=Positive) Input=Positive; else if (Input<=Negative) Input=Negative; return Input; } long my_satur_long(long Input, long Positive, long Negative, long DeadZone) { if (DeadZone!=0 && Input>-DeadZone && Input=Positive) Input=Positive; else if (Input<=Negative) Input=Negative; return Input; } /* real pid_regul(real Kp_regul, real Tperiod_regul, real Minimum, real Maximum, real yk, real *uk1, real *yk1, real *yzad, real *ek, real *ek1, real *ek2, real d0, real d1, real d2) { real uk; *ek = - yk + *yzad; uk = *uk1 + Kp_regul * ( d0 * *ek + d1 * *ek1 + d2 * *ek2 ); if (uk>Maximum) uk=Maximum; if (ukMaximum) uk=Maximum; if (ukMaximum) *VarIntegral=Maximum; if (*VarIntegralMaximum) VarOut=Maximum; if (VarOutStepP) || (deltaVar<-StepN)) { if (deltaVar>0) InpVarCurr += StepP; else InpVarCurr -= StepN; } else InpVarCurr=InpVarInstant; VarOut = InpVarCurr; return VarOut; } #pragma CODE_SECTION(zad_intensiv_q,".fast_run"); _iq zad_intensiv_q(_iq StepP, _iq StepN, _iq InpVarCurr, _iq InpVarInstant) { _iq deltaVar, VarOut; deltaVar = InpVarInstant-InpVarCurr; if ((deltaVar>StepP) || (deltaVar<-StepN)) { if (deltaVar>0) InpVarCurr += StepP; else InpVarCurr -= StepN; } else InpVarCurr=InpVarInstant; VarOut = InpVarCurr; return VarOut; } real pi_regul3(real Kp_regul, real ki_regul, real Tperiod_regul, real Minimum, real Maximum, real InpVar, real *InpVarPrev, real *OutVarPrev) { real VarOut; VarOut = Kp_regul*(ki_regul/Kp_regul*Tperiod_regul/2.0+1)*InpVar + Kp_regul*(ki_regul/Kp_regul*Tperiod_regul/2.0-1) * (*InpVarPrev) + *OutVarPrev; if (VarOut>Maximum) VarOut=Maximum; if (VarOutMaximum) VarOut=Maximum; if (VarOutdata_array == 0) return 0; for (i = 0; i < v->size_array; ++i) { summ_squares += _IQmpy(v->data_array[i], v->data_array[i]); } return _IQsqrt(summ_squares / v->size_array); } //_iq calc_rms_array_var_period(RMS_CALC_ARRAY_THINNING *v) { // _iq summ_squares = 0; // int i = 0; // int count_elem = 0; // if (v->signal_period > v->size_array) { // v->signal_period = v->size_array; // } // count_elem = v->signal_period; // i = v->last_elem_position; // while (count_elem > 0) { // summ_squares += _IQmpy(v->data_array[i], v->data_array[i]); // i -= 1; // if (i < 0) { i = v->size_array - 1; } // count_elem -= 1; // // } // v->Out_rms = _IQsqrt(summ_squares / v->signal_period); // return v->Out_rms; //} _iq calc_rms_array_var_period_IQ15(RMS_CALC_ARRAY_THINNING *v) { _iq16 summ_squares = 0; int i = 0; int count_elem = 0; if (v->data_array == 0) { return 0; } if (v->signal_period > v->size_array) { v->signal_period = v->size_array; } count_elem = v->signal_period; i = v->last_elem_position; while (count_elem > 0) { summ_squares += 0;//_IQ15mpy(v->data_array[i], v->data_array[i]); i -= 1; if (i < 0) { i = v->size_array - 1; } count_elem -= 1; } v->Out_rms =_IQ15toIQ(_IQ15sqrt(summ_squares / v->signal_period)); return v->Out_rms; } float fast_round(float x) { float d; long i; i = (long)x; if (x<0) { d = i - x; if (d>=0.5) i = i - 1; } else { d = x - i; if (d>=0.5) i = i + 1; } return (float)i; } float fast_round_with_delta(float prev, float x, float delta) { float d; long i; i = (long)x; if (x<0) { d = i - x; if (d>=0.5) i = i - 1; } else { d = x - i; if (d>=0.5) i = i + 1; } if (fabs(prev-x)>=delta) return (float)i; else return (float)prev; } float fast_round_with_limiter(float x, float lim) { float d; long i; if (fabs(x)<=lim) return 0; i = (long)x; if (x<0) { d = i - x; if (d>=0.5) i = i - 1; } else { d = x - i; if (d>=0.5) i = i + 1; } return (float)i; } #pragma CODE_SECTION(calc_rms,".fast_run"); _iq calc_rms(_iq input, _iq input_prev, _iq freq_signal) { static _iq pi_pwm = _IQ(PI*NORMA_FROTOR/(FREQ_PWM/5.0)); _iq cosw, sinw, wdt, y2, z1, z2, z3, y; wdt = _IQmpy(pi_pwm,freq_signal); sinw = _IQsinPU(wdt); cosw = _IQcosPU(wdt); if (cosw==0) return 0; z1 = input_prev - _IQmpy(input,cosw); // z2 = sinw; z3 = _IQdiv(z1,sinw); y2 = _IQmpy(input,input)+_IQmpy(z3,z3); // cosw = _IQsin(); y = _IQsqrt(y2); return y; }