|
@@ -0,0 +1,155 @@
|
|
1
|
+/*
|
|
2
|
+ * Author: Mihai Tudor Panu <mihai.t.panu@intel.com>
|
|
3
|
+ * Copyright (c) 2014 Intel Corporation.
|
|
4
|
+ *
|
|
5
|
+ * Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+ * a copy of this software and associated documentation files (the
|
|
7
|
+ * "Software"), to deal in the Software without restriction, including
|
|
8
|
+ * without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+ * distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+ * permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+ * the following conditions:
|
|
12
|
+ *
|
|
13
|
+ * The above copyright notice and this permission notice shall be
|
|
14
|
+ * included in all copies or substantial portions of the Software.
|
|
15
|
+ *
|
|
16
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+ */
|
|
24
|
+
|
|
25
|
+#include <iostream>
|
|
26
|
+#include <unistd.h>
|
|
27
|
+#include "math.h"
|
|
28
|
+#include "itg3200.h"
|
|
29
|
+
|
|
30
|
+#define READ_BUFFER_LENGTH 8
|
|
31
|
+
|
|
32
|
+//address and id
|
|
33
|
+#define ITG3200_I2C_ADDR 0x68
|
|
34
|
+#define ITG3200_ID 0x00
|
|
35
|
+
|
|
36
|
+//configuration registers
|
|
37
|
+#define ITG3200_SMPLRT_DIV 0x15
|
|
38
|
+#define ITG3200_DLPF_FS 0x16
|
|
39
|
+
|
|
40
|
+//interrupt registers
|
|
41
|
+#define ITG3200_INT_CFG 0x17
|
|
42
|
+#define ITG3200_INT_STATUS 0x1A
|
|
43
|
+
|
|
44
|
+//data registers (read only)
|
|
45
|
+#define ITG3200_TEMP_H 0x1B
|
|
46
|
+#define ITG3200_TEMP_L 0x1C
|
|
47
|
+#define ITG3200_XOUT_H 0x1D
|
|
48
|
+#define ITG3200_XOUT_L 0x1E
|
|
49
|
+#define ITG3200_YOUT_H 0x1F
|
|
50
|
+#define ITG3200_YOUT_L 0x20
|
|
51
|
+#define ITG3200_ZOUT_H 0x21
|
|
52
|
+#define ITG3200_ZOUT_L 0x22
|
|
53
|
+#define DATA_REG_SIZE 8
|
|
54
|
+
|
|
55
|
+//power management
|
|
56
|
+#define ITG3200_PWR_MGM 0x3E
|
|
57
|
+
|
|
58
|
+//useful values
|
|
59
|
+#define ITG3200_RESET 0x80
|
|
60
|
+#define ITG3200_SLEEP 0x40
|
|
61
|
+#define ITG3200_WAKEUP 0x00
|
|
62
|
+
|
|
63
|
+using namespace upm;
|
|
64
|
+
|
|
65
|
+Itg3200::Itg3200(int bus)
|
|
66
|
+{
|
|
67
|
+ //init bus and reset chip
|
|
68
|
+ m_i2c = mraa_i2c_init(bus);
|
|
69
|
+
|
|
70
|
+ mraa_i2c_address(m_i2c, ITG3200_I2C_ADDR);
|
|
71
|
+ m_buffer[0] = ITG3200_PWR_MGM;
|
|
72
|
+ m_buffer[1] = ITG3200_RESET;
|
|
73
|
+ mraa_i2c_write(m_i2c, m_buffer, 2);
|
|
74
|
+
|
|
75
|
+ Itg3200::calibrate();
|
|
76
|
+ Itg3200::update();
|
|
77
|
+}
|
|
78
|
+
|
|
79
|
+Itg3200::~Itg3200()
|
|
80
|
+{
|
|
81
|
+ mraa_i2c_stop(m_i2c);
|
|
82
|
+}
|
|
83
|
+
|
|
84
|
+mraa_result_t
|
|
85
|
+Itg3200::calibrate(void)
|
|
86
|
+{
|
|
87
|
+ int reads = 600;
|
|
88
|
+ int delay = 4000; // 4 milliseconds
|
|
89
|
+ int skip = 5; // initial samples to skip
|
|
90
|
+ int temp[3] = {0};
|
|
91
|
+
|
|
92
|
+ for(int i = 0; i < reads; i++){
|
|
93
|
+
|
|
94
|
+ Itg3200::update();
|
|
95
|
+ if (i > skip){
|
|
96
|
+ for (int j = 0; j < 3; j++){
|
|
97
|
+ temp[j] += m_rotation[j];
|
|
98
|
+ }
|
|
99
|
+ }
|
|
100
|
+ usleep(delay);
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ for(int i = 0; i < 3; i++){
|
|
104
|
+ m_offsets[i] = (-1) * temp[i] / (reads - skip);
|
|
105
|
+ }
|
|
106
|
+}
|
|
107
|
+
|
|
108
|
+float
|
|
109
|
+Itg3200::getTemperature()
|
|
110
|
+{
|
|
111
|
+ return 35.0 + (m_temperature + 13200.0) / 280.0;
|
|
112
|
+}
|
|
113
|
+
|
|
114
|
+float*
|
|
115
|
+Itg3200::getRotation()
|
|
116
|
+{
|
|
117
|
+ for(int i = 0; i < 3; i++){
|
|
118
|
+ m_angle[i] = m_rotation[i]/14.375;
|
|
119
|
+ }
|
|
120
|
+ return &m_angle[0];
|
|
121
|
+}
|
|
122
|
+
|
|
123
|
+int16_t*
|
|
124
|
+Itg3200::getRawValues()
|
|
125
|
+{
|
|
126
|
+ return &m_rotation[0];
|
|
127
|
+}
|
|
128
|
+
|
|
129
|
+int16_t
|
|
130
|
+Itg3200::getRawTemp()
|
|
131
|
+{
|
|
132
|
+ return m_temperature;
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+mraa_result_t
|
|
136
|
+Itg3200::update(void)
|
|
137
|
+{
|
|
138
|
+ mraa_i2c_address(m_i2c, ITG3200_I2C_ADDR);
|
|
139
|
+ mraa_i2c_write_byte(m_i2c, ITG3200_TEMP_H);
|
|
140
|
+
|
|
141
|
+ mraa_i2c_address(m_i2c, ITG3200_I2C_ADDR);
|
|
142
|
+ mraa_i2c_read(m_i2c, m_buffer, DATA_REG_SIZE);
|
|
143
|
+
|
|
144
|
+ //temp
|
|
145
|
+ //
|
|
146
|
+ m_temperature = (m_buffer[0] << 8 ) | m_buffer[1];
|
|
147
|
+ // x
|
|
148
|
+ m_rotation[0] = ((m_buffer[2] << 8 ) | m_buffer[3]) + m_offsets[0];
|
|
149
|
+ // y
|
|
150
|
+ m_rotation[1] = ((m_buffer[4] << 8 ) | m_buffer[5]) + m_offsets[1];
|
|
151
|
+ // z
|
|
152
|
+ m_rotation[2] = ((m_buffer[6] << 8 ) | m_buffer[7]) + m_offsets[2];
|
|
153
|
+
|
|
154
|
+ return MRAA_SUCCESS;
|
|
155
|
+}
|