summaryrefslogtreecommitdiff
path: root/common/mag_cal.c
blob: 1e71059921d2c95aa1721f0ec077aa69db3cd84c (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Copyright 2015 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "common.h"
#include "console.h"
#include "mag_cal.h"
#include "mat33.h"
#include "mat44.h"

#include "math.h"
#include "math_util.h"
#include "util.h"

/* Data from sensor is in 16th of uT */
#define MAG_CAL_RAW_UT      16

#define MAX_EIGEN_RATIO     FLOAT_TO_FP(25.0f)
#define MAX_EIGEN_MAG       FLOAT_TO_FP(80.0f * MAG_CAL_RAW_UT)
#define MIN_EIGEN_MAG       FLOAT_TO_FP(10.0f * MAG_CAL_RAW_UT)

#define MAX_FIT_MAG         MAX_EIGEN_MAG
#define MIN_FIT_MAG         MIN_EIGEN_MAG

#define CPRINTF(format, args...) cprintf(CC_ACCEL, format, ## args)
#define PRINTF_FLOAT(x)  ((int)((x) * 100.0f))

/*
 * eigen value magnitude and ratio test
 *
 * Using the magnetometer information, caculate the 3 eigen values/vectors
 * for the transformation. Check the eigen values are sane.
 */
static int moc_eigen_test(struct mag_cal_t *moc)
{
	mat33_fp_t S;
	fpv3_t eigenvals;
	mat33_fp_t eigenvecs;
	fp_t evmax, evmin, evmag;
	int eigen_pass;

	/* covariance matrix */
	S[0][0] = moc->acc[0][0] - fp_sq(moc->acc[0][3]);
	S[0][1] = S[1][0] =
		moc->acc[0][1] - fp_mul(moc->acc[0][3], moc->acc[1][3]);
	S[0][2] = S[2][0] =
		moc->acc[0][2] - fp_mul(moc->acc[0][3], moc->acc[2][3]);
	S[1][1] = moc->acc[1][1] - fp_sq(moc->acc[1][3]);
	S[1][2] = S[2][1] =
		moc->acc[1][2] - fp_mul(moc->acc[1][3], moc->acc[2][3]);
	S[2][2] = moc->acc[2][2] - fp_sq(moc->acc[2][3]);

	mat33_fp_get_eigenbasis(S, eigenvals, eigenvecs);

	evmax = (eigenvals[X] > eigenvals[Y]) ? eigenvals[X] : eigenvals[Y];
	evmax = (eigenvals[Z] > evmax) ? eigenvals[Z] : evmax;

	evmin = (eigenvals[X] < eigenvals[Y]) ? eigenvals[X] : eigenvals[Y];
	evmin = (eigenvals[Z] < evmin) ? eigenvals[Z] : evmin;

	evmag = fp_sqrtf(eigenvals[X] + eigenvals[Y] + eigenvals[Z]);

	eigen_pass = (fp_mul(evmin, MAX_EIGEN_RATIO) > evmax)
		&& (evmag > MIN_EIGEN_MAG)
		&& (evmag < MAX_EIGEN_MAG);

#if 0
	CPRINTF("mag eigenvalues: (%d %d %d), ",
		PRINTF_FLOAT(eigenvals[X]),
		PRINTF_FLOAT(eigenvals[Y]),
		PRINTF_FLOAT(eigenvals[Z]));

	CPRINTF("ratio %d, mag %d: pass %d\r\n",
		PRINTF_FLOAT(evmax / evmin),
		PRINTF_FLOAT(evmag),
		PRINTF_FLOAT(eigen_pass));
#endif

	return eigen_pass;
}

/*
 * Kasa sphere fitting with normal equation
 */
static int moc_fit(struct mag_cal_t *moc, fpv3_t bias, fp_t *radius)
{
	sizev4_t pivot;
	fpv4_t out;
	int success = 0;

	/*
	 * To reduce stack size, moc->acc is A,
	 * moc->acc_w is b: we are looking for out, where:
	 *
	 *    A    *   out   =    b
	 * (4 x 4)   (4 x 1)   (4 x 1)
	 */
	/* complete the matrix: */
	moc->acc[1][0] = moc->acc[0][1];
	moc->acc[2][0] = moc->acc[0][2];
	moc->acc[2][1] = moc->acc[1][2];
	moc->acc[3][0] = moc->acc[0][3];
	moc->acc[3][1] = moc->acc[1][3];
	moc->acc[3][2] = moc->acc[2][3];
	moc->acc[3][3] = FLOAT_TO_FP(1.0f);

	moc->acc_w[X] = fp_mul(moc->acc_w[X], FLOAT_TO_FP(-1));
	moc->acc_w[Y] = fp_mul(moc->acc_w[Y], FLOAT_TO_FP(-1));
	moc->acc_w[Z] = fp_mul(moc->acc_w[Z], FLOAT_TO_FP(-1));
	moc->acc_w[W] = fp_mul(moc->acc_w[W], FLOAT_TO_FP(-1));

	mat44_fp_decompose_lup(moc->acc, pivot);

	mat44_fp_solve(moc->acc, out, moc->acc_w, pivot);

	/*
	 * spherei is defined by:
	 * (x - xc)^2 + (y - yc)^2 + (z - zc)^2 = r^2
	 *
	 * Where r is:
	 * xc = -out[X] / 2, yc = -out[Y] / 2, zc = -out[Z] / 2
	 * r = sqrt(xc^2 + yc^2 + zc^2 - out[W])
	 */

	memcpy(bias, out, sizeof(fpv3_t));
	fpv3_scalar_mul(bias, FLOAT_TO_FP(-0.5f));

	*radius = fp_sqrtf(fpv3_dot(bias, bias) - out[W]);

#if 0
	CPRINTF("mag cal: bias (%d, %d, %d), R %d uT\n",
		PRINTF_FLOAT(bias[X] / MAG_CAL_RAW_UT),
		PRINTF_FLOAT(bias[Y] / MAG_CAL_RAW_UT),
		PRINTF_FLOAT(bias[Z] / MAG_CAL_RAW_UT),
		PRINTF_FLOAT(*radius / MAG_CAL_RAW_UT));
#endif

	/* TODO (menghsuan): bound on bias as well? */
	if (*radius > MIN_FIT_MAG && *radius < MAX_FIT_MAG)
		success = 1;

	return success;
}

void init_mag_cal(struct mag_cal_t *moc)
{
	memset(moc->acc, 0, sizeof(moc->acc));
	memset(moc->acc_w, 0, sizeof(moc->acc_w));
	moc->nsamples = 0;
}

int mag_cal_update(struct mag_cal_t *moc, const intv3_t v)
{
	int new_bias = 0;

	/* 1. run accumulators */
	fp_t w = fp_sq(v[X]) + fp_sq(v[Y]) + fp_sq(v[Z]);

	moc->acc[0][3] += v[X];
	moc->acc[1][3] += v[Y];
	moc->acc[2][3] += v[Z];
	moc->acc_w[W] += w;

	moc->acc[0][0] += fp_sq(v[X]);
	moc->acc[0][1] += fp_mul(v[X], v[Y]);
	moc->acc[0][2] += fp_mul(v[X], v[Z]);
	moc->acc_w[X] += fp_mul(v[X], w);

	moc->acc[1][1] += fp_sq(v[Y]);
	moc->acc[1][2] += fp_mul(v[Y], v[Z]);
	moc->acc_w[Y] += fp_mul(v[Y], w);

	moc->acc[2][2] += fp_sq(v[Z]);
	moc->acc_w[Z] += fp_mul(v[Z], w);

	if (moc->nsamples < MAG_CAL_MAX_SAMPLES)
		moc->nsamples++;

	/* 2. batch has enough samples? */
	if (moc->batch_size > 0 && moc->nsamples >= moc->batch_size) {
		fp_t inv = fp_div_dbz(FLOAT_TO_FP(1.0f),
				  INT_TO_FP((int)moc->nsamples));

		moc->acc[0][3] = fp_mul(moc->acc[0][3], inv);
		moc->acc[1][3] = fp_mul(moc->acc[1][3], inv);
		moc->acc[2][3] = fp_mul(moc->acc[2][3], inv);
		moc->acc_w[W] = fp_mul(moc->acc_w[W], inv);

		moc->acc[0][0] = fp_mul(moc->acc[0][0], inv);
		moc->acc[0][1] = fp_mul(moc->acc[0][1], inv);
		moc->acc[0][2] = fp_mul(moc->acc[0][2], inv);
		moc->acc_w[X] = fp_mul(moc->acc_w[X], inv);

		moc->acc[1][1] = fp_mul(moc->acc[1][1], inv);
		moc->acc[1][2] = fp_mul(moc->acc[1][2], inv);
		moc->acc_w[Y] = fp_mul(moc->acc_w[Y], inv);

		moc->acc[2][2] = fp_mul(moc->acc[2][2], inv);
		moc->acc_w[Z] = fp_mul(moc->acc_w[Z], inv);

		/* 3. eigen test */
		if (moc_eigen_test(moc)) {
			fpv3_t bias;
			fp_t radius;

			/* 4. Kasa sphere fitting */
			if (moc_fit(moc, bias, &radius)) {

				moc->bias[X] = fp_mul(bias[X], FLOAT_TO_FP(-1));
				moc->bias[Y] = fp_mul(bias[Y], FLOAT_TO_FP(-1));
				moc->bias[Z] = fp_mul(bias[Z], FLOAT_TO_FP(-1));

				moc->radius = radius;

				new_bias = 1;
			}
		}
		/* 5. reset for next batch */
		init_mag_cal(moc);
	}

	return new_bias;
}