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

Configuring and reading an LSM303DLHC Magnetometer. More...

Files

file  mag.h
 LSM303DLHC Magnetometer API Header.
 
file  mag.c
 LSM303DLHC Magnetometer API Implementation.
 

Macros

#define MAGREG_CRB   0x01
 Magnetometer Gain Register. More...
 
#define MAGREG_MR   0x02
 Magnetometer Mode Register. More...
 
#define MAGREG_XH   0x03
 First Magnetometer Output Register. More...
 

Enumerations

enum  MagRange {
  r1g3 = 1, r1g9 = 2, r2g5 = 3, r4g0 = 4,
  r4g7 = 5, r5g6 = 6, r8g1 = 7
}
 Magnetometer Range options. More...
 

Functions

Error magInit (MagRange r)
 Initialize the Magnetometer. More...
 
Error magRead (Vector3f *v)
 Read from the Magnetometer. More...
 
Error magWriteRegister (uint8_t reg, uint8_t val)
 Write a Magnetometer Register. More...
 

Variables

MagRange magRange
 Stored range to scale returned values. More...
 

Detailed Description

Configuring and reading an LSM303DLHC Magnetometer.

Macro Definition Documentation

#define MAGREG_CRB   0x01

Magnetometer Gain Register.

Definition at line 48 of file mag.c.

Referenced by magInit().

#define MAGREG_MR   0x02

Magnetometer Mode Register.

Definition at line 49 of file mag.c.

Referenced by magInit().

#define MAGREG_XH   0x03

First Magnetometer Output Register.

Definition at line 50 of file mag.c.

Referenced by magRead().

Enumeration Type Documentation

enum MagRange

Magnetometer Range options.

Enumerator
r1g3 

+- 1.3 Gauss

r1g9 

+- 1.9 Gauss

r2g5 

+- 2.5 Gauss

r4g0 

+- 4.0 Gauss

r4g7 

+- 4.7 Gauss

r5g6 

+- 5.6 Gauss

r8g1 

+- 8.1 Gauss

Definition at line 47 of file mag.h.

47  {
48  r1g3 = 1,
49  r1g9 = 2,
50  r2g5 = 3,
51  r4g0 = 4,
52  r4g7 = 5,
53  r5g6 = 6,
54  r8g1 = 7,
55 } MagRange;

Function Documentation

Error magInit ( MagRange  r)

Initialize the Magnetometer.

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

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

Definition at line 77 of file mag.c.

References ARGUMENT_ERROR, magRange, MAGREG_CRB, MAGREG_MR, magWriteRegister(), and SUCCESS.

77  {
78  if ((r <= 0) || (r >= 8)) {
79  return ARGUMENT_ERROR;
80  }
81  Error e = magWriteRegister(MAGREG_MR, 0x00); // Continuous Conversion
82  if (e != SUCCESS) {
83  return e;
84  }
85  e = magWriteRegister(MAGREG_CRB, (r << 5)); // Set Range
86  magRange = r;
87  return e;
88 }
Error magRead ( Vector3f v)

Read from the Magnetometer.

Magnetometer 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 90 of file mag.c.

References ARGUMENT_ERROR, MAG_ADDRESS, magRange, MAGREG_XH, r1g3, r1g9, r2g5, r4g0, r4g7, r5g6, r8g1, SUCCESS, TWI_NO_ANSWER, TWI_READ, TWI_WRITE, TWI_WRITE_ERROR, twiReadAck(), twiReadNak(), twiRepStart(), twiStart(), twiWrite(), Vector3f::x, Vector3f::y, and Vector3f::z.

90  {
91  if (v == NULL) {
92  return ARGUMENT_ERROR;
93  }
95  return TWI_NO_ANSWER;
96  }
97  if (twiWrite(MAGREG_XH)) {
98  return TWI_WRITE_ERROR;
99  }
101  return TWI_NO_ANSWER;
102  }
103  uint8_t xh = twiReadAck();
104  uint8_t xl = twiReadAck();
105  uint8_t zh = twiReadAck();
106  uint8_t zl = twiReadAck();
107  uint8_t yh = twiReadAck();
108  uint8_t yl = twiReadNak();
109 
110  int16_t x = *(int8_t *)(&xh);
111  x *= (1 << 8);
112  x |= xl;
113 
114  int16_t y = *(int8_t *)(&yh);
115  y *= (1 << 8);
116  y |= yl;
117 
118  int16_t z = *(int8_t *)(&zh);
119  z *= (1 << 8);
120  z |= zl;
121 
122  switch (magRange) {
123  case r1g3:
124  v->x = (((double)x) * 1.3 / MAG_NORMALIZE);
125  v->y = (((double)y) * 1.3 / MAG_NORMALIZE);
126  v->z = (((double)z) * 1.3 / MAG_NORMALIZE);
127  break;
128  case r1g9:
129  v->x = (((double)x) * 1.9 / MAG_NORMALIZE);
130  v->y = (((double)y) * 1.9 / MAG_NORMALIZE);
131  v->z = (((double)z) * 1.9 / MAG_NORMALIZE);
132  break;
133  case r2g5:
134  v->x = (((double)x) * 2.5 / MAG_NORMALIZE);
135  v->y = (((double)y) * 2.5 / MAG_NORMALIZE);
136  v->z = (((double)z) * 2.5 / MAG_NORMALIZE);
137  break;
138  case r4g0:
139  v->x = (((double)x) * 4.0 / MAG_NORMALIZE);
140  v->y = (((double)y) * 4.0 / MAG_NORMALIZE);
141  v->z = (((double)z) * 4.0 / MAG_NORMALIZE);
142  break;
143  case r4g7:
144  v->x = (((double)x) * 4.7 / MAG_NORMALIZE);
145  v->y = (((double)y) * 4.7 / MAG_NORMALIZE);
146  v->z = (((double)z) * 4.7 / MAG_NORMALIZE);
147  break;
148  case r5g6:
149  v->x = (((double)x) * 5.6 / MAG_NORMALIZE);
150  v->y = (((double)y) * 5.6 / MAG_NORMALIZE);
151  v->z = (((double)z) * 5.6 / MAG_NORMALIZE);
152  break;
153  case r8g1:
154  v->x = (((double)x) * 8.1 / MAG_NORMALIZE);
155  v->y = (((double)y) * 8.1 / MAG_NORMALIZE);
156  v->z = (((double)z) * 8.1 / MAG_NORMALIZE);
157  break;
158  default:
159  return ARGUMENT_ERROR;
160  }
161 
162  return SUCCESS;
163 }
Error magWriteRegister ( uint8_t  reg,
uint8_t  val 
)

Write a Magnetometer Register.

I2C should aready be initialized!

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

Definition at line 63 of file mag.c.

References MAG_ADDRESS, SUCCESS, TWI_NO_ANSWER, TWI_WRITE, TWI_WRITE_ERROR, twiStart(), twiStop(), and twiWrite().

Referenced by magInit().

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

Variable Documentation

MagRange magRange

Stored range to scale returned values.

Definition at line 54 of file mag.c.

Referenced by magInit(), and magRead().