Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Linux Hardware QT
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:
void motordelay(float times) {
  float i;
  for (i = 0; i < times; i++) {
          nanosleep(1);
   }
}
 

QT:
void motordelay()
{
    struct timespec slptm;
    slptm.tv_sec = 0;
    slptm.tv_nsec = 10;      //1000 ns = 1 us
    nanosleep(&slptm,NULL);
}
Posted 6-May-10 18:11pm
longhr108
Edited 13-May-10 1:38am
Moak11.4K
v3
Rate this: bad
good
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.
  Permalink  
v2
Rate this: bad
good
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.
Smile | :)
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 515
1 Maciej Los 349
2 Richard MacCutchan 220
3 BillWoodruff 209
4 Mathew Soji 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 13 May 2010
Copyright © CodeProject, 1999-2014
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