-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add base class for iterative changes - Add example #57 (non-breaking)
- Loading branch information
Showing
5 changed files
with
153 additions
and
2 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
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"); | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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"); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} |
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