diff --git a/Source/Meadow.Foundation.Peripherals/Motors.Stepper.Em542s/Driver/Em542s.cs b/Source/Meadow.Foundation.Peripherals/Motors.Stepper.Em542s/Driver/Em542s.cs index f1cff5ceab..4462015111 100644 --- a/Source/Meadow.Foundation.Peripherals/Motors.Stepper.Em542s/Driver/Em542s.cs +++ b/Source/Meadow.Foundation.Peripherals/Motors.Stepper.Em542s/Driver/Em542s.cs @@ -5,7 +5,6 @@ using System; using System.Runtime.CompilerServices; using System.Threading; -using System.Threading.Tasks; namespace Meadow.Foundation.Motors.Stepper; @@ -14,16 +13,25 @@ namespace Meadow.Foundation.Motors.Stepper; /// public class Em542s : IStepperMotor { + private const int MinimumMicrosecondDelayRequiredByDrive = 5; // per the data sheet + private readonly IDigitalOutputPort _pulsePort; private readonly IDigitalOutputPort _directionPort; private readonly IDigitalOutputPort? _enablePort; - private const int MinimumStartupDelayMicroseconds = 50; - private const int LinearAccelerationConstant = 40; - private const int MinimumMicrosecondDelayRequiredByDrive = 5; private float _usPerCall; - private readonly object _syncRoot = new(); + /// + /// Gets or sets the minimum step dwell time when motor changes from stationary to moving. Motors with more mass or larger steps require more dwell. + /// + public int MinimumStartupDwellMicroseconds { get; set; } = 50; + + /// + /// Gets or sets a constant that affects the rate of linear acceleration for the motor. A lower value yields faster acceleration. + /// Motors with more mass or larger steps require slower acceleration + /// + public int LinearAccelerationConstant { get; set; } = 40; + /// public RotationDirection Direction { get; set; } @@ -31,7 +39,7 @@ public class Em542s : IStepperMotor public int StepsPerRevolution { get; } /// - /// Gets a value indicating whether the logic for the stepper motor driver is inverted. + /// Gets a value indicating whether or not the logic for the stepper motor driver is inverted. /// public bool InverseLogic { get; } @@ -43,7 +51,6 @@ public class Em542s : IStepperMotor /// The optional digital output port for enabling or disabling the motor (if available). /// The number of steps per revolution for the stepper motor (default is 200). /// A value indicating whether the logic for the stepper motor driver is inverted (default is false). - public Em542s( IDigitalOutputPort pulse, IDigitalOutputPort direction, @@ -73,24 +80,23 @@ public Em542s( [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] private void CalculateCallDuration() { - Task.Run(() => - { - var temp = 0; - var calls = 100000; + // this estimates how long a method call takes on the current platform. + // this is used below to provide a sub-millisecond "delay" used for step dwell + var temp = 0; + var calls = 100000; - var start = Environment.TickCount; + var start = Environment.TickCount; - for (var i = 0; i < calls; i++) - { - temp = i; - } + for (var i = 0; i < calls; i++) + { + temp = i; + } - var et = Environment.TickCount - start; + var et = Environment.TickCount - start; - _usPerCall = et * 1000 / (float)calls; + _usPerCall = et * 1000 / (float)calls; - Resolver.Log.Info($"us per call: {calls} / {et} = {_usPerCall}"); - }); + Resolver.Log.Info($"us per call: {calls} / {et} = {_usPerCall}"); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] @@ -128,7 +134,7 @@ public void Step(int steps, RotationDirection direction, Frequency rate) if (targetus < MinimumMicrosecondDelayRequiredByDrive) throw new ArgumentOutOfRangeException( "Rate cannot have pulses shorter than 5us. Use 200KHz or less."); - var us = targetus < MinimumStartupDelayMicroseconds ? MinimumStartupDelayMicroseconds : targetus; + var us = targetus < MinimumStartupDwellMicroseconds ? MinimumStartupDwellMicroseconds : targetus; for (var step = 0; step < steps; step++) {