COSA
An Object-Oriented Platform for Arduino Programming
MPU6050.cpp
Go to the documentation of this file.
1 
21 #include "MPU6050.hh"
22 
23 void
24 MPU6050::write(Register reg, uint8_t value)
25 {
26  twi.acquire(this);
27  twi.write((uint8_t) reg, &value, sizeof(value));
28  twi.release();
29 }
30 
31 void
32 MPU6050::write(Register reg, void* buffer, size_t count)
33 {
34  twi.acquire(this);
35  twi.write((uint8_t) reg, buffer, count);
36  twi.release();
37 }
38 
39 uint8_t
41 {
42  uint8_t res;
43  twi.acquire(this);
44  twi.write((uint8_t) reg);
45  twi.read(&res, sizeof(res));
46  twi.release();
47  return (res);
48 }
49 
50 void
51 MPU6050::read(Register reg, void* buffer, size_t count)
52 {
53  twi.acquire(this);
54  twi.write((uint8_t) reg);
55  twi.read(buffer, count);
56  twi.release();
57 }
58 
59 bool
60 MPU6050::begin(uint8_t clksel)
61 {
62  // Sanity check the identity register value
63  if (read(WHO_AM_I) != 0x68) return (false);
64 
65  // Set clock source (default PLL with X axis gyroscope reference)
66  pwr_mgmt_1_t pwr;
67  pwr.CLKSEL = clksel;
68  write(PWR_MGMT_1, pwr);
69 
70  // Set accelerometer full range (default +/-2 g)
71  accel_config_t accel;
72  accel.AFS_SEL = AFS_RANGE_2G;
73  write(ACCEL_CONFIG, accel);
74 
75  // Set gyroscope full range (default +/-250 dps)
76  gyro_config_t gyro;
77  gyro.FS_SEL = FS_RANGE_250;
78  write(GYRO_CONFIG, gyro);
79 
80  return (true);
81 }
82 
83 bool
85 {
86  return (true);
87 }
88 
89 int16_t
91 {
92  int16_t temperature;
93  read(TEMP_OUT, &temperature, sizeof(temperature));
94  return (swap(temperature) + 12410) / 34;
95 }
96 
97 void
99 {
100  read(ACCEL_OUT, &m, sizeof(m));
101  swap<motion_t>(&m);
102  m.temp = (m.temp + 12410) / 34;
103 }
104 
105 void
107 {
108  read(ACCEL_OUT, &s, sizeof(s));
109  swap<sample_t>(&s);
110 }
111 
112 void
114 {
115  read(GYRO_OUT, &s, sizeof(s));
116  swap<sample_t>(&s);
117 }
118 
119 IOStream&
121 {
122  MPU6050::motion_t value;
123  mpu.read_motion(value);
124  outs << PSTR("MPU6050::accelerometer(x = ") << value.accel.x
125  << PSTR(", y = ") << value.accel.y
126  << PSTR(", z = ") << value.accel.z
127  << PSTR(")") << endl;
128  outs << PSTR("MPU6050::temperature = ") << value.temp << endl;
129  outs << PSTR("MPU6050::gyroscope(x = ") << value.gyro.x
130  << PSTR(", y = ") << value.gyro.y
131  << PSTR(", z = ") << value.gyro.z
132  << PSTR(")") << endl;
133  return (outs);
134 }
bool begin(uint8_t clksel=CLKSEL_PLL_GYRO_X_REF)
Definition: MPU6050.cpp:60
TWI twi
Definition: TWI.cpp:27
Temperature Measurement.
Definition: MPU6050.hh:167
bool end()
Definition: MPU6050.cpp:84
Who Am I.
Definition: MPU6050.hh:194
#define PSTR(s)
Definition: Types.h:202
uint8_t read(Register reg)
Definition: MPU6050.cpp:40
void read_gyroscope(sample_t &s)
Definition: MPU6050.cpp:113
void write(Register reg, uint8_t value)
Definition: MPU6050.cpp:24
sample_t accel
Definition: MPU6050.hh:96
Gyroscope Configuration.
Definition: MPU6050.hh:132
void release()
Definition: TWI.cpp:58
IOStream & operator<<(IOStream &outs, MPU6050 &mpu)
Definition: MPU6050.cpp:120
int read(void *buf, size_t size)
Definition: TWI.hh:326
#define swap(a, b)
Definition: Canvas.cpp:164
int write(void *buf, size_t size)
Definition: TWI.hh:282
Accelerometer Configuration.
Definition: MPU6050.hh:133
IOStream & endl(IOStream &outs)
Definition: IOStream.hh:817
Accelerometer Measurements.
Definition: MPU6050.hh:157
Gyroscope Measurement.
Definition: MPU6050.hh:170
int16_t temp
Definition: MPU6050.hh:97
void read_motion(motion_t &m)
Definition: MPU6050.cpp:98
void acquire(TWI::Driver *dev)
Definition: TWI.cpp:36
Power Management 1.
Definition: MPU6050.hh:188
uint8_t FS_SEL
Full Scale Range.
Definition: MPU6050.hh:224
int16_t read_temperature()
Definition: MPU6050.cpp:90
uint8_t CLKSEL
< As bitfields.
Definition: MPU6050.hh:427
sample_t gyro
Definition: MPU6050.hh:98
uint8_t AFS_SEL
Full Scale Range.
Definition: MPU6050.hh:256
void read_accelerometer(sample_t &s)
Definition: MPU6050.cpp:106