AHRS library for the Polulu minIMU-9 Ability to interface with the Polulu Python minIMU-9 monitor
Diff: L3G4200D.cpp
- Revision:
- 1:3272ece36ce1
- Parent:
- 0:dc35364e2291
--- a/L3G4200D.cpp Thu Apr 12 13:47:23 2012 +0000 +++ b/L3G4200D.cpp Mon Apr 23 14:31:08 2012 +0000 @@ -1,5 +1,5 @@ /* mbed L3G4200D Library version 0.1 - * Copyright (c) 2012 Prediluted + * Copyright (c) 2012 Ported to MBED and modified by Prediluted (Kris Reynolds) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,116 +20,52 @@ * THE SOFTWARE. */ #include <L3G4200D.h> +#include <math.h> +#include "mbed.h" -// L3G4200D I2C address -const int L3G4200D::GYR_ADDRESS = 0xd2; -// L3G4200D register addresses -const int L3G4200D::WHO_AM_I = 0x0f; -const int L3G4200D::CTRL_REG1 = 0x20; -const int L3G4200D::CTRL_REG2 = 0x21; -const int L3G4200D::CTRL_REG3 = 0x22; -const int L3G4200D::CTRL_REG4 = 0x23; -const int L3G4200D::CTRL_REG5 = 0x24; -const int L3G4200D::REFERENCE = 0x25; -const int L3G4200D::OUT_TEMP = 0x26; -const int L3G4200D::STATUS_REG = 0x27; -const int L3G4200D::OUT_X_L = 0x28; -const int L3G4200D::OUT_X_H = 0x29; -const int L3G4200D::OUT_Y_L = 0x2a; -const int L3G4200D::OUT_Y_H = 0x2b; -const int L3G4200D::OUT_Z_L = 0x2c; -const int L3G4200D::OUT_Z_H = 0x2d; -const int L3G4200D::FIFO_CTRL_REG = 0x2e; -const int L3G4200D::FIFO_SRC_REG = 0x2f; -const int L3G4200D::INT1_CFG = 0x30; -const int L3G4200D::INT1_SRC = 0x31; -const int L3G4200D::INT1_THS_XH = 0x32; -const int L3G4200D::INT1_THS_XL = 0x33; -const int L3G4200D::INT1_THS_YH = 0x34; -const int L3G4200D::INT1_THS_YL = 0x35; -const int L3G4200D::INT1_THS_ZH = 0x36; -const int L3G4200D::INT1_THS_ZL = 0x37; -const int L3G4200D::INT1_DURATION = 0x38; + +L3G4200D::L3G4200D( PinName sda = p9, PinName scl = p10 ) : i2c( sda, scl ) { + writeReg( L3G4200D_CTRL_REG1, 0x0f ); +} -// ----------------------------------------------- -L3G4200D::L3G4200D( PinName sda = p9, PinName scl = p10 ) : _i2c( sda, scl ) { +void L3G4200D::writeReg( byte reg, byte value ) { + char data[2] = { reg, value }; + i2c.write( GYR_ADDRESS, data, 2 ); +} - // Check that you're talking with an L3G4200D device - if ( this->registerRead( WHO_AM_I ) == 0xd3 ) { - _status = 0; - } else { - _status = 1; - return; - } - - // Enable normal mode... - this->registerWrite( CTRL_REG1, 0x0f ); - +char L3G4200D::readReg( byte reg ) { + char value[1]; + char data[1] = { reg }; + i2c.write( GYR_ADDRESS, data, 1 ); + i2c.read( GYR_ADDRESS, value, 1 ); + return value[0]; } -L3G4200D::L3G4200D( ) : _i2c( p9, p10 ) {}; +void L3G4200D::read( void ) { + char data[1] = { L3G4200D_OUT_X_L | (1<<7) }; + char output[6]; + i2c.write( GYR_ADDRESS, data, 1 ); -// ----------------------------------------------- -int L3G4200D::registerRead( int reg ) { - _bytes[0] = reg & 0xff; - _status = _i2c.write( GYR_ADDRESS, _bytes, 1 ); - if ( _status == 0 ) { - _status = _i2c.read( GYR_ADDRESS + 1, _bytes, 1 ); - return( _bytes[0] ); - } - return( 0 ); -} + i2c.read( GYR_ADDRESS, output, 6 ); -// ----------------------------------------------- -void L3G4200D::registerWrite( int reg, char data ) { - _bytes[0] = reg & 0xff; - _bytes[1] = data & 0xff; - _status = _i2c.write( GYR_ADDRESS, _bytes, 2 ); + g.x = short( output[1] << 8 | output[0] ); + g.y = short( output[3] << 8 | output[2] ); + g.z = short( output[5] << 8 | output[4] ); } -// ----------------------------------------------- -std::vector<short> L3G4200D::read( void ) { - std::vector<short> gyr( 3, 0 ); - _bytes[0] = OUT_X_L | (1<<7); - _status = _i2c.write( GYR_ADDRESS, _bytes, 1 ); - if ( _status == 0 ) { - _status = _i2c.read( GYR_ADDRESS + 1, _bytes, 6 ); - if ( _status == 0 ) { - for ( int i=0; i<3; i++ ) { - gyr[i] = short( _bytes[2*i] | ( _bytes[2*i+1] << 8 )); - } - } - } - - return( gyr ); +void L3G4200D::vector_cross(const Plane *a,const Plane *b, Plane *out) { + out->x = a->y*b->z - a->z*b->y; + out->y = a->z*b->x - a->x*b->z; + out->z = a->x*b->y - a->y*b->x; } -// ----------------------------------------------- -std::vector<float> L3G4200D::angularVelocity( void ) { - - std::vector<float> angv(3, 0); - const float fs[] = { 250., 500., 2000., 2000. }; - float fullscale = fs[ ( this->registerRead( L3G4200D::CTRL_REG4 ) >> 4 ) & 0x3 ]; - std::vector<short> g = this->read(); - if ( _status == 0 ) { - for ( int i=0; i<3; i++ ) { - angv[i] = float( g[i] ) / 32768. * fullscale; - } - } - return( angv ); +float L3G4200D::vector_dot(const Plane *a,const Plane *b) { + return a->x*b->x+a->y*b->y+a->z*b->z; } - -// ----------------------------------------------- -int L3G4200D::temperature( void ) { - - _bytes[0] = OUT_TEMP; - _status = _i2c.write( GYR_ADDRESS, _bytes, 1 ); - if ( _status == 0 ) { - _status = _i2c.read( GYR_ADDRESS + 1, _bytes, 1 ); - if ( _status == 0 ) { - return( int( _bytes[0] ) ); - } - } - return( 0 ); +void L3G4200D::vector_normalize(Plane *a) { + float mag = sqrt(vector_dot(a,a)); + a->x /= mag; + a->y /= mag; + a->z /= mag; } \ No newline at end of file