Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C:
C#
void motordelay(float times) {
  float i;
  for (i = 0; i < times; i++) {
          nanosleep(1);
   }
}



QT:
MIDL
void motordelay()
{
    struct timespec slptm;
    slptm.tv_sec = 0;
    slptm.tv_nsec = 10;      //1000 ns = 1 us
    nanosleep(&slptm,NULL);
}
Posted
Updated 13-May-10 0:38am
v3

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.
 
Share this answer
 
v2
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.
:)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900