Exploring BeagleBone  V1.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions
exploringBB::ADXL345 Class Reference

Specific class for the ADXL345 Accelerometer. More...

#include <ADXL345.h>

Public Types

enum  RANGE { PLUSMINUS_2_G = 0, PLUSMINUS_4_G = 1, PLUSMINUS_8_G = 2, PLUSMINUS_16_G = 3 }
 An enumeration to define the gravity range of the sensor. More...
 
enum  RESOLUTION { NORMAL = 0, HIGH = 1 }
 The resolution of the sensor. High is only available in +/- 16g range. More...
 

Public Member Functions

 ADXL345 (BusDevice *busDevice)
 
virtual int readSensorState ()
 
virtual void setRange (ADXL345::RANGE range)
 
virtual ADXL345::RANGE getRange ()
 
virtual void setResolution (ADXL345::RESOLUTION resolution)
 
virtual ADXL345::RESOLUTION getResolution ()
 
virtual short getAccelerationX ()
 
virtual short getAccelerationY ()
 
virtual short getAccelerationZ ()
 
virtual float getPitch ()
 
virtual float getRoll ()
 
virtual void displayPitchAndRoll (int iterations=600)
 
virtual ~ADXL345 ()
 

Detailed Description

Specific class for the ADXL345 Accelerometer.

Member Enumeration Documentation

An enumeration to define the gravity range of the sensor.

Enumerator
PLUSMINUS_2_G 

plus/minus 2g

PLUSMINUS_4_G 

plus/minus 4g

PLUSMINUS_8_G 

plus/minus 8g

PLUSMINUS_16_G 

plus/minus 16g

43  {
44  PLUSMINUS_2_G = 0,
45  PLUSMINUS_4_G = 1,
46  PLUSMINUS_8_G = 2,
47  PLUSMINUS_16_G = 3
48  };
plus/minus 16g
Definition: ADXL345.h:47
plus/minus 2g
Definition: ADXL345.h:44
plus/minus 4g
Definition: ADXL345.h:45
plus/minus 8g
Definition: ADXL345.h:46

The resolution of the sensor. High is only available in +/- 16g range.

Enumerator
NORMAL 

NORMAL 10-bit resolution.

HIGH 

HIGH 13-bit resolution.

50  {
51  NORMAL = 0,
52  HIGH = 1
53  };
NORMAL 10-bit resolution.
Definition: ADXL345.h:51
HIGH 13-bit resolution.
Definition: ADXL345.h:52

Constructor & Destructor Documentation

exploringBB::ADXL345::ADXL345 ( BusDevice busDevice)

The constructor for the ADXL345 accelerometer object. It passes the bus number and the device address (with is 0x53 by default) to the constructor of I2CDevice. All of the states are initialized and the registers are updated.

Parameters
I2CBusThe bus number that the ADXL345 device is on - typically 0 or 1
I2CAddressThe address of the ADLX345 device (default 0x53, but can be altered)
127  {
128  //this->I2CAddress = I2CAddress;
129  //this->I2CBus = I2CBus;
130  this->device = busDevice;
131  this->accelerationX = 0;
132  this->accelerationY = 0;
133  this->accelerationZ = 0;
134  this->pitch = 0.0f;
135  this->roll = 0.0f;
136  this->registers = NULL;
137  this->range = ADXL345::PLUSMINUS_16_G;
138  this->resolution = ADXL345::HIGH;
139  this->device->writeRegister(POWER_CTL, 0x08);
140  this->updateRegisters();
141 }
plus/minus 16g
Definition: ADXL345.h:47
#define POWER_CTL
Definition: ADXL345.cpp:53
HIGH 13-bit resolution.
Definition: ADXL345.h:52
virtual int writeRegister(unsigned int registerAddress, unsigned char value)=0
exploringBB::ADXL345::~ADXL345 ( )
virtual
196 {}

Member Function Documentation

void exploringBB::ADXL345::displayPitchAndRoll ( int  iterations = 600)
virtual

Useful debug method to display the pitch and roll values in degrees on a single standard output line

