QuadroCopter  0.1.4
quadro::i2c::LSM303Magnetometer Class Reference

#include <LSM303Magnetometer.h>

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

Public Member Functions

 LSM303Magnetometer (unsigned char _deviceAddress=MAG_ADDRESS, int _busId=1)
 
double calcHeading ()
 
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

double heading
 Store current calculated heading 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

LSM303Magnetometer::LSM303Magnetometer ( unsigned char  _deviceAddress = MAG_ADDRESS,
int  _busId = 1 
)

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

See also
start() to start reading the device and updating heading values.
Parameters
_deviceAddress
_busId
23 {
24  setDeviceAddress( _deviceAddress );
25  setBusId( _busId );
26  initDevice();
29 }
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

double LSM303Magnetometer::calcHeading ( )

Calculates heading using the following equation :

x2 = ( x ) * cos( -1 * atan2( X, sqrt( Y * Y + Z * Z )) ) + ( z ) * sin( -1 * atan2( X, sqrt( Y * Y + Z * Z )) ); y2 = ( x ) * sin( atan2( Y, sqrt( X * X + Z * Z )) ) * sin( -1 * atan2( X, sqrt( Y * Y + Z * Z )) ) + ( y ) * cos( atan2( Y, sqrt( X * X + Z * Z )) ) - ( z ) * sin( atan2( Y, sqrt( X * X + Z * Z )) ) * cos( -1 * atan2( X, sqrt( Y * Y + Z * Z )) );

return ( atan2( x2, y2 ) * 180 ) / 3.14159265358979323846;

Returns
double Current heading calculation
32 {
33  double x2 = 0, y2 = 0;
34 
35  double t_roll = x * x + z * z;
36  float rollRadians = ( float ) atan2(( double ) y, sqrt( t_roll ));
37 
38  double t_pitch = y * y + z * z;
39  float pitchRadians = ( float ) atan2(( double ) x, sqrt( t_pitch ));
40 
41  float cosRoll = ( float ) cos( rollRadians );
42  float sinRoll = ( float ) sin( rollRadians );
43 
44  float cosPitch = ( float ) cos( -1 * pitchRadians );
45  float sinPitch = ( float ) sin( -1 * pitchRadians );
46 
47  x2 = ( x ) * cosPitch + ( z ) * sinPitch;
48  y2 = ( x ) * sinRoll * sinPitch + ( y ) * cosRoll - ( z ) * sinRoll * cosPitch;
49 
50  return ( atan2( x2, y2 ) * 180 ) / M_PI;
51 }
int LSM303Magnetometer::start ( )

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

Parameters
none
Exceptions
i2cSetupException
Returns
int deviceStatus : 1 = on, 0 = off or 2 = error
64 {
65  //pthread_create doesn't throw an exception, only returns error codes - these are handled below.
66  threadRet = pthread_create( &threadHandle, NULL, LSM303Magnetometer::runMainSensorUpdateThread, this );
67 
68  //Thread returned 0 (Success Code)
69  if ( threadRet != 0 ) {
70  //The sonic sensor thread failed, set the status to "Error" and throw an appropriate exception.
72  if ( threadRet == EAGAIN ) {
73  //Failed because of resource unavailability, try once more and then throw an exception on failure
74  threadRet = pthread_create( &threadHandle, NULL, LSM303Magnetometer::runMainSensorUpdateThread, this );
75  if ( threadRet != 0 ) {
76  throw new i2cSetupException(
77  "(LVMaxSonarEZ) " + i2c::THREAD_FATAL + " : errorNumber = "
78  + to_string( threadRet ) );
79  }
80  }
81  else if ( threadRet == EPERM ) {
82  //Thread creation failed because of invalid permissions on the system to create threads.
83  throw new i2cSetupException(
84  "(LVMaxSonarEZ) " + i2c::THREAD_PERMISSIONS + " : errorNumber = " + to_string( threadRet ) );
85  }
86  else if ( threadRet == EINVAL ) {
87  //Thread creation failed because the argument used is invalid.
88  throw new i2cSetupException(
89  "(LVMaxSonarEZ) " + i2c::THREAD_INVALID_ARG + " : errorNumber = " + to_string( threadRet ) );
90  }
91  else {
92  //An unknown error occurred - unknown error code.
93  throw new i2cSetupException(
94  "(LVMaxSonarEZ) " + i2c::THREAD_UNKNOWN + " : errorNumber = " + to_string( threadRet ) );
95  }
96  }
97 
98  //The thread started correctly, so set the status of the Sonic Sensor to "On".
100 
101  //return the device status - realistically it should always be "On" if we get this far...
102  return getStatus();
103 
104 }
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

double quadro::i2c::LSM303Magnetometer::heading

Store current calculated heading value;.


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