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.

TMS320F28335: TMS320F28335: ECap, EPWM & pid code Integration

Part Number: TMS320F28335


Hi,

Previously, I worked on eCap and ePWM module in my previous thread and then I was successful to implement both modules. The task was to take input from the function generator as digital pulse signal having freq and duty cycle from eCap funtion and then copying or generating the same pulse with same freq and duty cycle through ePWM function. The code was working perfectly and I can see variables values PWM_Period & PWM_Duty in terms of TBPRD (for 10KHZ  PWM_Period =15000 &  PWM_Duty = 7500 respectively)....(Previous Work)

The Problem comes when I add the PID code routine from line 44-62 and line 293-314 in my code of previous work. Here i am not using any DCL library for PID simulation. For some background information, like in aurdino, Matlab or other microcontroller, the pid routine code is used commonly for pid simulation. Therefore, I added the pid routine code from line 44-62 & line 293-314 respectively in my code. The reason for adding pid routine code is to update the period & duty cycle both after pid while loop in terms of NEW_Period/NEW_Duty.

After resolving the building errors, I simulated the code on my hardware and check the variables again PWM_Period & PWM_Duty in terms of TBPRD. These variables are not updating and showing WRONG values. I change the knob from the function generator like from 10Khz to 20KHz but the PWM_Period & PWM_Duty variables are not updating themselves. Due to which, NEW_Period/NEW_Duty are also showing incorrect values. Probably, It may be possible mistake that the placement of pid routine in my code is not at the correct line position.

  

Please suggest accordingly.

Regards

Arsalan

  • Hi Arsalan,

    To clarify, there was no issue with your code before adding lines 293-314? If so, can you set a breakpoint at line 293 of your code and identify the first instance where you see unexpected values?

    Thank you,

    Luke

  • Hi Luke,

    Thanks for your reply.

    Since I am not familiar much about the CCS tool. Could you suggest stepwise how to use the breakpoints in my program and watch the execution of program so that i could figure out the problem?  I have to look these 4 variables PWM_Period, PWM_Duty, NEW_Period & NEW_Duty before and after adding pid code lines. 293-314.

    Before adding these lines, the PWM_Period, PWM_Duty variables are showing the correct values but after adding pid code lines 293-314, It shows incorrect numbers on expressions window.

    The code:

    Please suggest accordingly

    Thanks

    Kind Regards

    Arsalan

  • Hi Arsalan,

    To set breakpoints in CCS, you just need to double click on the line number in your code before you begin running the program.

    To observe the value of different variables, you can right click on any variable and click "add watch expression". You can also hover over a variable with your mouse to observe it's value, even if it has not been added to the watch window.

  • Hi Luke Jones,

    Thanks for your reply.

    First Change:

    I was having a warning (at variable updated_dutycyle->statement is unreachable). In order to remove this, I changed the while(1) by while(e > 1e-3).

    2nd Change: 

    Line 307-317. I use a 'if statement' for the defining the pid_out limits between 0-3V.

    After all changes and removing building errors, I simulated the code on hardware and used Breakpoints at variables. PWM_Period, PWM_Duty, NEW_Period & NEW_Duty. from line 323. What I noticed after using breakpoints is described below:

    (Remember H/W & S/W both breakpoints are used, shown in breakpoints window) 

    Picture 1, when 10KHz is applied from Func Generator, and then I press Green button (Debug) one time: the variables at expression window: PWM_Period, PWM_Duty gives correct values 14990 & 7495 approximately. (TBPRD =15000 & TBPRD/2=7500 ).

    Picture 2, Now press Green button (Debug) 2nd time: the variables at expression window: PWM_Period, PWM_Duty remain same which is correct for 10Khz but variable NEW_Duty becomes 0. Remember NEW_Period & NEW_Duty variables must be updated with new period and new duty cycle after pid while loop.

     

    Picture 3, Now press Green button (Debug) 3rd time: the variables at expression window: PWM_Period, PWM_Duty remain same which is correct again for 10Khz but variables NEW_Period & NEW_Duty both become 0. Remember these two variables must be updated new period and new duty cycle after pid while loop.

    Picture 4, Now press Green button (Debug) 4th time: the variables at expression window: PWM_Period, PWM_Duty, NEW_Period & NEW_Duty, all become 0. But here all the values are incorrect.

    After Picture 4, further execution/debug keep all variables to 0 (no changes). Well in our case as I am expecting,  PWM_Period, PWM_Duty must remain 15000 & 7500 for 10KHz signal from Func Gen. and   NEW_Period & NEW_Duty must be some values after updating from pid routine. code. 

    Please suggest in the code to resolve this issues.

     Line 293-324: 

    Thanks

    Kind Regards

    Arsalan

  • Arsalan,

    The second screenshot in your most recent post seems to be where things go wrong. Can you also add pid_out and updated_dutycycle to your expression window, and step through each line of code one by one at that point in your code. After each step, please check to make sure ALL variables in the expression window have their expected values.

    Thank you,

    Luke

  • Hi Luke,

    Thanks for your reply. 

    When I added more variables and checked at different breakpoints then I got the very first problem from Duty_cyle (double) variable which is not updating itself.

    I use breakpoints simuation for One Cycle starting from

    Line 293: Duty_cycle = (int32)PWM_Duty / (int32)PWM_Period; // This number is a ratio between 0-1

    till Line 324: NEW_Period = NEW_Duty * 2;

    I found the issues from the beginning, the variable '"Duty_cycle" (double) which is not updating itself as you can see from the Expression Window. see Pictures below:

    Picture 1 (Breakpoint cycle start)

    Picture 2

    Picture 2. (Breakpoint cycle end)

    Please suggest accordingly

    Thanks

    Kind Regards

    Arsalan

  • Hi Arselan,

    In line 293, it seems you are setting Duty_cycle equal to the division of two integers, where the numerator is smaller than the denominator. This will always result in a value of 0. If you want to cast PWM_Duty and PWM_Period to an integer but still obtain the ratio between them, you need to recast both numbers to a double like this:

    Duty_cycle = (double)((int32)PWM_Duty) / (double)((int32)PWM_Period);

    Hopefully my syntax is correct... let me know if this works for you.

    --Luke

  • Hi Luke,

    Yes this 'double' works fine. Thanks.

    I will resolve this issue and will ask again in another post about the loop because the loop is creating an issue to update the NEW_Period & NEW_Duty once i collect more data.

    Thanks 

    Kind Regards

    Arsalan

  • Hi Muhammad,

    Understood, glad this solution worked for you. Let me know if you have any other questions, otherwise I will close this thread.

    Thank you,

    Luke