Parameters
iterationsThe number of 0.1s iterations to take place.
186  {
187  int count = 0;
188  while(count < iterations){
189  cout << "Pitch:"<< this->getPitch() << " Roll:" << this->getRoll() << " \r"<<flush;
190  usleep(100000);
191  this->readSensorState();
192  count++;
193  }
194 }
virtual float getPitch()
Definition: ADXL345.h:78
virtual int readSensorState()
Definition: ADXL345.cpp:149
virtual float getRoll()
Definition: ADXL345.h:79
virtual short exploringBB::ADXL345::getAccelerationX ( )
inlinevirtual
75 { return accelerationX; }
virtual short exploringBB::ADXL345::getAccelerationY ( )
inlinevirtual
76 { return accelerationY; }
virtual short exploringBB::ADXL345::getAccelerationZ ( )
inlinevirtual
77 { return accelerationZ; }
virtual float exploringBB::ADXL345::getPitch ( )
inlinevirtual
78 { return pitch; }
virtual ADXL345::RANGE exploringBB::ADXL345::getRange ( )
inlinevirtual
71 { return this->range; }
virtual ADXL345::RESOLUTION exploringBB::ADXL345::getResolution ( )
inlinevirtual
73 { return this->resolution; }
virtual float exploringBB::ADXL345::getRoll ( )
inlinevirtual
79 { return roll; }
int exploringBB::ADXL345::readSensorState ( )
virtual

Read the sensor state. This method checks that the device is being correctly read by using the device ID of the ADXL345 sensor. It will read in the accelerometer registers and pass them to the combineRegisters() method to be processed.

Returns
0 if the registers are successfully read and -1 if the device ID is incorrect.
149  {
150  this->registers = this->device->readRegisters(BUFFER_SIZE, 0x00);
151  if(*this->registers!=0xe5){
152  perror("ADXL345: Failure Condition - Sensor ID not Verified");
153  return -1;
154  }
155  this->accelerationX = this->combineRegisters(*(registers+DATAX1), *(registers+DATAX0));
156  this->accelerationY = this->combineRegisters(*(registers+DATAY1), *(registers+DATAY0));
157  this->accelerationZ = this->combineRegisters(*(registers+DATAZ1), *(registers+DATAZ0));
158  this->resolution = (ADXL345::RESOLUTION) (((*(registers+DATA_FORMAT))&0x08)>>3);
159  this->range = (ADXL345::RANGE) ((*(registers+DATA_FORMAT))&0x03);
160  this->calculatePitchAndRoll();
161  return 0;
162 }
#define DATAZ1
Definition: ADXL345.cpp:63
RESOLUTION
The resolution of the sensor. High is only available in +/- 16g range.
Definition: ADXL345.h:50
#define DATAX1
Definition: ADXL345.cpp:59
#define DATA_FORMAT
Definition: ADXL345.cpp:57
#define DATAX0
Definition: ADXL345.cpp:58
#define DATAZ0
Definition: ADXL345.cpp:62
RANGE
An enumeration to define the gravity range of the sensor.
Definition: ADXL345.h:43
#define BUFFER_SIZE
The ADXL345 has 0x40 registers (0x01 to 0x1C are reserved and should not be accessed) ...
Definition: ADXL345.h:31
virtual unsigned char * readRegisters(unsigned int number, unsigned int fromAddress=0)=0
#define DATAY0
Definition: ADXL345.cpp:60
#define DATAY1
Definition: ADXL345.cpp:61
void exploringBB::ADXL345::setRange ( ADXL345::RANGE  range)
virtual

Set the ADXL345 gravity range according to the RANGE enumeration

Parameters
rangeOne of the four possible gravity ranges defined by the RANGE enumeration
168  {
169  this->range = range;
170  updateRegisters();
171 }
void exploringBB::ADXL345::setResolution ( ADXL345::RESOLUTION  resolution)
virtual

Set the ADXL345 resolution according to the RESOLUTION enumeration

Parameters
resolutioneither HIGH or NORMAL resolution. HIGH resolution is only available if the range is set to +/- 16g
177  {
178  this->resolution = resolution;
179  updateRegisters();
180 }

The documentation for this class was generated from the following files: