Skip to content

Commit

Permalink
Merge pull request #65 from WildernessLabs/Pca9685_updates
Browse files Browse the repository at this point in the history
Pca9685 updates
  • Loading branch information
ctacke committed Jun 23, 2024
2 parents 3c1c88e + 2550f29 commit 104da7c
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 384 deletions.
18 changes: 9 additions & 9 deletions Source/FeatherWings.sln
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_External", "_External", "{
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Meadow.F7", "..\..\Meadow.Core\source\implementations\f7\Meadow.F7\Meadow.F7.csproj", "{59624F1F-91C6-4D1A-B408-BA8E85B764B8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Servos.ServoCore", "..\..\Meadow.Foundation\Source\Meadow.Foundation.Peripherals\Servos.ServoCore\Driver\Servos.ServoCore.csproj", "{F7DF19C0-6ABF-44EB-8DF0-F6223A3753B6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ICs.IOExpanders.Pca9685", "..\..\Meadow.Foundation\Source\Meadow.Foundation.Peripherals\ICs.IOExpanders.Pca9685\Driver\ICs.IOExpanders.Pca9685.csproj", "{31DAEBA3-F497-4FFD-9BD2-88771CCC457A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ICs.IOExpanders.Ht16k33", "..\..\Meadow.Foundation\Source\Meadow.Foundation.Peripherals\ICs.IOExpanders.Ht16k33\Driver\ICs.IOExpanders.Ht16k33.csproj", "{664930F3-8A51-469E-A8C2-AD8B3062BA8A}"
Expand Down Expand Up @@ -137,6 +135,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OLED128x64Wing_Sample", "OL
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Displays.Sh110x", "..\..\Meadow.Foundation\Source\Meadow.Foundation.Peripherals\Displays.Sh110x\Driver\Displays.Sh110x.csproj", "{640E9537-46D5-4CCE-9A2A-DEAE752B0FC8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Servos", "..\..\Meadow.Foundation\Source\Meadow.Foundation.Libraries_and_Frameworks\Servos\Driver\Servos.csproj", "{0A969417-E71C-45AC-8C7F-0B0328FDCE0B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -245,12 +245,6 @@ Global
{59624F1F-91C6-4D1A-B408-BA8E85B764B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59624F1F-91C6-4D1A-B408-BA8E85B764B8}.Release|Any CPU.Build.0 = Release|Any CPU
{59624F1F-91C6-4D1A-B408-BA8E85B764B8}.Release|Any CPU.Deploy.0 = Release|Any CPU
{F7DF19C0-6ABF-44EB-8DF0-F6223A3753B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7DF19C0-6ABF-44EB-8DF0-F6223A3753B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7DF19C0-6ABF-44EB-8DF0-F6223A3753B6}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{F7DF19C0-6ABF-44EB-8DF0-F6223A3753B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7DF19C0-6ABF-44EB-8DF0-F6223A3753B6}.Release|Any CPU.Build.0 = Release|Any CPU
{F7DF19C0-6ABF-44EB-8DF0-F6223A3753B6}.Release|Any CPU.Deploy.0 = Release|Any CPU
{31DAEBA3-F497-4FFD-9BD2-88771CCC457A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{31DAEBA3-F497-4FFD-9BD2-88771CCC457A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{31DAEBA3-F497-4FFD-9BD2-88771CCC457A}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
Expand Down Expand Up @@ -405,6 +399,12 @@ Global
{640E9537-46D5-4CCE-9A2A-DEAE752B0FC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{640E9537-46D5-4CCE-9A2A-DEAE752B0FC8}.Release|Any CPU.Build.0 = Release|Any CPU
{640E9537-46D5-4CCE-9A2A-DEAE752B0FC8}.Release|Any CPU.Deploy.0 = Release|Any CPU
{0A969417-E71C-45AC-8C7F-0B0328FDCE0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0A969417-E71C-45AC-8C7F-0B0328FDCE0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A969417-E71C-45AC-8C7F-0B0328FDCE0B}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{0A969417-E71C-45AC-8C7F-0B0328FDCE0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A969417-E71C-45AC-8C7F-0B0328FDCE0B}.Release|Any CPU.Build.0 = Release|Any CPU
{0A969417-E71C-45AC-8C7F-0B0328FDCE0B}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -435,7 +435,6 @@ Global
{28B2EF36-0BFE-41DC-B253-631D82E84EE7} = {293F2FC1-21F9-4825-B2C5-EBFA6876E364}
{8F504BB6-31D4-4BF4-B3C3-D9FE39CB29D3} = {8E81ED23-EEDE-417E-9084-D6BBBBA5CA2C}
{59624F1F-91C6-4D1A-B408-BA8E85B764B8} = {BB1A7E97-4559-459B-BDF8-4ED41758AB29}
{F7DF19C0-6ABF-44EB-8DF0-F6223A3753B6} = {BB1A7E97-4559-459B-BDF8-4ED41758AB29}
{31DAEBA3-F497-4FFD-9BD2-88771CCC457A} = {BB1A7E97-4559-459B-BDF8-4ED41758AB29}
{664930F3-8A51-469E-A8C2-AD8B3062BA8A} = {BB1A7E97-4559-459B-BDF8-4ED41758AB29}
{BEAD4EA5-8716-45C7-934B-BDC2528EECC6} = {BB1A7E97-4559-459B-BDF8-4ED41758AB29}
Expand Down Expand Up @@ -465,6 +464,7 @@ Global
{E7671E7B-C24D-4C69-865B-56D457C0EF1D} = {C589E391-452A-4DC1-B260-487014DA27CE}
{7068B557-1B72-44C1-BF2B-36EFBB8E78D3} = {8AEC28BA-BACD-47A5-BFEC-95F7624B0B7F}
{640E9537-46D5-4CCE-9A2A-DEAE752B0FC8} = {BB1A7E97-4559-459B-BDF8-4ED41758AB29}
{0A969417-E71C-45AC-8C7F-0B0328FDCE0B} = {BB1A7E97-4559-459B-BDF8-4ED41758AB29}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {673CD5FD-E4C7-48D7-85EA-3CB6FAD87939}
Expand Down
137 changes: 43 additions & 94 deletions Source/MotorWing/Driver/DCMotor.cs
Original file line number Diff line number Diff line change
@@ -1,145 +1,94 @@
using Meadow.Foundation.ICs.IOExpanders;
using System;
using Meadow.Hardware;
using static Meadow.Foundation.FeatherWings.MotorWing;

namespace Meadow.Foundation.FeatherWings
{
/// <summary>
/// Motor commands
/// </summary>
public enum Commmand
{
/// <summary>
/// Move forward
/// </summary>
FORWARD,
/// <summary>
/// Move backwards
/// </summary>
BACKWARD,
/// <summary>
/// Release
/// </summary>
RELEASE
}

/// <summary>
/// Represents a DC Motor
/// </summary>
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;

/// <summary>
/// Creates a DCMotor driver
/// </summary>
/// <param name="num"></param>
/// <param name="pca9685"></param>
/// <exception cref="ArgumentException"></exception>
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);
}

/// <summary>
/// Controls the motor direction/action
/// </summary>
/// <param name="command">The action</param>
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);
}
}

/// <summary>
/// Control the DC Motor speed/throttle
/// </summary>
/// <param name="speed">The 8-bit PWM value, 0 is off, 255 is on</param>
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);
}

/// <summary>
/// Control the DC Motor speed/throttle
/// </summary>
/// <param name="speed">The 12-bit PWM value, 0 is off, 4096 is on</param>
public void PreciseSpeed(short speed)
/// <param name="speed">0 is off, 1 is on</param>
public void SetSpeed(double speed)
{
if (speed > 4096)
speed = 4096;

if (speed < 0)
speed = 0;

pca9685.SetPwm(_pwmPin, 0, speed);
pwmPort.DutyCycle = speed;
}

/// <summary>
/// Stops the motor
/// </summary>
public void Stop()
{
Run(Commmand.RELEASE);
pca9685.SetPwm(_pwmPin, 0, 0);
Run(Direction.Release);
pwmPort.DutyCycle = 0;
}
}
}
30 changes: 0 additions & 30 deletions Source/MotorWing/Driver/Motor.cs

This file was deleted.

86 changes: 86 additions & 0 deletions Source/MotorWing/Driver/MotorWing.Enums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace Meadow.Foundation.FeatherWings
{
public partial class MotorWing
{
/// <summary>
/// Represents the index of DC motors on the MotorWing.
/// </summary>
public enum DCMotorIndex : byte
{
/// <summary>
/// Represents DC Motor 1.
/// </summary>
Motor1 = 0,
/// <summary>
/// Represents DC Motor 2.
/// </summary>
Motor2 = 1,
/// <summary>
/// Represents DC Motor 3.
/// </summary>
Motor3 = 2,
/// <summary>
/// Represents DC Motor 4.
/// </summary>
Motor4 = 3
}

/// <summary>
/// Represents the index of stepper motors on the MotorWing.
/// </summary>
public enum StepperMotorIndex : byte
{
/// <summary>
/// Represents Stepper Motor 1.
/// </summary>
Motor1 = 0,
/// <summary>
/// Represents Stepper Motor 2.
/// </summary>
Motor2 = 1
}

/// <summary>
/// Defines the different motor styles for stepper motors.
/// </summary>
public enum Style
{
/// <summary>
/// Single step mode. Moves one step at a time.
/// </summary>
SINGLE = 1,
/// <summary>
/// Double step mode. Moves two steps at a time.
/// </summary>
DOUBLE = 2,

/// <summary>
/// Interleaved mode. Alternates between single and double steps.
/// </summary>
INTERLEAVE = 3,
/// <summary>
/// Microstep mode. Moves in smaller steps for smoother motion.
/// </summary>
MICROSTEP = 4
}

/// <summary>
/// Defines the direction of motor movement.
/// </summary>
public enum Direction
{
/// <summary>
/// Moves the motor forward.
/// </summary>
Forward,
/// <summary>
/// Moves the motor in reverse (backward).
/// </summary>
Reverse,
/// <summary>
/// Releases the motor. Applies only to stepper motors.
/// </summary>
Release
}
}
}
Loading

0 comments on commit 104da7c

Please sign in to comment.