-1

How can I write a timecritic backgroundapplication for Windows IoT in C#?

The target is to Programm a stepper Motor via C# over the Raspberry Pi 2 with Windows IoT and Visual Studio 2015. Prgramming via Remote-Debug works fine but there is no sleep or delay available. Also System.Threading.Thread.Sleep(time) is not possible.

Also if we implement a timer the time between the steps is not the same at every step.

It is like 5 steps fast - 2 slowly - 5 steps fast - 2 slowly - 5 steps fast - and so on....

How can I Programm the Motor that he is acting correct?

Data:

Motor: HSY214 - F0.8 A NEMA8 - Steppermotor

Driver: A4988

Sinatr
  • 20,892
  • 15
  • 90
  • 319
Danex
  • 61
  • 1
  • 7
  • Also, refer to this thread: [Windows Store apps embrace asynchrony - and an "asynchronous pause" is provided by Task.Delay. So within an asynchronous method, you'd write:](http://stackoverflow.com/a/12641288/2093880) – usefulBee May 01 '17 at 16:48

5 Answers5

2

We have added C# support for Software PWM and Hardware PWM in the iot-devices project. You may also see this C++ example of driving a stepper motor using Windows IoT Core. If necessary, you may implement a functionality similar to Thread.Sleep using System.Threading.Tasks.Task.Delay.

Paul DeCarlo
  • 416
  • 2
  • 10
2

Maybe in this way:

Task.Delay(-1).Wait(100);

or

Task.Delay(100).Wait(-1);

or

Task.Delay(100).Wait();

in all case the result is the same.

I made a simple test for:

while (true)
        {
            pin26.Write(GpioPinValue.High);
            Task.Delay(-1).Wait(100);
            pin26.Write(GpioPinValue.Low);
            Task.Delay(-1).Wait(100);
        }

Result of pulses width is not so good - from 98ms to 116ms, worse result is for short delay time... 100ms delay

10ms delay

Will be better if you use external servo drive for this motor with step direction interface.

Jarek
  • 307
  • 4
  • 12
1

You should have a law relating the I/O status with the time, and a clock with a proper resolution. Then you have a thread continuosly looping and checking the elapsed time with the clock. By applying the law you can then rise/lower I/O according.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • can you explain that in form of a Code? Because of I have a Background Application in C# and I am not able to do any timecritic Task, there is no sleep or something. Please give me an example. – Danex Oct 06 '15 at 08:01
0

We could use ThreadPoolTimer

Tasks may also be referred to as threads, particularly when they exist within a task. One of the standard thread objects in Windows is the Timer....In Windows, instead of using a delay, we can start a timer thread. Once the thread is started, we can go about doing other useful things, and the timer will alert us when the period has expired. The timer does this through a Timer Tick event.

By Rick Lesniak

usefulBee
  • 9,250
  • 10
  • 51
  • 89
-4

It looks like this is the way:

var t = Task.Run(async delegate
         {
             await Task.Delay(1000);
             return 42;
          });
  t.Wait();

I am used to doing it like this:

Thread.Sleep(1000);

So why is C# IoT coding so much more convoluted ? I also program using C++ on Raspberry Pi, and it looks like the future of IoT is Linux, if MS-10 C# expects 6 lines of code to do a fixed delay in a thread.