Skip to content
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

Converting analog values to f32 on arduino nano and uno behave strange #226

Closed
ChrisRega opened this issue Nov 12, 2021 · 3 comments
Closed
Labels
compiler-bug Not a bug in avr-hal, but a bug in the rust compiler/LLVM

Comments

@ChrisRega
Copy link

ChrisRega commented Nov 12, 2021

Hello,

I got an ADXL335 on arduino nano and uno and wanted to do some float-math with the analog values. I stumbled over the following:

#[arduino_hal::entry]
fn main() -> ! {
    let dp = arduino_hal::Peripherals::take().unwrap();
    let pins = arduino_hal::pins!(dp);
    let mut adc = arduino_hal::Adc::new(dp.ADC, Default::default());
    let a0 = pins.a0.into_analog_input(&mut adc);
    let mut serial = arduino_hal::default_serial!(dp, pins, 9600);
    loop {
        let input_data = a0.analog_read(&mut adc);
        let test_data_f = f32::from(input_data);
        ufmt::uwriteln!(&mut serial, "test: {} ", input_data).void_unwrap();
        let test_data_f_str = uFmt_f32::Three(test_data_f);
        ufmt::uwriteln!(&mut serial, "value as f32: {} ", test_data_f_str).void_unwrap();
        ufmt::uwriteln!(&mut serial, "f32 back to u16: {} ", test_data_f as u16).void_unwrap();
        arduino_hal::delay_ms(500);
    }
}

This results in the output:

test: 328 
value as f32: 0.000 
f32 back to u16: 0

The strange thing is, that if i hardcode the input value to an u16 value in the same range like:

let input_data: u16 = 400;

and then run the code, the float conversion works:

test: 400 
value as f32: 400.000 
f32 back to u16: 400 

Am I missing something here?
Many thanks for your help in advance!
Edit: I checked and can also confirm this on simavr - so nothing is wrong with my hardware. Shall i ask this question on the rust-compiler repo?

@Rahix
Copy link
Owner

Rahix commented Nov 13, 2021

This is probably one of the known floating point problems in the compiler. Search the issues in rust-lang/rust, you'll find reports similar to yours.

For now, I would recommend to not use floats - it isn't a great idea on AVR anyway because all floating point operations must be implemented in software which is slow and eats lots of space. Often you can, for example, use the Fixed<i16> type from the fixed crate instead.

@Rahix Rahix added the compiler-bug Not a bug in avr-hal, but a bug in the rust compiler/LLVM label Nov 13, 2021
@djdisodo
Copy link

image
it's not related to this issue but this failes to compile if i don't turn of lto

image
i wasn't able to represent the issue

@Rahix
Copy link
Owner

Rahix commented Jun 26, 2022

it's not related to this issue but this failes to compile if i don't turn of lto

This happens when compiler-builtins is not built in release-mode because it then generates references to items from libcore which it is not allowed to do. In release-mode these references are optimized out. Also check #131.

i wasn't able to represent the issue

Yeah, with the latest compiler changes I think these problems should be resolved. Let's close this issue for now.

@Rahix Rahix closed this as completed Jun 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-bug Not a bug in avr-hal, but a bug in the rust compiler/LLVM
Projects
None yet
Development

No branches or pull requests

3 participants