Page 1 of 1
How to use mathematical functions
Posted: Mon Feb 01, 2021 11:17 am
by ales.coppelli
Hi to all.
I'm developing a simple lsm303dhlc accelerometer driver
like user module so I'm writing the C version that
which will be added and exposed to the Python interpreter.
( I have a STM32F411 Discovery board and the lsm303dhlc
accelerometer is hard linked to the PB6(scl) and PB9(sda) board pin).
In the user module directory there is this 'micropython.mk' file:
Code: Select all
USERMODULES_DIR := $(USERMOD_DIR)
# Add all C files to SRC_USERMOD.
SRC_USERMOD += $(USERMODULES_DIR)/lsm303dlhc.c
CFLAGS_USERMOD += -I$(USERMODULES_DIR)
All ok until now.
I would like to add to my C file some mathematical functions
( like 'sqrt' to calculate the standard deviation
that it's equal to sqrt(variance) )
I thought it was enough to add
in "lsm303dlhc.c" and add the line
in 'micropython.mk'.
But that's not enough, it doesn't work.
What should I do?
Re: How to use mathematical functions
Posted: Mon Feb 01, 2021 11:54 am
by stijn
it doesn't work.
Please post the actual error you're getting otherwise it's really hard to produce sensible answers.
Re: How to use mathematical functions
Posted: Mon Feb 01, 2021 12:21 pm
by ales.coppelli
Sorry for my incomplete post..
Here the logs:
Code: Select all
CC ../../../project/lsm303dlhc/lsm303dlhc.c
CC ../../lib/oofatfs/ff.c
CC ../../lib/oofatfs/ffunicode.c
CC build-STM32F411DISC/pins_STM32F411DISC.c
LINK build-STM32F411DISC/firmware.elf
arm-none-eabi-ld: cannot find -lm
Makefile:618: recipe for target 'build-STM32F411DISC/firmware.elf' failed
make: *** [build-STM32F411DISC/firmware.elf] Error 1
Re: How to use mathematical functions
Posted: Tue Feb 02, 2021 4:09 am
by jimmo
ales.coppelli wrote: ↑Mon Feb 01, 2021 11:17 am
I would like to add to my C file some mathematical functions
( like 'sqrt' to calculate the standard deviation
that it's equal to sqrt(variance) )
Likely you're compiling for single-precision float, in which case you should be using sqrtf not sqrt. sqrt will only be available when compiling for double-precision. Both are provided by MicroPython's math library (lib/libm and lib/libm_double).
In general though, use MICROPY_FLOAT_C_FUN which will use the correct version automatically based on single/double precision.
e.g. MICROPY_FLOAT_C_FUN(sqrt)(variance)
ales.coppelli wrote: ↑Mon Feb 01, 2021 11:17 am
LDFLAGS_USERMOD += -lm
-lm would be what you'd use if you were compiling for a non-baremetal target. We use the copy in lib/libm instead (and the Makefile already handles this).
Re: How to use mathematical functions
Posted: Wed Feb 03, 2021 6:59 am
by ales.coppelli
Thank you very much: always precise and decisive.
Re: How to use mathematical functions
Posted: Sun Feb 07, 2021 4:51 pm
by v923z
ales.coppelli wrote: ↑Mon Feb 01, 2021 11:17 am
I would like to add to my C file some mathematical functions
( like 'sqrt' to calculate the standard deviation
that it's equal to sqrt(variance) )
Pretty much everything you can think of is already implemented in ulab:
https://github.com/v923z/micropython-ulab/.
You can also directly pipe data into arrays:
https://micropython-ulab.readthedocs.io ... array.html, and then operate on them in numpy fashion.
Re: How to use mathematical functions
Posted: Mon Feb 08, 2021 4:07 pm
by ales.coppelli
This library can only be compiled for some stm32 cards.
I am wrong?
P.S.= Your library is amazing!!!
Re: How to use mathematical functions
Posted: Mon Feb 08, 2021 5:41 pm
by v923z
ales.coppelli wrote: ↑Mon Feb 08, 2021 4:07 pm
This library can only be compiled for some stm32 cards.
I think this is not true. Here are a couple of counter-examples:
https://github.com/v923z/micropython-ulab/#firmware
https://gitlab.com/rcolistete/micropyth ... 6/Firmware
https://gitlab.com/rcolistete/micropyth ... 2/Firmware
(Note that the compiled firmware in the links above contains a relatively old version of ulab. We have improved a lot of things since, so you might be better off compiling yourself.)
I know that circuitpython ships with ulab on nordic chips.
The library is a simple user module, therefore, I don't see, why you couldn't compile it for anything, really. If you run into difficulties, you should raise an issue on github, so that we can fix it.