stm32duino / LSM6DSOX

Arduino library to support the LSM6DSOX 3D accelerometer and 3D gyroscope
27 stars 16 forks source link

Add basic examples #8

Closed Benhalor closed 3 years ago

Benhalor commented 3 years ago

Hello, Having spend somes hours to understand the basics, I hope this example might help beginners to start with this powerful library. Best regards,

cparata commented 3 years ago

Hi @Benhalor , thanks a lot for contribution! Which hardware setup are you using for these examples? Are you using STEVAL-MKI197V1 connected by wires to a Nucleo board? It would be great to explain at the beginning of each sketch what is the hardware setup in order to help users to reproduce your examples. If you are also interested on MLC feature of LSM6DSOX, you can find an example of usage here. Best Regards, Carlo

pnaybour commented 3 years ago

Dear @Benhalor @cparata The are really useful contributions. I have been using this library with Adafruit Feather STM32F405 Express and Adafruit LSM6DSOX + LIS3MDL - Precision 9 DoF IMU - STEMMA QT / Qwiic. And making some progress, but it's goo to see the code from others. I haven't tried the Fifo mode yet. It is really useful to see your code. I will upload what I have working later.

Benhalor commented 3 years ago

Hi @cparata I'm using this library with a modified esp32cam device and the Adafruit LSM6DSOX , but not sure this specific setup is a good example. Maybe @pnaybour would share a better example with his Adafruit Feather STM32F405. Thanks for sharing the example of the MLC feature. Why not copying this example in this repo ? Best regards

cparata commented 3 years ago

Hi @Benhalor , I added the example of MLC in the X-NUCLEO-IKS01A3 library because the hardware setup requires a X-NUCLEO-IKS01A3 + STEVAL-MKI197V1 as explained in the sketch code. I would like to keep in this library only examples that work with the pure component (STEVAL-MKI197V1 or Adafruit LSM6DSOX are fine for me) and a Nucleo board where we can explain how to connect with wires the pins of the LSM6DSOX board to a Nucleo board. In this way, it would be easier for users to get the necessary hardware and reproduce a working setup to test the library. I would like also to inform you that I'm going to update all MEMS and ToF libraries in the STM32duino repository adding "begin" and "end" APIs in all sensor classes in order to be more compliant with Arduino specs and in order to get possible to allocate the sensors as global variables as you did in your examples (I think that with current library version your implementation is not safe). On your side, you will have just to add a "lsm6dsoxSensor.begin()" in your code after the "Wire.setClock(400000);" in order to be compliant with the new library version. I let you know when the new library version will be available. Best Regards, Carlo

Benhalor commented 3 years ago

Hi @cparata,

Ok, I could add some comments to the example to explain how to use the LSM6DSOX with a nulceo board. Just to be sure : the default I2C pins are D15 / D14 right ?

Improving the sensor interface would be great! The classic arduino way to declare a sensor, is just calling a default construcor "LSM6DSOXSensor lsm6dsoxSensor". Then as you said, the sensor would be started with a "lsm6dsoxSensor.begin(&Wire)" or sm6dsoxSensor.begin(&spi, cs, 2000000)".

pnaybour commented 3 years ago

As requested I have uploaded my existing code. It's not the best in the world. It needs more error checking, but it does work. My next step is to extend it to multiple IMU sensor and multiple fusions and improve the error detection as in @Benhalor version

cparata commented 3 years ago

Hi @cparata,

Ok, I could add some comments to the example to explain how to use the LSM6DSOX with a nulceo board. Just to be sure : the default I2C pins are D15 / D14 right ?

Improving the sensor interface would be great! The classic arduino way to declare a sensor, is just calling a default construcor "LSM6DSOXSensor lsm6dsoxSensor". Then as you said, the sensor would be started with a "lsm6dsoxSensor.begin(&Wire)" or sm6dsoxSensor.begin(&spi, cs, 2000000)".

