1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#![allow(dead_code)]
extern crate i2cdev;
extern crate i2csensors;
extern crate byteorder;
use i2csensors::Gyroscope;
use i2csensors::Vec3;
use std::thread;
use std::time::Duration;
use std::error::Error;
use i2cdev::core::I2CDevice;
#[cfg(any(target_os = "linux", target_os = "android"))]
use i2cdev::linux::{LinuxI2CDevice,LinuxI2CError};
use byteorder::{ByteOrder, BigEndian, LittleEndian};
pub const L3GD20_I2C_ADDR: u16 = 0x6A;
const L3GD20_CTRL_REG1: u8 = 0x20;
const L3GD20_CTRL_REG2: u8 = 0x21;
const L3GD20_CTRL_REG3: u8 = 0x22;
const L3GD20_CTRL_REG4: u8 = 0x23;
const L3GD20_CTRL_REG5: u8 = 0x24;
const L3GD20_INCREMENT_BIT: u8 = 0x80;
const L3GD20_OUT: u8 = 0x28;
const L3GD20_WHO_AM_I: u8 = 0x0F;
const L3GD20_ID: u8 = 0b11010100;
#[derive(Debug, Copy, Clone)]
pub enum L3GD20PowerMode {
PowerDown = 0b00000000,
Sleep,
Normal = 0b00001000
}
#[derive(Debug, Copy, Clone)]
pub enum L3GD20GyroscopeDataRate {
Hz95 = 0b00,
Hz190 = 0b01,
Hz380 = 0b10,
Hz760 = 0b11
}
#[derive(Debug, Copy, Clone)]
pub enum L3GD20GyroscopeBandwidth {
BW1 = 0b00,
BW2 = 0b01,
BW3 = 0b10,
BW4 = 0b11
}
#[derive(Debug, Copy, Clone)]
pub enum L3GD20GyroscopeFS {
dps250 = 0b00,
dps500 = 0b01,
dps2000 = 0b10
}
#[derive(Debug, Copy, Clone)]
pub struct L3GD20GyroscopeSettings {
pub DR: L3GD20GyroscopeDataRate,
pub BW: L3GD20GyroscopeBandwidth,
pub power_mode: L3GD20PowerMode,
pub zen: bool,
pub yen: bool,
pub xen: bool,
pub sensitivity: L3GD20GyroscopeFS,
pub continuous_update: bool
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn get_linux_l3gd20_i2c_device() -> Result<LinuxI2CDevice,LinuxI2CError> {
let gyro = try!(LinuxI2CDevice::new("/dev/i2c-1", L3GD20_I2C_ADDR));
Ok(gyro)
}
pub struct L3GD20<T: I2CDevice + Sized> {
pub gyroscope: T,
g_gain: f32,
}
impl<T> L3GD20<T>
where T: I2CDevice + Sized
{
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn new(mut gyro: T, mut gyro_settings: L3GD20GyroscopeSettings) -> Result<L3GD20<T>, T::Error> {
assert!(try!(gyro.smbus_read_byte_data(L3GD20_WHO_AM_I)) == L3GD20_ID, "L3GD20 ID didn't match for device at given I2C address.");
let mut ctrl_reg1: u8 = 0_u8 | ((gyro_settings.DR as u8) << 6) | ((gyro_settings.BW as u8) << 4);
match gyro_settings.power_mode {
L3GD20PowerMode::PowerDown => {
ctrl_reg1 |= L3GD20PowerMode::PowerDown as u8;
},
L3GD20PowerMode::Sleep => {
ctrl_reg1 = (ctrl_reg1 | L3GD20PowerMode::Normal as u8) & 0b11111000;
},
L3GD20PowerMode::Normal => {
ctrl_reg1 |= L3GD20PowerMode::Normal as u8;
if gyro_settings.xen { ctrl_reg1 |= 0b001 };
if gyro_settings.yen { ctrl_reg1 |= 0b010 };
if gyro_settings.zen { ctrl_reg1 |= 0b100 };
}
}
try!(gyro.smbus_write_byte_data(L3GD20_CTRL_REG1, ctrl_reg1));
let mut ctrl_reg4: u8 = 0_u8 | ((gyro_settings.sensitivity as u8) << 4);
if !gyro_settings.continuous_update {
ctrl_reg4 |= 0b10000000;
}
try!(gyro.smbus_write_byte_data(L3GD20_CTRL_REG4, ctrl_reg4));
let mut g_gain: f32;
match gyro_settings.sensitivity {
L3GD20GyroscopeFS::dps250 => {
g_gain = 8.75;
},
L3GD20GyroscopeFS::dps500 => {
g_gain = 17.50;
},
L3GD20GyroscopeFS::dps2000 => {
g_gain = 70.0;
}
}
Ok(L3GD20 {
gyroscope: gyro,
g_gain: g_gain / 1000.0,
})
}
#[cfg(any(target_os = "linux", target_os = "android"))]
fn read_gyroscope_raw(&mut self) -> Result<(i16, i16, i16), T::Error> {
let mut buf: [u8; 6] = [0;6];
self.gyroscope.write(&[L3GD20_INCREMENT_BIT | L3GD20_OUT]);
self.gyroscope.read(&mut buf);
let x_raw = LittleEndian::read_i16(&buf[0..2]);
let y_raw = LittleEndian::read_i16(&buf[2..4]);
let z_raw = LittleEndian::read_i16(&buf[4..6]);
Ok((x_raw, y_raw, z_raw))
}
}
impl<T> Gyroscope for L3GD20<T>
where T: I2CDevice + Sized
{
type Error = T::Error;
#[cfg(not(any(target_os = "linux", target_os = "android")))]
fn angular_rate_reading(&mut self) -> Result<Vec3, T::Error> {
Ok(Vec3::zeros())
}
#[cfg(any(target_os = "linux", target_os = "android"))]
fn angular_rate_reading(&mut self) -> Result<Vec3, T::Error> {
let (x_raw, y_raw, z_raw) = try!(self.read_gyroscope_raw());
let angular_velocity = Vec3 {
x: (x_raw as f32) * self.g_gain,
y: (y_raw as f32) * self.g_gain,
z: (z_raw as f32) * self.g_gain
};
Ok(angular_velocity)
}
}