Nicolas Borla
/
LSM9DS1_Library
IMU library
Revision 2:0e123c149624, committed 2021-03-12
- Comitter:
- boro
- Date:
- Fri Mar 12 13:05:46 2021 +0000
- Parent:
- 1:87d535bf8c53
- Commit message:
- d
Changed in this revision
--- a/LSM9DS1.cpp Mon Oct 26 16:14:04 2015 +0000 +++ b/LSM9DS1.cpp Fri Mar 12 13:05:46 2021 +0000 @@ -19,6 +19,8 @@ local, and you've found our code helpful, please buy us a round! Distributed as-is; no warranty is given. + +Modified: Nicolas Borla, 20.01.2019 ******************************************************************************/ #include "LSM9DS1.h" @@ -36,7 +38,7 @@ #define LSM9DS1_COMMUNICATION_TIMEOUT 1000 float magSensitivity[4] = {0.00014, 0.00029, 0.00043, 0.00058}; -extern Serial pc; +//extern Serial pc; LSM9DS1::LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr, uint8_t mAddr) :i2c(sda, scl) @@ -170,7 +172,7 @@ // each device. Store those in a variable so we can return them. uint8_t mTest = mReadByte(WHO_AM_I_M); // Read the gyro WHO_AM_I uint8_t xgTest = xgReadByte(WHO_AM_I_XG); // Read the accel/mag WHO_AM_I - pc.printf("%x, %x, %x, %x\n\r", mTest, xgTest, _xgAddress, _mAddress); + printf("%x, %x, %x, %x\n\r", mTest, xgTest, _xgAddress, _mAddress); uint16_t whoAmICombined = (xgTest << 8) | mTest; if (whoAmICombined != ((WHO_AM_I_AG_RSP << 8) | WHO_AM_I_M_RSP)) @@ -524,6 +526,9 @@ ay -= aBiasRaw[Y_AXIS]; az -= aBiasRaw[Z_AXIS]; } + accX = static_cast<float>(ax)/32768.0f*2.0f*9.81f; + accY = static_cast<float>(ay)/32768.0f*2.0f*9.81f; + accZ = static_cast<float>(az)/32768.0f*2.0f*9.81f; } int16_t LSM9DS1::readAccel(lsm9ds1_axis axis) @@ -546,6 +551,10 @@ mx = (temp[1] << 8) | temp[0]; // Store x-axis values into mx my = (temp[3] << 8) | temp[2]; // Store y-axis values into my mz = (temp[5] << 8) | temp[4]; // Store z-axis values into mz + + magX = static_cast<float>(mx)/32768.0f*4.0f; + magY = static_cast<float>(my)/32768.0f*4.0f; + magZ = static_cast<float>(mz)/32768.0f*4.0f; } int16_t LSM9DS1::readMag(lsm9ds1_axis axis) @@ -575,6 +584,9 @@ gy -= gBiasRaw[Y_AXIS]; gz -= gBiasRaw[Z_AXIS]; } + gyroX = static_cast<float>(gx)/32768.0f*245.0f*3.14159265358979323846f/180.0f; + gyroY = static_cast<float>(gy)/32768.0f*245.0f*3.14159265358979323846f/180.0f; + gyroZ = static_cast<float>(gz)/32768.0f*245.0f*3.14159265358979323846f/180.0f; } int16_t LSM9DS1::readGyro(lsm9ds1_axis axis)
--- a/LSM9DS1.h Mon Oct 26 16:14:04 2015 +0000 +++ b/LSM9DS1.h Fri Mar 12 13:05:46 2021 +0000 @@ -61,6 +61,10 @@ float gBias[3], aBias[3], mBias[3]; int16_t gBiasRaw[3], aBiasRaw[3], mBiasRaw[3]; + float gyroX, gyroY, gyroZ; // x, y, and z axis readings of the gyroscope (float value) + float accX, accY, accZ; // x, y, and z axis readings of the accelerometer (float value) + float magX, magY, magZ; // x, y, and z axis readings of the magnetometer (float value) + // LSM9DS1 -- LSM9DS1 class constructor // The constructor will set up a handful of private variables, and set the // communication mode as well. @@ -312,7 +316,8 @@ * Any OR'd combination of ZIEN, YIEN, XIEN * - activeLow = Interrupt active configuration * Can be either INT_ACTIVE_HIGH or INT_ACTIVE_LOW - */ - latch: latch gyroscope interrupt request. + * - latch: latch gyroscope interrupt request. + */ void configMagInt(uint8_t generator, h_lactive activeLow, bool latch = true); /** configMagThs() -- Configure the threshold of a gyroscope axis
--- a/main.cpp Mon Oct 26 16:14:04 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -#include "LSM9DS1.h" - -DigitalOut myled(LED1); -Serial pc(USBTX, USBRX); - -int main() { - //LSM9DS1 lol(p9, p10, 0x6B, 0x1E); - LSM9DS1 lol(p9, p10, 0xD6, 0x3C); - lol.begin(); - if (!lol.begin()) { - pc.printf("Failed to communicate with LSM9DS1.\n"); - } - lol.calibrate(); - while(1) { - lol.readTemp(); - lol.readMag(); - lol.readGyro(); - - //pc.printf("%d %d %d %d %d %d %d %d %d\n\r", lol.calcGyro(lol.gx), lol.calcGyro(lol.gy), lol.calcGyro(lol.gz), lol.ax, lol.ay, lol.az, lol.mx, lol.my, lol.mz); - //pc.printf("%d %d %d\n\r", lol.calcGyro(lol.gx), lol.calcGyro(lol.gy), lol.calcGyro(lol.gz)); - pc.printf("gyro: %d %d %d\n\r", lol.gx, lol.gy, lol.gz); - pc.printf("accel: %d %d %d\n\r", lol.ax, lol.ay, lol.az); - pc.printf("mag: %d %d %d\n\n\r", lol.mx, lol.my, lol.mz); - myled = 1; - wait(2); - myled = 0; - wait(2); - } -}