Using Thread Sleep in an application to Control a motor - Is there a better alternate

44 views Asked by At

All,

FYI: C# .net app running on Windows controlling a piece of hardware (DC motor) via I/O board

I have a program that controls a DC motor. I create a dedicated (higher priority) thread (to control the rotation of the motor independently of the running application).

Based on a specific running use-case, the program issues the motor a command that includes angular position information when to stop motor rotation (technically, the dedicated motor thread receives the position, wakes up, energizes the motor, and then monitors the motor progress until the desired position is reached).

Question:

The dedicated motor thread uses Thread.Sleep(30ms) function to wait for the hardware to:

-Energize the motor

-Start moving the motor in a direction

-Allow some time to make some progress based on the speed of the motor (before checking for desired end condition, ie has the motor reached desired position?)

Since the CPU runs at 4.7GHz, you want some time to elapse before doing any logic for checking if the motor reached its desired position.

Is there a better a way of doing this other than Thread.Sleep()? I really don't care that much about precise timing or the lack of it because Sleep() doesn't give you that but more interested in having smooth operation of the motor and as close to real-time checking as possible on Windows to see if the motor reached desired position.

0

There are 0 answers