Skip to content

Commit c2f5385

Browse files
committed
Add Steering Calibration to the ServiceProvider
- Remove service locator usage - Fix warnings sharpbrick#71 non-breaking
1 parent 1d1280a commit c2f5385

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

examples/SharpBrick.PoweredUp.Examples/ExampleCalibrationSteering.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Threading.Tasks;
2+
using Microsoft.Extensions.DependencyInjection;
23
using SharpBrick.PoweredUp;
34
using SharpBrick.PoweredUp.Functions;
45
using static SharpBrick.PoweredUp.Directions;
@@ -13,7 +14,7 @@ public override async Task ExecuteAsync()
1314
{
1415
var motor = technicMediumHub.A.GetDevice<TechnicLargeLinearMotor>();
1516

16-
var calibration = new LinearMidCalibration(serviceProvider);
17+
var calibration = serviceProvider.GetService<LinearMidCalibration>();
1718
await calibration.ExecuteAsync(motor);
1819

1920
await Task.Delay(5000);

src/SharpBrick.PoweredUp/Functions/DiscoverPorts.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class DiscoverPorts
1616
private readonly IPoweredUpProtocol _protocol;
1717
private readonly byte _hubId;
1818
private readonly ILogger<DiscoverPorts> _logger;
19-
private readonly IDisposable _disposable;
2019
private TaskCompletionSource<int> _taskCompletionSource;
2120
private int _stageTwoCount;
2221
private int _stageTwoExpected;
@@ -110,7 +109,7 @@ private void UpdateKnowledge(byte[] data, PoweredUpMessage message)
110109
{
111110
var port = knowledge.Port(_hubId, msg.PortId);
112111

113-
RequestPortModePropertiesAsync(port);
112+
_ = RequestPortModePropertiesAsync(port); // discard the task to supress the await error
114113
}
115114

116115
if (applicableMessage)
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
using System;
22
using System.Reactive.Linq;
33
using System.Threading.Tasks;
4-
using Microsoft.Extensions.DependencyInjection;
54
using Microsoft.Extensions.Logging;
65
using static SharpBrick.PoweredUp.Directions;
76

87
namespace SharpBrick.PoweredUp.Functions
98
{
109
public class LinearMidCalibration
1110
{
12-
private IServiceProvider _serviceProvider;
11+
private readonly ILogger<LinearMidCalibration> _logger;
1312

1413
public byte MaxPower { get; set; } = 10;
1514
public byte Speed { get; set; } = 10;
1615

17-
public LinearMidCalibration(IServiceProvider serviceProvider)
16+
public LinearMidCalibration(ILogger<LinearMidCalibration> logger)
1817
{
19-
this._serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
18+
this._logger = logger ?? throw new ArgumentNullException(nameof(logger));
2019
}
2120

2221
public async Task ExecuteAsync(TachoMotor motor)
@@ -26,46 +25,44 @@ public async Task ExecuteAsync(TachoMotor motor)
2625
throw new ArgumentNullException(nameof(motor));
2726
}
2827

29-
var logger = _serviceProvider.GetService<ILoggerFactory>().CreateLogger<LinearMidCalibration>();
30-
31-
logger.LogInformation("Start Linear Mid Calibration");
28+
_logger.LogInformation("Start Linear Mid Calibration");
3229
await motor.TryLockDeviceForCombinedModeNotificationSetupAsync(motor.ModeIndexSpeed, motor.ModeIndexPosition);
3330
await motor.SetupNotificationAsync(motor.ModeIndexSpeed, true, 1);
3431
await motor.SetupNotificationAsync(motor.ModeIndexPosition, true, 1);
3532
await motor.UnlockFromCombinedModeNotificationSetupAsync(true);
3633

37-
logger.LogInformation("Start CW");
34+
_logger.LogInformation("Start CW");
3835
await motor.StartPowerAsync((sbyte)(CW * MaxPower));
3936

4037
await motor.SpeedObservable.Where(x => x.SI == 0).FirstAsync().GetAwaiter();
41-
logger.LogInformation("Reached End by detecting no speed movement.");
38+
_logger.LogInformation("Reached End by detecting no speed movement.");
4239

4340
var cwVal = motor.Position;
44-
logger.LogInformation($"CW End at {cwVal}.");
41+
_logger.LogInformation($"CW End at {cwVal}.");
4542

46-
logger.LogInformation("Start CCW");
43+
_logger.LogInformation("Start CCW");
4744
await motor.StartPowerAsync((sbyte)(CCW * MaxPower));
4845

4946
await motor.SpeedObservable.Where(x => x.SI == 0).FirstAsync().GetAwaiter();
50-
logger.LogInformation("Reached End by detecting no speed movement.");
47+
_logger.LogInformation("Reached End by detecting no speed movement.");
5148

5249
var ccwVal = motor.Position;
53-
logger.LogInformation($"CW End at {ccwVal}.");
50+
_logger.LogInformation($"CW End at {ccwVal}.");
5451

5552
await motor.StopByBrakeAsync();
5653

5754
var range = Math.Abs(cwVal - ccwVal);
5855
var extend = (uint)(range / 2);
59-
logger.LogInformation($"Total Range: {range} Extend from center: {extend}");
56+
_logger.LogInformation($"Total Range: {range} Extend from center: {extend}");
6057

6158
await motor.StartSpeedForDegreesAsync(extend, (sbyte)(CW * Speed), MaxPower, SpecialSpeed.Hold, SpeedProfiles.None);
6259

6360
await motor.SpeedObservable.Where(x => x.SI == 0).FirstAsync().GetAwaiter();
6461
await motor.SetZeroAsync();
65-
logger.LogInformation($"Moved to center. Reset Position.");
62+
_logger.LogInformation($"Moved to center. Reset Position.");
6663

6764
await motor.UnlockFromCombinedModeNotificationSetupAsync(false);
68-
logger.LogInformation($"End Linear Mid Calibration");
65+
_logger.LogInformation($"End Linear Mid Calibration");
6966
}
7067
}
7168
}

src/SharpBrick.PoweredUp/IServiceCollectionExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using SharpBrick.PoweredUp.Devices;
3+
using SharpBrick.PoweredUp.Functions;
34
using SharpBrick.PoweredUp.Hubs;
45

56
namespace SharpBrick.PoweredUp
@@ -10,6 +11,7 @@ public static IServiceCollection AddPoweredUp(this IServiceCollection self)
1011
=> self
1112
.AddSingleton<IHubFactory, HubFactory>()
1213
.AddSingleton<IDeviceFactory, DeviceFactory>()
13-
.AddSingleton<PoweredUpHost>();
14+
.AddSingleton<PoweredUpHost>()
15+
.AddTransient<LinearMidCalibration>();
1416
}
1517
}

0 commit comments

Comments
 (0)