Using while loop in c block

reza hakimi_20486
reza hakimi_20486 Altair Community Member
edited August 2023 in Community Q&A

Hello
In a program, I want the C block to continuously read the input and change the output according to the input
Now I have a few questions
1. Should I put the whole program in a while loop?
2. If I put it in the while loop, the speed of the simulation will decrease drastically, what is the solution to this problem?
3. Is there any other tool in psim other than Block C that has the ability to generate code and do this for me
I will put the image of the program in the continuation of the question

Tagged:

Best Answer

  • Albert_Dunford
    Albert_Dunford
    Altair Employee
    edited August 2023 Answer ✓

    there is no break to your c code. That while loop will keep on running, and never stop. the while loop is not necessary keep the if else statements the c block executes at each time step, you are defeating this action by putting in the while loop. Please consider how does the code ever exit that while loop you have created? it is infinite.

    We need to consider how c code works on an MCU. Typically it is interrupt driven, at some rate the inputs will be sampled, then you will do calculations on these inputs and make decisions, then do it all again.

    Your code does not follow this structure you are trying to enforce the polling from within the code when really you should be letting PSIM do this for you. This is the point of the code generation and you are trying to work around this.  Please review this tutorial video ,

    I answered a support question on the other day. Trying to do edge detection on the encoder outputs with GPIOs is only going to work with very slow motor speeds. The peripheral encoder inputs is what you need to be using, they are properly design for this.

    If you truly want to do this, you need to put a ZOH in front of the c block and remove the loop structure and just have the if else statements. please look in the examples at the BLDC setups, they do something similar to what you want. examples\Code Generation\F2806x Target\TI High-Voltage BLDC Drive (sensored)\level5

    to get any decent performance you are likely going to overload the CPU.

Answers

  • Albert_Dunford
    Albert_Dunford
    Altair Employee
    edited August 2023 Answer ✓

    there is no break to your c code. That while loop will keep on running, and never stop. the while loop is not necessary keep the if else statements the c block executes at each time step, you are defeating this action by putting in the while loop. Please consider how does the code ever exit that while loop you have created? it is infinite.

    We need to consider how c code works on an MCU. Typically it is interrupt driven, at some rate the inputs will be sampled, then you will do calculations on these inputs and make decisions, then do it all again.

    Your code does not follow this structure you are trying to enforce the polling from within the code when really you should be letting PSIM do this for you. This is the point of the code generation and you are trying to work around this.  Please review this tutorial video ,

    I answered a support question on the other day. Trying to do edge detection on the encoder outputs with GPIOs is only going to work with very slow motor speeds. The peripheral encoder inputs is what you need to be using, they are properly design for this.

    If you truly want to do this, you need to put a ZOH in front of the c block and remove the loop structure and just have the if else statements. please look in the examples at the BLDC setups, they do something similar to what you want. examples\Code Generation\F2806x Target\TI High-Voltage BLDC Drive (sensored)\level5

    to get any decent performance you are likely going to overload the CPU.