Pololu Minimu-9 library
Revision 0:6ee4ef99c382, committed 2012-02-21
- Comitter:
- bengo
- Date:
- Tue Feb 21 13:34:27 2012 +0000
- Commit message:
- first release
Changed in this revision
diff -r 000000000000 -r 6ee4ef99c382 L3G4200D.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/L3G4200D.cpp Tue Feb 21 13:34:27 2012 +0000 @@ -0,0 +1,133 @@ +/* mbed L3G4200D Library version 0beta1 + * Copyright (c) 2012 bengo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + #include <L3G4200D.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, PinName scl ) : _i2c( sda, scl ){ + + // 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 ); + +} + +// ----------------------------------------------- +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 ); +} + +// ----------------------------------------------- +void L3G4200D::registerWrite( int reg, char data ) { + _bytes[0] = reg & 0xff; + _bytes[1] = data & 0xff; + _status = _i2c.write( GYR_ADDRESS, _bytes, 2 ); +} + +// ----------------------------------------------- +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 ); +} + +// ----------------------------------------------- +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 ); +} + + +// ----------------------------------------------- +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 ); +} \ No newline at end of file
diff -r 000000000000 -r 6ee4ef99c382 L3G4200D.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/L3G4200D.h Tue Feb 21 13:34:27 2012 +0000 @@ -0,0 +1,111 @@ +/* mbed L3G4200D Library version 0beta1 + * Copyright (c) 2012 bengo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef L3G4200D_h +#define L3G4200D_h + +#include "mbed.h" +#include <vector> + +class L3G4200D { + +public: + + /** + * Create an L3G4200D object connected to the specified I2C pins + * @param sda I2C SDA pin + * @param scl I2C SCL pin + */ + L3G4200D( PinName sda, PinName scl ); + + /** + * Return status code of prevoius function call + */ + inline int getStatus( void ) { return( _status ); } + + /** + * Read specified register content + * @param reg register address + */ + int registerRead( int reg ); + + /** + * Write to specified register + * @param reg register address + * @parma data data to be written + */ + void registerWrite( int reg, char data ); + + /** + * read gyroscope vector + */ + std::vector<short> read( void ); + + /** + * Read angular velogity (in degrees per second) + */ + std::vector<float> angularVelocity( void ); + + /** + * Read temperature (in celsius) + */ + int temperature( void ); + + // Device registers addresses + static const int WHO_AM_I; + static const int CTRL_REG1; + static const int CTRL_REG2; + static const int CTRL_REG3; + static const int CTRL_REG4; + static const int CTRL_REG5; + static const int REFERENCE; + static const int OUT_TEMP; + static const int STATUS_REG; + static const int OUT_X_L; + static const int OUT_X_H; + static const int OUT_Y_L; + static const int OUT_Y_H; + static const int OUT_Z_L; + static const int OUT_Z_H; + static const int FIFO_CTRL_REG; + static const int FIFO_SRC_REG; + static const int INT1_CFG; + static const int INT1_SRC; + static const int INT1_THS_XH; + static const int INT1_THS_XL; + static const int INT1_THS_YH; + static const int INT1_THS_YL; + static const int INT1_THS_ZH; + static const int INT1_THS_ZL; + static const int INT1_DURATION; + +private: + + int _status; + I2C _i2c; + char _bytes[7]; + + static const int GYR_ADDRESS; + +}; + +#endif // L3G4200D_h \ No newline at end of file
diff -r 000000000000 -r 6ee4ef99c382 LSM303.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LSM303.cpp Tue Feb 21 13:34:27 2012 +0000 @@ -0,0 +1,209 @@ +/* mbed LSM303 Library version 0beta1 + * Copyright (c) 2012 bengo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include <LSM303.h> +#include <cmath> + +// LSM303 I2C addresses +const int LSM303::ACC_ADDRESS = 0x30; +const int LSM303::MAG_ADDRESS = 0x3c; +// LSM303 register addresses +const int LSM303::ACC_CTRL_REG1 = 0x20; +const int LSM303::ACC_CTRL_REG2 = 0x21; +const int LSM303::ACC_CTRL_REC3 = 0x22; +const int LSM303::ACC_CTRL_REG4 = 0x23; +const int LSM303::ACC_CTRL_REG5 = 0x24; +const int LSM303::ACC_HP_FILTER_RESET = 0x25; +const int LSM303::ACC_REFERENCE = 0x26; +const int LSM303::ACC_STATUS_REG = 0x27; +const int LSM303::ACC_OUT_X_L = 0x28; +const int LSM303::ACC_OUT_X_H = 0x29; +const int LSM303::ACC_OUT_Y_L = 0x2a; +const int LSM303::ACC_OUT_Y_H = 0x2b; +const int LSM303::ACC_OUT_Z_L = 0x2c; +const int LSM303::ACC_OUT_Z_H = 0x2d; +const int LSM303::ACC_INT1_CFG = 0x30; +const int LSM303::ACC_INT1_SOURCE = 0x31; +const int LSM303::ACC_INT1_THS = 0x32; +const int LSM303::ACC_INT1_DURATION = 0x33; +const int LSM303::ACC_INT2_CFG = 0x34; +const int LSM303::ACC_INT2_SOURCE = 0x35; +const int LSM303::ACC_INT2_THS = 0x36; +const int LSM303::ACC_INT2_DURATION = 0x37; +const int LSM303::MAG_CRA_REG = 0x00; +const int LSM303::MAG_CRB_REG = 0x01; +const int LSM303::MAG_MR_REG = 0x02; +const int LSM303::MAG_OUT_X_H = 0x03; +const int LSM303::MAG_OUT_X_L = 0x04; +const int LSM303::MAG_OUT_Y_H = 0x07; +const int LSM303::MAG_OUT_Y_L = 0x08; +const int LSM303::MAG_OUT_Z_H = 0x05; +const int LSM303::MAG_OUT_Z_L = 0x6; +const int LSM303::MAG_SR_REG = 0x9; +const int LSM303::MAG_IRA_REG = 0xa; +const int LSM303::MAG_IRB_REG = 0xb; +const int LSM303::MAG_IRC_REG = 0xc; +const int LSM303::MAG_WHO_AM_I = 0xf; +// + +// ------------------------------------------- +LSM303::LSM303( PinName sda, PinName scl ) : _i2c( sda, scl ) { + // Get SA0 pin status + _bytes[0] = ACC_CTRL_REG1; + _i2c.write( ACC_ADDRESS, _bytes, 1 ); + int sa0low = _i2c.read( ACC_ADDRESS+1, _bytes, 1 ); + _bytes[0] = ACC_CTRL_REG1; + _i2c.write( ACC_ADDRESS+2, _bytes, 1 ); + int sa0hig = _i2c.read( ACC_ADDRESS+2+1, _bytes, 1 ); + if( sa0low == 0 && sa0hig != 0 ) { + _SA0Pad = 0x0; + } + else if( sa0low != 0 && sa0hig == 0 ) { + _SA0Pad = 0x2; + } + else { + _status = 1; + return; + } + // Check that you're talking with an LM303DLM device + _bytes[0] = MAG_WHO_AM_I; + _i2c.write( MAG_ADDRESS, _bytes, 1 ); + _status = _i2c.read( MAG_ADDRESS+1, _bytes, 1 ); + if( _bytes[0] == 0x3c ) { + _status = 0; + } + else { + _status = 1; + return; + } + // Enable normal mode... + // ... On accelerometer + this->accRegisterWrite( ACC_CTRL_REG1, 0x27 ); + if( _status != 0 ) { + return; + } + // ... And on magnetometer + this->magRegisterWrite( MAG_MR_REG, 0x00 ); +} + +// ------------------------------------------- +int LSM303::accRegisterRead( int reg ) { + _bytes[0] = reg & 0xff; + _status = _i2c.write( ACC_ADDRESS + _SA0Pad, _bytes, 1 ); + if( _status == 0 ) { + _status = _i2c.read( ACC_ADDRESS + _SA0Pad + 1, _bytes, 1 ); + return( _bytes[0] ); + } + return( 0 ); +} + +// ------------------------------------------- +void LSM303::accRegisterWrite( int reg, char data ) { + _bytes[0] = reg & 0xff; + _bytes[1] = data & 0xff; + _status = _i2c.write( ACC_ADDRESS + _SA0Pad, _bytes, 2 ); +} + +// ------------------------------------------- +int LSM303::magRegisterRead( int reg ) { + _bytes[0] = reg & 0xff; + _status = _i2c.write( MAG_ADDRESS, _bytes, 1 ); + if( _status == 0 ) { + _status = _i2c.read( MAG_ADDRESS + 1, _bytes, 1 ); + return( _bytes[0] ); + } + return( 0 ); +} + +// ------------------------------------------- +void LSM303::magRegisterWrite( int reg, char data ) { + _bytes[0] = reg & 0xff; + _bytes[1] = data & 0xff; + _status = _i2c.write( MAG_ADDRESS, _bytes, 2 ); +} + + +// ------------------------------------------- +std::vector<short> LSM303::accRead( void ) { + std::vector<short> acc( 3, 0 ); + _bytes[0] = ACC_OUT_X_L | (1<<7); + _status = _i2c.write( ACC_ADDRESS + _SA0Pad, _bytes, 1 ); + if( _status == 0 ) { + _status = _i2c.read( ACC_ADDRESS + _SA0Pad + 1, _bytes, 6 ); + if( _status == 0 ) { + for( int i=0; i<3; i++ ) { + acc[i] = ( short( _bytes[2*i] ) | short(_bytes[2*i+1]) << 8 ); + } + } + } + return( acc ); +} + +// ------------------------------------------- +std::vector<float> LSM303::acceleration( void ) { + + const float cal[3][2] = { { 16291.5, -16245.4 }, { 16819.0, -16253.0 }, { 16994.8, -15525.6 } }; + + std::vector<float> acc( 3, 0 ); + int fs = ( this->accRegisterRead( ACC_CTRL_REG4 ) >> 4 ) & 0x3; + std::vector<short> a = this->accRead(); + if( _status == 0 ) { + for( int i=0; i<3; i++ ) { + acc[i] = acc[i] * ( (cal[i][0] - cal[i][1]) / 32768. ) + (cal[i][0]+cal[i][1])/2.; + acc[i] = float( a[i] ) * pow(2.,(fs+1)) / 32768.; + } + } + return( acc ); +} + +// ------------------------------------------- +std::vector<short> LSM303::magRead( void ) { + std::vector<short> mag( 3, 0 ); + _bytes[0] = MAG_OUT_X_H; + _status = _i2c.write( MAG_ADDRESS, _bytes, 1 ); + if( _status == 0 ) { + _status = _i2c.read( MAG_ADDRESS + 1, _bytes, 6 ); + if( _status == 0 ) { + mag[0] = short( _bytes[0] << 8 ) | short( _bytes[1] ); + mag[1] = short( _bytes[4] << 8 ) | short( _bytes[5] ); + mag[2] = short( _bytes[2] << 8 ) | short( _bytes[3] ); + } + } + return( mag ); +} + +// ------------------------------------------- +std::vector<float> LSM303::magneticField( void ) { + + float gainxy[] = { 1100., 855., 670., 450., 400., 330., 230. }; + float gainz[] = { 980., 760., 600., 400., 355., 295., 205. }; + + std::vector<float> mag( 3, 0 ); + int gn = ( this->magRegisterRead( MAG_CRB_REG ) >> 5 ) & 0x7; + std::vector<short> m = this->magRead(); + if( _status == 0 ) { + mag[0] = float( m[0] ) / gainxy[gn-1]; + mag[1] = float( m[1] ) / gainxy[gn-1]; + mag[2] = float( m[2] ) / gainz[gn-1]; + } + return( mag ); +} \ No newline at end of file
diff -r 000000000000 -r 6ee4ef99c382 LSM303.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LSM303.h Tue Feb 21 13:34:27 2012 +0000 @@ -0,0 +1,141 @@ +/* mbed LSM303 Library version 0beta1 + * Copyright (c) 2012 bengo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef LSM303_h +#define LSM303_h + +#include "mbed.h" +#include <vector> + +class LSM303 { + +public: + + /** + * Create an LSM303 object connected to the specified I2C pins + * @param sda I2C SDA pin + * @param scl I2C SCL pin + */ + LSM303( PinName sda, PinName scl ); + + /** + * Return status code of prevoius function call + */ + inline int getStatus( void ) { return( _status ); } + + /** + * Read specified accelerometer register content + * @param reg register address + */ + int accRegisterRead( int reg ); + + /** + * Write to specified accelerometer register + * @param reg register address + * @parma data data to be written + */ + void accRegisterWrite( int reg, char data ); + + /** + * Read specified magnetometer register content + * @param reg register address + */ + int magRegisterRead( int reg ); + + /** + * Write to specified magnetometer register + * @param reg register address + * @parma data data to be written + */ + void magRegisterWrite( int reg, char data ); + + /** + * Read accelerometer vector + */ + std::vector<short> accRead( void ); + + /** + * Read acceleration + */ + std::vector<float> acceleration( void ); + + /** + * Read magnetometer vector + */ + std::vector<short> magRead( void ); + + /** + * Read magnetic field vector + */ + std::vector<float> magneticField( void ); + + // Device registers addresses + static const int ACC_CTRL_REG1; + static const int ACC_CTRL_REG2; + static const int ACC_CTRL_REC3; + static const int ACC_CTRL_REG4; + static const int ACC_CTRL_REG5; + static const int ACC_HP_FILTER_RESET; + static const int ACC_REFERENCE; + static const int ACC_STATUS_REG; + static const int ACC_OUT_X_L; + static const int ACC_OUT_X_H; + static const int ACC_OUT_Y_L; + static const int ACC_OUT_Y_H; + static const int ACC_OUT_Z_L; + static const int ACC_OUT_Z_H; + static const int ACC_INT1_CFG; + static const int ACC_INT1_SOURCE; + static const int ACC_INT1_THS; + static const int ACC_INT1_DURATION; + static const int ACC_INT2_CFG; + static const int ACC_INT2_SOURCE; + static const int ACC_INT2_THS; + static const int ACC_INT2_DURATION; + static const int MAG_CRA_REG; + static const int MAG_CRB_REG; + static const int MAG_MR_REG; + static const int MAG_OUT_X_H; + static const int MAG_OUT_X_L; + static const int MAG_OUT_Y_H; + static const int MAG_OUT_Y_L; + static const int MAG_OUT_Z_H; + static const int MAG_OUT_Z_L; + static const int MAG_SR_REG; + static const int MAG_IRA_REG; + static const int MAG_IRB_REG; + static const int MAG_IRC_REG; + static const int MAG_WHO_AM_I; + +private: + + int _status; + I2C _i2c; + int _SA0Pad; + char _bytes[7]; + + static const int ACC_ADDRESS; + static const int MAG_ADDRESS; + +}; + +#endif // LSM303_h \ No newline at end of file
diff -r 000000000000 -r 6ee4ef99c382 minimu9.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/minimu9.cpp Tue Feb 21 13:34:27 2012 +0000 @@ -0,0 +1,51 @@ +/* mbed Minimu-9 Library version 0beta1 + * Copyright (c) 2012 bengo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include <minimu9.h> + +// ------------------------------------------- +minimu9::minimu9( PinName sda, PinName scl ) : _i2c(sda, scl), _lsm( sda, scl ), _l3g( sda, scl ) { + _status = _lsm.getStatus() * _l3g.getStatus(); +} + +// ------------------------------------------- +std::vector<short> minimu9::read( void ) { + std::vector<short> data(9,0); + std::vector<short> acc = _lsm.accRead(); + _status = _lsm.getStatus(); + if( _status == 0 ) { + std::vector<short> mag = _lsm.magRead(); + _status = _lsm.getStatus(); + if( _status == 0 ) { + std::vector<short> gyr = _l3g.read(); + _status = _l3g.getStatus(); + if( _status == 0 ) { + for( int i=0; i<3; i++ ) { + data[i ] = acc[i]; + data[i+3] = mag[i]; + data[i+6] = gyr[i]; + } + } + } + } + return( data ); +}
diff -r 000000000000 -r 6ee4ef99c382 minimu9.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/minimu9.h Tue Feb 21 13:34:27 2012 +0000 @@ -0,0 +1,142 @@ +/* mbed Minimu_9 Library version 0beta1 + * Copyright (c) 2012 bengo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef minimu_h +#define minimu9_h + +#include "mbed.h" +#include <LSM303.h> +#include <L3G4200D.h> +#include <vector> + +/** Minimu-9 accelerometer, magnetometer, gyroscope sensor controller library + * + * Example: + * @code + * #include "mbed.h" + * #include <minimu9.h> + * #include <vector> + * + * Serial pc(USBTX, USBRX); // tx, rx + * + * int main() { + * + * minimu9 minimu( p28, p27 ); + * + * int status = minimu.getStatus(); + * if( status == 0 ) { + * for( int i=0; i<100000; i++ ) { + * std::vector<int> data = minimu.read(); + * if( status == 0 ) { + * pc.printf("\x1b[2J\x1b[f\x1b[33mLSM303 minimu read status: \x1b[32m%d\n\r", minimu.getStatus() ); + * } + * else { + * pc.printf("\x1b[2J\x1b[f\x1b[33mLSM303 minimu read status: \x1b[31m%d\n\r", minimu.getStatus() ); + * } + * + * pc.printf("\x1b[33mLSM303 acc: \x1b[37m%8d %8d %8d\n\r", (short)data[0], (short)data[1], (short)data[2] ); + * pc.printf("\x1b[33mLSM303 mag: \x1b[37m%8d %8d %8d\n\r", (short)data[3], (short)data[4], (short)data[5] ); + * pc.printf("\x1b[33mL3G4200D gyr: \x1b[37m%8d %8d %8d\n\r", (short)data[6], (short)data[7], (short)data[8] ); + * wait( 1 ); + * } + * } + * return( 0 ); + * } + * @endcode +*/ + +class minimu9 { + +public: + + /** + * Create a minimu9 object connected to the specified I2C pins + * @param sda I2C SDA pin + * @param scl I2C SCL pin + */ + minimu9( PinName sda, PinName scl ); + + /** + * Return status code of prevoius function call + */ + inline int getStatus( void ) { return( _status ); } + + /** + * Read all three sensors vectors (accelerometer, magnetometer, gyroscope) + */ + std::vector<short> read( void ); + + /** + * Read accelerometer vector + */ + inline std::vector<short> accRead( void ) { return( _lsm.accRead() ); } + + /** + * Read acceleration (in g units) + */ + inline std::vector<float> acceleration( void ) { return( _lsm.acceleration() ); } + + /** + * Read magnetometer vector + */ + inline std::vector<short> magRead( void ) { return( _lsm.magRead() ); } + + /** + * Read magnetic filed (in gauss) + */ + inline std::vector<float> magneticField( void ) { return( _lsm.magneticField() ); } + + /** + * Read gyroscope vector + */ + inline std::vector<short> gyrRead( void ) { return( _l3g.read() ); } + + /** + * Read angular velogity (in degrees per second) + */ + inline std::vector<float> angularVelocity( void ) { return( _l3g.angularVelocity() ); } + + /** + * Read temperature (in celsius) + */ + inline int temperature( void ) { return( _l3g.temperature() ); } + + /** + * Return LSM303 associated object + */ + inline LSM303 getLSM303( void ) { return( _lsm ); } + + /** + * Return L3G4200D associated object + */ + inline L3G4200D getL3G4200D( void ) { return( _l3g ); } + +private: + + int _status; + I2C _i2c; + LSM303 _lsm; + L3G4200D _l3g; + +}; + +#endif // minimu9_h \ No newline at end of file