-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #851 from WildernessLabs/AtlasDO
Atlas Gravity Dissolved Oxygen sensor
- Loading branch information
Showing
11 changed files
with
275 additions
and
4 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
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
Binary file added
BIN
+1.14 MB
...ls/Sensors.Environmental.AtlasScientificGravityDOMeter/Datasheet/Gravity_DO_datasheet.pdf
Binary file not shown.
Binary file added
BIN
+2.72 MB
...ripherals/Sensors.Environmental.AtlasScientificGravityDOMeter/Datasheet/Mini_DO_probe.pdf
Binary file not shown.
130 changes: 130 additions & 0 deletions
130
...nsors.Environmental.AtlasScientificGravityDOMeter/Driver/AtlasScientificGravityDOMeter.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,130 @@ | ||
using Meadow.Hardware; | ||
using Meadow.Peripherals.Sensors.Environmental; | ||
using Meadow.Units; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Meadow.Foundation.Sensors.Environmental; | ||
|
||
/// <summary> | ||
/// Atlas Scientific Analog Gravity Dissolved Oxygen Meter | ||
/// </summary> | ||
public partial class AtlasScientificGravityDOMeter : SamplingSensorBase<double>, IDissolvedOxygenSensor | ||
{ | ||
/// <summary> | ||
/// Raised when a new sensor percentage saturation reading is ready | ||
/// </summary> | ||
public event EventHandler<IChangeResult<double>> SaturationUpdated = delegate { }; | ||
|
||
/// <summary> | ||
/// The calibration value for the sensor in air | ||
/// </summary> | ||
public Voltage CalibrationInAir { get; set; } = new Voltage(0.05, Voltage.UnitType.Volts); | ||
|
||
/// <summary> | ||
/// Returns the analog input port | ||
/// </summary> | ||
protected IAnalogInputPort AnalogInputPort { get; } | ||
|
||
/// <summary> | ||
/// Last saturation value read from the sensor (0.0-1.0) | ||
/// </summary> | ||
public double? Saturation { get; protected set; } | ||
|
||
/// <summary> | ||
/// Creates a new AtlasScientificGravityDOMeter object | ||
/// </summary> | ||
/// <param name="analogInputPin">Analog pin the temperature sensor is connected to</param> | ||
/// <param name="sampleCount">How many samples to take during a given reading</param> | ||
/// <param name="sampleInterval">The time, to wait in between samples during a reading</param> | ||
public AtlasScientificGravityDOMeter(IPin analogInputPin, int sampleCount = 5, TimeSpan? sampleInterval = null) | ||
: this(analogInputPin.CreateAnalogInputPort(sampleCount, sampleInterval ?? TimeSpan.FromMilliseconds(40), new Voltage(3.3))) | ||
{ } | ||
|
||
/// <summary> | ||
/// Creates a new AtlasScientificGravityDOMeter object | ||
/// </summary> | ||
/// <param name="analogInputPort">The port for the analog input pin</param> | ||
public AtlasScientificGravityDOMeter(IAnalogInputPort analogInputPort) | ||
{ | ||
AnalogInputPort = analogInputPort; | ||
|
||
AnalogInputPort.Subscribe | ||
( | ||
IAnalogInputPort.CreateObserver( | ||
result => | ||
{ | ||
ChangeResult<double> changeResult = new() | ||
{ | ||
New = VoltageToSaturation(result.New), | ||
Old = Saturation | ||
}; | ||
Saturation = changeResult.New; | ||
RaiseEventsAndNotify(changeResult); | ||
} | ||
) | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// Get the current voltage, useful for calibration | ||
/// </summary> | ||
/// <returns></returns> | ||
public Task<Voltage> GetCurrentVoltage() | ||
{ | ||
return AnalogInputPort.Read(); | ||
} | ||
|
||
/// <summary> | ||
/// Reads data from the sensor | ||
/// </summary> | ||
/// <returns>The latest sensor reading</returns> | ||
protected override async Task<double> ReadSensor() | ||
{ | ||
var voltage = await AnalogInputPort.Read(); | ||
var newSaturation = VoltageToSaturation(voltage); | ||
Saturation = newSaturation; | ||
return newSaturation; | ||
} | ||
|
||
/// <summary> | ||
/// Starts continuously sampling the sensor | ||
/// </summary> | ||
public override void StartUpdating(TimeSpan? updateInterval) | ||
{ | ||
lock (samplingLock) | ||
{ | ||
if (IsSampling) { return; } | ||
IsSampling = true; | ||
AnalogInputPort.StartUpdating(updateInterval); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Stops sampling the sensor | ||
/// </summary> | ||
public override void StopUpdating() | ||
{ | ||
lock (samplingLock) | ||
{ | ||
if (!IsSampling) { return; } | ||
IsSampling = false; | ||
AnalogInputPort.StopUpdating(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Raise change events for subscribers | ||
/// </summary> | ||
/// <param name="changeResult">The change result with the current sensor data</param> | ||
protected override void RaiseEventsAndNotify(IChangeResult<double> changeResult) | ||
{ | ||
SaturationUpdated?.Invoke(this, changeResult); | ||
base.RaiseEventsAndNotify(changeResult); | ||
} | ||
|
||
double VoltageToSaturation(Voltage voltage) | ||
{ | ||
return voltage.Millivolts / CalibrationInAir.Millivolts; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...eripherals/Sensors.Environmental.AtlasScientificGravityDOMeter/Driver/Readme.md
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 @@ | ||
# Meadow.Foundation.Sensors.Environmental.AltasScientificGravityDOMeter |
27 changes: 27 additions & 0 deletions
27
...cientificGravityDOMeter/Driver/Sensors.Environmental.AtlasScientificGravityDOMeter.csproj
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,27 @@ | ||
<Project Sdk="Meadow.Sdk/1.1.0"> | ||
<PropertyGroup> | ||
<PackageReadmeFile>Readme.md</PackageReadmeFile> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>10.0</LangVersion> | ||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<PackageIcon>icon.png</PackageIcon> | ||
<Authors>Wilderness Labs, Inc</Authors> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<OutputType>Library</OutputType> | ||
<AssemblyName>AtlasScientificGravityDOMeter</AssemblyName> | ||
<Company>Wilderness Labs, Inc</Company> | ||
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.Foundation/</PackageProjectUrl> | ||
<PackageId>Meadow.Foundation.Sensors.Environmental.AtlasScientificGravityDOMeter</PackageId> | ||
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl> | ||
<PackageTags>Meadow,Meadow.Foundation,Environmental,Atlas,Scientific,Gravity,DO,Dissolved,Oxygen,Meter</PackageTags> | ||
<Version>0.1.0</Version> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Description>Atlas Scientific analog gravity dissolved oxygen sensor</Description> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<None Include=".\Readme.md" Pack="true" PackagePath=""/> | ||
<None Include="..\..\..\icon.png" Pack="true" PackagePath="" /> | ||
<ProjectReference Include="..\..\..\Meadow.Foundation.Core\Meadow.Foundation.Core.csproj" /> | ||
</ItemGroup> | ||
</Project> |
20 changes: 20 additions & 0 deletions
20
.../Samples/AtlasScientificGravityDOMeter_Sample/AtlasScientificGravityDOMeter_Sample.csproj
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,20 @@ | ||
<Project Sdk="Meadow.Sdk/1.1.0"> | ||
<PropertyGroup> | ||
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl> | ||
<Company>Wilderness Labs, Inc</Company> | ||
<Authors>Wilderness Labs, Inc</Authors> | ||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<OutputType>Library</OutputType> | ||
<AssemblyName>App</AssemblyName> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\..\..\Meadow.Core\Source\implementations\f7\Meadow.F7\Meadow.F7.csproj" /> | ||
<ProjectReference Include="..\..\Driver\Sensors.Environmental.AtlasScientificGravityDOMeter.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Update="meadow.config.yaml"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
</Project> |
68 changes: 68 additions & 0 deletions
68
...l.AtlasScientificGravityDOMeter/Samples/AtlasScientificGravityDOMeter_Sample/MeadowApp.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,68 @@ | ||
using Meadow; | ||
using Meadow.Devices; | ||
using Meadow.Foundation.Sensors.Environmental; | ||
using Meadow.Units; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sensors.Environmental.AtlasScientificGravityDOMeter_Sample | ||
{ | ||
public class MeadowApp : App<F7FeatherV2> | ||
{ | ||
//<!=SNIP=> | ||
|
||
AtlasScientificGravityDOMeter sensor; | ||
|
||
public override Task Initialize() | ||
{ | ||
Resolver.Log.Info("Initialize..."); | ||
|
||
sensor = new AtlasScientificGravityDOMeter(Device.Pins.A01); | ||
sensor.CalibrationInAir = new Voltage(0.04, Voltage.UnitType.Volts); | ||
|
||
// Example that uses an IObservable subscription to only be notified when the saturation changes | ||
var consumer = AtlasScientificGravityDOMeter.CreateObserver( | ||
handler: result => | ||
{ | ||
string oldValue = (result.Old is { } old) ? $"{old * 100:n1}" : "n/a"; | ||
string newValue = $"{result.New * 100:n1}"; | ||
Resolver.Log.Info($"New: {newValue}%, Old: {oldValue}%"); | ||
}, | ||
filter: null | ||
); | ||
sensor.Subscribe(consumer); | ||
|
||
// optional classical .NET events can also be used: | ||
sensor.SaturationUpdated += (sender, result) => | ||
{ | ||
// string oldValue = (result.Old is { } old) ? $"{old * 100:n0}%" : "n/a"; | ||
// Resolver.Log.Info($"Updated - New: {result.New * 100:n0}%, Old: {oldValue}"); | ||
}; | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public override async Task Run() | ||
{ | ||
Resolver.Log.Info("Run..."); | ||
|
||
await ReadSensor(); | ||
|
||
//example calibration setting, ensure the sensor is set up for calibration | ||
var calibrationVoltage = await sensor.GetCurrentVoltage(); | ||
sensor.CalibrationInAir = calibrationVoltage; | ||
|
||
Resolver.Log.Info($"Calibration voltage: {calibrationVoltage.Volts}V"); | ||
|
||
sensor.StartUpdating(TimeSpan.FromSeconds(2)); | ||
} | ||
|
||
protected async Task ReadSensor() | ||
{ | ||
var saturation = await sensor.Read(); | ||
Resolver.Log.Info($"Initial saturation: {saturation * 100:N1}%"); | ||
} | ||
|
||
//<!=SNOP=> | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...sScientificGravityDOMeter/Samples/AtlasScientificGravityDOMeter_Sample/meadow.config.yaml
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,2 @@ | ||
MonoControl: | ||
Options: --jit |
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