Skip to content

Commit

Permalink
Add LinearSpeedChange
Browse files Browse the repository at this point in the history
- Add base class for iterative changes
- Add example

#57 (non-breaking)
  • Loading branch information
tthiery committed Sep 2, 2020
1 parent 2ea4db1 commit 529a87d
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 2 deletions.
50 changes: 50 additions & 0 deletions examples/SharpBrick.PoweredUp.Examples/ExampleRampUp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SharpBrick.PoweredUp;
using SharpBrick.PoweredUp.Functions;

namespace Example
{
public class ExampleRampUp : BaseExample
{
public override async Task ExecuteAsync()
{
using (var technicMediumHub = Host.FindByType<TechnicMediumHub>())
{
var stopWatch = new Stopwatch();

var motor = technicMediumHub.A.GetDevice<TechnicLargeLinearMotor>();

// ramp up with linear speed
var rampUp = ServiceProvider.GetService<LinearSpeedChange>();

await technicMediumHub.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Red);

stopWatch.Start();
await rampUp.ExecuteAsync(motor, 20, 100, 40, 5_000);
var redPhase = stopWatch.ElapsedMilliseconds;

await technicMediumHub.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Green);

await Task.Delay(2_000);

await technicMediumHub.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Orange);

// ramp down with linear speed
var rampDown = ServiceProvider.GetService<LinearSpeedChange>();

var beforeOrangePhase = stopWatch.ElapsedMilliseconds;
await rampDown.ExecuteAsync(motor, 100, 0, 100, 10_000);
var orangePhase = stopWatch.ElapsedMilliseconds - beforeOrangePhase;
stopWatch.Stop();

await technicMediumHub.SwitchOffAsync();

// time delays (parameter) + 100s of BLE messages async/await ops
Log.LogInformation($"Red Phase: {redPhase}ms; Orange Phase: {orangePhase}ms");
}
}
}
}
3 changes: 2 additions & 1 deletion examples/SharpBrick.PoweredUp.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ static async Task Main(string[] args)
//example = new Example.ExampleSetHubProperty();
//example = new Example.ExampleHubPropertyObserving();
//example = new Example.ExampleDiscoverByType();
example = new Example.ExampleCalibrationSteering();
//example = new Example.ExampleCalibrationSteering();
example = new Example.ExampleRampUp();

// NOTE: Examples are programmed object oriented style. Base class implements methods Configure, DiscoverAsync and ExecuteAsync to be overwriten on demand.
await example.InitHostAndDiscoverAsync(enableTrace);
Expand Down
54 changes: 54 additions & 0 deletions src/SharpBrick.PoweredUp/Functions/IterativeChange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace SharpBrick.PoweredUp.Functions
{
public abstract class IterativeChange<T>
{
private readonly ILogger<IterativeChange<T>> _logger;

protected abstract T Function(int idx);
protected abstract Task ChangeAsync(T value, CancellationTokenSource cts);

public IterativeChange(ILogger<IterativeChange<T>> logger)
{
_logger = logger;
}

protected async Task IterativeExecuteAsync(int iterations, int delayInMilliseconds, CancellationTokenSource cts = default)
{
_logger.LogInformation($"Start iterative change over {iterations} steps");

for (int idx = 0; idx < iterations; idx++)
{
_logger.LogInformation($"+ Iteration: {idx}");
if (cts?.IsCancellationRequested ?? false)
{
_logger.LogInformation("+ Cancelled (before Actor)");

break;
}

var value = Function(idx);

_logger.LogInformation($"+ Value: {value}");

await ChangeAsync(value, cts);

if (cts?.IsCancellationRequested ?? false)
{
_logger.LogInformation("+ Cancelled (after Actor)");

break;
}

_logger.LogInformation($"+ Waiting {delayInMilliseconds}ms");

await Task.Delay(delayInMilliseconds);
}

_logger.LogInformation("Finished iterative change");
}
}
}
44 changes: 44 additions & 0 deletions src/SharpBrick.PoweredUp/Functions/LinearSpeedChange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace SharpBrick.PoweredUp.Functions
{
public class LinearSpeedChange : IterativeChange<sbyte>
{
private readonly ILogger<LinearSpeedChange> _logger;
private TachoMotor _motor;
private sbyte _startSpeed;

public byte MaxPower { get; set; } = 100;

public sbyte IterationStep { get; private set; }

public LinearSpeedChange(ILogger<LinearSpeedChange> logger)
: base(logger)
{
_logger = logger;
}

public async Task ExecuteAsync(TachoMotor motor, sbyte startSpeed, sbyte endSpeed, int steps, int milliseconds, CancellationTokenSource cts = default)
{
_logger.LogInformation($"Start execute {nameof(LinearSpeedChange)} ({startSpeed} - {endSpeed} over {steps} steps and {milliseconds}ms)");
_startSpeed = startSpeed;
_motor = motor ?? throw new ArgumentNullException(nameof(motor));

IterationStep = (sbyte)((endSpeed - startSpeed) / steps);
_logger.LogInformation($"+ IterationStep: {IterationStep}");

await IterativeExecuteAsync(steps, milliseconds / steps, cts);

_logger.LogInformation($"Finished {nameof(LinearSpeedChange)} ");
}

protected override sbyte Function(int idx)
=> (sbyte)(_startSpeed + IterationStep * (idx + 1));

protected override Task ChangeAsync(sbyte value, CancellationTokenSource cts)
=> _motor.StartSpeedAsync(value, MaxPower);
}
}
4 changes: 3 additions & 1 deletion src/SharpBrick.PoweredUp/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static IServiceCollection AddPoweredUp(this IServiceCollection self)
// functions
.AddTransient<DiscoverPorts>()
.AddTransient<TraceMessages>()
.AddTransient<LinearMidCalibration>();
.AddTransient<LinearMidCalibration>()
.AddTransient<LinearSpeedChange>()
;
}
}

0 comments on commit 529a87d

Please sign in to comment.