QuadroCopter  0.1.4
quadro::i2c::L3GD20H Class Reference

#include <L3GD20H.h>

Inheritance diagram for quadro::i2c::L3GD20H:
quadro::i2c::i2cDevice quadro::IDevice

Classes

struct  vector
 

Public Types

enum  _switch { ON = 0x7F, OFF = 0x00 }
 

Public Member Functions

 L3GD20H (unsigned char _deviceAddress=GYRO_ADDRESS, int _busId=1)
 
int start ()
 
- 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

vector< int > rad
 Gyroscope Radian vector (X, Y, Z) More...
 
vector< float > angle
 Gyroscope Angle Vector (X, Y, Z) More...
 

Protected Member Functions

void setDeviceAddress (unsigned char _deviceAddress)
 
void setBusId (int _busId)
 
int setPower (_switch gyroSwitch)
 
void calcX (void)
 
void calcY (void)
 
void calcZ (void)
 
- 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& )
 

Static Protected Member Functions

static void * runMainSensorUpdateThread (void *static_inst)
 

Protected Attributes

_switch gyroSwitch
 enum value for the Gyroscope power status. More...
 
- 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
 

Additional Inherited Members

- Protected Types inherited from quadro::i2c::i2cDevice
enum  deviceStatus { On = 1, Off = 0, Error = 2 }
 

Detailed Description

L3GD20H : A class that provides control of the L3GD20H which contains : gyroscope.

Member Enumeration Documentation

Please refer to the documentation provided for your L3GD20H for further details (CTRL1 pg 36 point 7.2)

Enumerator
ON 

0x7F 200 Hz Data Rate, Power On and X, Y, Z enabled

OFF 

0x00 Off

76  {
77  ON = 0x7F,
78  OFF = 0x00
79  };
0x00 Off
Definition: L3GD20H.h:78
0x7F 200 Hz Data Rate, Power On and X, Y, Z enabled
Definition: L3GD20H.h:77

Constructor & Destructor Documentation

L3GD20H::L3GD20H ( unsigned char  _deviceAddress = GYRO_ADDRESS,
int  _busId = 1 
)

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

See also
start() to start reading the device and updating X, Y and Z values.
Parameters
_deviceAddress
_busId
23 {
24  setDeviceAddress( _deviceAddress );
25  setBusId( _busId );
26  initDevice();
27 
28  if ( setPower( _switch::ON ) < 0 ) {
29  throw new i2cSetupException( "Critical Error : L3GD20H - Attempted to set the Gyroscopes Power state but this action failed." );
30  }
31 }
void setDeviceAddress(unsigned char _deviceAddress)
Definition: L3GD20H.h:101
void initDevice()
Definition: i2cDevice.cpp:33
Definition: i2cSetupException.h:24
void setBusId(int _busId)
Definition: L3GD20H.h:108
int setPower(_switch gyroSwitch)
Definition: L3GD20H.cpp:33

Member Function Documentation

void L3GD20H::calcX ( void  )
protected

Calculate the Gyro X angular rate of change using the equation : X = SensorRawValue * 0.00875 * 0.02 sets the angle vector x value.

100 {
101  rad.x = ( int16_t ) ( getValueFromRegister( OUT_X_L ) | ( getValueFromRegister( OUT_X_H ) << 8 ));
102  rad.x *= 0.00875F;
103  angle.x = float( rad.x * DT );
104 }
vector< int > rad
Gyroscope Radian vector (X, Y, Z)
Definition: L3GD20H.h:89
#define OUT_X_L
Definition: L3GD20H.h:34
vector< float > angle
Gyroscope Angle Vector (X, Y, Z)
Definition: L3GD20H.h:90
T x
Definition: L3GD20H.h:86
#define DT
Definition: L3GD20H.h:41
#define OUT_X_H
Definition: L3GD20H.h:35
short getValueFromRegister(unsigned char _registerAddress)
Definition: i2cDevice.cpp:114
void L3GD20H::calcY ( void  )
protected

Calculate the Gyro Y angular rate of change using the equation : Y = SensorRawValue * 0.00875 * 0.02 sets the angle vector y value.

107 {
108  rad.y = ( int16_t ) ( getValueFromRegister( OUT_Y_L ) | ( getValueFromRegister( OUT_Y_H ) << 8 ));
109  rad.y *= 0.00875F;
110  angle.y = float( rad.y * DT );
111 }
vector< int > rad
Gyroscope Radian vector (X, Y, Z)
Definition: L3GD20H.h:89
#define OUT_Y_L
Definition: L3GD20H.h:36
vector< float > angle
Gyroscope Angle Vector (X, Y, Z)
Definition: L3GD20H.h:90
#define OUT_Y_H
Definition: L3GD20H.h:37
#define DT
Definition: L3GD20H.h:41
short getValueFromRegister(unsigned char _registerAddress)
Definition: i2cDevice.cpp:114
T y
Definition: L3GD20H.h:86
void L3GD20H::calcZ ( void  )
protected

Calculate the Gyro Z angular rate of change using the equation : Z = SensorRawValue * 0.00875 * 0.02 sets the angle vector z value.

