stm32duino / LSM6DSR

Arduino library to support the LSM6DSR 3D accelerometer and 3D gyroscope
7 stars 7 forks source link

Library compatibility with SAMD architecture #2

Closed TinManAkshay closed 3 years ago

TinManAkshay commented 3 years ago

Hi,

We are going to have a custom board (SAMD51 MCU) which LSM6 module on it and I plan to use this Arduino based library having the development platform to be Arduino IDE. I am thinking to use the same example on the repository but little modified to extract the Accelerometer values. I am getting this warning """"WARNING: library LSM6DSR-master claims to run on stm32, avr, sam architecture(s) and may be incompatible with your current board which runs on samd architecture(s).""""

Is it true that it is not compatible with SAMD arch. In that case, do i have modified something in the libraries? Can anyone please suggest since its critical and deadline is getting closer for our project.

Thanks, Akshay

fpistm commented 3 years ago

Hi @TinManAkshay As far as I can tell, I think it could be compatible as the library seems only used default Arduino API.

@cparata any feedback on this ?

cparata commented 3 years ago

Hi @TinManAkshay , it should be compatible, because the library uses only standard Arduino APIs. Unfortunately, I do not have a device based on SAMD platform to test the library with it. So, I suggest to test the library with your platform and if it is fine, we can update the library.properties file in order to support also samd platforms and remove your warning. Let me know, how it works on your side. Best Regards, Carlo

TinManAkshay commented 3 years ago

Hi @cparata & @fpistm , Alright. Once we have the boards in the house, I will test the libraries and will let you know and if its working smoothly.

Also, we are not using any of the Interrupt pins (INT1/2) of the LSM6 module. I hope libraries will still work on getting the Accelerometer values right?

Thanks -Akshay

cparata commented 3 years ago

Hi @TinManAkshay , yes, the driver should work also if you do not use the interrupt pins. The only thing, if you are connecting the component to the I2C bus, I suggest to mount a Pull Down resistor on INT1 pin in order to disable the I3C support in the component that could interfere with the I2C protocol. If you are connecting the sensor to the SPI bus instead you do not need this patch. Best Regards, Carlo

TinManAkshay commented 3 years ago

@cparata ,

Yes we have I2C connection with the module. But I have a concern that we don't have interrupt connection to the MCU, so how would you suggest in that case?

-Akshay

cparata commented 3 years ago

Hi @TinManAkshay , As I said before, it is not an issue if you do not connect the interrupt pins to the MCU. I would just add only a Pull-Down resistor on INT1 pin of the sensor in order to disable the I3C support on the sensor as you can see in the Datasheet of the component. Best Regards, Carlo

TinManAkshay commented 3 years ago

Alright!! Thanks a lot for the info. Appreciate it.

TinManAkshay commented 3 years ago

Hi,

I have got this https://www.digikey.com/catalog/en/partgroup/lsm6dsr-adapter-board/89058 LSM6 dev board to test your library. So far I have been testing this sensor with SAMD51 micro-controller and I have kept INT_1 of the sensor open since our custom board also doesnt have anything connected to this pin of the sensor, not even pull-down resistor.

This is the result I am getting (attached)

image

TinManAkshay commented 3 years ago

Hi @cparata @fpistm ,

I am only getting values at accelerometer[0] which are varying rapidly but [1] and [2] are '0'. I dont know why is this showing behavior like. I should receive at those Y and Z axis. Any suggestion could be great help?

Thanks, Akshay

cparata commented 3 years ago

Hi @TinManAkshay , could you share the sketch that you are using? Best Regards, Carlo

cparata commented 3 years ago

Anyway, the output that you showed is a bit strange if the board is still. If the component is still on the table (without any inclination), I expect 2 components with a value close to 0 (the range between -50 to +50 should be fine) and the other component with a value close to +/-1000 (the range between +/-950 to +/-1050 should be fine).

cparata commented 3 years ago

