-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
171 additions
and
3 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
47 changes: 47 additions & 0 deletions
47
Source/Meadow.Foundation.Core/Simulation/SimulatedAccelerometer.cs
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,47 @@ | ||
using Meadow.Peripherals.Sensors; | ||
using Meadow.Peripherals.Sensors.Motion; | ||
using Meadow.Units; | ||
using System; | ||
|
||
namespace Meadow.Foundation.Sensors; | ||
|
||
public class SimulatedAccelerometer : SimulatedSamplingSensorBase<Acceleration3D>, IAccelerometer | ||
{ | ||
private Random random = new Random(); | ||
|
||
public Acceleration3D? Acceleration3D { get; private set; } | ||
|
||
public override SimulationBehavior[] SupportedBehaviors => new[] { SimulationBehavior.RandomWalk }; | ||
|
||
public override Type ValueType => typeof(Acceleration3D); | ||
|
||
public SimulatedAccelerometer() | ||
{ | ||
Acceleration3D = new Acceleration3D | ||
{ | ||
X = new Acceleration(0, Acceleration.UnitType.Gravity), | ||
Y = new Acceleration(0, Acceleration.UnitType.Gravity), | ||
Z = new Acceleration(1, Acceleration.UnitType.Gravity), | ||
}; | ||
} | ||
|
||
public override void SetSensorValue(object value) | ||
{ | ||
Acceleration3D = (Acceleration3D)value; | ||
} | ||
|
||
protected override Acceleration3D GenerateSimulatedValue(SimulationBehavior behavior) | ||
{ | ||
switch (behavior) | ||
{ | ||
case SimulationBehavior.RandomWalk: | ||
var rX = new Acceleration(random.NextDouble() - 0.5, Acceleration.UnitType.Gravity); | ||
var rY = new Acceleration(random.NextDouble() - 0.5, Acceleration.UnitType.Gravity); | ||
var rZ = new Acceleration(random.NextDouble() - 0.5, Acceleration.UnitType.Gravity); | ||
this.Acceleration3D = new Acceleration3D(rX, rY, rZ); | ||
break; | ||
} | ||
|
||
return this.Acceleration3D.Value; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
Source/Meadow.Foundation.Core/Simulation/SimulatedCurrentSensor.cs
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,45 @@ | ||
using Meadow.Peripherals.Sensors; | ||
using Meadow.Units; | ||
using System; | ||
|
||
namespace Meadow.Foundation.Sensors; | ||
|
||
public class SimulatedCurrentSensor : SimulatedSamplingSensorBase<Current>, ICurrentSensor | ||
{ | ||
private Random random = new Random(); | ||
|
||
private Current maxCurrent; | ||
private Current minCurrent; | ||
|
||
public Current? Current { get; private set; } | ||
|
||
public override SimulationBehavior[] SupportedBehaviors => new[] { SimulationBehavior.RandomWalk }; | ||
|
||
public override Type ValueType => typeof(Current); | ||
|
||
public SimulatedCurrentSensor(Current? maxCurrent = null, Current? minCurrent = null) | ||
{ | ||
this.minCurrent = minCurrent ?? new Current(0, Units.Current.UnitType.Amps); | ||
this.maxCurrent = maxCurrent ?? new Current(1, Units.Current.UnitType.Amps); | ||
|
||
Current = 0.Amps(); | ||
} | ||
|
||
public override void SetSensorValue(object value) | ||
{ | ||
Current = (Current)value; | ||
} | ||
|
||
protected override Current GenerateSimulatedValue(SimulationBehavior behavior) | ||
{ | ||
switch (behavior) | ||
{ | ||
case SimulationBehavior.RandomWalk: | ||
var r = random.NextDouble() * (maxCurrent.Amps - minCurrent.Amps) + minCurrent.Amps; | ||
this.Current = new Current(r); | ||
break; | ||
} | ||
|
||
return this.Current.Value; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
Source/Meadow.Foundation.Core/Simulation/SimulatedSamplingSensorBase.cs
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,69 @@ | ||
using Meadow.Peripherals.Sensors; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Meadow.Foundation.Sensors; | ||
|
||
public abstract class SimulatedSamplingSensorBase<UNIT> : ISimulatedSensor, ISamplingSensor<UNIT> | ||
where UNIT : struct | ||
{ | ||
public event EventHandler<IChangeResult<UNIT>>? Updated; | ||
|
||
private Timer updateTimer; | ||
private SimulationBehavior simulationBehavior; | ||
|
||
public virtual SimulationBehavior[] SupportedBehaviors => new[] { SimulationBehavior.None }; | ||
public abstract Type ValueType { get; } | ||
public TimeSpan UpdateInterval { get; private set; } | ||
public bool IsSampling { get; private set; } | ||
protected UNIT? PreviousReading { get; private set; } | ||
|
||
protected abstract UNIT GenerateSimulatedValue(SimulationBehavior behavior); | ||
public abstract void SetSensorValue(object value); | ||
|
||
|
||
protected SimulatedSamplingSensorBase() | ||
{ | ||
UpdateInterval = TimeSpan.FromSeconds(5); | ||
updateTimer = new Timer(UpdateTimerProc, null, -1, -1); | ||
} | ||
|
||
private async void UpdateTimerProc(object _) | ||
{ | ||
var newVal = await Read(); | ||
|
||
Updated?.Invoke(this, new ChangeResult<UNIT>(newVal, PreviousReading)); | ||
PreviousReading = newVal; | ||
|
||
updateTimer.Change(UpdateInterval, TimeSpan.FromMilliseconds(-1)); | ||
} | ||
|
||
public Task<UNIT> Read() | ||
{ | ||
return Task.FromResult(GenerateSimulatedValue(simulationBehavior)); | ||
} | ||
|
||
public virtual void StartSimulation(SimulationBehavior behavior) | ||
{ | ||
simulationBehavior = behavior; | ||
} | ||
|
||
public void StartUpdating(TimeSpan? updateInterval = null) | ||
{ | ||
IsSampling = true; | ||
|
||
if (updateInterval != null) | ||
{ | ||
UpdateInterval = updateInterval.Value; | ||
} | ||
|
||
updateTimer.Change(UpdateInterval, TimeSpan.FromMilliseconds(-1)); | ||
} | ||
|
||
public void StopUpdating() | ||
{ | ||
IsSampling = false; | ||
updateTimer.Change(TimeSpan.FromMilliseconds(-1), TimeSpan.FromMilliseconds(-1)); | ||
} | ||
} |