Skip to content

Commit a7024ae

Browse files
raiden00plxiaoxiang781216
authored andcommitted
examples/foc: protect control loop with critical section
If the controller frequency is high, system timer interrupts will eventually interrupt the controller function, thereby increasing the execution time. This may lead to skipping the control cycle, which negatively affects the control algorithm. With this option enabled, interrupts are disabled for the duration of the controller function execution. Here example results from CONFIG_EXAMPLES_FOC_PERF output for b-g431b-esc1 board with CONFIG_EXAMPLES_FOC_NOTIFIER_FREQ=10000: 1. CONFIG_EXAMPLES_FOC_CONTROL_CRITSEC=n exec ticks=5258 nsec=30929 per ticks=21268 nsec=125105 2. CONFIG_EXAMPLES_FOC_CONTROL_CRITSEC=y exec ticks=3428 nsec=20164 per ticks=19203 nsec=112958 The difference is ~12us!
1 parent b85a5ed commit a7024ae

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

examples/foc/Kconfig

+18
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ config EXAMPLES_FOC_PERF
4747
bool "Enable performance meassurements"
4848
default n
4949

50+
config EXAMPLES_FOC_CONTROL_CRITSEC
51+
bool "Protect controller thread with critical section"
52+
default y
53+
depends on BUILD_FLAT
54+
---help---
55+
Protect controller thread with critical section.
56+
57+
If the controller frequency is high, system timer interrupts will
58+
eventually interrupt the controller function, thereby increasing the
59+
execution time. This may lead to skipping the control cycle, which
60+
negatively affects the control algorithm.
61+
62+
With this option enabled, interrupts are disabled for the duration
63+
of the controller function execution.
64+
65+
This option uses the kernel internal API directly, which means
66+
it won't work outside of FLAT build.
67+
5068
if EXAMPLES_FOC_PERF
5169

5270
config EXAMPLES_FOC_PERF_LIVE

examples/foc/foc_fixed16_thr.c

+16
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@
5050
# error
5151
#endif
5252

53+
/* Critical section */
54+
55+
#ifdef CONFIG_EXAMPLES_FOC_CONTROL_CRITSEC
56+
# define foc_enter_critical() irqstate_t intflags = enter_critical_section()
57+
# define foc_leave_critical() leave_critical_section(intflags)
58+
#else
59+
# define foc_enter_critical()
60+
# define foc_leave_critical()
61+
#endif
62+
5363
/****************************************************************************
5464
* Private Type Definition
5565
****************************************************************************/
@@ -340,6 +350,8 @@ int foc_fixed16_thr(FAR struct foc_ctrl_env_s *envp)
340350

341351
while (motor.mq.quit == false)
342352
{
353+
foc_enter_critical();
354+
343355
if (motor.mq.start == true)
344356
{
345357
/* Get FOC device state */
@@ -390,13 +402,15 @@ int foc_fixed16_thr(FAR struct foc_ctrl_env_s *envp)
390402

391403
/* Start from the beginning of the control loop */
392404

405+
foc_leave_critical();
393406
continue;
394407
}
395408

396409
/* Ignore control logic if controller not started yet */
397410

398411
if (motor.mq.start == false)
399412
{
413+
foc_leave_critical();
400414
usleep(1000);
401415
continue;
402416
}
@@ -503,6 +517,8 @@ int foc_fixed16_thr(FAR struct foc_ctrl_env_s *envp)
503517
/* Increase counter */
504518

505519
motor.time += 1;
520+
521+
foc_leave_critical();
506522
}
507523

508524
errout:

examples/foc/foc_float_thr.c

+16
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@
5050
# error
5151
#endif
5252

53+
/* Critical section */
54+
55+
#ifdef CONFIG_EXAMPLES_FOC_CONTROL_CRITSEC
56+
# define foc_enter_critical() irqstate_t intflags = enter_critical_section()
57+
# define foc_leave_critical() leave_critical_section(intflags)
58+
#else
59+
# define foc_enter_critical()
60+
# define foc_leave_critical()
61+
#endif
62+
5363
/****************************************************************************
5464
* Private Type Definition
5565
****************************************************************************/
@@ -353,6 +363,8 @@ int foc_float_thr(FAR struct foc_ctrl_env_s *envp)
353363

354364
while (motor.mq.quit == false)
355365
{
366+
foc_enter_critical();
367+
356368
if (motor.mq.start == true)
357369
{
358370
/* Get FOC device state */
@@ -403,13 +415,15 @@ int foc_float_thr(FAR struct foc_ctrl_env_s *envp)
403415

404416
/* Start from the beginning of the control loop */
405417

418+
foc_leave_critical();
406419
continue;
407420
}
408421

409422
/* Ignore control logic if controller not started yet */
410423

411424
if (motor.mq.start == false)
412425
{
426+
foc_leave_critical();
413427
usleep(1000);
414428
continue;
415429
}
@@ -516,6 +530,8 @@ int foc_float_thr(FAR struct foc_ctrl_env_s *envp)
516530
/* Increase counter */
517531

518532
motor.time += 1;
533+
534+
foc_leave_critical();
519535
}
520536

521537
errout:

0 commit comments

Comments
 (0)