114 {
115  rad.z = -( int16_t ) ( getValueFromRegister( OUT_Z_L )
116  | ( getValueFromRegister( OUT_Z_H ) << 8 ));
117  rad.z *= 0.00875F;
118  angle.z = float( rad.z * DT );
119 }
vector< int > rad
Gyroscope Radian vector (X, Y, Z)
Definition: L3GD20H.h:89
vector< float > angle
Gyroscope Angle Vector (X, Y, Z)
Definition: L3GD20H.h:90
T z
Definition: L3GD20H.h:86
#define OUT_Z_L
Definition: L3GD20H.h:38
#define DT
Definition: L3GD20H.h:41
#define OUT_Z_H
Definition: L3GD20H.h:39
short getValueFromRegister(unsigned char _registerAddress)
Definition: i2cDevice.cpp:114
void * L3GD20H::runMainSensorUpdateThread ( void *  static_inst)
staticprotected

runMainSensorUpdateThread starts a continuous thread updating the programs values from the sensors values.

See also
start() to start this thread running
stop() to stop this thread running
Parameters
static_inst
Returns
0
42 {
43  L3GD20H* gyro = ( L3GD20H* ) static_inst;
44  int startTime;
45  while ( 1 ) {
46  startTime = Timer::milliTimer();
47  gyro->calcX();
48  gyro->calcY();
49  gyro->calcZ();
50  while ( Timer::milliTimer() - startTime < ( DT * 1000 )) {
51  usleep( 100 );
52  }
53  }
54 }
void calcY(void)
Definition: L3GD20H.cpp:106
Definition: L3GD20H.h:51
void calcZ(void)
Definition: L3GD20H.cpp:113
static int milliTimer()
Definition: Timer.cpp:7
#define DT
Definition: L3GD20H.h:41
void calcX(void)
Definition: L3GD20H.cpp:99
void quadro::i2c::L3GD20H::setBusId ( int  _busId)
inlineprotectedvirtual

Sets the devices I2C Bus Id - to satisfy the interface

Parameters
_busId

Implements quadro::i2c::i2cDevice.

108 { this->busId = _busId; }
int busId
Definition: i2cDevice.h:232
void quadro::i2c::L3GD20H::setDeviceAddress ( unsigned char  _deviceAddress)
inlineprotectedvirtual

Sets the devices I2C device address - to satisfy the interface

Parameters
_deviceAddress

Implements quadro::i2c::i2cDevice.

101 { this->deviceAddress = _deviceAddress; }
unsigned char deviceAddress
Definition: i2cDevice.h:223
int L3GD20H::setPower ( _switch  gyroSwitch)
protected

Set the power status on the Gyroscope

Parameters
gyroSwitch
Returns
int
34 {
37  writeToDevice( 2 );
38  return gyroSwitch;
39 }
_switch gyroSwitch
enum value for the Gyroscope power status.
Definition: L3GD20H.h:110
void setRegisterValue(unsigned char _registerValue)
Definition: i2cDevice.cpp:76
int writeToDevice(size_t _bufferSize)
Definition: i2cDevice.cpp:155
void setRegisterAddress(unsigned char _registerAddress)
Definition: i2cDevice.cpp:74
#define CTRL1
Definition: L3GD20H.h:33
int L3GD20H::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
57 {
58  //pthread_create doesn't throw an exception, only returns error codes - these are handled below.
59  threadRet = pthread_create( &threadHandle, NULL, L3GD20H::runMainSensorUpdateThread, this );
60 
61  //Thread returned 0 (Success Code)
62  if ( threadRet != 0 ) {
63  //The sonic sensor thread failed, set the status to "Error" and throw an appropriate exception.
65  if ( threadRet == EAGAIN ) {
66  //Failed because of resource unavailability, try once more and then throw an exception on failure
67  threadRet = pthread_create( &threadHandle, NULL, L3GD20H::runMainSensorUpdateThread, this );
68  if ( threadRet != 0 ) {
69  throw new i2cSetupException(
70  "(LVMaxSonarEZ) " + i2c::THREAD_FATAL + " : errorNumber = "
71  + to_string( threadRet ) );
72  }
73  }
74  else if ( threadRet == EPERM ) {
75  //Thread creation failed because of invalid permissions on the system to create threads.
76  throw new i2cSetupException(
77  "(LVMaxSonarEZ) " + i2c::THREAD_PERMISSIONS + " : errorNumber = " + to_string( threadRet ) );
78  }
79  else if ( threadRet == EINVAL ) {
80  //Thread creation failed because the argument used is invalid.
81  throw new i2cSetupException(
82  "(LVMaxSonarEZ) " + i2c::THREAD_INVALID_ARG + " : errorNumber = " + to_string( threadRet ) );
83  }
84  else {
85  //An unknown error occurred - unknown error code.
86  throw new i2cSetupException(
87  "(LVMaxSonarEZ) " + i2c::THREAD_UNKNOWN + " : errorNumber = " + to_string( threadRet ) );
88  }
89  }
90 
91  //The thread started correctly, so set the status of the Sonic Sensor to "On".
93 
94  //return the device status - realistically it should always be "On" if we get this far...
95  return getStatus();
96 
97 }
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
static void * runMainSensorUpdateThread(void *static_inst)
Definition: L3GD20H.cpp:41
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

vector<float> quadro::i2c::L3GD20H::angle

Gyroscope Angle Vector (X, Y, Z)

_switch quadro::i2c::L3GD20H::gyroSwitch
protected

enum value for the Gyroscope power status.

vector<int> quadro::i2c::L3GD20H::rad

Gyroscope Radian vector (X, Y, Z)


The documentation for this class was generated from the following files:
  • /Users/michaelbrookes/ClionProjects/Quadro/lib/i2c/devices/L3GD20H/L3GD20H.h
  • /Users/michaelbrookes/ClionProjects/Quadro/lib/i2c/devices/L3GD20H/L3GD20H.cpp