diff --git a/.github/workflows/develop-ci.yml b/.github/workflows/develop-ci.yml index 2673eb8..5d0a0bc 100644 --- a/.github/workflows/develop-ci.yml +++ b/.github/workflows/develop-ci.yml @@ -3,6 +3,7 @@ name: Develop Build on: workflow_dispatch: pull_request: + branches: [ develop ] push: branches: [ develop ] @@ -69,7 +70,6 @@ jobs: uses: actions/checkout@v3 with: path: Meadow.Foundation.FeatherWings - ref: develop - name: Setup .NET SDK uses: actions/setup-dotnet@v1 diff --git a/Source/CharlieWing/Driver/CharlieWing.csproj b/Source/CharlieWing/Driver/CharlieWing.csproj index efad651..5ce9102 100644 --- a/Source/CharlieWing/Driver/CharlieWing.csproj +++ b/Source/CharlieWing/Driver/CharlieWing.csproj @@ -24,6 +24,6 @@ - + diff --git a/Source/CharlieWing/Samples/CharlieWing_Sample/CharlieWing_Sample.csproj b/Source/CharlieWing/Samples/CharlieWing_Sample/CharlieWing_Sample.csproj index 1522353..5427c71 100644 --- a/Source/CharlieWing/Samples/CharlieWing_Sample/CharlieWing_Sample.csproj +++ b/Source/CharlieWing/Samples/CharlieWing_Sample/CharlieWing_Sample.csproj @@ -6,8 +6,8 @@ App - - + + diff --git a/Source/DotstarWing/Driver/DotstarWing.csproj b/Source/DotstarWing/Driver/DotstarWing.csproj index bc91a33..a8a0815 100644 --- a/Source/DotstarWing/Driver/DotstarWing.csproj +++ b/Source/DotstarWing/Driver/DotstarWing.csproj @@ -24,6 +24,6 @@ - + diff --git a/Source/DotstarWing/Samples/DotstarWing_Sample/DotstarWing_Sample.csproj b/Source/DotstarWing/Samples/DotstarWing_Sample/DotstarWing_Sample.csproj index d0a6646..e233ad1 100644 --- a/Source/DotstarWing/Samples/DotstarWing_Sample/DotstarWing_Sample.csproj +++ b/Source/DotstarWing/Samples/DotstarWing_Sample/DotstarWing_Sample.csproj @@ -6,8 +6,8 @@ App - - + + diff --git a/Source/GPSWing/Driver/GPSWing.csproj b/Source/GPSWing/Driver/GPSWing.csproj index 24d24ba..fb1a551 100644 --- a/Source/GPSWing/Driver/GPSWing.csproj +++ b/Source/GPSWing/Driver/GPSWing.csproj @@ -24,6 +24,6 @@ - + diff --git a/Source/GPSWing/Samples/GPSWing_Sample/GPSWing_Sample.csproj b/Source/GPSWing/Samples/GPSWing_Sample/GPSWing_Sample.csproj index cad8bc0..2ecf6fa 100644 --- a/Source/GPSWing/Samples/GPSWing_Sample/GPSWing_Sample.csproj +++ b/Source/GPSWing/Samples/GPSWing_Sample/GPSWing_Sample.csproj @@ -6,7 +6,7 @@ App - + diff --git a/Source/KeyboardWing/Driver/KeyboardWing.csproj b/Source/KeyboardWing/Driver/KeyboardWing.csproj index a231bde..260652d 100644 --- a/Source/KeyboardWing/Driver/KeyboardWing.csproj +++ b/Source/KeyboardWing/Driver/KeyboardWing.csproj @@ -24,8 +24,8 @@ - - - + + + diff --git a/Source/KeyboardWing/Samples/KeyboardWing_Sample/KeyboardWing_Sample.csproj b/Source/KeyboardWing/Samples/KeyboardWing_Sample/KeyboardWing_Sample.csproj index 1cae599..f3558e0 100644 --- a/Source/KeyboardWing/Samples/KeyboardWing_Sample/KeyboardWing_Sample.csproj +++ b/Source/KeyboardWing/Samples/KeyboardWing_Sample/KeyboardWing_Sample.csproj @@ -6,7 +6,7 @@ App - + diff --git a/Source/LedMatrix8x16Wing/Driver/LedMatrix8x16Wing.cs b/Source/LedMatrix8x16Wing/Driver/LedMatrix8x16Wing.cs index 34f5073..15452f1 100644 --- a/Source/LedMatrix8x16Wing/Driver/LedMatrix8x16Wing.cs +++ b/Source/LedMatrix8x16Wing/Driver/LedMatrix8x16Wing.cs @@ -156,7 +156,10 @@ public void Fill(Color fillColor, bool updateDisplay = false) { Fill(0, 0, Width, Height, fillColor); - if (updateDisplay) Show(); + if (updateDisplay) + { + Show(); + } } /// @@ -174,7 +177,7 @@ public void Fill(int x, int y, int width, int height, Color fillColor) { for (int j = 0; j < height; j++) { - DrawPixel(i, j, isColored); + DrawPixel(x + i, y + j, isColored); } } } diff --git a/Source/LedMatrix8x16Wing/Driver/LedMatrix8x16Wing.csproj b/Source/LedMatrix8x16Wing/Driver/LedMatrix8x16Wing.csproj index fb85c5f..f00d6bd 100644 --- a/Source/LedMatrix8x16Wing/Driver/LedMatrix8x16Wing.csproj +++ b/Source/LedMatrix8x16Wing/Driver/LedMatrix8x16Wing.csproj @@ -24,6 +24,6 @@ - + diff --git a/Source/LedMatrix8x16Wing/Samples/LedMatrix8x16Wing_Sample/LedMatrix8x16Wing_Sample.csproj b/Source/LedMatrix8x16Wing/Samples/LedMatrix8x16Wing_Sample/LedMatrix8x16Wing_Sample.csproj index fe7b6e3..4a3c19b 100644 --- a/Source/LedMatrix8x16Wing/Samples/LedMatrix8x16Wing_Sample/LedMatrix8x16Wing_Sample.csproj +++ b/Source/LedMatrix8x16Wing/Samples/LedMatrix8x16Wing_Sample/LedMatrix8x16Wing_Sample.csproj @@ -6,8 +6,8 @@ App - - + + diff --git a/Source/MotorWing/Driver/DCMotor.cs b/Source/MotorWing/Driver/DCMotor.cs index ecb39ee..ce8f13b 100644 --- a/Source/MotorWing/Driver/DCMotor.cs +++ b/Source/MotorWing/Driver/DCMotor.cs @@ -1,136 +1,85 @@ using Meadow.Foundation.ICs.IOExpanders; -using System; +using Meadow.Hardware; +using static Meadow.Foundation.FeatherWings.MotorWing; namespace Meadow.Foundation.FeatherWings { - /// - /// Motor commands - /// - public enum Commmand - { - /// - /// Move forward - /// - FORWARD, - /// - /// Move backwards - /// - BACKWARD, - /// - /// Release - /// - RELEASE - } - /// /// Represents a DC Motor /// - public class DCMotor : Motor + public class DCMotor { - readonly byte _pwmPin; - readonly byte _in1; - readonly byte _in2; + private readonly IPwmPort pwmPort; + private readonly IDigitalOutputPort in1; + private readonly IDigitalOutputPort in2; + + readonly Pca9685 controller; /// /// Creates a DCMotor driver /// - /// - /// - /// - public DCMotor(short num, Pca9685 pca9685) : base(pca9685) + public DCMotor(DCMotorIndex motorIndex, Pca9685 pca9685) { + controller = pca9685; - if (num < 0 || num > 3) + switch (motorIndex) { - throw new ArgumentException("Motor must be between 0 and 3"); - } - - switch (num) - { - case 0: - _pwmPin = 8; - _in2 = 9; - _in1 = 10; + case DCMotorIndex.Motor1: + pwmPort = controller.Pins.LED8.CreatePwmPort(pca9685.Frequency); + in1 = controller.Pins.LED10.CreateDigitalOutputPort(); + in2 = controller.Pins.LED9.CreateDigitalOutputPort(); break; - case 1: - _pwmPin = 13; - _in2 = 12; - _in1 = 11; + case DCMotorIndex.Motor2: + pwmPort = controller.Pins.LED13.CreatePwmPort(pca9685.Frequency); + in1 = controller.Pins.LED11.CreateDigitalOutputPort(); + in2 = controller.Pins.LED12.CreateDigitalOutputPort(); break; - case 2: - _pwmPin = 2; - _in2 = 3; - _in1 = 4; + case DCMotorIndex.Motor3: + pwmPort = controller.Pins.LED2.CreatePwmPort(pca9685.Frequency); + in1 = controller.Pins.LED4.CreateDigitalOutputPort(); + in2 = controller.Pins.LED3.CreateDigitalOutputPort(); break; - case 3: - _pwmPin = 7; - _in2 = 6; - _in1 = 5; + case DCMotorIndex.Motor4: + pwmPort = controller.Pins.LED7.CreatePwmPort(pca9685.Frequency); + in1 = controller.Pins.LED5.CreateDigitalOutputPort(); + in2 = controller.Pins.LED6.CreateDigitalOutputPort(); break; - } - Run(Commmand.RELEASE); - + Run(Direction.Release); } /// /// Controls the motor direction/action /// /// The action - public virtual void Run(Commmand command) + public virtual void Run(Direction command) { - if (command == Commmand.FORWARD) - { - pca9685.SetPin(_in2, false); - pca9685.SetPin(_in1, true); - } - - if (command == Commmand.BACKWARD) + if (command == Direction.Forward) { - pca9685.SetPin(_in2, true); - pca9685.SetPin(_in1, false); + in1.State = true; + in2.State = false; } - - if (command == Commmand.RELEASE) - { - pca9685.SetPin(_in1, false); - pca9685.SetPin(_in2, false); - } - } - - /// - /// Control the DC Motor speed/throttle - /// - /// The 8-bit PWM value, 0 is off, 255 is on - public override void SetSpeed(short speed) - { - if (speed < 0) + else if (command == Direction.Reverse) { - speed = 0; + in1.State = false; + in2.State = true; } - if (speed > 255) + if (command == Direction.Release) { - speed = 255; + in1.State = false; + in2.State = false; } - - pca9685.SetPwm(_pwmPin, 0, speed * 16); } /// /// Control the DC Motor speed/throttle /// - /// The 12-bit PWM value, 0 is off, 4096 is on - public void PreciseSpeed(short speed) + /// 0 is off, 1 is on + public void SetSpeed(double speed) { - if (speed > 4096) - speed = 4096; - - if (speed < 0) - speed = 0; - - pca9685.SetPwm(_pwmPin, 0, speed); + pwmPort.DutyCycle = speed; } /// @@ -138,8 +87,8 @@ public void PreciseSpeed(short speed) /// public void Stop() { - Run(Commmand.RELEASE); - pca9685.SetPwm(_pwmPin, 0, 0); + Run(Direction.Release); + pwmPort.DutyCycle = 0; } } } \ No newline at end of file diff --git a/Source/MotorWing/Driver/Motor.cs b/Source/MotorWing/Driver/Motor.cs deleted file mode 100644 index 4fa1dce..0000000 --- a/Source/MotorWing/Driver/Motor.cs +++ /dev/null @@ -1,30 +0,0 @@ -using Meadow.Foundation.ICs.IOExpanders; - -namespace Meadow.Foundation.FeatherWings -{ - /// - /// Represents a generic motor - /// - public abstract class Motor - { - /// - /// The pca9685 instance used to control the motor - /// - protected readonly Pca9685 pca9685; - - /// - /// Creates a Motor driver - /// - /// A pca9685 instance - public Motor(Pca9685 pca9685) - { - this.pca9685 = pca9685; - } - - /// - /// Set the motor speed - /// - /// The motor speed - public abstract void SetSpeed(short speed); - } -} \ No newline at end of file diff --git a/Source/MotorWing/Driver/MotorWing.Enums.cs b/Source/MotorWing/Driver/MotorWing.Enums.cs new file mode 100644 index 0000000..5db6f56 --- /dev/null +++ b/Source/MotorWing/Driver/MotorWing.Enums.cs @@ -0,0 +1,86 @@ +namespace Meadow.Foundation.FeatherWings +{ + public partial class MotorWing + { + /// + /// Represents the index of DC motors on the MotorWing. + /// + public enum DCMotorIndex : byte + { + /// + /// Represents DC Motor 1. + /// + Motor1 = 0, + /// + /// Represents DC Motor 2. + /// + Motor2 = 1, + /// + /// Represents DC Motor 3. + /// + Motor3 = 2, + /// + /// Represents DC Motor 4. + /// + Motor4 = 3 + } + + /// + /// Represents the index of stepper motors on the MotorWing. + /// + public enum StepperMotorIndex : byte + { + /// + /// Represents Stepper Motor 1. + /// + Motor1 = 0, + /// + /// Represents Stepper Motor 2. + /// + Motor2 = 1 + } + + /// + /// Defines the different motor styles for stepper motors. + /// + public enum Style + { + /// + /// Single step mode. Moves one step at a time. + /// + SINGLE = 1, + /// + /// Double step mode. Moves two steps at a time. + /// + DOUBLE = 2, + + /// + /// Interleaved mode. Alternates between single and double steps. + /// + INTERLEAVE = 3, + /// + /// Microstep mode. Moves in smaller steps for smoother motion. + /// + MICROSTEP = 4 + } + + /// + /// Defines the direction of motor movement. + /// + public enum Direction + { + /// + /// Moves the motor forward. + /// + Forward, + /// + /// Moves the motor in reverse (backward). + /// + Reverse, + /// + /// Releases the motor. Applies only to stepper motors. + /// + Release + } + } +} \ No newline at end of file diff --git a/Source/MotorWing/Driver/MotorWing.cs b/Source/MotorWing/Driver/MotorWing.cs index b1f505f..9a0a5f0 100644 --- a/Source/MotorWing/Driver/MotorWing.cs +++ b/Source/MotorWing/Driver/MotorWing.cs @@ -7,7 +7,7 @@ namespace Meadow.Foundation.FeatherWings /// /// Represents Adafruit's Feather DC and Stepper Motor Wing /// - public class MotorWing + public partial class MotorWing { readonly Pca9685 pca9685; @@ -27,47 +27,25 @@ public MotorWing(II2cBus i2cBus, Units.Frequency frequency, byte address = (byte pca9685 = new Pca9685(i2cBus, frequency, address); } - /// - /// Initialize the MotorWings - /// - public void Initialize() - { - pca9685.Initialize(); - } - /// /// Returns a instance of a Stepper Motor object /// /// The number of steps the motor has - /// The stepper motor port we want to use: only 0 or 1 are valid + /// The stepper motor port /// StepperMotor - public StepperMotor GetStepper(short stepperMotorIndex, int steps) + public StepperMotor GetStepper(StepperMotorIndex motorIndex, int steps) { - if (stepperMotorIndex != 1 && stepperMotorIndex != 2) - { - throw new ArgumentException("Stepper num must be 1 or 2"); - } - - stepperMotorIndex--; - - return new StepperMotor(steps, stepperMotorIndex, pca9685); + return new StepperMotor(steps, motorIndex, pca9685); } /// /// Returns a instance of a DC Motor object /// - /// The motor port we want to use: 1 thru 4 are valid + /// The motor port /// DCMotor - public DCMotor GetMotor(short dcMotorIndex) + public DCMotor GetMotor(DCMotorIndex motorIndex) { - if ((dcMotorIndex < 1) || (dcMotorIndex > 4)) - { - throw new ArgumentException("Motor must be between 1 and 4 inclusive"); - } - - dcMotorIndex--; - - return new DCMotor(dcMotorIndex, pca9685); + return new DCMotor(motorIndex, pca9685); } } } \ No newline at end of file diff --git a/Source/MotorWing/Driver/MotorWing.csproj b/Source/MotorWing/Driver/MotorWing.csproj index 39e0df5..7b59b56 100644 --- a/Source/MotorWing/Driver/MotorWing.csproj +++ b/Source/MotorWing/Driver/MotorWing.csproj @@ -24,6 +24,6 @@ - + diff --git a/Source/MotorWing/Driver/StepperMotor.cs b/Source/MotorWing/Driver/StepperMotor.cs index e3fd330..b911ca1 100644 --- a/Source/MotorWing/Driver/StepperMotor.cs +++ b/Source/MotorWing/Driver/StepperMotor.cs @@ -1,138 +1,108 @@ using Meadow.Foundation.ICs.IOExpanders; +using Meadow.Hardware; using System; using System.Threading; +using static Meadow.Foundation.FeatherWings.MotorWing; namespace Meadow.Foundation.FeatherWings { /// - /// Motor style + /// Represents a Stepper Motor and provides functionality to control it. /// - public enum Style + public class StepperMotor { - /// - /// Single - /// - SINGLE = 1, - /// - /// Double - /// - DOUBLE = 2, - /// - /// Interleave - /// - INTERLEAVE = 3, - /// - /// Microstep - /// - MICROSTEP = 4 - } + private readonly IPwmPort portA; + private readonly IPwmPort portB; - /// - /// Motor direction - /// - public enum Direction - { - /// - /// Forward motor direction - /// - FORWARD, - /// - /// Backwards moto direction - /// - BACKWARD - } + private int currentStep; + private double rpmDelay; + private readonly int motorSteps; - /// - /// Represents a Stepper Motor - /// - public class StepperMotor : Motor - { - int _currentstep; - double _rpmDelay; - readonly int _motorSteps; - readonly byte _pwmA; - readonly byte _AIN2; - readonly byte _AIN1; - readonly byte _pwmB; - readonly byte _BIN2; - readonly byte _BIN1; + private readonly IPwmPort pwmPortA; + private readonly IDigitalOutputPort in1A; + private readonly IDigitalOutputPort in2A; + + private readonly IPwmPort pwmPortB; + private readonly IDigitalOutputPort in1B; + private readonly IDigitalOutputPort in2B; - const short MICROSTEPS = 8; - readonly byte[] _microStepCurve = { 0, 50, 98, 142, 180, 212, 236, 250, 255 }; - //private readonly int[] microstepcurve = {0, 25, 50, 74, 98, 120, 141, 162, 180,197, 212, 225, 236, 244, 250, 253, 255}//MICROSTEPS == 16 + private const short MICROSTEPS = 8; + private readonly byte[] microStepCurve = { 0, 50, 98, 142, 180, 212, 236, 250, 255 }; /// - /// Creates a Stepper motor objet un-initialized + /// Initializes a new instance of the class. /// - /// The number of steps per revolution - /// The Stepper motor port - /// The PCS968 diver object - public StepperMotor(int steps, int num, Pca9685 pca9685) : base(pca9685) + /// The number of steps per revolution. + /// The index of the stepper motor port. + /// The PCA9685 driver object. + /// Thrown when an invalid stepper motor index is provided. + public StepperMotor(int steps, StepperMotorIndex motorIndex, Pca9685 pca9685) { - - if (num == 0) + if (motorIndex == StepperMotorIndex.Motor1) { - _pwmA = 8; - _AIN2 = 9; - _AIN1 = 10; - _pwmB = 13; - _BIN2 = 12; - _BIN1 = 11; + pwmPortA = pca9685.Pins.LED8.CreatePwmPort(pca9685.Frequency); + in1A = pca9685.Pins.LED10.CreateDigitalOutputPort(); + in2A = pca9685.Pins.LED9.CreateDigitalOutputPort(); + + pwmPortB = pca9685.Pins.LED13.CreatePwmPort(pca9685.Frequency); + in1B = pca9685.Pins.LED11.CreateDigitalOutputPort(); + in2B = pca9685.Pins.LED12.CreateDigitalOutputPort(); } - else if (num == 1) + else if (motorIndex == StepperMotorIndex.Motor2) { - _pwmA = 2; - _AIN2 = 3; - _AIN1 = 4; - _pwmB = 7; - _BIN2 = 6; - _BIN1 = 5; + pwmPortA = pca9685.Pins.LED2.CreatePwmPort(pca9685.Frequency); + in1A = pca9685.Pins.LED4.CreateDigitalOutputPort(); + in2A = pca9685.Pins.LED3.CreateDigitalOutputPort(); + + pwmPortB = pca9685.Pins.LED7.CreatePwmPort(pca9685.Frequency); + in1B = pca9685.Pins.LED5.CreateDigitalOutputPort(); + in2B = pca9685.Pins.LED6.CreateDigitalOutputPort(); } else { - throw new ArgumentException("Stepper num must be 0 or 1"); + throw new ArgumentException("Stepper motor index must be Motor1 or Motor2"); } - _motorSteps = steps; + motorSteps = steps; SetSpeed(15); - _currentstep = 0; + currentStep = 0; } /// - /// Set the delay for the Stepper Motor speed in RPM + /// Sets the speed of the stepper motor in revolutions per minute (RPM). /// - /// The desired RPM - public override void SetSpeed(short rpm) + /// The desired RPM. + public void SetSpeed(short rpm) { - _rpmDelay = 60000.0 / (_motorSteps * rpm); + rpmDelay = 60000.0 / (motorSteps * rpm); } /// - /// Move the stepper with the given RPM + /// Moves the stepper motor a specified number of steps at the current speed. /// - /// The number of steps to move. Negative number moves the stepper backwards - /// How to perform the step + /// The number of steps to move. A negative value moves the stepper backwards. + /// The style of stepping to use. public virtual void Step(int steps = 1, Style style = Style.SINGLE) { if (steps > 0) { - Step(steps, Direction.FORWARD, style); + Step(steps, Direction.Forward, style); } else { - Step(Math.Abs(steps), Direction.BACKWARD, style); + Step(Math.Abs(steps), Direction.Reverse, style); } } /// - /// Move the stepper with the given RPM + /// Moves the stepper motor a specified number of steps in a given direction and style. /// - /// The number of steps to move - /// The direction to go - /// How to perform the step + /// The number of steps to move. + /// The direction to move. + /// The style of stepping to use. protected virtual void Step(int steps, Direction direction, Style style) { - int delay = (int)_rpmDelay; + int delay = (int)rpmDelay; if (style == Style.INTERLEAVE) { delay /= 2; @@ -152,11 +122,11 @@ protected virtual void Step(int steps, Direction direction, Style style) } /// - /// Move the stepper one step only + /// Moves the stepper motor one step in a given direction and style. /// - /// The direction to go - /// How to perform the step - /// The current location + /// The direction to move. + /// The style of stepping to use. + /// The current step position. protected virtual int Step(Direction direction, Style style) { int ocrb, ocra; @@ -164,195 +134,158 @@ protected virtual int Step(Direction direction, Style style) if (style == Style.SINGLE) { - if ((_currentstep / (MICROSTEPS / 2)) % 2 != 0) // we're at an odd step, weird + if (currentStep / (MICROSTEPS / 2) % 2 != 0) // Odd step { - if (direction == Direction.FORWARD) + if (direction == Direction.Forward) { - _currentstep += MICROSTEPS / 2; + currentStep += MICROSTEPS / 2; } else { - _currentstep -= MICROSTEPS / 2; + currentStep -= MICROSTEPS / 2; } } - else - { // go to the next even step - if (direction == Direction.FORWARD) + else // Even step + { + if (direction == Direction.Forward) { - _currentstep += MICROSTEPS; + currentStep += MICROSTEPS; } else { - _currentstep -= MICROSTEPS; + currentStep -= MICROSTEPS; } } } else if (style == Style.DOUBLE) { - if (((_currentstep / (MICROSTEPS / 2) % 2)) != 0) - { // we're at an even step, weird - if (direction == Direction.FORWARD) + if (currentStep / (MICROSTEPS / 2) % 2 != 0) // Even step + { + if (direction == Direction.Forward) { - _currentstep += MICROSTEPS / 2; + currentStep += MICROSTEPS / 2; } else { - _currentstep -= MICROSTEPS / 2; + currentStep -= MICROSTEPS / 2; } } - else - { // go to the next odd step - if (direction == Direction.FORWARD) + else // Odd step + { + if (direction == Direction.Forward) { - _currentstep += MICROSTEPS; + currentStep += MICROSTEPS; } else { - _currentstep -= MICROSTEPS; + currentStep -= MICROSTEPS; } } } else if (style == Style.INTERLEAVE) { - if (direction == Direction.FORWARD) + if (direction == Direction.Forward) { - _currentstep += MICROSTEPS / 2; + currentStep += MICROSTEPS / 2; } else { - _currentstep -= MICROSTEPS / 2; + currentStep -= MICROSTEPS / 2; } } else if (style == Style.MICROSTEP) { - if (direction == Direction.FORWARD) + if (direction == Direction.Forward) { - _currentstep++; + currentStep++; } else { - // BACKWARDS - _currentstep--; + currentStep--; } - _currentstep += MICROSTEPS * 4; - _currentstep %= MICROSTEPS * 4; + currentStep += MICROSTEPS * 4; + currentStep %= MICROSTEPS * 4; ocra = ocrb = 0; - if ((_currentstep >= 0) && (_currentstep < MICROSTEPS)) + if (currentStep < MICROSTEPS) { - ocra = _microStepCurve[MICROSTEPS - _currentstep]; - ocrb = _microStepCurve[_currentstep]; + ocra = microStepCurve[MICROSTEPS - currentStep]; + ocrb = microStepCurve[currentStep]; } - else if ((_currentstep >= MICROSTEPS) && (_currentstep < MICROSTEPS * 2)) + else if (currentStep < MICROSTEPS * 2) { - ocra = _microStepCurve[_currentstep - MICROSTEPS]; - ocrb = _microStepCurve[MICROSTEPS * 2 - _currentstep]; + ocra = microStepCurve[currentStep - MICROSTEPS]; + ocrb = microStepCurve[MICROSTEPS * 2 - currentStep]; } - else if ((_currentstep >= MICROSTEPS * 2) && - (_currentstep < MICROSTEPS * 3)) + else if (currentStep < MICROSTEPS * 3) { - ocra = _microStepCurve[MICROSTEPS * 3 - _currentstep]; - ocrb = _microStepCurve[_currentstep - MICROSTEPS * 2]; + ocra = microStepCurve[MICROSTEPS * 3 - currentStep]; + ocrb = microStepCurve[currentStep - MICROSTEPS * 2]; } - else if ((_currentstep >= MICROSTEPS * 3) && - (_currentstep < MICROSTEPS * 4)) + else { - ocra = _microStepCurve[_currentstep - MICROSTEPS * 3]; - ocrb = _microStepCurve[MICROSTEPS * 4 - _currentstep]; + ocra = microStepCurve[currentStep - MICROSTEPS * 3]; + ocrb = microStepCurve[MICROSTEPS * 4 - currentStep]; } } - _currentstep += MICROSTEPS * 4; - _currentstep %= MICROSTEPS * 4; + currentStep += MICROSTEPS * 4; + currentStep %= MICROSTEPS * 4; - pca9685.SetPwm(_pwmA, 0, ocra * 16); - pca9685.SetPwm(_pwmB, 0, ocrb * 16); + pwmPortA.DutyCycle = ocra / 255.0; + pwmPortB.DutyCycle = ocrb / 255.0; - // release all - int latch_state = 0; // all motor pins to 0 + int latchState = 0; - // Serial.println(step, DEC); if (style == Style.MICROSTEP) { - if ((_currentstep >= 0) && (_currentstep < MICROSTEPS)) - latch_state |= 0x03; - if ((_currentstep >= MICROSTEPS) && (_currentstep < MICROSTEPS * 2)) - latch_state |= 0x06; - if ((_currentstep >= MICROSTEPS * 2) && (_currentstep < MICROSTEPS * 3)) - latch_state |= 0x0C; - if ((_currentstep >= MICROSTEPS * 3) && (_currentstep < MICROSTEPS * 4)) - latch_state |= 0x09; + if (currentStep < MICROSTEPS) + latchState |= 0x03; + else if (currentStep < MICROSTEPS * 2) + latchState |= 0x06; + else if (currentStep < MICROSTEPS * 3) + latchState |= 0x0C; + else + latchState |= 0x09; } else { - switch (_currentstep / (MICROSTEPS / 2)) + switch (currentStep / (MICROSTEPS / 2)) { case 0: - latch_state |= 0x1; // energize coil 1 only + latchState |= 0x1; break; case 1: - latch_state |= 0x3; // energize coil 1+2 + latchState |= 0x3; break; case 2: - latch_state |= 0x2; // energize coil 2 only + latchState |= 0x2; break; case 3: - latch_state |= 0x6; // energize coil 2+3 + latchState |= 0x6; break; case 4: - latch_state |= 0x4; // energize coil 3 only + latchState |= 0x4; break; case 5: - latch_state |= 0xC; // energize coil 3+4 + latchState |= 0xC; break; case 6: - latch_state |= 0x8; // energize coil 4 only + latchState |= 0x8; break; case 7: - latch_state |= 0x9; // energize coil 1+4 + latchState |= 0x9; break; } } - if ((latch_state & 0x1) == 0x1) - { - pca9685.SetPin(_AIN2, true); - } - else - { - pca9685.SetPin(_AIN2, false); - } - - if ((latch_state & 0x2) == 0x2) - { - pca9685.SetPin(_BIN1, true); - } - else - { - pca9685.SetPin(_BIN1, false); - } - - if ((latch_state & 0x4) == 0x4) - { - pca9685.SetPin(_AIN1, true); - } - else - { - pca9685.SetPin(_AIN1, false); - } - - if ((latch_state & 0x8) == 0x8) - { - pca9685.SetPin(_BIN2, true); - } - else - { - pca9685.SetPin(_BIN2, false); - } - - return _currentstep; + in1A.State = (latchState & 0x1) != 0; + in1B.State = (latchState & 0x2) != 0; + in2A.State = (latchState & 0x4) != 0; + in2B.State = (latchState & 0x8) != 0; + return currentStep; } } } diff --git a/Source/MotorWing/Sample/MotorWing_Sample/MeadowApp.cs b/Source/MotorWing/Sample/MotorWing_Sample/MeadowApp.cs index 3c4a14b..d403c75 100644 --- a/Source/MotorWing/Sample/MotorWing_Sample/MeadowApp.cs +++ b/Source/MotorWing/Sample/MotorWing_Sample/MeadowApp.cs @@ -20,7 +20,6 @@ public override Task Initialize() var i2CBus = Device.CreateI2cBus(I2cBusSpeed.FastPlus); motorWing = new MotorWing(i2CBus, new Frequency(100, Frequency.UnitType.Hertz), 0x61); - motorWing.Initialize(); return Task.CompletedTask; } @@ -28,16 +27,16 @@ public override Task Initialize() public override async Task Run() { //Get DC motor 1 - var dcMotor1 = motorWing.GetMotor(1); + var dcMotor1 = motorWing.GetMotor(MotorWing.DCMotorIndex.Motor1); //Get DC motor 2 - var dcMotor2 = motorWing.GetMotor(2); + var dcMotor2 = motorWing.GetMotor(MotorWing.DCMotorIndex.Motor2); //Get Stepper motor number 2 - var stepper = motorWing.GetStepper(2, 200); + var stepper = motorWing.GetStepper(MotorWing.StepperMotorIndex.Motor2, 200); - dcMotor1.Run(Commmand.FORWARD); - dcMotor2.Run(Commmand.BACKWARD); + dcMotor1.Run(MotorWing.Direction.Forward); + dcMotor2.Run(MotorWing.Direction.Reverse); while (true) { diff --git a/Source/MotorWing/Sample/MotorWing_Sample/MotorWing_Sample.csproj b/Source/MotorWing/Sample/MotorWing_Sample/MotorWing_Sample.csproj index c0e76c6..cda84ff 100644 --- a/Source/MotorWing/Sample/MotorWing_Sample/MotorWing_Sample.csproj +++ b/Source/MotorWing/Sample/MotorWing_Sample/MotorWing_Sample.csproj @@ -6,7 +6,7 @@ App - + diff --git a/Source/NeoPixelWing/Driver/NeoPixelWing.csproj b/Source/NeoPixelWing/Driver/NeoPixelWing.csproj index ca2fee9..fca73a4 100644 --- a/Source/NeoPixelWing/Driver/NeoPixelWing.csproj +++ b/Source/NeoPixelWing/Driver/NeoPixelWing.csproj @@ -24,7 +24,7 @@ - - + + diff --git a/Source/NeoPixelWing/Sample/NeoPixelWing_Sample/NeoPixelWing_Sample.csproj b/Source/NeoPixelWing/Sample/NeoPixelWing_Sample/NeoPixelWing_Sample.csproj index 8389224..d46580c 100644 --- a/Source/NeoPixelWing/Sample/NeoPixelWing_Sample/NeoPixelWing_Sample.csproj +++ b/Source/NeoPixelWing/Sample/NeoPixelWing_Sample/NeoPixelWing_Sample.csproj @@ -6,7 +6,7 @@ App - + diff --git a/Source/NineDofImuWing/Driver/NineDofImuWing.csproj b/Source/NineDofImuWing/Driver/NineDofImuWing.csproj index 7850c36..9062eb9 100644 --- a/Source/NineDofImuWing/Driver/NineDofImuWing.csproj +++ b/Source/NineDofImuWing/Driver/NineDofImuWing.csproj @@ -24,7 +24,7 @@ - - + + diff --git a/Source/NineDofImuWing/Sample/NineDofImuWing_Sample/NineDofImuWing_Sample.csproj b/Source/NineDofImuWing/Sample/NineDofImuWing_Sample/NineDofImuWing_Sample.csproj index 795bca3..4c4e684 100644 --- a/Source/NineDofImuWing/Sample/NineDofImuWing_Sample/NineDofImuWing_Sample.csproj +++ b/Source/NineDofImuWing/Sample/NineDofImuWing_Sample/NineDofImuWing_Sample.csproj @@ -6,7 +6,7 @@ App - + diff --git a/Source/OLED128x32Wing/Driver/OLED128x32Wing.csproj b/Source/OLED128x32Wing/Driver/OLED128x32Wing.csproj index ad1ba84..29bcbac 100644 --- a/Source/OLED128x32Wing/Driver/OLED128x32Wing.csproj +++ b/Source/OLED128x32Wing/Driver/OLED128x32Wing.csproj @@ -24,6 +24,6 @@ - + diff --git a/Source/OLED128x32Wing/Sample/OLED128x32Wing_Sample/OLED128x32Wing_Sample.csproj b/Source/OLED128x32Wing/Sample/OLED128x32Wing_Sample/OLED128x32Wing_Sample.csproj index 3a8b919..4f22588 100644 --- a/Source/OLED128x32Wing/Sample/OLED128x32Wing_Sample/OLED128x32Wing_Sample.csproj +++ b/Source/OLED128x32Wing/Sample/OLED128x32Wing_Sample/OLED128x32Wing_Sample.csproj @@ -6,7 +6,7 @@ App - + diff --git a/Source/OLED128x64Wing/Driver/OLED128x64Wing.csproj b/Source/OLED128x64Wing/Driver/OLED128x64Wing.csproj index a5cbb39..88c5ece 100644 --- a/Source/OLED128x64Wing/Driver/OLED128x64Wing.csproj +++ b/Source/OLED128x64Wing/Driver/OLED128x64Wing.csproj @@ -24,6 +24,6 @@ - + diff --git a/Source/OLED128x64Wing/Sample/OLED128x64Wing_Sample/OLED128x64Wing_Sample.csproj b/Source/OLED128x64Wing/Sample/OLED128x64Wing_Sample/OLED128x64Wing_Sample.csproj index b2b474c..2cad525 100644 --- a/Source/OLED128x64Wing/Sample/OLED128x64Wing_Sample/OLED128x64Wing_Sample.csproj +++ b/Source/OLED128x64Wing/Sample/OLED128x64Wing_Sample/OLED128x64Wing_Sample.csproj @@ -6,8 +6,8 @@ App - - + + diff --git a/Source/ServoWing/Driver/ServoWing.cs b/Source/ServoWing/Driver/ServoWing.cs index e4c9687..2e8895c 100644 --- a/Source/ServoWing/Driver/ServoWing.cs +++ b/Source/ServoWing/Driver/ServoWing.cs @@ -1,8 +1,10 @@ using Meadow.Foundation.ICs.IOExpanders; using Meadow.Foundation.Servos; using Meadow.Hardware; +using Meadow.Peripherals.Servos; using Meadow.Units; using System; +using static Meadow.Foundation.Servos.AngularServo; namespace Meadow.Foundation.FeatherWings { @@ -46,23 +48,23 @@ public ServoWing( this.portCount = portCount; pca9685 = new Pca9685(i2cBus, frequency, address); - pca9685.Initialize(); } /// /// Returns the specified servo /// /// - /// - public Servo GetServo(byte portIndex, ServoConfig servoConfig) + /// The pulse angle corresponding to the minimum angle of the servo. + /// The pulse angle corresponding to the maximum angle of the servo. + public AngularServo GetServo(byte portIndex, PulseAngle minPulseAngle, PulseAngle maxPulseAngle) { if ((portIndex < 0) || (portIndex > portCount)) { throw new ArgumentException($"Servo num must be between 1 and {portCount}", "num"); } - var pwm = pca9685.CreatePwmPort(portIndex); - var servo = new Servo(pwm, servoConfig); + var pwm = pca9685.CreatePwmPort(GetPinForPortIndex(portIndex)); + var servo = new AngularServo(pwm, minPulseAngle, maxPulseAngle); return servo; } @@ -71,20 +73,29 @@ public Servo GetServo(byte portIndex, ServoConfig servoConfig) /// Returns the specified continues rotation servo /// /// - /// - /// - /// - public IContinuousRotationServo GetContinuousRotatioServo(byte portIndex, ServoConfig servoConfig) + /// The minimum pulse duration for the servo. + /// The maximum pulse duration for the servo. + public IContinuousRotationServo GetContinuousRotatioServo(byte portIndex, TimePeriod minimumPulseDuration, TimePeriod maximumPulseDuration) { - if ((portIndex < 0) || (portIndex > portCount)) - { - throw new ArgumentException($"Continuous Rotatio Servo num must be between 1 and {portCount}", "num"); - } + var pin = GetPinForPortIndex(portIndex); - var pwm = pca9685.CreatePwmPort(portIndex); - var servo = new ContinuousRotationServo(pwm, servoConfig); + var pwm = pca9685.CreatePwmPort(pin); + var servo = new ContinuousRotationServo(pwm, minimumPulseDuration, maximumPulseDuration); return servo; } + + private IPin? GetPinForPortIndex(byte portIndex) + { + foreach (var pin in pca9685.Pins) + { + if ((byte)pin.Key == portIndex) + { + return pin; + } + } + + return null; + } } } \ No newline at end of file diff --git a/Source/ServoWing/Driver/ServoWing.csproj b/Source/ServoWing/Driver/ServoWing.csproj index beed718..131f07c 100644 --- a/Source/ServoWing/Driver/ServoWing.csproj +++ b/Source/ServoWing/Driver/ServoWing.csproj @@ -24,7 +24,7 @@ - - + + diff --git a/Source/ServoWing/Sample/ServoWing_Sample/MeadowApp.cs b/Source/ServoWing/Sample/ServoWing_Sample/MeadowApp.cs index 0870a58..7ff3ed5 100644 --- a/Source/ServoWing/Sample/ServoWing_Sample/MeadowApp.cs +++ b/Source/ServoWing/Sample/ServoWing_Sample/MeadowApp.cs @@ -15,7 +15,7 @@ public class MeadowApp : App // ServoWing servoWing; - Servo servo; + AngularServo servo; public override Task Initialize() { @@ -23,7 +23,9 @@ public override Task Initialize() servoWing = new ServoWing(Device.CreateI2cBus(I2cBusSpeed.FastPlus)); - servo = servoWing.GetServo(0, NamedServoConfigs.SG90); + servo = servoWing.GetServo(0, + new AngularServo.PulseAngle(NamedServoConfigs.SG90.MinimumAngle, new TimePeriod(NamedServoConfigs.SG90.MinimumPulseDuration, TimePeriod.UnitType.Milliseconds)), + new AngularServo.PulseAngle(NamedServoConfigs.SG90.MaximumAngle, new TimePeriod(NamedServoConfigs.SG90.MaximumPulseDuration, TimePeriod.UnitType.Milliseconds))); return Task.CompletedTask; } @@ -33,19 +35,19 @@ public override async Task Run() while (true) { Console.WriteLine("0"); - await servo.RotateTo(new Angle(0, AU.Degrees)); + servo.RotateTo(new Angle(0, AU.Degrees)); await Task.Delay(1000); Console.WriteLine("45"); - await servo.RotateTo(new Angle(45, AU.Degrees)); + servo.RotateTo(new Angle(45, AU.Degrees)); await Task.Delay(1000); Console.WriteLine("90"); - await servo.RotateTo(new Angle(90, AU.Degrees)); + servo.RotateTo(new Angle(90, AU.Degrees)); await Task.Delay(1000); Console.WriteLine("135"); - await servo.RotateTo(new Angle(135, AU.Degrees)); + servo.RotateTo(new Angle(135, AU.Degrees)); await Task.Delay(1000); } } diff --git a/Source/ServoWing/Sample/ServoWing_Sample/ServoWing_Sample.csproj b/Source/ServoWing/Sample/ServoWing_Sample/ServoWing_Sample.csproj index a74142c..227b7e3 100644 --- a/Source/ServoWing/Sample/ServoWing_Sample/ServoWing_Sample.csproj +++ b/Source/ServoWing/Sample/ServoWing_Sample/ServoWing_Sample.csproj @@ -6,7 +6,7 @@ App - +