Skip to content

Commit

Permalink
Finish RampUp
Browse files Browse the repository at this point in the history
- Waiting Time is adjusted to command execution time (IO)

#57 non-breaking
  • Loading branch information
tthiery committed Dec 23, 2020
1 parent e7ba9a8 commit 57109a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions examples/SharpBrick.PoweredUp.Examples/ExampleRampUp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override async Task ExecuteAsync()
await technicMediumHub.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Red);

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

await technicMediumHub.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Green);
Expand All @@ -36,7 +36,7 @@ public override async Task ExecuteAsync()
var rampDown = ServiceProvider.GetService<LinearSpeedChange>();

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

Expand Down
22 changes: 20 additions & 2 deletions src/SharpBrick.PoweredUp/Functions/IterativeChange.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand All @@ -7,22 +8,26 @@ 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)");
Expand All @@ -42,12 +47,25 @@ protected async Task IterativeExecuteAsync(int iterations, int delayInMillisecon

break;
}
var endTime = _stopwatch.ElapsedMilliseconds;

_logger.LogInformation($"+ Waiting {delayInMilliseconds}ms");
var currentDuration = endTime - startTime;
var waiting = delayInMilliseconds - currentDuration;

await Task.Delay(delayInMilliseconds);
_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");
}
}
Expand Down

0 comments on commit 57109a0

Please sign in to comment.