QuadroCopter  0.1.4
quadro::i2c::LSM303Accelerometer Class Reference

#include <LSM303Accelerometer.h>

Inheritance diagram for quadro::i2c::LSM303Accelerometer:
quadro::i2c::LSM303DLHC quadro::i2c::i2cDevice quadro::IDevice

Public Member Functions

 LSM303Accelerometer (unsigned char _deviceAddress=ACCEL_ADDRESS, int _busId=1)
 
float calcRoll ()
 
float calcPitch ()
 
int start ()
 
- Public Member Functions inherited from quadro::i2c::LSM303DLHC
 LSM303DLHC ()
 
 ~LSM303DLHC ()
 
void loadRecommendedFlightSettings ()
 
uint8_t getPowerSettings ()
 
uint8_t getHighPassSettings ()
 
uint8_t getInt1Settings ()
 
uint8_t getDataSettings ()
 
uint8_t getMemorySettings ()
 
uint8_t getInterruptSettings ()
 
uint8_t getFIFOSettings ()
 
uint8_t getInterrupt1CFGSettings ()
 
uint8_t getInterrupt2CFGSettings ()
 
uint8_t getClickCFGSettings ()
 
uint8_t getClickSRCSettings ()
 
uint8_t getCRARegMSettings ()
 
uint8_t getMRRegMSettings ()
 
void startSensorThread ()
 
- Public Member Functions inherited from quadro::i2c::i2cDevice
 ~i2cDevice ()
 
 i2cDevice ()
 
void initDevice () throw ( i2cSetupException& )
 
short getValueFromRegister (unsigned char _registerAddress)
 
void setRegisterValue (unsigned char _registerValue)
 
void setRegisterAddress (unsigned char _registerAddress)
 
int writeToDevice (size_t _bufferSize) throw ( i2cSetupException& )
 
int stop (pthread_t threadHandle)
 
- Public Member Functions inherited from quadro::IDevice
virtual ~IDevice ()
 

Public Attributes

float pitch
 Stores the calculated Pitch value. More...
 
float roll
 Stores the calculated Roll value. More...
 
- Public Attributes inherited from quadro::i2c::LSM303DLHC
unsigned int dataTimer
 set from the devices data rate settings More...
 

Additional Inherited Members

- Protected Types inherited from quadro::i2c::i2cDevice
enum  deviceStatus { On = 1, Off = 0, Error = 2 }
 
- Protected Member Functions inherited from quadro::i2c::LSM303DLHC
void setDeviceAddress (unsigned char _deviceAddress)
 
void setBusId (int _busId)
 
- Protected Member Functions inherited from quadro::i2c::i2cDevice
int getStatus ()
 
void setStatus (deviceStatus _status)
 
int getDeviceFileHandle ()
 
const char * getFilePath ()
 
void setBusPaths ()
 
int validateBusId () throw ( i2cSetupException& )
 
char * validateBusPath (char *_busProposedPath) throw ( i2cSetupException& )
 
void selectABusPath ()
 
int connectToDevice ()
 
int openDevice () throw ( i2cSetupException& )
 
short readDevice (size_t _bufferSize) throw ( i2cSetupException& )
 
- Protected Attributes inherited from quadro::i2c::i2cDevice
const char * deviceBusPath
 
unsigned char deviceAddress
 
unsigned char registerValue
 
unsigned char registerAddress
 
char readAndWriteBuffer [TWO_BYTES]
 
char writeBufferOnly [ONE_BYTE]
 
char errMessage [MAX_BUFF]
 
int fileHandle
 
int busId
 
bool deviceInitialised
 

Constructor & Destructor Documentation

LSM303Accelerometer::LSM303Accelerometer ( unsigned char  _deviceAddress = ACCEL_ADDRESS,
int  _busId = 1 
)

LSM303Accelerometer Constructor, initialises the device - writes default settings to the sensor

