Skip to content

Commit

Permalink
more servo work
Browse files Browse the repository at this point in the history
  • Loading branch information
ctacke committed Jan 5, 2022
1 parent 3a15d9f commit c8fdc47
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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. Returns -1 if the angle is unknown.
/// Returns the current angle.
/// </summary>
public Angle? Angle { get; protected set; }

Expand All @@ -26,7 +27,7 @@ public AngularServoBase(IPwmPort pwm, ServoConfig config)
/// </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 void RotateTo(Angle angle, bool stopAfterMotion = false)
public async Task RotateTo(Angle angle, bool stopAfterMotion = false)
{
if (!PwmPort.State)
{
Expand All @@ -41,21 +42,24 @@ public void RotateTo(Angle angle, bool stopAfterMotion = false)

// 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;
}

/// <summary>
/// Stops the signal that controls the servo angle.
/// </summary>
public override void Stop()
{
base.Stop();
Angle = null;
if (stopAfterMotion)
{
Stop();
}
}

protected float CalculatePulseDuration(Angle angle)
Expand Down
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; }
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
using System;
using Meadow.Units;

namespace Meadow.Foundation.Servos
{
public interface IAngularServo : IServo
{
void RotateTo(Angle angle, bool stopAfterMotion = false);

Angle? Angle { get; }
}

public interface IServo
{
ServoConfig Config { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Meadow.Hardware;
using Meadow.Units;
using System;

namespace Meadow.Foundation.Servos
{
Expand Down Expand Up @@ -45,10 +43,5 @@ protected virtual void SendCommandPulse(float pulseDuration)
{
PwmPort.DutyCycle = CalculateDutyCycle(pulseDuration);
}

public void RotateTo(Angle angle, bool stopAfterMotion = false)
{
throw new NotImplementedException();
}
}
}

0 comments on commit c8fdc47

Please sign in to comment.