Click here to Skip to main content
13,053,276 members (61,313 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I use nanosleep() function in Linux to delay a motor. The platform is QT4.5.3, Linux 2.6.24, ARM AT91SAM9263-ARM9-240MHz. I use the C language and QT to control the step motor, but the speed is too slow.

void motordelay(float times) {
  float i;
  for (i = 0; i < times; i++) {

void motordelay()
    struct timespec slptm;
    slptm.tv_sec = 0;
    slptm.tv_nsec = 10;      //1000 ns = 1 us
Posted 6-May-10 17:11pm
Updated 13-May-10 0:38am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If your CPU runs at 240MHZ one CPU cycle is about 4.167ns so every system call etc is talking this time. Since you call nanosleep(1) every call is taking at least 4.167 times the time you requested.
Try calling nanosleep with higher values!

if you look in the manpage from nanosleep (man 2 nanosleep). You would have read the following notes:

man 2 nanosleep
If the interval specified in req is not an exact multiple of the granuâlarity underlying clock (see time(7)), then the interval will be rounded up to the next multiple.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

From nanosleep(2) man page:

Therefore, nanosleep() pauses always for at least the specified time, however it can take up to 10 ms longer than specified until the process becomes runnable again

See [^], [^].

Bottom line: Linux (like Windows) is not a real time operativ system, and your best bet in order to control a step motor is using a microcontroller.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 13 May 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100