Yes, indeed. Actually, I prefer to leave the constructors as they are, because I think that the driver APIs must be completely agnostic about the used bus (I2C or SPI). I'm going to update the LSM6DSOX library with the new (begin/end) APIs. If you can test it with your setup and update your PR, it would be great! Then I have to update all MEMS sensors libraries and examples in order to be compliant with the new philosophy. Please, give a look to the new README that I'm going to update for the usage of the library. Thanks in advance and Best Regards, Carlo

cparata commented 3 years ago

Hi @cparata,

Ok, I could add some comments to the example to explain how to use the LSM6DSOX with a nulceo board. Just to be sure : the default I2C pins are D15 / D14 right ?

Improving the sensor interface would be great! The classic arduino way to declare a sensor, is just calling a default construcor "LSM6DSOXSensor lsm6dsoxSensor". Then as you said, the sensor would be started with a "lsm6dsoxSensor.begin(&Wire)" or sm6dsoxSensor.begin(&spi, cs, 2000000)".

Yes D15 and D14 are the Nucleo pins respectively for I2C SCL and I2C SDA. Best Regards, Carlo

cparata commented 3 years ago

I updated the LSM6DSOX library in the master repo adding begin/end APIs. Let me know if it works on your hardware setup. Best Regards, Carlo

Benhalor commented 3 years ago

Hi @cparata

Thanks for adding begin/end interface. The last commit is working successfully on my setup, using the begin method. I updated the example files accordingly.

For the wiring with a Nucleo board : is there an integrated pull-up resistor ?

Best regards,

cparata commented 3 years ago

Hi @Benhalor , If the sensor board does not mount the I2C Pull-Up resistors, you must mount them externally on the Nucleo board. You can take inspiration from the description that I put in the sketch for FlightSense satellite here. Thank you and best regards. Carlo

cparata commented 3 years ago

Hi @Benhalor , I checked the Adafruit LSM6DSOX schematics and it already mounts the I2C Pull-Up resistors, so you don't need to mount them externally on Nucleo boards. Besides, Nucleo boards are powered at 3.3V, so the Vin pin of the Adafruit LSM6DSOX board must be connected to the 3V3 pin of the Nucleo board instead of the 5V pin. Best Regards, Carlo

Benhalor commented 3 years ago

Hi @cparata Thanks for these informations. I have just updated the code. Anything else to modify ? Best regards,

cparata commented 3 years ago

Hi @Benhalor , for the basic example, I think that it is fine as it is. Regarding the FIFO example, I think it would be useful to manage the FIFO full event using the interrupt. In this way, you don't need to poll the FIFO status continuously but only when the interrupt is generated. Also because FIFO feature is useful when you can use the MCU to do other things while the sensor stores data into the FIFO and so interrupt mode is preferred in this case. You can use "Set_FIFO_INT1_FIFO_Full" API, to configure the sensor in order to generate the interrupt on INT1 when the FIFO is full. Obviously, you need also to connect INT1 of the LSM6DSOX board to your board and configure the pin as INPUT and the interrupt as RISING. You can give a look at FreeFall detection example for LSM6DSO (it works at the same way also for LSM6DSOX) here to get inspiration how to configure interrupt. Best Regards, Carlo

alvaro-oliver commented 3 years ago

Hi @Benhalor, thanks for the examples. This put in on the right track, specially with FIFO implementation. Please check your sensor enable line, you're enabling accelerometer twice, so no gyro data is printed:

if (lsm6dsoxSensor.Enable_X() == LSM6DSOX_OK && lsm6dsoxSensor.Enable_X() == LSM6DSOX_OK) {

Regards,

Álvaro

cparata commented 3 years ago

Hi @Benhalor , could you update the PR using only the basic example, so I will merge it in the master repo? We already added the FIFO example, so now the only missing part is the basic example. Thanks in advance and Best Regards, Carlo

cparata commented 3 years ago

I added the Hello World example, so I can close this PR. Thanks for contribution! Best Regards, Carlo