See also
start() to start reading the device and updating roll and pitch values.
Parameters
_deviceAddress
_busId
10 {
11  setDeviceAddress( _deviceAddress );
12  setBusId( _busId );
13  initDevice();
16 }
void initDevice()
Definition: i2cDevice.cpp:33
void setDeviceAddress(unsigned char _deviceAddress)
Definition: LSM303DLHC.h:495
void loadRecommendedFlightSettings()
Definition: LSM303DLHC.cpp:22
void setBusId(int _busId)
Definition: LSM303DLHC.h:502
void startSensorThread()
Definition: LSM303DLHC.cpp:192

Member Function Documentation

float LSM303Accelerometer::calcPitch ( )

Calculates pitch using the following equation : atan2( Y, Z ) * 180.0 ) / 3.14159265358979323846

Returns
float Current pitch calculation.
24 {
25  return (float) ( ( atan2( ( double ) y, ( double ) z ) * 180.0 ) / M_PI );
26 }
float LSM303Accelerometer::calcRoll ( )

Calculates roll using the following equation : atan2( X, sqrt( Y * Y + Z * Z )) * 180.0 ) / 3.14159265358979323846 )

Returns
float Current roll calculation
19 {
20  return (float) ( ( atan2( ( double ) x, sqrt( double( y * y + z * z ))) * 180.0 ) / M_PI );
21 }
int LSM303Accelerometer::start ( )

start() - Allows the independent control to start the thread that updates the Accelerometer readings.

Parameters
none
Exceptions
i2cSetupException
Returns
int deviceStatus : 1 = on, 0 = off or 2 = error
29 {
30  //pthread_create doesn't throw an exception, only returns error codes - these are handled below.
31  threadRet = pthread_create( &threadHandle, NULL, LSM303Accelerometer::runMainSensorUpdateThread, this );
32 
33  //Thread returned 0 (Success Code)
34  if ( threadRet != 0 ) {
35  //The sonic sensor thread failed, set the status to "Error" and throw an appropriate exception.
37  if ( threadRet == EAGAIN ) {
38  //Failed because of resource unavailability, try once more and then throw an exception on failure
39  threadRet = pthread_create( &threadHandle, NULL, LSM303Accelerometer::runMainSensorUpdateThread, this );
40  if ( threadRet != 0 ) {
41  throw new i2cSetupException(
42  "(LVMaxSonarEZ) " + i2c::THREAD_FATAL + " : errorNumber = "
43  + to_string( threadRet ) );
44  }
45  }
46  else if ( threadRet == EPERM ) {
47  //Thread creation failed because of invalid permissions on the system to create threads.
48  throw new i2cSetupException(
49  "(LVMaxSonarEZ) " + i2c::THREAD_PERMISSIONS + " : errorNumber = " + to_string( threadRet ) );
50  }
51  else if ( threadRet == EINVAL ) {
52  //Thread creation failed because the argument used is invalid.
53  throw new i2cSetupException(
54  "(LVMaxSonarEZ) " + i2c::THREAD_INVALID_ARG + " : errorNumber = " + to_string( threadRet ) );
55  }
56  else {
57  //An unknown error occurred - unknown error code.
58  throw new i2cSetupException(
59  "(LVMaxSonarEZ) " + i2c::THREAD_UNKNOWN + " : errorNumber = " + to_string( threadRet ) );
60  }
61  }
62 
63  //The thread started correctly, so set the status of the Sonic Sensor to "On".
65 
66  //return the device status - realistically it should always be "On" if we get this far...
67  return getStatus();
68 
69 }
const string THREAD_FATAL
Definition: i2cSetupException.h:20
Definition: i2cSetupException.h:24
void setStatus(deviceStatus _status)
Definition: i2cDevice.cpp:80
Error
Status Error (thread is disabled)
Definition: LVMaxSonarEZ.h:92
const string THREAD_PERMISSIONS
Definition: i2cSetupException.h:21
int getStatus()
Definition: i2cDevice.cpp:85
const string THREAD_UNKNOWN
Definition: i2cSetupException.h:19
const string THREAD_INVALID_ARG
Definition: i2cSetupException.h:22
On
Status On (thread is active)
Definition: LVMaxSonarEZ.h:90

Member Data Documentation

float quadro::i2c::LSM303Accelerometer::pitch

Stores the calculated Pitch value.

float quadro::i2c::LSM303Accelerometer::roll

Stores the calculated Roll value.


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