I also suggest to connect INT1 of the component to one pin of SAMD51 (let's say D4 of the Arduino header) and add at the beginning of your sketch in the setup function:

pinMode(D4, OUTPUT); digitalWrite(D4, 0);

In this way, we are sure that the I3C interface of the component is disabled. Could you make also this test?

TinManAkshay commented 3 years ago

Hi @cparata ,

I have tried connecting INT_1 to A5 as mentioned in the example as well as to D5 (since D4 is not there). But still behavior is still same. I am getting first value and '0' other values. I am moving the accelerometer breakout board, but yeah same behavior.

I am using this sketch:

#include <Wire.h>
#include <LSM6DSRSensor.h>
#include <lsm6dsr_reg.h>

#define INT_1 A5

// Components
LSM6DSRSensor *AccGyr;
uint16_t windowLen = 25;

void setup() {

//  pinMode(5, OUTPUT);
//  digitalWrite(5, 0);
  pinMode(LED_BUILTIN, OUTPUT);
  // Force INT1 of LSM6DSR low in order to enable I2C
//  pinMode(INT_1, OUTPUT);

//  digitalWrite(INT_1, LOW);

  delay(200);

  // Initialize serial for output.
  Serial.begin(115200);

  // Initialize I2C bus.
  Wire.begin();

  AccGyr = new LSM6DSRSensor (&Wire, LSM6DSR_I2C_ADD_L);
  AccGyr->Enable_X();

}

void loop() {

  // Read accelerometer and gyroscope.
  int32_t accelerometer[3];
  uint32_t tstart;

  AccGyr->Get_X_Axes(accelerometer);

  digitalWrite(LED_BUILTIN, HIGH);
  // Output data.
  //Serial.println("LSM6DSR: | Acc[mg]: ");
  Serial.println((float)(accelerometer[0]/1000));
  Serial.println((float)(accelerometer[1]/1000));
  Serial.println((float)(accelerometer[2]/1000));

  Serial.println("****************");

  delay(2);
 /* Serial.print(pitch2);
  Serial.print(",");
  Serial.print(roll2);
*/
  digitalWrite(LED_BUILTIN, LOW);

}
TinManAkshay commented 3 years ago

Sometimes i get 2nd value as a steady value, as in, no change in the value at all after moving the accelerometer. See down below the result:

image

cparata commented 3 years ago

Hi @TinManAkshay , how did you connect SA0 pin? To GND or to Vdd? I guess to GND if you are using LSM6DSR_I2C_ADD_L. Can you try to print the integer values of the Axis without the float casting?

Serial.println(accelerometer[0]); Serial.println(accelerometer[1]); Serial.println(accelerometer[2]);

cparata commented 3 years ago

Can you also specify the wire connections between the adapter and the SAMD51 board just to clarify your hardware setup?

TinManAkshay commented 3 years ago

@cparata ,

This is the schematic i am following to connect to the MCU and you will see INT_1 is kept open, so does in our custom board.

image

This is the result you have asked w/0 float casting. I noticed one thing in serial monitor is that 2nd value is incrementing but 3rd value (244) is steady.

image

cparata commented 3 years ago

SA0 is connected to VDD, so you must use LSM6DSR_I2C_ADD_H instead of LSM6DSR_I2C_ADD_L. Try to create the instance of the sensor in this way:

AccGyr = new LSM6DSRSensor (&Wire, LSM6DSR_I2C_ADD_H);

Best Regards, Carlo

TinManAkshay commented 3 years ago

I did the same actually. But the same output.

cparata commented 3 years ago

Could you try this sketch after connecting INT1 to A5?

#include <Wire.h>
#include <LSM6DSRSensor.h>

#define INT_1 A5

// Components
LSM6DSRSensor *AccGyr;

void setup() {

  pinMode(A5, OUTPUT);
  digitalWrite(A5, LOW);

  delay(200);

  // Initialize serial for output.
  Serial.begin(115200);

  // Initialize I2C bus.
  Wire.begin();

  AccGyr = new LSM6DSRSensor (&Wire, LSM6DSR_I2C_ADD_H);
  AccGyr->Enable_X();
}

void loop() {

  // Read accelerometer and gyroscope.
  int32_t accelerometer[3];

  AccGyr->Get_X_Axes(accelerometer);

  // Output data.
  Serial.println(accelerometer[0]);
  Serial.println(accelerometer[1]);
  Serial.println(accelerometer[2]);
  Serial.println("****************");

  delay(2);
}
fpistm commented 3 years ago

Do you have pull up on I2C lines ?

TinManAkshay commented 3 years ago

@cparata @fpistm ,

I tried connecting INT_1 to A5 and put it to low. Also, there are pull-up resistors connected to I2C lines. But output is still same. This is critical for me to get the accurate results for our project since deadline is very near. I dont know what has been going wrong here.

cparata commented 3 years ago

Hi @TinManAkshay , unfortunately, I do not know what could be the issue. I'm sure that this library works with STM32 because I tested it with a Nucleo board. Could you also share the schematics and Data Sheet of the SAMD MCU? Best Regards, Carlo

TinManAkshay commented 3 years ago

@cparata ,

I am using this dev board to connect to the LSM6

https://www.adafruit.com/product/3857?gclid=CjwKCAiAv4n9BRA9EiwA30WNDxEE0tUVJvLwKvxD4ZTIkDXRwtAhT99AqwqPrq-KR4IZ75vjzSROxhoC3hsQAvD_BwE

fpistm commented 3 years ago

All info here: https://learn.adafruit.com/adafruit-feather-m4-express-atsamd51/downloads

cparata commented 3 years ago

Hi @TinManAkshay , could you also check that your wire connections are like in this file: https://www.st.com/content/ccc/resource/technical/layouts_and_diagrams/schematic_pack/group1/98/bb/ee/ca/4d/4d/4c/da/STEVAL-MKI194V1_SCHEMATIC/files/steval-mki194v1_schematic.pdf/jcr:content/translations/en.steval-mki194v1_schematic.pdf

So: Pin1 -> Vdd Pin2 -> Vdd Pin4 -> GND Pin5 -> GND Pin22 -> Vdd Pin21 -> SDA of SAMD Pin20 -> SCL of SAMD Pin19 -> Vdd Pin14 -> A5 of SAMD putting it LOW Pin13 -> GND

All other pins can stand unconnected.

Best Regards, Carlo

TinManAkshay commented 3 years ago

@cparata,

Every pin's connection is up to the mark. Still same behavior. I am getting only 1st value which is changing as I move the board. 2nd value is incrementing and 3rd value is just 244.

cparata commented 3 years ago

Hi @TinManAkshay , could you compile my sketch and send me a snapshot of the output in the time? I would like to understand better the values that you are seeing. Best Regards, Carlo

cparata commented 3 years ago

For example you can find below the output that I can see with my sketch using the LSM6DSR and a Nucleo-L476RG. Unfortunately, I do not have a SAMD board to test the component also with it.

Log

TinManAkshay commented 3 years ago

Hi @cparata ,

I have two MCUs ..SAMD21 and SAMD51

1 . With SAMD21, the snapshot of the output is below: Screenshot (26)

2 With SAMD51, the snapshot of the output is below: Screenshot (25)

cparata commented 3 years ago

Hi @TinManAkshay , do you have a Nucleo board to test the behavior of the LSM6DSR with it. In this way, if you get the same results that I see, we can exclude that the LSM6DSR board is broken. I would like also to look at your hardware setup. Could you provide a picture of that? Finally what are the values of the Pull-Up resistors that you are using on the I2C lines? Best Regards, Carlo

TinManAkshay commented 3 years ago

@cparata ,

No I dont have that nucleo board. I am sending you the picture of my HW setup and I have 4.7k ohms pull-ups IMG-0158 IMG-0159 IMG-0160 IMG-0161

cparata commented 3 years ago

Hi @TinManAkshay , It seems to me that you connected the I2C resistors between the pins of the MCU and the ones of the LSM6DSR. In this way they are not pulled-up. Try to connect one of the pin of the resistors (for both resistors SDA and SCL) to 3.3V and give a try to this configuration. Obviously you need also to link directly the I2C pins of the MCU to the respective ones of LSM6DSR. Best Regards, Carlo

TinManAkshay commented 3 years ago

@cparata ,

Oh my goodness, now its working. I can notice the accurate change in the movement of the board in +- X, Y and Z axisi. I made very silly mistake and you were right that pull-ups were not pulled-up correctly. These libraries are working. What's the unit of the values? Are they in "mg's"? I'd need float values in g's for my further calculation in my project's firmware.

You can now remove the warning about the incompatibility with SAMD architecture.

Thanks a lot for your help. Appreciate it!!

cparata commented 3 years ago

Hi @TinManAkshay , Great! Yes, the values are in mg. In order to convert them in float values in g, you can apply a cast like this:

(float)accelerometer[0]/1000.0f ...

Or even better in order to have more accuracy, you can call the Get_X_AxesRaw and Get_X_Sensitivity doing something like that:

int16_t acc_raw[3]; float acc_in_g[3]; float acc_sensitivity;

AccGyr->Get_X_AxesRaw(acc_raw); AccGyr->Get_X_Sensitivity(&acc_sensitivity); acc_in_g[0]=((float)acc_raw[0]acc_sensitivity)/1000.0f; acc_in_g[1]=((float)acc_raw[1]acc_sensitivity)/1000.0f; acc_in_g[2]=((float)acc_raw[2]*acc_sensitivity)/1000.0f;

I will update the library properties to support also samd arch. Best Regards, Carlo

TinManAkshay commented 3 years ago

Got it!. In order to enhance the performance, what if I increase the sample rate as well as +-2g to +-8g?

cparata commented 3 years ago

You can use whatever full scale and output data rate you want according your application requirements. In order to change the full scale to 8g, you can call in the setup:

AccGyr->Set_X_FS(8);

In order to change the Output Data Rate (you can set it up to 6667Hz) you can call in the setup:

AccGyr->Set_X_ODR(6667.0f);

Best Regards, Carlo

cparata commented 3 years ago

Hi @TinManAkshay , I updated the library adding the samd architecture. Now, you should not see any warning during compilation. Best Regards, Carlo

fpistm commented 3 years ago

Hi @TinManAkshay I would just point that if you had shared you wiring setup as requested by @cparata 2days ago this would save us lot of time.

More information you provide better and faster is the support.

TinManAkshay commented 3 years ago

@cparata - Thanks a lot again!

@fpistm - Yeah I totally understand. I am really sorry for making a silly mistake. I really appreciate all the support. Thanks again!