Now you can download a copy of these docs so you can use them offline! Download now
ADXL345_I2C.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "ADXL345_I2C.h" 00008 #include "DigitalModule.h" 00009 #include "I2C.h" 00010 00011 const UINT8 ADXL345_I2C::kAddress; 00012 const UINT8 ADXL345_I2C::kPowerCtlRegister; 00013 const UINT8 ADXL345_I2C::kDataFormatRegister; 00014 const UINT8 ADXL345_I2C::kDataRegister; 00015 const double ADXL345_I2C::kGsPerLSB; 00016 00023 ADXL345_I2C::ADXL345_I2C(UINT8 moduleNumber, ADXL345_I2C::DataFormat_Range range) 00024 : m_i2c (NULL) 00025 { 00026 DigitalModule *module = DigitalModule::GetInstance(moduleNumber); 00027 if (module) 00028 { 00029 m_i2c = module->GetI2C(kAddress); 00030 00031 // Turn on the measurements 00032 m_i2c->Write(kPowerCtlRegister, kPowerCtl_Measure); 00033 // Specify the data format to read 00034 m_i2c->Write(kDataFormatRegister, kDataFormat_FullRes | (UINT8)range); 00035 } 00036 } 00037 00041 ADXL345_I2C::~ADXL345_I2C() 00042 { 00043 delete m_i2c; 00044 m_i2c = NULL; 00045 } 00046 00053 double ADXL345_I2C::GetAcceleration(ADXL345_I2C::Axes axis) 00054 { 00055 INT16 rawAccel = 0; 00056 if(m_i2c) 00057 { 00058 m_i2c->Read(kDataRegister + (UINT8)axis, sizeof(rawAccel), (UINT8 *)&rawAccel); 00059 00060 // Sensor is little endian... swap bytes 00061 rawAccel = ((rawAccel >> 8) & 0xFF) | (rawAccel << 8); 00062 } 00063 return rawAccel * kGsPerLSB; 00064 } 00065 00071 ADXL345_I2C::AllAxes ADXL345_I2C::GetAccelerations() 00072 { 00073 AllAxes data = {0.0}; 00074 INT16 rawData[3]; 00075 if (m_i2c) 00076 { 00077 m_i2c->Read(kDataRegister, sizeof(rawData), (UINT8*)rawData); 00078 00079 // Sensor is little endian... swap bytes 00080 for (INT32 i=0; i<3; i++) 00081 { 00082 rawData[i] = ((rawData[i] >> 8) & 0xFF) | (rawData[i] << 8); 00083 } 00084 00085 data.XAxis = rawData[0] * kGsPerLSB; 00086 data.YAxis = rawData[1] * kGsPerLSB; 00087 data.ZAxis = rawData[2] * kGsPerLSB; 00088 } 00089 return data; 00090 } 00091
Generated on Thu Jan 12 2012 22:35:17 for WPILibC++ by
1.7.1