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.

including iqmath.lib in matlab legacy_code -tool



Hi,

i  use the C2000-target blocksets in order to produce firmware for my C2812-board. Everything is working fine.

I can use the blocksets to test them in a simulink-simulation. And after compiling and buildung the Code is working fine on my ezdspC2812-Board.

Now I want to write some handwritten-code in c an include it in my existing projekt.  I use the legacy_code tool in such a way, that I write an m-File in oder to build a s-function:

Furthermore I want to use IQmath.lib in oder to have access to the build-in functions in my c-routines. My code is working fine when no build-in function of the IQmath.lib is used. I can use the s-function in simulation and I can use it for code generation.

Bat if I use one of the Biuld-In-Functions like _IQ15sin(A) my simulation crashes. Any ideas?

Chris

Here is my script to produce n s-function "pid_regler" which can be used in a simulink-file and which can be used for code generation.

clear, clc;
def=legacy_code('initialize');
def.HeaderFiles={'dmctype.h','IQmathLib.h','pid_reg3.h'};
def.SourceFiles={'pid_reg3.c'};
def.HostLibFiles={'IQmath.lib'};
def.SFunctionName ='pid_regler';

def.OutputFcnSpec ='int32 y1 = legacy_pid(int32 u1[2], int32 p1[6], int32 work1[4])';

legacy_code('sfcn_cmex_generate',def);

