33using Meadow . Units ;
44using System ;
55using System . Threading . Tasks ;
6+ using YamlDotNet . Core . Tokens ;
67
78namespace Meadow . Devices . Clima . Controllers ;
89
@@ -33,6 +34,14 @@ public SensorController(IClimaHardware clima)
3334 {
3435 latestData = new SensorData ( ) ;
3536 this . clima = clima ;
37+
38+ if ( Resolver . App . Settings . TryGetValue ( "Clima.OffsetToNorth" , out string offsetToNorthSetting ) )
39+ {
40+ if ( Double . TryParse ( offsetToNorthSetting , out double trueNorth ) )
41+ {
42+ OffsetToNorth = new Azimuth ( trueNorth ) ;
43+ }
44+ }
3645 }
3746
3847 /// <summary>
@@ -218,15 +227,33 @@ private void RainGaugeUpdated(object sender, IChangeResult<Length> e)
218227 Resolver . Log . InfoIf ( LogSensorData , $ "Rain Gauge: { e . New . Millimeters : 0.#} mm") ;
219228 }
220229
230+ /// <summary>
231+ /// 0 to 360 degree offset to true north updated from app.config.yaml
232+ /// </summary>
233+ /// <remarks>
234+ /// After install of Clima, point wind vane at true north, run Clima_Demo and record uncalibrated WindDirection.
235+ /// Update app.config.yaml with the uncalibrated WindDirection.
236+ /// Deploy the updated app.config.yaml and this direction will be reported as 0 Degrees.
237+ /// </remarks>
238+ /// <example>
239+ /// # Settings for Clima
240+ /// Clima:
241+ /// OffsetToNorth: 0.0
242+ /// </example>
243+ public Azimuth OffsetToNorth { get ; private set ; } = new Azimuth ( 0.0 ) ;
244+
221245 private void WindvaneUpdated ( object sender , IChangeResult < Azimuth > e )
222246 {
223- windVaneBuffer . Append ( e . New ) ;
247+ Azimuth newAzimuth = e . New - OffsetToNorth ;
248+ windVaneBuffer . Append ( newAzimuth ) ;
249+
250+ Azimuth mean = windVaneBuffer . Mean ( ) ;
224251
225252 lock ( latestData )
226253 {
227- latestData . WindDirection = windVaneBuffer . Mean ( ) ;
254+ latestData . WindDirection = mean ;
228255 }
229256
230- Resolver . Log . InfoIf ( LogSensorData , $ "Wind Vane: { e . New . DecimalDegrees } (mean : { windVaneBuffer . Mean ( ) . DecimalDegrees } )") ;
257+ Resolver . Log . InfoIf ( LogSensorData , $ "Wind Vane: { newAzimuth } , Average: { mean . DecimalDegrees } (uncalibrated : { e . New } )") ;
231258 }
232259}
0 commit comments