11using System ;
22using System . Reactive . Linq ;
33using System . Threading . Tasks ;
4- using Microsoft . Extensions . DependencyInjection ;
54using Microsoft . Extensions . Logging ;
65using static SharpBrick . PoweredUp . Directions ;
76
87namespace 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}
0 commit comments