C# software stepper motor controller??

I'm working on a software stepper motor controller in C#. The program uses a Measurement Computing PCI-INT32 digital I/O board to output a series of pulses to a stepper motor. The pulses are generated in software. I've tried two methods to achieve this: 1) use a timer control to oscillate and turn the output on and off, or 2) use a while loop with a Thread.Sleep() line to generate delays.

The problem I am having is that it won't generate a proper, fast pulse
train - for example, using Thread.Sleep(1), which should produce a pulse every 1ms actually runs rather eratically at much less than this speed. The alternative method using timers produces very high CPU usage, and a similarly erratic pulse train. Also, for some strange reason the timer refused to let me set its interval property in code (the line pulseTimer.Interval = pulse_length; simply froze the system).

I have also tried a dodgy "hack" method of producing pulses - use a while loop that counts up to, say, 100,000 to generate a delay (rather than Thread.Sleep). This actually produces a much faster pulse train, but I am reluctant to use this method as I obviously have no control over the actual length (in ms) of the delay.

How can I get C# to generate a nice, smooth, accurate pulse train? By the way, I need to use delays of less than 1ms (to achieve an output frequency up to around 4000Hz), but Thread.Sleep() only allows an integer value for delay.


  • MMM a bit low level for C# if you ask me, but try to set the priority of the thread... That might make it more relieble... its default is 5 and the GC runs at a 2 or a 3 priority.

    The problem with a thread is it's a request to execute code, thus not garentied to run at regular intervals. The priority might improve appon this, you will have to see, but there is only so much CPU to use and if you not carefull, you could cause Deadlock.
  • [b][red]This message was edited by lyubo at 2003-5-2 9:48:56[/red][/b][hr]
    // create TimeSpan object in ticks
    // I'm not sure for the constructor, I don't know TimeSpan by heart ;-)
    // read the docs if i'm wrong! I'm not wrong of course ;p
    TimeSpan interval = new TimeSpan(long ticks);
    // trying a sample constructor
    // when you need pulse
    Another way is to try using a Timer. I believe, it can, too, use ticks.
    P.S. : 1 Tick is 100 ns ! ! !

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion