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

Configuring and reading an L3GD20. More...

Files

file  gyro.h
 L3GD20 Gyroscope API Header.
 
file  gyro.c
 L3GD20 Gyroscope API Implementation.
 

Macros

#define GYROREG_CTRL1   0x20
 Gyroscope Control Register 1. More...
 
#define GYROREG_CTRL4   0x23
 Gyroscope Control Register 4. More...
 
#define GYROREG_OUTXL   0x28
 First Gyroscope Output Register. More...
 

Enumerations

enum  GyroRange { r250DPS, r500DPS, r2000DPS }
 Gyroscope Range options. More...
 

Functions

Error gyroInit (GyroRange r)
 Initializes the Gyroscope. More...
 
Error gyroRead (Vector3f *v)
 Get a set of gyroscope data. More...
 
Error gyroWriteByte (uint8_t reg, uint8_t val)
 Write a Gyroscope Register. More...
 

Variables

GyroRange gyroRange
 Stored range to scale returned values. More...
 

Detailed Description

Configuring and reading an L3GD20.

Macro Definition Documentation

#define GYROREG_CTRL1   0x20

Gyroscope Control Register 1.

Definition at line 48 of file gyro.c.

Referenced by gyroInit().

#define GYROREG_CTRL4   0x23

Gyroscope Control Register 4.

Definition at line 49 of file gyro.c.

Referenced by gyroInit().

#define GYROREG_OUTXL   0x28

First Gyroscope Output Register.

Definition at line 50 of file gyro.c.

Referenced by gyroRead().

Enumeration Type Documentation

enum GyroRange

Gyroscope Range options.

Enumerator
r250DPS 

+- 250 Degrees per Second

r500DPS 

+- 500 Degrees per Second

r2000DPS 

+- 2000 Degrees per Second

Definition at line 47 of file gyro.h.

47  {
48  r250DPS,
49  r500DPS,
50  r2000DPS,
51 } GyroRange;

Function Documentation

Error gyroInit ( GyroRange  r)

Initializes the Gyroscope.

I2C should already be initialized.

Parameters
rGyroRange to use
Returns
TWI_NO_ANSWER, TWI_WRITE_ERROR, ARGUMENT_ERROR or SUCCESS

Definition at line 75 of file gyro.c.

References ARGUMENT_ERROR, gyroRange, GYROREG_CTRL1, GYROREG_CTRL4, gyroWriteByte(), r2000DPS, r250DPS, r500DPS, and SUCCESS.

Referenced by orientationInit().

75  {
76  uint8_t v;
77  switch (r) {
78  case r250DPS:
79  v = 0x00;
80  break;
81  case r500DPS:
82  v = 0x10;
83  break;
84  case r2000DPS:
85  v = 0x20;
86  break;
87  default:
88  return ARGUMENT_ERROR;
89  }
90  gyroRange = r;
92  if (e != SUCCESS) {
93  return e;
94  }
96  return e;
97 }
Error gyroRead ( Vector3f v)

Get a set of gyroscope data.

gyroInit() should already be called.

Parameters
vData Destionation
Returns
TWI_NO_ANSWER, TWI_WRITE_ERROR, ARGUMENT_ERROR or SUCCESS
Examples:
hardwareTest.c, and uartFlight.c.

Definition at line 99 of file gyro.c.

References ARGUMENT_ERROR, GYRO_ADDRESS, GYROFILTERFACTOR, gyroRange, GYROREG_OUTXL, r2000DPS, r250DPS, r500DPS, 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().

99  {
100  // Simple Software Low-Pass
101  static double gyroSumX = 0, gyroSumY = 0, gyroSumZ = 0;
102  static double gyroFilterX = 0, gyroFilterY = 0, gyroFilterZ = 0;
103 
104  if (v == NULL) {
105  return ARGUMENT_ERROR;
106  }
108  return TWI_NO_ANSWER;
109  }
110  if (twiWrite(GYROREG_OUTXL | 0x80)) { // Auto Increment
111  return TWI_WRITE_ERROR;
112  }
114  return TWI_NO_ANSWER;
115  }
116 
117  uint8_t xl = twiReadAck();
118  uint8_t xh = twiReadAck();
119  uint8_t yl = twiReadAck();
120  uint8_t yh = twiReadAck();
121  uint8_t zl = twiReadAck();
122  uint8_t zh = twiReadNak();
123 
124  int16_t x = *(int8_t *)(&xh);
125  x *= (1 << 8);
126  x |= xl;
127 
128  int16_t y = *(int8_t *)(&yh);
129  y *= (1 << 8);
130  y |= yl;
131 
132  int16_t z = *(int8_t *)(&zh);
133  z *= (1 << 8);
134  z |= zl;
135 
136  switch (gyroRange) {
137  case r250DPS:
138  v->x = (((double)x) * 250 / 0x8000);
139  v->y = (((double)y) * 250 / 0x8000);
140  v->z = (((double)z) * 250 / 0x8000);
141  break;
142  case r500DPS:
143  v->x = (((double)x) * 500 / 0x8000);
144  v->y = (((double)y) * 500 / 0x8000);
145  v->z = (((double)z) * 500 / 0x8000);
146  break;
147  case r2000DPS:
148  v->x = (((double)x) * 2000 / 0x8000);
149  v->y = (((double)y) * 2000 / 0x8000);
150  v->z = (((double)z) * 2000 / 0x8000);
151  break;
152  default:
153  return ARGUMENT_ERROR;
154  }
155 
156  gyroSumX = gyroSumX - gyroFilterX + v->x;
157  gyroFilterX = gyroSumX / GYROFILTERFACTOR;
158  v->x = gyroFilterX;
159 
160  gyroSumY = gyroSumY - gyroFilterY + v->y;
161  gyroFilterY = gyroSumY / GYROFILTERFACTOR;
162  v->y = gyroFilterY;
163 
164  gyroSumZ = gyroSumZ - gyroFilterZ + v->z;
165  gyroFilterZ = gyroSumZ / GYROFILTERFACTOR;
166  v->z = gyroFilterZ;
167 
168  return SUCCESS;
169 }
Error gyroWriteByte ( uint8_t  reg,
uint8_t  val 
)

Write a Gyroscope Register.

I2C should aready be initialized!

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

Definition at line 61 of file gyro.c.

References TWI_NO_ANSWER.

Referenced by gyroInit().

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

Variable Documentation

GyroRange gyroRange

Stored range to scale returned values.

Definition at line 52 of file gyro.c.

Referenced by gyroInit(), and gyroRead().