-
Notifications
You must be signed in to change notification settings - Fork 3
Add operator to convert raw adc units to volt #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bruno-f-cruz
wants to merge
3
commits into
main
Choose a base branch
from
feat-add-analog-voltage-converter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+101
−0
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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; | ||
| } | ||
| } | ||
|
|
||
| /// <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 + " " + | ||
| "}"; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.