I have a PID loop for a small vehicle implemented in a microcontroller. Currently the PID loop is implemented in a timer interrupt. I want to implement an FSM that will have states that enable or disable the function that the PID loop is controlling, so in some states the PID loop should be disabled and in some it's enabled. I want to implement this control in a way that minimally disturbs the PID loop.

I've been told that I shouldn't disable the timer interrupt if possible. Is a good option for this to implement the FSM itself inside of the timer interrupt and then update the PID loop inside the FSM states?

The alternative would be to have just the PID loop inside the timer interrupt and then the FSM implemented inside the microcontroller's main(), which I think would need to temporarily enable or disable the timer loop while updating state.


You could probably just have the FSM outside the interrupt routine since it is not a time critical function of your system as a PID is.

Then in the FSM can update an enable or disable variable that will enable or disable to use the PID computed value (rather than not compute)

A pseudo-code would look like

void time_interrupt() 
    if USE_IT

void main()
    for( , ,) 
        switch FSM:
            case A: 
                 USE_IT = false
            case B: 
                 USE_IT = true

Deciding where to deploy the FSM, either in the timer interrupt or in the main routine, is the easiest part. Either way will work just fine. Probably, the FSM is not that critical, hence it could be conveniently running within main(), whereas disabling/enabling the timer interrupt is not really necessary if you resort to the use of a dedicated flag (protected with a mutex) for that.

The trickiest part, instead, is represented by how you stop and reinstate the PID controller without yielding unwanted phenomena. As you know, a PID contains internal states that need to be carefully blended with the plant dynamics that certainly continues to evolve in the meantime.

There exist suitable ways to implement the so-called bumpless switching you may look for in literature or on the web. One interesting example is this practical resource.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.