|
@@ -0,0 +1,156 @@
|
|
1
|
+/*
|
|
2
|
+ * Author: Brendan Le Foll <brendan.le.foll@intel.com>
|
|
3
|
+ * Copyright (c) 2014 Intel Corporation.
|
|
4
|
+ *
|
|
5
|
+ * Code based on LSM303DLH sample by Jim Lindblom SparkFun Electronics
|
|
6
|
+ * and the CompensatedCompass.ino by Frankie Chu from SeedStudio
|
|
7
|
+ *
|
|
8
|
+ * Permission is hereby granted, free of charge, to any person obtaining
|
|
9
|
+ * a copy of this software and associated documentation files (the
|
|
10
|
+ * "Software"), to deal in the Software without restriction, including
|
|
11
|
+ * without limitation the rights to use, copy, modify, merge, publish,
|
|
12
|
+ * distribute, sublicense, and/or sell copies of the Software, and to
|
|
13
|
+ * permit persons to whom the Software is furnished to do so, subject to
|
|
14
|
+ * the following conditions:
|
|
15
|
+ *
|
|
16
|
+ * The above copyright notice and this permission notice shall be
|
|
17
|
+ * included in all copies or substantial portions of the Software.
|
|
18
|
+ *
|
|
19
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
20
|
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
21
|
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
22
|
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
23
|
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
24
|
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
25
|
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
26
|
+ */
|
|
27
|
+
|
|
28
|
+#include <iostream>
|
|
29
|
+#include <unistd.h>
|
|
30
|
+#include <stdlib.h>
|
|
31
|
+
|
|
32
|
+#include "lsm303.h"
|
|
33
|
+
|
|
34
|
+using namespace upm;
|
|
35
|
+
|
|
36
|
+LSM303::LSM303(int bus, int addrMag, int addrAcc)
|
|
37
|
+{
|
|
38
|
+ mraa_result_t ret = MRAA_SUCCESS;
|
|
39
|
+
|
|
40
|
+ m_addrMag = addrMag;
|
|
41
|
+ m_addrAcc = addrAcc;
|
|
42
|
+
|
|
43
|
+ m_i2c = mraa_i2c_init(bus);
|
|
44
|
+
|
|
45
|
+ buf[0] = CTRL_REG1_A;
|
|
46
|
+ buf[1] = 0x27;
|
|
47
|
+ ret = mraa_i2c_address(m_i2c, m_addrAcc);
|
|
48
|
+ ret = mraa_i2c_write(m_i2c, buf, 2);
|
|
49
|
+
|
|
50
|
+ // 0x27 = normal power mode, all accel axes on
|
|
51
|
+ buf[0] = CTRL_REG1_A;
|
|
52
|
+ buf[1] = 0x27;
|
|
53
|
+ ret = mraa_i2c_address(m_i2c, m_addrAcc);
|
|
54
|
+ ret = mraa_i2c_write(m_i2c, buf, 2);
|
|
55
|
+
|
|
56
|
+ // scale == 2, can be 4 or 8
|
|
57
|
+ buf[0] = CTRL_REG4_A;
|
|
58
|
+ buf[1] = 0x00;
|
|
59
|
+ ret = mraa_i2c_address(m_i2c, m_addrAcc);
|
|
60
|
+ ret = mraa_i2c_write(m_i2c, buf, 2);
|
|
61
|
+
|
|
62
|
+ // 0x14 = mag 30Hz output rate
|
|
63
|
+ buf[0] = CRA_REG_M;
|
|
64
|
+ buf[1] = 0x14;
|
|
65
|
+ ret = mraa_i2c_address(m_i2c, m_addrMag);
|
|
66
|
+ ret = mraa_i2c_write(m_i2c, buf, 2);
|
|
67
|
+
|
|
68
|
+ // magnetic scale = +/-1.3Gaussmagnetic scale = +/-1.3Gauss
|
|
69
|
+ buf[0] = CRB_REG_M;
|
|
70
|
+ buf[1] = 0x20; // MAG_SCALE_1_3;
|
|
71
|
+ ret = mraa_i2c_address(m_i2c, m_addrMag);
|
|
72
|
+ ret = mraa_i2c_write(m_i2c, buf, 2);
|
|
73
|
+
|
|
74
|
+ // 0x00 = continouous conversion mode
|
|
75
|
+ buf[0] = MR_REG_M;
|
|
76
|
+ buf[1] = 0x00;
|
|
77
|
+ ret = mraa_i2c_address(m_i2c, m_addrMag);
|
|
78
|
+ ret = mraa_i2c_write(m_i2c, buf, 2);
|
|
79
|
+}
|
|
80
|
+
|
|
81
|
+LSM303::~LSM303() {
|
|
82
|
+ mraa_i2c_stop(m_i2c);
|
|
83
|
+}
|
|
84
|
+
|
|
85
|
+float
|
|
86
|
+LSM303::getHeading()
|
|
87
|
+{
|
|
88
|
+ if (getCoordinates() != MRAA_SUCCESS) {
|
|
89
|
+ return -1;
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ float heading = 180 * atan2(coor[Y], coor[X])/M_PI;
|
|
93
|
+
|
|
94
|
+ if (heading < 0)
|
|
95
|
+ heading += 360;
|
|
96
|
+
|
|
97
|
+ return heading;
|
|
98
|
+}
|
|
99
|
+
|
|
100
|
+uint8_t*
|
|
101
|
+LSM303::getRawAccelData()
|
|
102
|
+{
|
|
103
|
+ return &accel[0];
|
|
104
|
+}
|
|
105
|
+
|
|
106
|
+uint8_t*
|
|
107
|
+LSM303::getRawCoorData()
|
|
108
|
+{
|
|
109
|
+ return &coor[0];
|
|
110
|
+}
|
|
111
|
+
|
|
112
|
+mraa_result_t
|
|
113
|
+LSM303::getCoordinates()
|
|
114
|
+{
|
|
115
|
+ mraa_result_t ret = MRAA_SUCCESS;
|
|
116
|
+
|
|
117
|
+ memset(&buf[0], 0, sizeof(uint8_t)*6);
|
|
118
|
+ ret = mraa_i2c_address(m_i2c, m_addrMag);
|
|
119
|
+ ret = mraa_i2c_write_byte(m_i2c, OUT_X_H_M);
|
|
120
|
+ ret = mraa_i2c_address(m_i2c, m_addrMag);
|
|
121
|
+ int num = mraa_i2c_read(m_i2c, buf, 6);
|
|
122
|
+ if (num != 6) {
|
|
123
|
+ return ret;
|
|
124
|
+ }
|
|
125
|
+ // convert to coordinates
|
|
126
|
+ for (int i=0; i<3; i++) {
|
|
127
|
+ coor[i] = (buf[0*i] << 8) | buf[1*i];
|
|
128
|
+ }
|
|
129
|
+ // note that coor array is in XZY order
|
|
130
|
+ //printf("X=%x, Y=%x, Z=%x\n", coor[X], coor[Y], coor[Z]);
|
|
131
|
+
|
|
132
|
+ return ret;
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+// helper function that writes a value to the acc and then reads
|
|
136
|
+int
|
|
137
|
+LSM303::readThenWrite(uint8_t reg)
|
|
138
|
+{
|
|
139
|
+ mraa_i2c_address(m_i2c, m_addrAcc);
|
|
140
|
+ mraa_i2c_write_byte(m_i2c, reg);
|
|
141
|
+ mraa_i2c_address(m_i2c, m_addrAcc);
|
|
142
|
+ return (int) mraa_i2c_read_byte(m_i2c);
|
|
143
|
+}
|
|
144
|
+
|
|
145
|
+mraa_result_t
|
|
146
|
+LSM303::getAcceleration()
|
|
147
|
+{
|
|
148
|
+ mraa_result_t ret = MRAA_SUCCESS;
|
|
149
|
+
|
|
150
|
+ accel[2] = (readThenWrite(OUT_X_L_A) << 8) | (readThenWrite(OUT_X_H_A));
|
|
151
|
+ accel[0] = (readThenWrite(OUT_Y_L_A) << 8) | (readThenWrite(OUT_Y_H_A));
|
|
152
|
+ accel[1] = (readThenWrite(OUT_Z_L_A) << 8) | (readThenWrite(OUT_Z_H_A));
|
|
153
|
+ //printf("X=%x, Y=%x, Z=%x\n", accel[X], accel[Y], accel[Z]);
|
|
154
|
+
|
|
155
|
+ return ret;
|
|
156
|
+}
|