mag_calibration.cpp 8.16 KB
Newer Older
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
/****************************************************************************
 *
 *   Copyright (C) 2013 PX4 Development Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/**
35
 * @file mag_calibration.cpp
36
 *
37
38
39
40
41
42
 * Magnetometer calibration routine
 */

#include "mag_calibration.h"
#include "commander_helper.h"
#include "calibration_routines.h"
43
#include "calibration_messages.h"
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <poll.h>
#include <math.h>
#include <fcntl.h>
#include <drivers/drv_hrt.h>
#include <uORB/topics/sensor_combined.h>
#include <drivers/drv_mag.h>
#include <mavlink/mavlink_log.h>
#include <systemlib/param/param.h>
#include <systemlib/err.h>

58
59
60
61
62
/* oddly, ERROR is not defined for c++ */
#ifdef ERROR
# undef ERROR
#endif
static const int ERROR = -1;
63

64
65
static const char *sensor_name = "mag";

66
int do_mag_calibration(int mavlink_fd)
67
{
hauptmech's avatar
hauptmech committed
68
	int32_t device_id;
69
70
	mavlink_log_info(mavlink_fd, CAL_STARTED_MSG, sensor_name);
	mavlink_log_info(mavlink_fd, "don't move system");
71
72
73
74

	/* 45 seconds */
	uint64_t calibration_interval = 45 * 1000 * 1000;

75
	/* maximum 500 values */
76
	const unsigned int calibration_maxcount = 240;
Lorenz Meier's avatar
Lorenz Meier committed
77
	unsigned int calibration_counter;
78
79
80
81
82
83
84
85
86
87

	struct mag_scale mscale_null = {
		0.0f,
		1.0f,
		0.0f,
		1.0f,
		0.0f,
		1.0f,
	};

88
	int res = OK;
89

90
91
	/* erase old calibration */
	int fd = open(MAG_DEVICE_PATH, O_RDONLY);
hauptmech's avatar
hauptmech committed
92
93
94

	device_id = ioctl(fd, DEVIOCGDEVICEID, 0);

95
	res = ioctl(fd, MAGIOCSSCALE, (long unsigned int)&mscale_null);
96

97
98
99
	if (res != OK) {
		mavlink_log_critical(mavlink_fd, CAL_FAILED_RESET_CAL_MSG);
	}
100

101
102
103
	if (res == OK) {
		/* calibrate range */
		res = ioctl(fd, MAGIOCCALIBRATE, fd);
104

105
		if (res != OK) {
Lorenz Meier's avatar
Lorenz Meier committed
106
107
108
			mavlink_log_critical(mavlink_fd, "Skipped scale calibration");
			/* this is non-fatal - mark it accordingly */
			res = OK;
109
110
		}
	}
111

112
	close(fd);
113

Lorenz Meier's avatar
Lorenz Meier committed
114
115
116
	float *x = NULL;
	float *y = NULL;
	float *z = NULL;
117

118
119
120
	if (res == OK) {
		/* allocate memory */
		mavlink_log_info(mavlink_fd, CAL_PROGRESS_MSG, sensor_name, 20);
121

Lorenz Meier's avatar
Lorenz Meier committed
122
123
124
		x = reinterpret_cast<float *>(malloc(sizeof(float) * calibration_maxcount));
		y = reinterpret_cast<float *>(malloc(sizeof(float) * calibration_maxcount));
		z = reinterpret_cast<float *>(malloc(sizeof(float) * calibration_maxcount));
125

126
127
		if (x == NULL || y == NULL || z == NULL) {
			mavlink_log_critical(mavlink_fd, "ERROR: out of memory");
128
129
130
131
132
133
134
135
136
137
138
139
140
141

			/* clean up */
			if (x != NULL) {
				free(x);
			}

			if (y != NULL) {
				free(y);
			}

			if (z != NULL) {
				free(z);
			}

142
			res = ERROR;
Lorenz Meier's avatar
Lorenz Meier committed
143
			return res;
144
		}
145

Lorenz Meier's avatar
Lorenz Meier committed
146
147
148
	} else {
		/* exit */
		return ERROR;
149
150
	}

151
	if (res == OK) {
152
		int sub_mag = orb_subscribe(ORB_ID(sensor_mag0));
Lorenz Meier's avatar
Lorenz Meier committed
153

154
155
156
157
158
159
160
161
		if (sub_mag < 0) {
			mavlink_log_critical(mavlink_fd, "No mag found, abort");
			res = ERROR;
		}  else {
			struct mag_report mag;

			/* limit update rate to get equally spaced measurements over time (in ms) */
			orb_set_interval(sub_mag, (calibration_interval / 1000) / calibration_maxcount);
Lorenz Meier's avatar
Lorenz Meier committed
162

163
164
165
			/* calibrate offsets */
			uint64_t calibration_deadline = hrt_absolute_time() + calibration_interval;
			unsigned poll_errcount = 0;
166

167
			mavlink_log_info(mavlink_fd, "Turn on all sides: front/back,left/right,up/down");
168

169
			calibration_counter = 0U;
Lorenz Meier's avatar
Lorenz Meier committed
170

171
172
			while (hrt_absolute_time() < calibration_deadline &&
			       calibration_counter < calibration_maxcount) {
173

174
175
176
177
				/* wait blocking for new data */
				struct pollfd fds[1];
				fds[0].fd = sub_mag;
				fds[0].events = POLLIN;
178

179
				int poll_ret = poll(fds, 1, 1000);
180

181
182
				if (poll_ret > 0) {
					orb_copy(ORB_ID(sensor_mag0), sub_mag, &mag);
183

184
185
186
					x[calibration_counter] = mag.x;
					y[calibration_counter] = mag.y;
					z[calibration_counter] = mag.z;
187

188
					calibration_counter++;
189

190
191
192
193
194
195
					if (calibration_counter % (calibration_maxcount / 20) == 0) {
						mavlink_log_info(mavlink_fd, CAL_PROGRESS_MSG, sensor_name, 20 + (calibration_counter * 50) / calibration_maxcount);
					}

				} else {
					poll_errcount++;
196
				}
197

198
199
200
201
202
				if (poll_errcount > 1000) {
					mavlink_log_critical(mavlink_fd, CAL_FAILED_SENSOR_MSG);
					res = ERROR;
					break;
				}
203
			}
204

205
			close(sub_mag);
206
207
208
209
210
211
212
213
		}
	}

	float sphere_x;
	float sphere_y;
	float sphere_z;
	float sphere_radius;

214
	if (res == OK && calibration_counter > (calibration_maxcount / 2)) {
Lorenz Meier's avatar
Lorenz Meier committed
215

216
217
218
219
		/* sphere fit */
		mavlink_log_info(mavlink_fd, CAL_PROGRESS_MSG, sensor_name, 70);
		sphere_fit_least_squares(x, y, z, calibration_counter, 100, 0.0f, &sphere_x, &sphere_y, &sphere_z, &sphere_radius);
		mavlink_log_info(mavlink_fd, CAL_PROGRESS_MSG, sensor_name, 80);
220

221
222
223
224
225
		if (!isfinite(sphere_x) || !isfinite(sphere_y) || !isfinite(sphere_z)) {
			mavlink_log_critical(mavlink_fd, "ERROR: NaN in sphere fit");
			res = ERROR;
		}
	}
226

227
	if (x != NULL) {
228
		free(x);
229
	}
230

231
	if (y != NULL) {
232
		free(y);
233
	}
234

235
	if (z != NULL) {
236
		free(z);
237
	}
238

239
240
241
	if (res == OK) {
		/* apply calibration and set parameters */
		struct mag_scale mscale;
242

243
244
		fd = open(MAG_DEVICE_PATH, 0);
		res = ioctl(fd, MAGIOCGSCALE, (long unsigned int)&mscale);
245

246
247
248
		if (res != OK) {
			mavlink_log_critical(mavlink_fd, "ERROR: failed to get current calibration");
		}
249

250
251
252
253
		if (res == OK) {
			mscale.x_offset = sphere_x;
			mscale.y_offset = sphere_y;
			mscale.z_offset = sphere_z;
254

255
			res = ioctl(fd, MAGIOCSSCALE, (long unsigned int)&mscale);
256

257
258
259
			if (res != OK) {
				mavlink_log_critical(mavlink_fd, CAL_FAILED_APPLY_CAL_MSG);
			}
260
261
		}

262
		close(fd);
263

264
265
		if (res == OK) {
			/* set parameters */
hauptmech's avatar
hauptmech committed
266
267
268
			if (param_set(param_find("SENS_MAG_ID"), &(device_id))) {
				res = ERROR;
			}
269
			if (param_set(param_find("SENS_MAG_XOFF"), &(mscale.x_offset))) {
270
				res = ERROR;
271
			}
272

273
			if (param_set(param_find("SENS_MAG_YOFF"), &(mscale.y_offset))) {
274
				res = ERROR;
275
			}
276

277
			if (param_set(param_find("SENS_MAG_ZOFF"), &(mscale.z_offset))) {
278
				res = ERROR;
279
			}
280

281
			if (param_set(param_find("SENS_MAG_XSCALE"), &(mscale.x_scale))) {
282
				res = ERROR;
283
			}
284

285
			if (param_set(param_find("SENS_MAG_YSCALE"), &(mscale.y_scale))) {
286
				res = ERROR;
287
			}
288

289
			if (param_set(param_find("SENS_MAG_ZSCALE"), &(mscale.z_scale))) {
290
				res = ERROR;
291
			}
292

293
294
295
			if (res != OK) {
				mavlink_log_critical(mavlink_fd, CAL_FAILED_SET_PARAMS_MSG);
			}
296

297
			mavlink_log_info(mavlink_fd, CAL_PROGRESS_MSG, sensor_name, 90);
298
299
		}

300
301
302
		if (res == OK) {
			/* auto-save to EEPROM */
			res = param_save_default();
303

304
305
306
307
			if (res != OK) {
				mavlink_log_critical(mavlink_fd, CAL_FAILED_SAVE_PARAMS_MSG);
			}
		}
308

309
310
311
312
		mavlink_log_info(mavlink_fd, "mag off: x:%.2f y:%.2f z:%.2f Ga", (double)mscale.x_offset,
				 (double)mscale.y_offset, (double)mscale.z_offset);
		mavlink_log_info(mavlink_fd, "mag scale: x:%.2f y:%.2f z:%.2f", (double)mscale.x_scale,
				 (double)mscale.y_scale, (double)mscale.z_scale);
313

314
315
		if (res == OK) {
			mavlink_log_info(mavlink_fd, CAL_DONE_MSG, sensor_name);
316

317
318
319
		} else {
			mavlink_log_info(mavlink_fd, CAL_FAILED_MSG, sensor_name);
		}
320
	}
Lorenz Meier's avatar
Lorenz Meier committed
321
322

	return res;
323
}