Thread: Linux/C: Unexpected CPU-Delays between several "usleeps"

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    5

    Linux/C: Unexpected CPU-Delays between several "usleeps"

    Hallo,

    ich have a problem.

    currently i try to programm a robo-spider with 6 legs and 2 motors each leg on linux/c and with parport. Therefor i use servomotors which need exact millisecond-PWMs.
    When i want to push 6 Servos to the ground in ordner to hold the body, i need to program 6 different PWM-Duty-Cycles in one loop.
    In my project the times for the angles i want are the following:

    -> Servo 1 - Servo 6 = S1-S6

    S1 = 1,95ms
    S2 = 1,700ms
    S3 = 1,5ms
    S4 = 1,4ms
    S5 = 1,25ms
    S6 = 1,1ms

    Now to the loop:

    Code:
    while(1){
    outb((S1|S2|S3|S4|S5|S6),DATAPORT);
    usleep(1100);
    outb((S1|S2|S3|S4|S5),DATAPORT);
    usleep(150); //1250-1100
    outb((S1|S2|S3|S4),DATAPORT);
    usleep(150); //..
    outb((S1|S2|S3),DATAPORT);
    usleep(100); //..
    outb((S1|S2),DATAPORT);
    usleep(200); //..
    outb((S1),DATAPORT);
    usleep(250);
    outb(0b00000000,DATAPORT);
    usleep(20000); //PWM-Period
    }
    This happens: All angles are wrong because the CPU has some strange delays. All Duty-Cycles are not the same as i messured them before. Do you know how to solve this problem? It needs to be more real-time.

    And sorry for my bad englisch. I would be happy if you could help me!
    Last edited by smolnar; 11-07-2016 at 10:35 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,215
    Linux isn't a real-time operating system. It is designed to be 'fair' to all userland processes, where each ready process will eventually get a slice of CPU time.
    The upshot of this is that even the most greedy of high priority processes will get descheduled at some inopportune moment.

    Quote Originally Posted by https://linux.die.net/man/3/usleep
    The usleep() function suspends execution of the calling thread for (at least) usec microseconds. The sleep may be lengthened slightly by any system activity or by the time spent processing the call or by the granularity of system timers.
    There is only a lower bound to the sleep interval, there is no guaranteed upper bound. At best, you get some kind of average a little over what you asked for.

    This could be worth looking at.
    RTAI - the RealTime Application Interface for Linux - Homepage

    Another idea is to implement your own kernel driver (where you would have more control over timing specifics) to implement the actual 'outb' and sleep patterns. You would then communicate with this driver via the standard driver interface (open/read/write/close/ioctl).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    5
    @Salem, thx for your very good answer!
    I will go on with my programming with an average delay value and if this not works i will look at the RTAI-thing. I think RTAI is what i need.
    Kernel driver... i read much about it and saw few examples but i think its too complicated for me in ordner to just get 6 angles with which i can work with.

    What do you think about working with threads?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,215
    Threads won't solve the underlying fairness 'problem' of the Linux kernel. You will still experience out of tolerance sleep delays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  2. "std::stringstream" unexpected behavior (simple example)
    By symbiote in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2012, 09:00 PM
  3. I am confused by "Linux thread" and "NPTL"
    By meili100 in forum Linux Programming
    Replies: 6
    Last Post: 03-27-2008, 12:14 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread