xyControl  0.1
Quadrotor Flight Controller on AVR Basis
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
Files | Macros | Enumerations | Functions | Variables
Accelerometer Driver

Configuring and reading an LSM303DLHC Accelerometer. More...

Files

file  acc.h
 LSM303DLHC Accelerometer API Header.
 
file  acc.c
 LSM303DLHC Accelerometer API Implementation.
 

Macros

#define ACCREG_CTRL1   0x20
 Accelerometer Control Register 1. More...
 
#define ACCREG_CTRL4   0x23
 Accelerometer Control Register 4. More...
 
#define ACCREG_XL   0x28
 First Accelerometer Output Register. More...
 

Enumerations

enum  AccRange { r2G, r4G, r8G, r16G }
 Accelerometer Range options. More...
 

Functions

Error accInit (AccRange r)
 Initialize the Accelerometer. More...
 
Error accRead (Vector3f *v)
 Read from the Accelerometer. More...
 
Error accWriteRegister (uint8_t reg, uint8_t val)
 Write an Accelerometer Register. More...
 

Variables

AccRange accRange
 Stored range to scale returned values. More...
 

Detailed Description

Configuring and reading an LSM303DLHC Accelerometer.

Macro Definition Documentation

#define ACCREG_CTRL1   0x20

Accelerometer Control Register 1.

Definition at line 49 of file acc.c.

Referenced by accInit().

#define ACCREG_CTRL4   0x23

Accelerometer Control Register 4.

Definition at line 50 of file acc.c.

Referenced by accInit().

#define ACCREG_XL   0x28

First Accelerometer Output Register.

Definition at line 51 of file acc.c.

Referenced by accRead().

Enumeration Type Documentation

enum AccRange

Accelerometer Range options.

Enumerator
r2G 

+- 2G

r4G 

+- 4G

r8G 

+- 8G

r16G 

+- 16G

Definition at line 47 of file acc.h.

47  {
48  r2G,
49  r4G,
50  r8G,
51  r16G,
52 } AccRange;

Function Documentation

Error accInit ( AccRange  r)

Initialize the Accelerometer.

Call before accRead(). I2C should already be initialized!

Parameters
rAccRange to use.
Returns
TWI_NO_ANSWER, TWI_WRITE_ERROR, ARGUMENT_ERROR or SUCCESS.

Definition at line 76 of file acc.c.

References accRange, ACCREG_CTRL1, ACCREG_CTRL4, accWriteRegister(), ARGUMENT_ERROR, r16G, r2G, r4G, r8G, and SUCCESS.

Referenced by orientationInit().

76  {
77  uint8_t v;
78  switch (r) {
79  case r2G:
80  v = 0x00;
81  break;
82  case r4G:
83  v = 0x10;
84  break;
85  case r8G:
86  v = 0x20;
87  break;
88  case r16G:
89  v = 0x30;
90  break;
91  default:
92  return ARGUMENT_ERROR;
93  }
94  accRange = r;
95  Error e = accWriteRegister(ACCREG_CTRL1, 0x57); // Enable all axes, 100Hz
96  if (e != SUCCESS) {
97  return e;
98  }
100  return e;
101 }
Error accRead ( Vector3f v)

Read from the Accelerometer.

Accelerometer should already be initialized!

Parameters
vVector3f for the read values
Returns
TWI_NO_ANSWER, TWI_WRITE_ERROR, ARGUMENT_ERROR or SUCCESS.
Examples:
hardwareTest.c, and uartFlight.c.

Definition at line 103 of file acc.c.

References ACC_ADDRESS, ACCFILTERFACTOR, accRange, ACCREG_XL, ARGUMENT_ERROR, r16G, r2G, r4G, r8G, SUCCESS, TWI_NO_ANSWER, TWI_READ, TWI_WRITE, TWI_WRITE_ERROR, twiReadAck(), twiReadNak(), twiRepStart(), twiStart(), twiWrite(), Vector3f::x, Vector3f::y, and Vector3f::z.

Referenced by orientationTask().

103  {
104  static double accSumX = 0; /* Buffer for X Low-Pass. */
105  static double accSumY = 0; /* Buffer for Y Low-Pass. */
106  static double accSumZ = 0; /* Buffer for Z Low-Pass. */
107  static double accFilterX = 0; /* Buffer for X Low-Pass. */
108  static double accFilterY = 0; /* Buffer for Y Low-Pass. */
109  static double accFilterZ = 0; /* Buffer for Z Low-Pass. */
110 
111  if (v == NULL) {
112  return ARGUMENT_ERROR;
113  }
114  if (twiStart(ACC_ADDRESS | TWI_WRITE)) {
115  return TWI_NO_ANSWER;
116  }
117  if (twiWrite(ACCREG_XL | (1 << 7))) { // Auto Increment
118  return TWI_WRITE_ERROR;
119  }
121  return TWI_NO_ANSWER;
122  }
123 
124  uint8_t xl = twiReadAck();
125  uint8_t xh = twiReadAck();
126  uint8_t yl = twiReadAck();
127  uint8_t yh = twiReadAck();
128  uint8_t zl = twiReadAck();
129  uint8_t zh = twiReadNak();
130 
131  int16_t x = *(int8_t *)(&xh);
132  x *= (1 << 8);
133  x |= xl;
134 
135  int16_t y = *(int8_t *)(&yh);
136  y *= (1 << 8);
137  y |= yl;
138 
139  int16_t z = *(int8_t *)(&zh);
140  z *= (1 << 8);
141  z |= zl;
142 
143  switch (accRange) {
144  case r2G:
145  v->x = (((double)x) * 2 / 0x8000);
146  v->y = (((double)y) * 2 / 0x8000);
147  v->z = (((double)z) * 2 / 0x8000);
148  break;
149  case r4G:
150  v->x = (((double)x) * 4 / 0x8000);
151  v->y = (((double)y) * 4 / 0x8000);
152  v->z = (((double)z) * 4 / 0x8000);
153  break;
154  case r8G:
155  v->x = (((double)x) * 8 / 0x8000);
156  v->y = (((double)y) * 8 / 0x8000);
157  v->z = (((double)z) * 8 / 0x8000);
158  break;
159  case r16G:
160  v->x = (((double)x) * 16 / 0x8000);
161  v->y = (((double)y) * 16 / 0x8000);
162  v->z = (((double)z) * 16 / 0x8000);
163  break;
164  default:
165  return ARGUMENT_ERROR;
166  }
167 
168  accSumX = accSumX - accFilterX + v->x;
169  accFilterX = accSumX / ACCFILTERFACTOR;
170  v->x = accFilterX;
171 
172  accSumY = accSumY - accFilterY + v->y;
173  accFilterY = accSumY / ACCFILTERFACTOR;
174  v->y = accFilterY;
175 
176  accSumZ = accSumZ - accFilterZ + v->z;
177  accFilterZ = accSumZ / ACCFILTERFACTOR;
178  v->z = accFilterZ;
179 
180  return SUCCESS;
181 }
Error accWriteRegister ( uint8_t  reg,
uint8_t  val 
)

Write an Accelerometer Register.

I2C should aready be initialized!

Parameters
regRegister Address
valNew Value
Returns
TWI_NO_ANSWER, TWI_WRITE_ERROR or SUCCESS.

Definition at line 62 of file acc.c.

References TWI_NO_ANSWER.

Referenced by accInit().

62  {
64  return TWI_NO_ANSWER;
65  }
66  if (twiWrite(reg)) {
67  return TWI_WRITE_ERROR;
68  }
69  if (twiWrite(val)) {
70  return TWI_WRITE_ERROR;
71  }
72  twiStop();
73  return SUCCESS;
74 }

Variable Documentation

AccRange accRange

Stored range to scale returned values.

Definition at line 53 of file acc.c.

Referenced by accInit(), and accRead().