I'm currently working with embedded system implemented on SMT32F103C8T6 Blue Pill microcontroller based on FreeRTOS. I want to control the velocity and position of DC motor using PID controller. For the position control, i got no problem at all. But, for the velocity control, i'm confused on how do i get the time difference for calculating the sum of error (for integral controller) and delta of error (for derivative controller). sum_error += current_error * delta_time; diff_error = (current_error - prev_error)/delta_time;

In most applications, we could use time = millis() - prevMillis;. But in FreeRTOS, we shall not use millis, as it would disturb the task schedulling of the RTOS.

So, is there any other way to get the time difference? Notes: I've tried using a constant value of 0.01 (ms), but it doesn't work (the PID controller seems not working).

  • \$\begingroup\$ Your question is confusing. Are you using a stepper motor? Other than a linear motor it is the only one where you have position control. To make a stepper motor spin fast you need other routines to take control of the motor. \$\endgroup\$ – user105652 Dec 14 '18 at 1:42
  • 1
    \$\begingroup\$ Your idea that querying the time would disturb the scheduling is entirely mistaken. However, if you have an RTOS with a scheduling guarantee you trust, you could schedule for an interval and use that as the denominator, if you trust that the interval will actually be achieved. Or you can schedule for a desired interval but use the actual interval in your calculation. \$\endgroup\$ – Chris Stratton Dec 14 '18 at 1:51
  • 2
    \$\begingroup\$ I'm voting to close this question as off-topic because it is based on a false premise \$\endgroup\$ – Chris Stratton Dec 14 '18 at 1:51
  • \$\begingroup\$ I see. Sorry for the false premise & off-topic question. I haven't got the hang of this forum rules \$\endgroup\$ – Marchio Kevin Dec 29 '18 at 12:55

The standard FreeRTOS way of getting "time" is xTaskGetTickCount(). You can use configTICK_RATE_HZ to convert ticks to time.

You can also use one of the hardware timers you probably have for timestamping.


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