Home > Mobile >  Proper way to implement (DC motor) PI current controller with RTOS
Proper way to implement (DC motor) PI current controller with RTOS

Time:12-05

with my hobby project I started to rewrite the classic interrupt driven software to a new one with RTOS. I am using an STM32 with FreeRTOS but it is not important because my question is generally related to the problem that how to implement a fast PI with an RTOS.

  1. Idea: Put the current (torque) controller to an interrupt (for example: to the interrupt of the timer which used for the PWM) and then put the position controller to a thread, or into a sw timer.

  2. Idea Put the current controller to a thread witch waits for a signal based on a timer interrupt where the signal will be raised.

Current controller should be fast so I cannot put into a sw timer, but documents which I read about the RTOS said that I should put minimal code into the interrupt handler function when using RTOS and a PI controller is not that I think. This is why the 1. idea seems to be problematic.

But if I put the fast current controller into a thread which a signaled much faster than the systick, will it be able to handle it at all? Is it a terrible idea?

So the exact question is that: How should I implement a fast PI controller with an RTOS when the required frequency is well above the system frequency?

Thanks for the help!

CodePudding user response:

But if I put the fast current controller into a thread which a signaled much faster than the systick, will it be able to handle it at all? Is it a terrible idea?

You simply do not understand what the RTOS is. The tasks can switch as fast as you wish and it is not related to the timebasr RTOS clock. You may have 10Hz base RTOS clock and switch tasks many million times per second (assuming fast enough uC).

How to implement it logically.

  1. In the timer interrupt you collect the sensor data, send them to the queue for the PID main task. Leave interrupt routine
  2. When the queue receive your data, the PID task is run and does all the necessary calculations. Then it sends the data (using another queue) to the execution task. And then is waiting for the data from the sensors (it is BLOCKED and not consuming any processor resources).
  3. Execution task starts running when gots the data from its queue and ammends the current (for example). Then waits again for the new data.

Task switching is not related to the RTOS base clock. It is not like "normal" operating system. You decide when you pass the control to the RTOS (which then will decide which task to execute depending on the priorities and objects thew tasks are blocked on).

Insatead of queues you can use direct task notifications, semaphores or other IPC primitives available.

  • Related