-
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 LinearSpeedChange - Add base class for iterative changes - Add example - Waiting Time is adjusted to command execution time (IO) #57 non-breaking
- Loading branch information
Showing
5 changed files
with
171 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, 10_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, 20_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,72 @@ | ||
using System.Diagnostics; | ||
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; | ||
private readonly Stopwatch _stopwatch; | ||
|
||
protected abstract T Function(int idx); | ||
protected abstract Task ChangeAsync(T value, CancellationTokenSource cts); | ||
|
||
public IterativeChange(ILogger<IterativeChange<T>> logger) | ||
{ | ||
_logger = logger; | ||
_stopwatch = new Stopwatch(); | ||
} | ||
|
||
protected async Task IterativeExecuteAsync(int iterations, int delayInMilliseconds, CancellationTokenSource cts = default) | ||
{ | ||
_logger.LogInformation($"Start iterative change over {iterations} steps"); | ||
_stopwatch.Start(); | ||
|
||
for (int idx = 0; idx < iterations; idx++) | ||
{ | ||
_logger.LogInformation($"+ Iteration: {idx}"); | ||
var startTime = _stopwatch.ElapsedMilliseconds; | ||
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; | ||
} | ||
var endTime = _stopwatch.ElapsedMilliseconds; | ||
|
||
var currentDuration = endTime - startTime; | ||
var waiting = delayInMilliseconds - currentDuration; | ||
|
||
_logger.LogInformation($"+ Waiting {delayInMilliseconds}ms ({currentDuration} already elapsed; {waiting} to go; walltime: {endTime})"); | ||
|
||
if (waiting > 0 && waiting <= int.MaxValue) | ||
{ | ||
await Task.Delay((int)waiting); | ||
} | ||
else | ||
{ | ||
_logger.LogError($"+ IO Time exceeded Waiting Time. Reducing Step Count or Increasing Delay Time might help."); | ||
} | ||
} | ||
|
||
_stopwatch.Stop(); | ||
|
||
_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