Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

C# software stepper motor controller??

Daniel CiddorDaniel Ciddor Posts: 7Member
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.

Comments

  • BrutesBrutes Posts: 162Member
    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.
  • lyubolyubo Posts: 17Member
    [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
    (years,months,days,minutes,seconds,milliseconds,ticks)
    .
    .
    .
    // when you need pulse
    Thread.Sleep(interval);
    ------------
    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.