From 3adf40c72af48975c860422ab508afb30433fefa Mon Sep 17 00:00:00 2001
From: bruno-f-cruz <7049351+bruno-f-cruz@users.noreply.github.com>
Date: Sat, 1 Feb 2025 10:43:24 -0800
Subject: [PATCH 1/3] Add operator to convert raw adc units to volt
---
Interface/Harp.Behavior/AdcToVolt.cs | 99 ++++++++++++++++++++++++++++
1 file changed, 99 insertions(+)
create mode 100644 Interface/Harp.Behavior/AdcToVolt.cs
diff --git a/Interface/Harp.Behavior/AdcToVolt.cs b/Interface/Harp.Behavior/AdcToVolt.cs
new file mode 100644
index 0000000..4c08f84
--- /dev/null
+++ b/Interface/Harp.Behavior/AdcToVolt.cs
@@ -0,0 +1,99 @@
+using System;
+using System.ComponentModel;
+using System.Linq;
+using System.Reactive.Linq;
+using Bonsai.Harp;
+using Bonsai;
+
+namespace Harp.Behavior
+{
+ ///
+ /// Represents an operator that convertes raw ADC values to Volts.
+ ///
+ [Description("Converts a sequence of raw ADC reads to Volt.")]
+ public class AdcToVolt : Transform
+ {
+ ///
+ /// Converts a with raw voltage readings to volts.
+ ///
+ ///
+ /// A sequence of objects representing the converted values.
+ ///
+ public override IObservable Process(IObservable source)
+ {
+ return source.Select(
+ value => new AnalogVoltData()
+ {
+ AnalogInput0 = AdcToVoltConverter(value.AnalogInput0),
+ AnalogInput1 = AdcToVoltConverter(value.AnalogInput1)
+ });
+ }
+
+ ///
+ /// Converts a sequence of values
+ /// with raw voltage readings to volts.
+ ///
+ ///
+ /// A sequence of objects representing the converted values.
+ ///
+ public IObservable> Process(IObservable> source)
+ {
+ return source.Select(
+ value =>
+ {
+ var payload = new AnalogVoltData()
+ {
+ AnalogInput0 = AdcToVoltConverter(value.Value.AnalogInput0),
+ AnalogInput1 = AdcToVoltConverter(value.Value.AnalogInput1)
+ };
+ return Timestamped.Create(payload, value.Seconds);
+ });
+ }
+
+ ///
+ /// Converts a raw ADC value to volts.
+ ///
+ /// The raw ADC value to be converted.
+ /// The converted voltage value.
+ private static float AdcToVoltConverter(short adcValue)
+ {
+ // ADC input = 2.0 V means 5.0 V on boards input
+ // 4096 -> 3.3/1.6 = 2.0625 V
+ // ~3972 -> 2.0 V @ ADC -> 5.0 V @ Analog input
+ return (float)(5.0 / 3972.0) * adcValue;
+ }
+ }
+
+ ///
+ /// Represents the converted values from raw adc units to volts from
+ /// a payload of the AnalogData register.
+ ///
+ public struct AnalogVoltData
+ {
+ ///
+ /// The voltage at the output of the ADC channel 0.
+ ///
+ public float AnalogInput0;
+
+ ///
+ /// The voltage at the output of the ADC channel 1.
+ ///
+ public float AnalogInput1;
+
+ ///
+ /// Returns a that represents the
+ /// converted AnalogData payload.
+ ///
+ ///
+ /// A that represents the
+ /// converted AnalogData payload.
+ ///
+ public override string ToString()
+ {
+ return "AnalogVoltData { " +
+ "AnalogInput0 = " + AnalogInput0 + ", " +
+ "AnalogInput1 = " + AnalogInput1 + " " +
+ "}";
+ }
+ }
+}
From ccf8c18c7fba5023cdc2f3b2c67909ceabf9d1d9 Mon Sep 17 00:00:00 2001
From: bruno-f-cruz <7049351+bruno-f-cruz@users.noreply.github.com>
Date: Sat, 1 Feb 2025 13:00:41 -0800
Subject: [PATCH 2/3] Fix adc to voltage conversion
---
Interface/Harp.Behavior/AdcToVolt.cs | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/Interface/Harp.Behavior/AdcToVolt.cs b/Interface/Harp.Behavior/AdcToVolt.cs
index 4c08f84..79b2158 100644
--- a/Interface/Harp.Behavior/AdcToVolt.cs
+++ b/Interface/Harp.Behavior/AdcToVolt.cs
@@ -57,10 +57,12 @@ public IObservable> Process(IObservableThe converted voltage value.
private static float AdcToVoltConverter(short adcValue)
{
- // ADC input = 2.0 V means 5.0 V on boards input
- // 4096 -> 3.3/1.6 = 2.0625 V
- // ~3972 -> 2.0 V @ ADC -> 5.0 V @ Analog input
- return (float)(5.0 / 3972.0) * adcValue;
+ // Full adc scale 4096 -> 3.3/1.6 = 2.0625V
+ // In pratice, HW supports max 5V which translates
+ // to = 15/39 (given by resistor) * 5V = 1.9231 VMax
+ // input to the ADC.
+ // 1.9231V/2.0625V * 4095 = 3818 @ 5V analog input
+ return (float)(5.0 / 3818) * adcValue;
}
}
From b9b923b0a2d71a05ebc3065e8ab8cfcfdd91726f Mon Sep 17 00:00:00 2001
From: brunocruz <7049351+bruno-f-cruz@users.noreply.github.com>
Date: Sat, 1 Feb 2025 13:52:00 -0800
Subject: [PATCH 3/3] Fix minor typo
---
Interface/Harp.Behavior/AdcToVolt.cs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/Interface/Harp.Behavior/AdcToVolt.cs b/Interface/Harp.Behavior/AdcToVolt.cs
index 79b2158..4495227 100644
--- a/Interface/Harp.Behavior/AdcToVolt.cs
+++ b/Interface/Harp.Behavior/AdcToVolt.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
@@ -58,7 +58,7 @@ public IObservable> Process(IObservable 3.3/1.6 = 2.0625V
- // In pratice, HW supports max 5V which translates
+ // In practice, HW supports max 5V which translates
// to = 15/39 (given by resistor) * 5V = 1.9231 VMax
// input to the ADC.
// 1.9231V/2.0625V * 4095 = 3818 @ 5V analog input