-
Notifications
You must be signed in to change notification settings - Fork 68
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 #250 from WildernessLabs/feature/servos
Feature/servos
- Loading branch information
Showing
9 changed files
with
123 additions
and
130 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...eadow.Foundation.Peripherals/Servos.ServoCore/Driver/Servos.ServoCore/AngularServoBase.cs
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,75 @@ | ||
using Meadow.Hardware; | ||
using Meadow.Units; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Meadow.Foundation.Servos | ||
{ | ||
public abstract class AngularServoBase : ServoBase, IAngularServo | ||
{ | ||
/// <summary> | ||
/// Returns the current angle. | ||
/// </summary> | ||
public Angle? Angle { get; protected set; } | ||
|
||
/// <summary> | ||
/// Instantiates a new Servo on the specified PWM Pin with the specified config. | ||
/// </summary> | ||
/// <param name="pwm"></param> | ||
/// <param name="config"></param> | ||
public AngularServoBase(IPwmPort pwm, ServoConfig config) | ||
: base(pwm, config) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Rotates the servo to a given angle. | ||
/// </summary> | ||
/// <param name="angle">The angle to rotate to.</param> | ||
/// <param name="stopAfterMotion">When <b>true</b> the PWM will stop after motion is complete.</param> | ||
public async Task RotateTo(Angle angle, bool stopAfterMotion = false) | ||
{ | ||
if (!PwmPort.State) | ||
{ | ||
PwmPort.Start(); | ||
} | ||
|
||
// angle check | ||
if (angle < Config.MinimumAngle || angle > Config.MaximumAngle) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(angle), "Angle must be within servo configuration tolerance."); | ||
} | ||
|
||
// calculate the appropriate pulse duration for the angle | ||
float pulseDuration = CalculatePulseDuration(angle); | ||
|
||
// send our pulse to the servo to make it move | ||
SendCommandPulse(pulseDuration); | ||
|
||
// wait for completion | ||
var rotationRequired = Math.Abs((Angle.HasValue ? Angle.Value.Degrees : 360) - angle.Degrees); | ||
var delay = (int)(8 * rotationRequired); // estimating 8ms / degree | ||
Console.WriteLine($"Start: {Angle?.Degrees??-1} End:={angle.Degrees}"); | ||
Console.WriteLine($"degrees={rotationRequired} Delay={delay}"); | ||
await Task.Delay(delay); | ||
|
||
// update the state | ||
Angle = angle; | ||
|
||
if (stopAfterMotion) | ||
{ | ||
Stop(); | ||
} | ||
} | ||
|
||
protected float CalculatePulseDuration(Angle angle) | ||
{ | ||
// offset + (angle percent * duration length) | ||
return Config.MinimumPulseDuration + (float)((angle.Degrees / Config.MaximumAngle.Degrees) * (Config.MaximumPulseDuration - Config.MinimumPulseDuration)); | ||
// sample calcs: | ||
// 0 degrees time = 1000 + ( (0 / 180) * 1000 ) = 1,000 microseconds | ||
// 90 degrees time = 1000 + ( (90 / 180) * 1000 ) = 1,500 microseconds | ||
// 180 degrees time = 1000 + ( (180 / 180) * 1000 ) = 2,000 microseconds | ||
} | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
...e/Meadow.Foundation.Peripherals/Servos.ServoCore/Driver/Servos.ServoCore/IAngularServo.cs
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,12 @@ | ||
using System.Threading.Tasks; | ||
using Meadow.Units; | ||
|
||
namespace Meadow.Foundation.Servos | ||
{ | ||
public interface IAngularServo : IServo | ||
{ | ||
Task RotateTo(Angle angle, bool stopAfterMotion = false); | ||
|
||
Angle? Angle { get; } | ||
} | ||
} |
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
7 changes: 0 additions & 7 deletions
7
Source/Meadow.Foundation.Peripherals/Servos.ServoCore/Driver/Servos.ServoCore/IServo.cs
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,14 +1,7 @@ | ||
using System; | ||
using Meadow.Units; | ||
|
||
namespace Meadow.Foundation.Servos | ||
{ | ||
public interface IServo | ||
{ | ||
ServoConfig Config { get; } | ||
|
||
void RotateTo(Angle angle); | ||
|
||
Angle? Angle { get; } | ||
} | ||
} |
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
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