rust-embedded / embedded-hal

A Hardware Abstraction Layer (HAL) for embedded systems
Apache License 2.0
2.01k stars 202 forks source link

Add ADC `Voltmeter` and `Ammeter` traits. #569

Open reitermarkus opened 10 months ago

reitermarkus commented 10 months ago

This adds an AdcChannel trait as mentioned in https://github.com/rust-embedded/embedded-hal/issues/377.

Still needs to be tested by implementing an actual driver. I feel there is something still missing with regard to how the return value can actually be used further. Is it even practical to have this trait without having a corresponding unit and a lower/upper bound for converting the raw value?

onkoe commented 10 months ago

That's a good point! Also, it may be challenging to take something implementing AdcChannel in a library if there's no way to tell what the voltage 'could' be.

However, how do you ask every implementation to know the voltage range of an arbitrary device? Would having an Option<Voltage> suffice? (i.e., we 'ask' the implementation to tell us if it knows)

My apologies if I've missed your point, though! 😄

reitermarkus commented 10 months ago

I have changed the API here now to measure voltage instead of the raw value.

the voltage range of an arbitrary device

I think you may have indeed missed my point. 😄 I am talking about the voltage range of the ADC itself. So it's not an arbitrary device's voltage, but the voltage the ADC is actually measuring. For example, with a 16-bit ADC with a range of ±5V, this means i16::MIN corresponds to -5V and i16::MAX to +5V.

Then you can swap out ADCs, as long as they support the range you need. And the rest of the code only depends on the measured voltage. As a simple example, I have a water depth sensor with a range of 0-5V corresponding to 0-5m, which would be a 1:1 mapping from mV to mm. So as long as I have any ADC that can measure between 0 and 5V, I can implement e.g. a WaterDepthSensor using the trait.

onkoe commented 10 months ago

Got it - I thought I was missing something! Thanks for the clarification ☺️

reitermarkus commented 10 months ago

Given ADCs measure either voltage or current, I have now split the AdcChannel trait into Voltmeter and Ammeter traits.

kurtjd commented 4 months ago

I would be interested in seeing this gain more traction. My particular use case is for developing generic drivers for analog temperature sensors. Would be nice for the drivers to not have to depend on any particular ADC implementation.