Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sleep less than 1 ms

Sleep less than 1 ms

Scheduled Pinned Locked Moved C / C++ / MFC
cssquestioncareer
19 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Adnan Merter

    Hi everybody, i have been working on a step controller program, and i succeed to control, but i cant run the step motors as fast as i want i am using the Sleep(1) function in "windows.h" to delay next step, but it is still so slow i need something like Sleep(0.05) :-D , is there someone who can suggest me a method to do this job? thanx,

    --always comes daylight after night-----

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #7

    Adnan Merter wrote:

    is there someone who can suggest me a method to do this job?

    Yes, use a microcontroller. :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

    In testa che avete, signor di Ceprano?

    A 1 Reply Last reply
    0
    • A Adnan Merter

      Hi everybody, i have been working on a step controller program, and i succeed to control, but i cant run the step motors as fast as i want i am using the Sleep(1) function in "windows.h" to delay next step, but it is still so slow i need something like Sleep(0.05) :-D , is there someone who can suggest me a method to do this job? thanx,

      --always comes daylight after night-----

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #8

      Hi, whatever sleep() function you use, you will get a stand-still for at least the time you specify, however it could be much longer as there are dozens of threads all running on a typical Windows system. The only scheme that might work to some extent is this: - assume a multi-core system; - create a program (or a thread in an existing program) that contains busy loops, so it is simply getting and checking time all the time, then setting the output(s) as required. Of course you need a high-precision timer, maybe the PerformanceCounter stuff. - make sure to elevate the thread's priority to PRIO_REAL_TIME, so nothing else is likely to steal the core. WARNING: it is quite a hack, I do not consider this good enough to provide steps to a stepper motor; steps will vary, resulting in horrible noise, and shaky operation. Also, as you are going to move something that may harm humans, you have to provide fail-safes; those cannot be trusted when their function also is based on your Windows app. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Happy New Year to all.
      We hope 2010 soon brings us automatic PRE tags!
      Until then, please insert them manually.


      A 1 Reply Last reply
      0
      • CPalliniC CPallini

        Adnan Merter wrote:

        is there someone who can suggest me a method to do this job?

        Yes, use a microcontroller. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        A Offline
        A Offline
        Adnan Merter
        wrote on last edited by
        #9

        thank you but, it is hard to input data to microcontroller instantly it is not user-frienly :) i need to run upto 8 motors at the same time and also, i am not good at programming pics i have to use computer

        --always comes daylight after night-----

        CPalliniC 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, whatever sleep() function you use, you will get a stand-still for at least the time you specify, however it could be much longer as there are dozens of threads all running on a typical Windows system. The only scheme that might work to some extent is this: - assume a multi-core system; - create a program (or a thread in an existing program) that contains busy loops, so it is simply getting and checking time all the time, then setting the output(s) as required. Of course you need a high-precision timer, maybe the PerformanceCounter stuff. - make sure to elevate the thread's priority to PRIO_REAL_TIME, so nothing else is likely to steal the core. WARNING: it is quite a hack, I do not consider this good enough to provide steps to a stepper motor; steps will vary, resulting in horrible noise, and shaky operation. Also, as you are going to move something that may harm humans, you have to provide fail-safes; those cannot be trusted when their function also is based on your Windows app. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Happy New Year to all.
          We hope 2010 soon brings us automatic PRE tags!
          Until then, please insert them manually.


          A Offline
          A Offline
          Adnan Merter
          wrote on last edited by
          #10

          you suggest me to keep my comp busy? it is not a stabil solution, i think :) i will try to connect an oscilator which creates particular frequency pulses. and write an event handler in my program which sends the motion code to output whenever the input is high is it possible? (i know it is dangerous, i can break my mainboard down :) )

          --always comes daylight after night-----

          L E 2 Replies Last reply
          0
          • A Adnan Merter

            you suggest me to keep my comp busy? it is not a stabil solution, i think :) i will try to connect an oscilator which creates particular frequency pulses. and write an event handler in my program which sends the motion code to output whenever the input is high is it possible? (i know it is dangerous, i can break my mainboard down :) )

            --always comes daylight after night-----

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #11

            Adnan Merter wrote:

            you suggest me to keep my comp busy?

            that is not my suggestion, it is the only idea that will make a Windows machine do what you want it to do at sub-millisecond levels. my real suggestion is you add external intelligence (i.e. a micro-controller) that generates (micro-)steps and ramps up and down to a new speed and takes care of safety measures, so all that is left for the PC to do is provide macroscopic commands (say once every second). :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Happy New Year to all.
            We hope 2010 soon brings us automatic PRE tags!
            Until then, please insert them manually.


            1 Reply Last reply
            0
            • A Adnan Merter

              thank you but, it is hard to input data to microcontroller instantly it is not user-frienly :) i need to run upto 8 motors at the same time and also, i am not good at programming pics i have to use computer

              --always comes daylight after night-----

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #12

              Adnan Merter wrote:

              it is hard to input data to microcontroller instantly

              Why do you need to input data instantly to the microcontroller?

              Adnan Merter wrote:

              it is not user-frienly i need to run upto 8 motors at the same time and also, i am not good at programming pics

              I've done it (just one motor, though, a long time ago), using 8051, it is amusing.

              Adnan Merter wrote:

              i have to use computer

              No you don't need. However, if you really want to use a PC then you have to either write kernel-space code or use a real time operative system. Both choices are not painless. Good luck. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • A Adnan Merter

                Hi everybody, i have been working on a step controller program, and i succeed to control, but i cant run the step motors as fast as i want i am using the Sleep(1) function in "windows.h" to delay next step, but it is still so slow i need something like Sleep(0.05) :-D , is there someone who can suggest me a method to do this job? thanx,

                --always comes daylight after night-----

                S Offline
                S Offline
                softwaremonkey
                wrote on last edited by
                #13

                You can use watiable timers to provide short and fairly deterministic delays. The time is specified in 100 nSec units but I don't know if sub-millisecond timeing is that realistic. I have used them to provide pretty accurate 1mSec delay (unlike sleep which isn't that accurate) You could give it a go. You will need the following API calls: CreateWaitableTimer() SetWaitableTimer() WaitForSingleObject() Obviously this needs to be in a worker thread rather than the main thread. If you have any problems using them, let me know as I have some code frags. Tony

                A 2 Replies Last reply
                0
                • A Adnan Merter

                  you suggest me to keep my comp busy? it is not a stabil solution, i think :) i will try to connect an oscilator which creates particular frequency pulses. and write an event handler in my program which sends the motion code to output whenever the input is high is it possible? (i know it is dangerous, i can break my mainboard down :) )

                  --always comes daylight after night-----

                  E Offline
                  E Offline
                  Emilio Garavaglia
                  wrote on last edited by
                  #14

                  That will probably violate the Shannon theorem. The pulses will simply get lost when your program is not listening the event handler (just moving the mouse will cause that, not to mention other running processes in the system !) Windows cannot handle things that are faster than widows itself in doing context switching. It simply cannot catch them up.

                  2 bugs found. > recompile ... 65534 bugs found. :doh:

                  A 1 Reply Last reply
                  0
                  • S softwaremonkey

                    You can use watiable timers to provide short and fairly deterministic delays. The time is specified in 100 nSec units but I don't know if sub-millisecond timeing is that realistic. I have used them to provide pretty accurate 1mSec delay (unlike sleep which isn't that accurate) You could give it a go. You will need the following API calls: CreateWaitableTimer() SetWaitableTimer() WaitForSingleObject() Obviously this needs to be in a worker thread rather than the main thread. If you have any problems using them, let me know as I have some code frags. Tony

                    A Offline
                    A Offline
                    Adnan Merter
                    wrote on last edited by
                    #15

                    thanx tony i will try what you suggest if i cant do this (most probably :-D ), i will let you know.

                    --always comes daylight after night-----

                    1 Reply Last reply
                    0
                    • E Emilio Garavaglia

                      That will probably violate the Shannon theorem. The pulses will simply get lost when your program is not listening the event handler (just moving the mouse will cause that, not to mention other running processes in the system !) Windows cannot handle things that are faster than widows itself in doing context switching. It simply cannot catch them up.

                      2 bugs found. > recompile ... 65534 bugs found. :doh:

                      A Offline
                      A Offline
                      Adnan Merter
                      wrote on last edited by
                      #16

                      thanx for warning, what do you suggest? i need any kind of idea, i have to do this job whatever it costs :)

                      --always comes daylight after night-----

                      E 1 Reply Last reply
                      0
                      • S softwaremonkey

                        You can use watiable timers to provide short and fairly deterministic delays. The time is specified in 100 nSec units but I don't know if sub-millisecond timeing is that realistic. I have used them to provide pretty accurate 1mSec delay (unlike sleep which isn't that accurate) You could give it a go. You will need the following API calls: CreateWaitableTimer() SetWaitableTimer() WaitForSingleObject() Obviously this needs to be in a worker thread rather than the main thread. If you have any problems using them, let me know as I have some code frags. Tony

                        A Offline
                        A Offline
                        Adnan Merter
                        wrote on last edited by
                        #17

                        i found this and it works fine, there is no problem so far but i cant how to set the timer less time periods and the ms is accurate ms here?

                        #include <windows.h>
                        #include <process.h>
                        #include <stdio.h>

                        unsigned __stdcall TF(void* arg) {
                        HANDLE timer=(HANDLE) arg;

                        while (1) {
                        WaitForSingleObject(timer,INFINITE);
                        printf(".");
                        }

                        }

                        int main(int argc, char* argv[]) {

                        HANDLE timer = CreateWaitableTimer(
                        0,
                        false, // false=>will be automatically reset
                        0); // name

                        LARGE_INTEGER li;

                        const int unitsPerSecond=10*1000*1000; // 100 nano seconds

                        // Set the event the first time 2 seconds
                        // after calling SetWaitableTimer

                        li.QuadPart=-(2*unitsPerSecond);
                        SetWaitableTimer(
                        timer,
                        &li,
                        750, // Set the event every 750 milli Seconds
                        0,
                        0,
                        false);

                        _beginthreadex(0,0,TF,(void*) timer,0,0);

                        // Wait forever,
                        while (1) ;

                        return 0;
                        }

                        --always comes daylight after night-----

                        S 1 Reply Last reply
                        0
                        • A Adnan Merter

                          i found this and it works fine, there is no problem so far but i cant how to set the timer less time periods and the ms is accurate ms here?

                          #include <windows.h>
                          #include <process.h>
                          #include <stdio.h>

                          unsigned __stdcall TF(void* arg) {
                          HANDLE timer=(HANDLE) arg;

                          while (1) {
                          WaitForSingleObject(timer,INFINITE);
                          printf(".");
                          }

                          }

                          int main(int argc, char* argv[]) {

                          HANDLE timer = CreateWaitableTimer(
                          0,
                          false, // false=>will be automatically reset
                          0); // name

                          LARGE_INTEGER li;

                          const int unitsPerSecond=10*1000*1000; // 100 nano seconds

                          // Set the event the first time 2 seconds
                          // after calling SetWaitableTimer

                          li.QuadPart=-(2*unitsPerSecond);
                          SetWaitableTimer(
                          timer,
                          &li,
                          750, // Set the event every 750 milli Seconds
                          0,
                          0,
                          false);

                          _beginthreadex(0,0,TF,(void*) timer,0,0);

                          // Wait forever,
                          while (1) ;

                          return 0;
                          }

                          --always comes daylight after night-----

                          S Offline
                          S Offline
                          softwaremonkey
                          wrote on last edited by
                          #18

                          If you set the time interval to zero, you can use the waitable timer as a one-shot delay. It this case, the "due time" is in 100nSec intervals (0.1 microsecond). Maybe you could retrigger by calling SetWaitableTimer() again but I suspect that this call could take a microsecond which defeats the object (could be worth a try). If you need a periodic timer with sub-millisecond period then maybe this isnt the thing for you but I thought it would be worth mentioning it, because its better than using Sleep(). :sigh: Tony

                          1 Reply Last reply
                          0
                          • A Adnan Merter

                            thanx for warning, what do you suggest? i need any kind of idea, i have to do this job whatever it costs :)

                            --always comes daylight after night-----

                            E Offline
                            E Offline
                            Emilio Garavaglia
                            wrote on last edited by
                            #19

                            Until the question remain in theese exact term, there can be no answer: "I have to go faster than light" "It is not possible in this universe" "I have to do it whatever it cost!" The proper answer is to change the universe! In your context, it means that motor steps and speed sholud be controller by a different processor (probably a microcontroller) that should have no operating system on it, but must just execute a simple program that manage the steps based on some "variable" you have to set from the extern, and that receive some information fom the "extern" to set those variables. And the "Extern" can be a PC taking with the microcontroller (may be via serial, or via ethernet) in getting/setting the values the cotroller use as parameters to govern the engine. Note that windows high resolution timers cannot help, since they exist to measure time, not to control process switching, that is never granted to happen in constant timeslot. What happens if you open Otlook or Word or Powerpoint while the motor is running ?

                            2 bugs found. > recompile ... 65534 bugs found. :doh:

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups