c# (Windows IoT) - Sleep() 还是 Delay()?

c# (Windows IoT) - Sleep() or Delay()?

如何在 C# 中为 Windows 物联网编写一个时间关键的后台应用程序?

目标是通过 C# 在 Raspberry Pi 2 上使用 Windows 物联网和 Visual Studio 2015 对步进电机进行编程。通过 Remote-Debug 编程工作正常,但有没有睡眠或延迟可用。 System.Threading.Thread.Sleep(time) 也是不可能的。

此外,如果我们实现一个计时器,则每一步的步骤之间的时间都不相同。

就像快5步-慢2步-快5步-慢2步-快5步-等等....

我怎样才能对电机进行编程以使其动作正确?

数据:

电机:HSY214 - F0.8 A NEMA8 - 步进电机

Driver:A4988

你应该有一个将 I/O 状态与时间联系起来的定律,以及一个具有适当分辨率的时钟。然后你有一个线程不断循环并用时钟检查经过的时间。通过应用该定律,您可以 rise/lower I/O 根据。

我们在 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 中添加了对软件 PWM 和硬件 PWM 的 C# 支持。

看起来是这样的:

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

我已经习惯这样做了:

Thread.Sleep(1000);

那么为什么 C# IoT 编码如此复杂? 我还在 Raspberry Pi 上使用 C++ 编程,看起来物联网的未来是 Linux,如果 MS-10 C# 期望 6 行代码在线程中执行固定延迟。

也许是这样:

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

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

Task.Delay(100).Wait();

在所有情况下,结果都是一样的。

我做了一个简单的测试:

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

脉冲宽度的结果不太好 - 从 98ms 到 116ms, 更糟糕的结果是延迟时间短...... 100ms delay

10ms delay

这个电机有步进方向接口,如果用外接伺服驱动就更好了

我们可以使用 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