-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from WildernessLabs/Pca9685_updates
Pca9685 updates
- Loading branch information
Showing
10 changed files
with
312 additions
and
384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.