+ Post New Thread
Results 1 to 15 of 15
  1. #1
    Member level 5
    Points: 707, Level: 5

    Join Date
    Mar 2018
    Posts
    88
    Helped
    0 / 0
    Points
    707
    Level
    5

    writing a program within ISR

    Hi

    I am implementing a PID controller and want to execute PID algorithm within an ISR. I have used the timer to trigger the interrupt . My problem is that how to keep ISR program small and how to redirect to other handler to deal with the PID. Secondly,The variables need to beglobal in order to correctly estimate the output value. How to do so? Using MPLABX IDE

  2. #2
    Super Moderator
    Points: 29,375, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,763
    Helped
    1108 / 1108
    Points
    29,375
    Level
    41
    Blog Entries
    9

    Re: writing a program within ISR

    This would be one of the few cases where performing routines within the Timer ISR would not be at all inconceivable, and it would obviously be presumed that the whole processing to be performed within each time slice is adequatelly sized and/or optimized to run in the shortest time.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  3. #3
    Member level 5
    Points: 707, Level: 5

    Join Date
    Mar 2018
    Posts
    88
    Helped
    0 / 0
    Points
    707
    Level
    5

    Re: writing a program within ISR

    Hi andre
    can you give details on my queries posted in #1



    •   AltAdvertisement

        
       

  4. #4
    Super Moderator
    Points: 75,476, Level: 67
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,299
    Helped
    3485 / 3485
    Points
    75,476
    Level
    67

    Re: writing a program within ISR

    Hi,

    A PID algorithm is small, and usuall doesnīt contain loops.
    Indeed itīs just multiplying and adding values.

    Only a couple of conditional branches are necessary to avoid overflow / underflow.

    So write the code in the ISR, estimate itīs run time and be sure that the maximum runtime is less than the interrupt-to-interrupt time.

    ****
    As so often in the posts: there are no specifications, thus we can give no detailed advice.

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  5. #5
    Member level 5
    Points: 707, Level: 5

    Join Date
    Mar 2018
    Posts
    88
    Helped
    0 / 0
    Points
    707
    Level
    5

    Re: writing a program within ISR

    Hi
    would the following strategy work. I think I can ensure that I do not load ISR
    Code:
    void _ISR _T3Interrupt(void)
    {
      IFS0bits.T3IF = 0;
    flag=1;
    }
    
    while(1)
    {
       if (flag==1)
    {
      execute my PID algorithm
       flag=0;
    }
    }



    •   AltAdvertisement

        
       

  6. #6
    Super Moderator
    Points: 79,807, Level: 68
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,063
    Helped
    4372 / 4372
    Points
    79,807
    Level
    68

    Re: writing a program within ISR

    If the "my PID algorithm" is short it will work but to be honest I would advise the method in post #5, especially if the MCU is doing other things as well. Technically, as long as the algorithm has run, finished and the ISR has returned before the next interrupt it will be safe but if another interrupt occurs (from any source) while still in the ISR it will at best give wrong results and could completely crash the program.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  7. #7
    Super Moderator
    Points: 29,375, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,763
    Helped
    1108 / 1108
    Points
    29,375
    Level
    41
    Blog Entries
    9

    Re: writing a program within ISR

    can you give details on my queries posted in #1
    A PID controller, likewise any other fixed-time-based digital process, should have priority in execution, and as sufficiently 'detailed' in post #2, this would be one of the few cases where I would particularly have preferred to place the PID process within the interrupt vector. If you prefer a more asynchronous approach, you will have to deal with the queue control of the read values.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  8. #8
    Member level 5
    Points: 707, Level: 5

    Join Date
    Mar 2018
    Posts
    88
    Helped
    0 / 0
    Points
    707
    Level
    5

    Re: writing a program within ISR

    Hi andre

    Do you feel the strategy adopted in #5 is incorrect. Will it not synchronously execute within the interrupt time period? Secondly I have seen sample codes using static double variable declarations. Could you please enlighten the benefit of the same



  9. #9
    Super Moderator
    Points: 29,375, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,763
    Helped
    1108 / 1108
    Points
    29,375
    Level
    41
    Blog Entries
    9

    Re: writing a program within ISR

    Do you feel the strategy adopted in #5 is incorrect
    I did not say anytime that the asynchronous strategy would be worse or better, but at least in my opinion running PID controller outside interrupt handler turn things more complex once this requires an extra layer of control in the program to manage the start of execution of each iteration in the main(), triggered on the Timer interrupt.

    Will it not synchronously execute within the interrupt time period?
    A PID controller will generate an output control value (Analog, PWM, etc ...) which regardless of whether it was generated within the ISR or not, its exhibition to the I/O interface have to be synchronous with the timer interrupt, and therefore, once again, in my oppinion it makes no sense to calculate and display the value in different vectors, namelly Main() and Interrupt(). Keep in mind that according to the classic model of digital control, it essentially consists of a data acquisition stage, a data processing stage, and an output stage. If some of them have to be computed in a chain, there is no impediment to put them in the same place, interrupt on this case.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  10. #10
    Super Moderator
    Points: 75,476, Level: 67
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,299
    Helped
    3485 / 3485
    Points
    75,476
    Level
    67

    Re: writing a program within ISR

    Hi,

    If the "my PID algorithm" is short it will work but to be honest I would advise the method in post #5, especially if the MCU is doing other things as well.
    Maybe it's personal taste, but here I don't agree.

    An example:
    * If one task is the PID controller (in my case to control the electrical energy for a chemical process)
    * another task is to display some values
    * another task is to poll pusbuttons

    Then I'd do the PID in interrupt context and the other tasks in main loop.
    I'd give the PID the highest priority. For a PID to work stable it needs stable timing, especially reading_the_inputs and updating_the_outputs.
    Any timing error will cause jitter and this jitter will cause noise and - depending on loop stability - ringing.
    If the in_to_out delay is too high, then even oscillation may occur.

    Maybe someone's argument is, that if thePID ISR takes too much time a new PID interrupt may be raised. This stricktly needs to be avoided. One needs to calculate/measure the maximum expectable ISR time. Then slow down the interrupt frequency.
    With my applications I keep the max expectable ISR time less than 80% of the interrupt period.
    In my eyes it's better to slow down PID ISR frequency but keep it constant, because one may adjust PID parameters to ensure stability.
    This constant behaviour is absolutely predictable.

    I know this sounds pedantic. But the chemical processes are critical .... and the product needs to be very uniform, one tiny irritation may cause the complete batch to be useless. A loss of 40,000€.

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  11. #11
    Member level 5
    Points: 707, Level: 5

    Join Date
    Mar 2018
    Posts
    88
    Helped
    0 / 0
    Points
    707
    Level
    5

    Re: writing a program within ISR

    Hi

    Can ISR handle large program? Should we declare all variables within ISR static?



    •   AltAdvertisement

        
       

  12. #12
    Super Moderator
    Points: 75,476, Level: 67
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,299
    Helped
    3485 / 3485
    Points
    75,476
    Level
    67

    Re: writing a program within ISR

    Hi,

    Can ISR handle large program?
    * large in instructions: Yes
    * large in time? --> already answered. It at least needs to faster than the interrupt period time.

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  13. #13
    Super Moderator
    Points: 257,269, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    44,887
    Helped
    13656 / 13656
    Points
    257,269
    Level
    100

    Re: writing a program within ISR

    Can ISR handle large program?
    The question has be answered in detail by KlausST. The total processing time of your controller task doesn't change either if it's performed in ISR or not. Problems arise if you don't know the actual processing time or if it's largely varying. Lengthy ISR routines however block the processing of other interrupts that must be performed in time, e.g interface handlers, unless you introduce well considered interrupt priorities.

    Should we declare all variables within ISR static?
    As in non ISR code, variables should be implemented as automatic or static depending on their function. Global or local static is just a matter of the variable scope. There are special considerations for those global variables that are accessed both in ISR and main level.



  14. #14
    Advanced Member level 3
    Points: 7,096, Level: 20
    Achievements:
    7 years registered

    Join Date
    Jul 2010
    Location
    Sweden
    Posts
    934
    Helped
    365 / 365
    Points
    7,096
    Level
    20

    Re: writing a program within ISR

    Quote Originally Posted by gary36 View Post
    Should we declare all variables within ISR static?
    You only need to declare them as static if they must keep their values between ISR executions.

    Global variables that are used by both the ISR and other parts of the software should be declared as "volatile".
    In your example in post #5, the variable "flag" should be "volatile".


    1 members found this post helpful.

  15. #15
    Advanced Member level 5
    Points: 16,434, Level: 30

    Join Date
    Nov 2012
    Posts
    3,010
    Helped
    730 / 730
    Points
    16,434
    Level
    30

    Re: writing a program within ISR

    PID controller and want to execute PID algorithm within an ISR...
    In general it is not a good idea, but you can set a flag that can be checked in the main routine. If your routine DOES take long time, it is better to search of alternative strategy.



--[[ ]]--