工具/软件:
您好、
我正在使用 MATHACL 或 IQ 库寻找 PI 补偿代码?
此致、
Yogesh
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
工具/软件:
您好、
我正在使用 MATHACL 或 IQ 库寻找 PI 补偿代码?
此致、
Yogesh
您好、Matthew、
我尝试使用"mathacl_mpy_div_op_LP_MSPM0G3507_nortos_ticlang"。 需要您的帮助才能更好地理解。
我正在计划进行某种 PI 仿真、以检查计算 是否正确。
设定点为3500、ADC 测量值为 3,400,3603600345 0 和3550。 第一次迭代结果如下所示:
错误= 3500-3400=100
error31 =(100 <<(31-12))-1 = 52428799 (0x031FFFFF)
Integrate = 0+(0x0CCCCCCCCC*0x031FFFFF >>>31)= 5242879 (0x004FFFFF)
比例= 0x2666666*0x031FFFFF >>31 = 15728639 (0x00EFFFFF)
输出=积分+比例= 20971518 (0x013FFFFE)
outputPI = 0x013FFFFE*300>>31 = 2
第一次迭代结果符合预期。 第二次迭代结果如下所示:
错误= 3500-3600=-100
error31 =(-100 <<(31-12))-1 =-52428801 (0xFCDFFFFF)
Integrate = 5242879 +(0x0CCCCCCCCC*0xFCDFFFFF>>>31)= 429496726 (0x19999996) --需要解释(理想为零)
比例 = 0x266666*0xFCDFFFFF>>>31 = 1272761547 (0x4BDCCCCB) --需要解释
输出=积分+比例= 150328553 (0x59999999) --钳位到更高的值
outputPI = 0x59999999*300>31=209 --这是错误的
#include "ti_msp_dl_config.h" /* Ki = 0.1, 0.1 x 2^31 = 2,147,483,648 = 0x0CCC CCCC */ #define Ki (0x0CCCCCCC) /* Kp = 0.3, 0.3 x 2^31 = 644,245,094 = 0x2666 6666 */ #define Kp (0x26666666) #define setpoint 3500 const DL_MathACL_operationConfig gMpyConfig = { .opType = DL_MATHACL_OP_TYPE_MPY_32, .opSign = DL_MATHACL_OPSIGN_UNSIGNED, .iterations = 1, .scaleFactor = 0, .qType = DL_MATHACL_Q_TYPE_Q31}; float res=0.0; volatile int32_t outputPI=0,errorQ31=0,integral = 0,Proportional=0,output=0; int array[4]={3400 ,3600, 3450 , 3550}, measured_value=0,error=0; int main(void) { SYSCFG_DL_init(); DL_TimerA_startCounter(PWM_0_INST); int i = 0; while (1) { measured_value=array[i]; error = setpoint-measured_value; errorQ31 = (error << (31-12))-1; DL_MathACL_startMpyOperation(MATHACL, &gMpyConfig, Ki, errorQ31); DL_MathACL_waitForOperation(MATHACL); integral = integral + DL_MathACL_getResultOne(MATHACL); if(integral>=0x59999999){ integral = 0x59999999; } else if(integral <0){ integral = 0; } DL_MathACL_startMpyOperation(MATHACL, &gMpyConfig, Kp, errorQ31); DL_MathACL_waitForOperation(MATHACL); Proportional = DL_MathACL_getResultOne(MATHACL); output = integral + Proportional ; if(output>=0x59999999){ output = 0x59999999; } else if(output < 0){ output = 0; } DL_MathACL_startMpyOperation(MATHACL, &gMpyConfig, 300, output); DL_MathACL_waitForOperation(MATHACL); outputPI = DL_MathACL_getResultOne(MATHACL); DL_TimerA_setCaptureCompareValue(PWM_0_INST, outputPI, DL_TIMER_CC_0_INDEX); i++; if(i>3){ i=0; } //res = _IQ30toF(output>>1); } }
请仔细查看、如果有任何更正、请告诉我。
此致、
Yogesh
尊敬的 Yegoesh:
PI 只是针对输入数据的乘法、加法和判断运算。
您可以 参考 我们的 IQMath 库来玩 mathacl。 它在 API 中集成了 MATHACL、因此用户只需要调用 API 并执行乘法操作。
有一个与此相关的 SDK 演示:
TI\mspm0_sdk_2_04_00_06\examples\nortos\LP_MSPM0G3507\IQmathacl_ops_test
您可以在 SDK 路径中找到它的源代码和标头:TI\mspm0_sdk_2_04_00_06\source\ti\IQMath
以下是 供您参考的乘法代码 jsut:
/** * @brief Multiply two values of IQN type, using MathACL. * * @param iqNInput1 IQN type value input to be multiplied. * @param iqNInput2 IQN type value input to be multiplied. * @param q_value IQ format. * * @return IQN type result of the multiplication. */ __STATIC_INLINE int_fast32_t __IQNmpy(int_fast32_t iqNInput1, int_fast32_t iqNInput2, const int8_t q_value) { /* write control */ MATHACL->CTL = 6 | (q_value<<8) | (1 << 5); /* write operands to HWA */ MATHACL->OP2 = iqNInput2; /* write trigger word last */ MATHACL->OP1 = iqNInput1; /* read iqmpy product */ return MATHACL->RES1; }
B.R.
Sal