如何使用nanosleep? [英] How to use nanosleep?

查看:1593
本文介绍了如何使用nanosleep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux中使用nanosleep()函数来延迟电动机.该平台是QT4.5.3,Linux 2.6.24,ARM AT91SAM9263-ARM9-240MHz.我使用C语言和QT来控制步进电机,但是速度太慢.

C:

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:




QT:

void motordelay()
{
    struct timespec slptm;
    slptm.tv_sec = 0;
    slptm.tv_nsec = 10;      //1000 ns = 1 us
    nanosleep(&slptm,NULL);
}

推荐答案

如果您的CPU以240MHz的频率运行,则一个CPU周期约为4.167ns,因此这次每次系统调用等都在讨论.自从您拨打nanosleep(1)以来,每次通话至少花费您请求时间的4.167倍.
尝试以更高的值调用nanosleep!

如果您从nanosleep(man 2 nanosleep)查找联机帮助页.您将阅读以下说明:

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
man 2 nanosleep

如果req中指定的间隔不是底层时钟的精确整数倍(请参阅time(7)),则间隔将四舍五入到下一个倍数.

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.


摘自nanosleep(2)手册页:

From nanosleep(2) man page:


因此,nanosleep()总是至少暂停指定的时间,但它可能比指定的时间长10 ms ,直到该过程再次可运行为止.

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.
:)


这篇关于如何使用nanosleep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