Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions Interface/Harp.Behavior/AdcToVolt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using Bonsai.Harp;
using Bonsai;

namespace Harp.Behavior
{
/// <summary>
/// Represents an operator that convertes raw ADC values to Volts.
/// </summary>
[Description("Converts a sequence of raw ADC reads to Volt.")]
public class AdcToVolt : Transform<AnalogDataPayload, AnalogVoltData>
{
/// <summary>
/// Converts a <see cref="AnalogDataPayload"/> with raw voltage readings to volts.
/// </summary>
/// <returns>
/// A sequence of <see cref="AnalogVoltData"/> objects representing the converted values.
/// </returns>
public override IObservable<AnalogVoltData> Process(IObservable<AnalogDataPayload> source)
{
return source.Select(
value => new AnalogVoltData()
{
AnalogInput0 = AdcToVoltConverter(value.AnalogInput0),
AnalogInput1 = AdcToVoltConverter(value.AnalogInput1)
});
}

/// <summary>
/// Converts a sequence of <see cref="Timestamped"/> <see cref="AnalogDataPayload"/> values
/// with raw voltage readings to volts.
/// </summary>
/// <returns>
/// A sequence of <see cref="Timestamped"/> <see cref="AnalogVoltData"/> objects representing the converted values.
/// </returns>
public IObservable<Timestamped<AnalogVoltData>> Process(IObservable<Timestamped<AnalogDataPayload>> 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);
});
}

/// <summary>
/// Converts a raw ADC value to volts.
/// </summary>
/// <param name="adcValue">The raw ADC value to be converted.</param>
/// <returns>The converted voltage value.</returns>
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;
Copy link
Copy Markdown
Contributor

@artursilva0 artursilva0 Feb 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 - To be more precise, the 2.0625V is the configured maximum range of the Atxmega ADC, however the maximum value that the ADC receives when the input is at 5V is dependent on input resistive divider, i.e. 15/39 * 5V for hw >=v2.0. Therefore Vmax input ADC = 1.92307V ~ 3918.2 => return (float)(5.0 / 3918.2) * adcValue;
2 - For hw <= v2.0 the value is 16/40*5V = 2V ~ 3970.9;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok this is not good... @glopesdev I can only think of having an extra property that sets this value and it is by default for hw>=2.0. From the point of view of the interface, there is no way to distinguish a message coming from different hw versions, unfortunately.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok after thinking about this. I think we have 2 options

  1. Have two options in the operator that can be toggled for the two converters
  2. Change the firmware in <hw 2.0 boards to correct the raw adc unit scale to the same scale as hw2.0. From the point of view of the interface they would be fully compatible.

I am leaning towards the second solution has it would make the system a bit more robust from the point of view of the user. So I think for now we just implement this operator for hw 2.0 and release a new firmware version with the full scale patched.

Copy link
Copy Markdown
Collaborator

@glopesdev glopesdev Feb 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bruno-f-cruz @artursilva0 @filcarv there is a 3rd option to consider: generate two different packages, one for Behavior v1 and one for Behavior v2.

It might make sense because there are other differences than just the scaling factor, for example the v1 only has one ADC, so the packages could be structurally different.

We could either keep them in the same repo and have two branches with different YML files, or two different repos.

}
}

/// <summary>
/// Represents the converted values from raw adc units to volts from
/// a payload of the AnalogData register.
/// </summary>
public struct AnalogVoltData
{
/// <summary>
/// The voltage at the output of the ADC channel 0.
/// </summary>
public float AnalogInput0;

/// <summary>
/// The voltage at the output of the ADC channel 1.
/// </summary>
public float AnalogInput1;

/// <summary>
/// Returns a <see cref="string"/> that represents the
/// converted AnalogData payload.
/// </summary>
/// <returns>
/// A <see cref="string"/> that represents the
/// converted AnalogData payload.
/// </returns>
public override string ToString()
{
return "AnalogVoltData { " +
"AnalogInput0 = " + AnalogInput0 + ", " +
"AnalogInput1 = " + AnalogInput1 + " " +
"}";
}
}
}