legacy_code('compile', def);
legacy_code('slblock_generate', def);

 

  • Hi Christoph,

    The mathworks may be able to help.  Send your question to c2000_expert@mathworks.com. and reference this post.

    Best Regards

    Lori

     

    Christoph Dr. Weber said:

    Hi,

    i  use the C2000-target blocksets in order to produce firmware for my C2812-board. Everything is working fine.

    I can use the blocksets to test them in a simulink-simulation. And after compiling and buildung the Code is working fine on my ezdspC2812-Board.

    Now I want to write some handwritten-code in c an include it in my existing projekt.  I use the legacy_code tool in such a way, that I write an m-File in oder to build a s-function:

    Furthermore I want to use IQmath.lib in oder to have access to the build-in functions in my c-routines. My code is working fine when no build-in function of the IQmath.lib is used. I can use the s-function in simulation and I can use it for code generation.

    Bat if I use one of the Biuld-In-Functions like _IQ15sin(A) my simulation crashes. Any ideas?

    Chris

    Here is my script to produce n s-function "pid_regler" which can be used in a simulink-file and which can be used for code generation.

    clear, clc;
    def=legacy_code('initialize');
    def.HeaderFiles={'dmctype.h','IQmathLib.h','pid_reg3.h'};
    def.SourceFiles={'pid_reg3.c'};
    def.HostLibFiles={'IQmath.lib'};
    def.SFunctionName ='pid_regler';

    def.OutputFcnSpec ='int32 y1 = legacy_pid(int32 u1[2], int32 p1[6], int32 work1[4])';

    legacy_code('sfcn_cmex_generate',def);

    legacy_code('compile', def);
    legacy_code('slblock_generate', def);

     

     

  • Chris

    I have the same problem as you, Would you post "pid_reg3.c",so I can trace my problem, Thanks.

    Best Regards

    John    

  • John,

    here is what the c2000expert wrote to me concerning my adressed problems:

    Chris -

    IQmath.lib contains processor-specific code - only the TI c2000 compiler can understand it. You can use the IQmath blocks that we provide as part of the Target Support package to simulate and generate code for the IQsin and IQcos functions.

    The Legacy code tool can only compile code that the host computer can understand (no IQMath calls).

    The TLC file that the Legacy Code Tool creates can be customized to call into processor specific code.

    In other words you can create an S-function with regular calls to the sin function from math.h library. In the TLC that is generated by the legacy code tool you replace this call with IQsin and you include IQMath.h

    I hope it makes sense, let me know if not.

    Regards,
    MathWorks C2000 team


    That means every function in iqmath.lib can not be used in simulation. Fortunately iqmathlib.h contains some macros for _IQmpy which can be used for simulation: here is my code:

     

    clear, clc;
    def=legacy_code('initialize');
    def.HeaderFiles={'dmctype.h','IQmathLib.h','pid_reg3.h'};
    def.SourceFiles={'pid_reg3.c'};
    %def.HostLibFiles={'C28x_FPU_Lib.lib'}; %Lib-Files can't be used!!!!!
    def.HostLibFiles={'IQmath.lib'};
    %def.HostLibFiles={'IQmath_fpu32.lib'}
    def.SFunctionName ='pid_regler';

    def.OutputFcnSpec ='void legacy_pid(int32 y1[3],int32 u1[2], int32 p1[6], int32 work1[4])';

    legacy_code('sfcn_cmex_generate',def);

    legacy_code('compile', def);
    legacy_code('sfcn_tlc_generate', def);
    legacy_code('slblock_generate', def);

     

    #include "dmctype.h"
    #include "IQmathLib.h"
    #include "pid_reg3.h"

    //Define Global Q_Value as 19->Maximum Value: 4095

    void legacy_pid(int32 *U_out, int32 *inp, int32 *param, int32 *memory)
    {
        int32 U_d;
        int32 U_out_presat;
        int32 inp_ref =(*inp);
        int32 inp_fdb =*(inp+1);
        int32 inp_err = inp_ref-inp_fdb;
        int32 K_p     = *param;
        int32 K_i     = *(param+1);
        int32 K_d     = *(param+2);
        int32 K_c     = *(param+3);
        int32 Max_out = *(param+4);
        int32 Min_out = *(param+5);
        int32 U_i     = *memory;
        int32 U_saterr= *(memory+1);
        int32 U_p     = *(memory+2);
        int32 U_p_1   = *(memory+3);
       
        U_p =_IQmpy(K_p,inp_err);
        U_i = (U_i + _IQmpy(K_i,U_p)); // + _IQmpy(K_c,U_saterr);
        U_i = U_i + _IQmpy(K_c,U_saterr);
        U_d = _IQmpy(K_d,(U_p -U_p_1));
        U_out_presat=U_p + U_i + U_d;
       
       
        if (U_out_presat > Max_out)
          *U_out = Max_out;
        else if (U_out_presat < Min_out)
           *U_out = Min_out;
        else
           *U_out = U_out_presat;

        U_saterr = *U_out-U_out_presat;
        U_p_1    = U_p;
       
        *memory     = U_i;
        *(memory+1) = U_saterr;
        *(memory+2) = U_p;
        *(memory+3) = U_p_1;
       
        *(U_out+1)  = U_out_presat; //Just for debugging purpose
        *(U_out+2)  = U_i;          //Just for debugging purpose
    }


    With regards

    Chris 


  • Chris

    Thanks for your kind post. I really appriciate your help. But I have tried many times, I have the same result as follows:

    undefined reference to '___IQmpy'  

    Best regards

    John

  • John,

    i added following macro in iqmathlib.h:

    #define   __IQmpy(A,B,GLOBAL_Q) (((long long)(A)*(B))>>(GLOBAL_Q))

    Put this before _IQmpy(A,B) is defined!

    Best ragards

    Chris

  • Chris,

    Now it's very OK. Thanks for your kind post. I really appreciate your help.

    And  like _IQ15sin(A), do you have any new idea to treat and to involve in matlab?

    Best Ragards

    John

     

     

  • Chris,

    My difficult problem is how to tune the PID controllers on the controller side to suit the specific motor being tested and to achieve optimal results.

    Hardware:
    *The High Voltage Digital Motor Control Kit(F28035 controlCARD)
    *PMSM Motor
    *BLDC Motor

    Can i use c2812pmsmsim.mdl ?

    my  PID controller tuning step:
    *i use  ac6_example.mdl(PMSM) and ac7_example.mdl(BLDC)
    *ac7_example.mdl(BLDC) have PI gain calculator
    PMSM Motor:
    Proportional gain(Kp) 0.0022
    Integral gain(Ki) 0.5(for TI scaling:0.011 )
    BLDC Motor:
    Proportional gain(Kp) 0.005
    Integral gain(Ki)  2.00(for TI scaling:0.02 )

    Testing them in  simulink-simulation is  working fine.
    But in c2812pmsmsim.mdl,the result is very awful.
    i find that inverter block iust  represents an average-value three-phase inverter. "Duty" contains the duty cycles [alpha_a,alpha_b,alpha_c] of the three inverter arms.
    and the DSP controller generates six pulse width modulation (PWM) signals using vector PWM technique for six power switching devices.

    How do i select good inverter model in simulink-simulation ? And how to get the optimal PID Gain in simulink-simulation ?
    Do you have any simulink-simulation to tune the PID controllers and to match processor in the loop result ?

    Best Regards

    John    

     

  • Hi, John

    Have you ever solved the problem with enabling _IQsin(A) "recognition" while making legacy tool function with IQmath.lib ?? I tried several tricks, made the legacy tool compile the prog, but it doesn't function correctly.. I would appreciate your help so much.

    Best regards,

    Alex.

  • Hi Everybody,

    I am trying to used the legacy tool to import my code into MATLAB. I have been working for a while and found a demo about fixed point quite useful. This help topic in MATLAB 'Calling Legacy Functions Using Fixed Point Signals' gave me really good understanding and the example was also nice. Now the problem is, in my original code I am using texas instruments iqmath library. I am not sure now how to use it with MATLAB i.e. initialize the variable with Q format in MATLAB.

    e.g. in the file timefixpt.h in MATLAB demo the fixed point is defined as->
    typedef int16_T myFixpt;
    and then it is used further in the .c file and similarly in matlab. In my case I am defining a variable with Q format e.g int iQ24_X

    Now my question is how can I assign this in my .m file in MATLAB also if I can get some explanation on using the built in iqsin, iqcos funtion it would be very helpful--


    Regards,

    Naqqash

  • Hi Everybody,

    I am trying to used the legacy tool to import my code into simulink. 

    where can I find some examples?

    For example, someone knows how to integrate this line of code:

        GpioDataRegs.GPACLEAR.all = 0x00000001 ?
    Best Regards,
    D. Detomaso