Skip to content

Commit

Permalink
added some simulation work
Browse files Browse the repository at this point in the history
  • Loading branch information
ctacke committed Apr 19, 2024
1 parent ef19b59 commit d5b4d4a
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Source/Meadow.Foundation.Core/Motors/BidirectionalDcMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public BidirectionalDcMotor(
/// </summary>
public void Stop()
{
_outputA.State = _outputB.State = _energizeHigh ? false : true;
var state = !_energizeHigh;
if (state == _outputA.State && state == _outputB.State) return;

_outputA.State = _outputB.State = state;
StateChanged?.Invoke(this, State);
}

Expand All @@ -88,7 +91,9 @@ public void Stop()
/// </summary>
public void StartClockwise()
{
_outputA.State = !(_outputB.State = _energizeHigh ? true : false);
var state = !(_outputB.State = _energizeHigh);
if (state == _outputA.State) return;
_outputA.State = state;
StateChanged?.Invoke(this, State);
}

Expand All @@ -97,7 +102,9 @@ public void StartClockwise()
/// </summary>
public void StartCounterClockwise()
{
_outputA.State = !(_outputB.State = _energizeHigh ? false : true);
var state = !(_outputB.State = !_energizeHigh);
if (state == _outputA.State) return;
_outputA.State = state;
StateChanged?.Invoke(this, State);
}
}
47 changes: 47 additions & 0 deletions Source/Meadow.Foundation.Core/Simulation/SimulatedAccelerometer.cs
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

Check warning on line 8 in Source/Meadow.Foundation.Core/Simulation/SimulatedAccelerometer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SimulatedAccelerometer'
{
private Random random = new Random();

public Acceleration3D? Acceleration3D { get; private set; }

Check warning on line 12 in Source/Meadow.Foundation.Core/Simulation/SimulatedAccelerometer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SimulatedAccelerometer.Acceleration3D'

public override SimulationBehavior[] SupportedBehaviors => new[] { SimulationBehavior.RandomWalk };

Check warning on line 14 in Source/Meadow.Foundation.Core/Simulation/SimulatedAccelerometer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SimulatedAccelerometer.SupportedBehaviors'

public override Type ValueType => typeof(Acceleration3D);

Check warning on line 16 in Source/Meadow.Foundation.Core/Simulation/SimulatedAccelerometer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SimulatedAccelerometer.ValueType'

public SimulatedAccelerometer()

Check warning on line 18 in Source/Meadow.Foundation.Core/Simulation/SimulatedAccelerometer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SimulatedAccelerometer.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)

Check warning on line 28 in Source/Meadow.Foundation.Core/Simulation/SimulatedAccelerometer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SimulatedAccelerometer.SetSensorValue(object)'
{
Acceleration3D = (Acceleration3D)value;
}

protected override Acceleration3D GenerateSimulatedValue(SimulationBehavior behavior)

Check warning on line 33 in Source/Meadow.Foundation.Core/Simulation/SimulatedAccelerometer.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SimulatedAccelerometer.GenerateSimulatedValue(SimulationBehavior)'
{
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;

Check warning on line 45 in Source/Meadow.Foundation.Core/Simulation/SimulatedAccelerometer.cs

View workflow job for this annotation

GitHub Actions / build

Nullable value type may be null.
}
}
45 changes: 45 additions & 0 deletions Source/Meadow.Foundation.Core/Simulation/SimulatedCurrentSensor.cs
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

Check warning on line 7 in Source/Meadow.Foundation.Core/Simulation/SimulatedCurrentSensor.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SimulatedCurrentSensor'
{
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;

Check warning on line 43 in Source/Meadow.Foundation.Core/Simulation/SimulatedCurrentSensor.cs

View workflow job for this annotation

GitHub Actions / build

Nullable value type may be null.
}
}
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));
}
}

0 comments on commit d5b4d4a

Please sign in to comment.