Tool/software:
Hi,
I am looking for PI compensation code using MATHACL or IQ library ?
Regards,
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.
Tool/software:
Hi,
I am looking for PI compensation code using MATHACL or IQ library ?
Regards,
Yogesh
Hi Yogesh!
Are you referring to PI as in Proportional-Integral?
-Matthew
Hi Matthew,
I have tried using "mathacl_mpy_div_op_LP_MSPM0G3507_nortos_ticlang". Need your help to understand better.
I am planning some kind of PI simulation to check whether the calculation is correct or not.
Set point is 3500 and ADC measured values are 3400 ,3600, 3450 and 3550. First iteration results as below:
error = 3500-3400=100
error31 = (100 << (31-12))-1 = 52428799 (0x031FFFFF)
integral = 0+(0x0CCCCCCC*0x031FFFFF >>31) = 5242879 (0x004FFFFF)
Proportional = 0x26666666*0x031FFFFF >>31 = 15728639(0x00EFFFFF)
output = integral + Proportional = 20971518 (0x013FFFFE)
outputPI = 0x013FFFFE*300>>31 = 2
First iteration results are as expected. Second iteration results as below:
error = 3500-3600=-100
error31 = (-100 << (31-12))-1 = -52428801(0xFCDFFFFF)
integral = 5242879 +(0x0CCCCCCC*0xFCDFFFFF>>31) = 429496726(0x19999996) -- Need explanation (Ideally zero)
Proportional = 0x26666666*0xFCDFFFFF>>31 = 1272761547(0x4BDCCCCB) -- Need explanation
output = integral + Proportional = 1503238553(0x59999999) -- clamped to higher value
outputPI = 0x59999999*300>>31 = 209 -- which is wrong
#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); } }
Please go through and let me know if any corrections.
Regards,
Yogesh
Hi Yogesh!
How are you taking time into account when calculating your integral?
-Matthew
Hi Yogesh!
We don't have an example of a PI compensator; however my suggestion here is to remove the MATHACL from your application, and test your algorithm in its simplest form. The MATHACL can be implemented after the algorithm is working correctly.
-Matthew
Hi Matthew,
Let me know the modifications required in my code.
I am not converting continuous time domain to discrete. Normal PI calculation only i am targeting.
Regards,
Yogesh
Hi Yegoesh,
PI is just some multiply, add, and judgement operations for input data.
You can refer to our IQMATH library to play with the mathacl. It has integrated the MATHACL inside the API, so user just requires call the API and do the multiply operation.
There is a SDK demo for this:
ti\mspm0_sdk_2_04_00_06\examples\nortos\LP_MSPM0G3507\iqmath\iqmath_mathacl_ops_test
You can find it source code and header in the SDK path: ti\mspm0_sdk_2_04_00_06\source\ti\iqmath
Below is the multiply code jsut for your reference:
/** * @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