Skip to content
LeafTeaNeko edited this page Mar 12, 2020 · 4 revisions

Hardware: Sparkfun LSM9DS1

Library: https://github.com/akimach/LSM9DS1_RaspberryPi_Library/blob/master/example/LSM9DS1_Basic_I2C.py

The linked library is a python-wrapper for the LSM9DS1 C library available at the ST Microelectronics website.

OVERVIEW:

The LSM9DS1 is a versatile, motion-sensing system-in-a-chip. It houses a 3-axis accelerometer, 3-axis gyroscope, and 3-axis magnetometer – nine degrees of freedom (9DOF) in a single IC. The LSM9DS1 measures each of these movement properties in three dimensions. That means it produces nine pieces of data: acceleration in x/y/z, angular rotation in x/y/z, and magnetic force in x/y/z. The LSM9DS1 Breakout has labels indicating the accelerometer and gyroscope axis orientations, which share a right-hand rule relationship with each other. Each sensor in the LSM9DS1 supports a wide spectrum of ranges: the accelerometer’s scale can be set to ± 2, 4, 8, or 16 g, the gyroscope supports ± 245, 500, and 2000 °/s, and the magnetometer has full-scale ranges of ± 4, 8, 12, or 16 gauss. Please refer to the product page for more details.

Product page: https://www.sparkfun.com/products/13284

WIRING:

The sensor has 13 pins in total. Connect the GND to ground of the Pi, and VDD to Pin1 of the Pi for 3.3V operating voltage. PIN3 and PIN5 on Raspberry Pi-zero (or Raspberry Pi-3B+) with PIN3 connected to SDA and PIN5 for SCL. The pins CSM, CSAG, SDO M, SDO AG are all connected to the 3.3V line (PIN1 on Raspi), this allows the sensor to communicate over I2C channels. The pins DEN, INT2, INT1, RDY can be left unconnected, they are for using the sensor in SPI communication channels.

MECHANISM:

The sensor notes the changes in magnetic fields due to rotation and uses the data to output acceleration, orientation, angular velocity and magnetometer values.

The IMPORTING THE IMU part of the code imports all the C libraries used by the sensors into a Python wrapper. This imports an object file created by the compiler to be used as an intermediary for Python wrapping. First the IMU object is created using the imu = lib.lsm9ds1_create() line, this allows us to use the functions available for the sensor to be used in parsing data and outputs. lib.lsm9ds1_calibrate(imu) is used to internally calibrate the sensor every time it has been initiated. Other calibration is required (refer to comments in the code). While lib.lsm9ds1_gyroAvailable(imu) == 0, lib.lsm9ds1_accelAvailable(imu) == 0, lib.lsm9ds1_magAvailable(imu) == 0 lines check for the availability of readings from gyroscope, accelerometer and magnetometer. lib.lsm9ds1_readGyro(imu), lib.lsm9ds1_readAccel(imu), lib.lsm9ds1_readMag(imu) read the output from the sensors on IMU. These readings are stored in the variables gx,gy,gz for gyroscope, ax,ay,az for the accelerometer and mx,my,mz for the magnetometer. These readings are parsed through the complimentary filter to obtain our results in rotation and angular velocity.

FILTERS:

The data collected through the IMU sensor can be passed through the complimentary filter designed for the system. The data from the IMU is highly noisy and such using the complimentary filter is a necessity for robust operation.

HOW-TO-USE:

STEP 1: Mount the sensor on platform (Breadboard or PCB)

STEP 2: Connect the 3.3V line to the VDD of the sensor Pin Connect the GND line to GND of the sensor Connect SDA line to PIN 3 of the Pi(Refer to your Raspi pinout diagram) Connect SCL line to the PIN5 of the Pi(Refer to your Raspi pinout diagram) Connect CSM, CSAG, SDO M, SDO AG to the 3.3V line for I2C Rest of the connections DEN, INT2, INT1, RDY are not necessary when using I2C connection.

STEP 3: Download the library to use the sensor, check library link for instructions and examples. Library: https://github.com/akimach/LSM9DS1_RaspberryPi_Library/blob/master/example/LSM9DS1_Basic_I2C.py

STEP 4: Make a new file/script in python, copy all the code from IMU function and call the function.

STEP 5: Run the code in console and check if there is any output.

STEP 6: The output in the console should have 9 values with the first row of values for gyration, the second row for angular acceleration and the third row for magnetometer.

STEP 7: Pass the output of the IMU through the complementary filter for more accurate measurements. The Complementary filter takes the Gyration values as GyroData, acceleration values as XLData and Magnetometer data as MagData. It also takes Magnetometer offset values as MagOffset. Refer to in-code comments on the working of complementary filter and mathematical model.

Clone this wiki locally