forked from cyoung/linux-mpu9150
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibimu.c
181 lines (152 loc) · 3.89 KB
/
libimu.c
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
////////////////////////////////////////////////////////////////////////////
//
// This file is part of linux-mpu9150
//
// Copyright (c) 2013 Pansenti, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <getopt.h>
#include <errno.h>
#include "mpu9150.h"
#include "linux_glue.h"
#include "libimu.h"
int i2c_bus;
int sample_rate;
int yaw_mix_factor;
int verbose;
void close_mpu(void)
{
mpu9150_exit();
}
int init_mpu(int sample_rate, int yaw_mix_factor)
{
int ret;
verbose = 1;
i2c_bus = DEFAULT_I2C_BUS;
// sample_rate = DEFAULT_SAMPLE_RATE_HZ;
// yaw_mix_factor = DEFAULT_YAW_MIX_FACTOR;
mpu9150_set_debug(verbose);
if ((ret = mpu9150_init(i2c_bus, sample_rate, yaw_mix_factor)) != 0) {
return ret;
}
set_cal(0, NULL);
set_cal(1, NULL);
return ret;
}
int enableFusion(void) {
if (enableAccelerometerFusion()) {
return -1;
}
return 0;
}
int disableFusion(void) {
if (disableAccelerometerFusion()) {
return -1;
}
return 0;
}
int read_mpu(float *pitch, float *roll, float *heading)
{
int ret;
static mpudata_t mpu;
memset(&mpu, 0, sizeof(mpudata_t));
ret = mpu9150_read(&mpu);
*pitch = mpu.fusedEuler[0];
*roll = mpu.fusedEuler[1];
*heading = mpu.fusedEuler[2];
return ret;
}
int read_mpu_raw(float *gx, float *gy, float *gz, float *ax, float *ay, float *az, float *mx, float *my, float *mz)
{
int ret;
static mpudata_t mpu;
memset(&mpu, 0, sizeof(mpudata_t));
ret = mpu9150_read(&mpu);
*gx = mpu.rawGyro[0];
*gy = mpu.rawGyro[1];
*gz = mpu.rawGyro[2];
*ax = mpu.rawAccel[0];
*ay = mpu.rawAccel[1];
*az = mpu.rawAccel[2];
*mx = mpu.rawMag[0];
*my = mpu.rawMag[1];
*mz = mpu.rawMag[2];
return ret;
}
int set_cal(int mag, char *cal_file)
{
int i;
FILE *f;
char buff[32];
long val[6];
caldata_t cal;
if (cal_file) {
f = fopen(cal_file, "r");
if (!f) {
perror("open(<cal-file>)");
return -1;
}
}
else {
if (mag) {
f = fopen("./magcal.txt", "r");
if (!f) {
printf("Default magcal.txt not found\n");
return 0;
}
}
else {
f = fopen("./accelcal.txt", "r");
if (!f) {
printf("Default accelcal.txt not found\n");
return 0;
}
}
}
memset(buff, 0, sizeof(buff));
for (i = 0; i < 6; i++) {
if (!fgets(buff, 20, f)) {
printf("Not enough lines in calibration file\n");
break;
}
val[i] = atoi(buff);
if (val[i] == 0) {
printf("Invalid cal value: %s\n", buff);
break;
}
}
fclose(f);
if (i != 6)
return -1;
cal.offset[0] = (short)((val[0] + val[1]) / 2);
cal.offset[1] = (short)((val[2] + val[3]) / 2);
cal.offset[2] = (short)((val[4] + val[5]) / 2);
cal.range[0] = (short)(val[1] - cal.offset[0]);
cal.range[1] = (short)(val[3] - cal.offset[1]);
cal.range[2] = (short)(val[5] - cal.offset[2]);
if (mag)
mpu9150_set_mag_cal(&cal);
else
mpu9150_set_accel_cal(&cal);
return 0;
}