Hello everyone,
I was wondering how can I make a precise delay that is shorter then 1ms
on C++(I need it to drive a servo motor via PC)?

If I understand correctly the standart delay function has a resolution of 1ms,
so it won't do it...

Thank you,
A.

Dani AI

Generated

Short answer: a normal Windows or Linux PC cannot reliably generate sub-millisecond, low-jitter pulses for servo timing. As points out, general-purpose OS schedulers and device interrupts introduce unpredictable latency; you can measure time precisely from user space, but you cannot reliably schedule very short pulses there.

If you must stay on the PC, the usual pattern is measurement + busy-wait: use a high-resolution counter to detect the moment to toggle a line, and raise thread priority and timer resolution to reduce jitter. See the Microsoft docs for QueryPerformanceCounter (high-resolution time source) and timeBeginPeriod (change timer granularity) for details: https://learn.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter and https://learn.microsoft.com/en-us/windows/win32/api/timeapi/nf-timeapi-timebeginperiod. This works for experiments but burns CPU, is vulnerable to preemption, and still cannot guarantee hard real-time behavior.

Recommended, practical approach: push the timing off the PC onto hardware built for it. Use a microcontroller or a dedicated USB/serial servo controller (the PC sends position commands; the hardware generates PWM with microsecond accuracy). Cheap microcontrollers (Arduino, etc.) are widely used for this; see https://www.arduino.cc/. That design eliminates OS jitter and is far simpler to debug.

Testing and hardening: verify pulse width and jitter with an oscilloscope or logic analyzer. If you must try on Linux, consider real-time patches or real-time scheduling with clock_nanosleep (man page: https://man7.org/linux/man-pages/man2/clock_nanosleep.2.html), and set the CPU governor to performance and disable power-saving C-states. Final note: serial-bit-timing tricks can work for rough pulses but are fragile due to buffering and driver behavior; for reliable sub-ms control, use dedicated hardware.

Recommended Answers

All 7 Replies

>>I was wondering how can I make a precise delay that is shorter then 1ms
depends on the operating system. Neither MS-Windows nor *nix will allow you to do that because neither operating systems are real-time os. There are a lot of other programs that have to be executed as well as yours and they all require cpu time. Its just not possible to devote that much time to just one process. You could give your program a higher priority but you risk the danger of halting the entire operating system when you do that.

Even though the delay functions have a resolution of 1 ms its very unlikely that will be achieved in real tests -- normally do not expect a better resolution than about 10 ms or so, depending on what other processes are doing.

On Windows, check out QueryPerformanceCounter() and QueryPerformanceFrequency().

On Windows, check out QueryPerformanceCounter() and QueryPerformanceFrequency().

those are not system delay functions.

No, but they can be used to roll your own by polling them in a loop.

Like AD said though, you are trying to do something a little unlikely...

Ok,
thanks.
(But I'll still try to find a way to do it,because using computer ports for
robotics expirements is realy comfortable :-) )

Thank you very much for the link,I will look into it.
I was also thinking of using the serial port (com ) to recieve a precise pulse
(the serial comunication rate(bps) should be fast enough for me to recieve the wanted
pulse lenght by playing with the amount of '1's ).


A.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.