40 #include <px4_platform_common/px4_config.h> 46 #include <px4_platform_common/posix.h> 47 #include <px4_platform_common/defines.h> 48 #include <px4_platform_common/time.h> 80 unsigned calibration_counter[
max_gyros] = { 0 }, slow_count = 0;
81 const unsigned calibration_count = 5000;
83 unsigned poll_errcount = 0;
88 for (
unsigned i = 0; i < 3; i++) {
97 for (
unsigned s = 0; s <
max_gyros; s++) {
99 fds[s].events = POLLIN;
106 while (slow_count < calibration_count) {
119 int poll_ret =
px4_poll(&fds[0], max_gyros, 1000);
122 unsigned update_count = calibration_count;
124 for (
unsigned s = 0; s <
max_gyros; s++) {
125 if (calibration_counter[s] >= calibration_count) {
174 calibration_counter[s]++;
179 if (calibration_counter[s] && calibration_counter[s] < update_count) {
180 update_count = calibration_counter[s];
185 if (update_count % (calibration_count / 20) == 0) {
190 if (slow_count < update_count) {
191 slow_count = update_count;
198 if (poll_errcount > 1000) {
204 for (
unsigned s = 0; s <
max_gyros; s++) {
205 if (worker_data->
device_id[s] != 0 && calibration_counter[s] < calibration_count / 2) {
229 gyro_scale_zero.
x_scale = 1.0f;
231 gyro_scale_zero.
y_scale = 1.0f;
233 gyro_scale_zero.
z_scale = 1.0f;
240 for (
unsigned s = 0; s <
max_gyros; s++) {
247 (void)sprintf(str,
"CAL_GYRO%u_ID", s);
256 (void)memcpy(&worker_data.
gyro_scale[s], &gyro_scale_zero,
sizeof(gyro_scale_zero));
273 (void)sprintf(str,
"CAL_GYRO%u_XOFF", s);
277 PX4_ERR(
"unable to reset %s", str);
280 (void)sprintf(str,
"CAL_GYRO%u_YOFF", s);
284 PX4_ERR(
"unable to reset %s", str);
287 (void)sprintf(str,
"CAL_GYRO%u_ZOFF", s);
291 PX4_ERR(
"unable to reset %s", str);
294 (void)sprintf(str,
"CAL_GYRO%u_XSCALE", s);
298 PX4_ERR(
"unable to reset %s", str);
301 (void)sprintf(str,
"CAL_GYRO%u_YSCALE", s);
305 PX4_ERR(
"unable to reset %s", str);
308 (void)sprintf(str,
"CAL_GYRO%u_ZSCALE", s);
312 PX4_ERR(
"unable to reset %s", str);
324 if (orb_gyro_count > max_gyros) {
325 calibration_log_critical(mavlink_log_pub,
"Detected %u gyros, but will calibrate only %u", orb_gyro_count, max_gyros);
328 for (
unsigned cur_gyro = 0; cur_gyro < orb_gyro_count && cur_gyro <
max_gyros; cur_gyro++) {
331 bool found_cur_gyro =
false;
333 for (
unsigned i = 0; i < orb_gyro_count && !found_cur_gyro; i++) {
345 if (report.device_id == (uint32_t)worker_data.
device_id[cur_gyro]) {
347 found_cur_gyro =
true;
356 worker_data.
device_id[cur_gyro] = report.device_id;
357 found_cur_gyro =
true;
362 if (!found_cur_gyro) {
369 if (worker_data.
device_id[cur_gyro] != 0) {
374 if (prio > device_prio_max) {
375 device_prio_max = prio;
376 device_id_primary = worker_data.
device_id[cur_gyro];
386 unsigned try_count = 0;
387 unsigned max_tries = 20;
409 const float maxoff = 0.01f;
414 fabsf(xdiff) > maxoff ||
415 fabsf(ydiff) > maxoff ||
416 fabsf(zdiff) > maxoff) {
428 }
while (res == PX4_ERROR && try_count <= max_tries);
430 if (try_count >= max_tries) {
437 for (
unsigned s = 0; s <
max_gyros; s++) {
448 bool tc_locked[3] = {
false};
450 for (
unsigned uorb_index = 0; uorb_index <
max_gyros; uorb_index++) {
451 if (worker_data.
device_id[uorb_index] != 0) {
455 int32_t tc_enabled_int;
458 if (tc_enabled_int == 1) {
461 memset(&sensor_correction, 0,
sizeof(sensor_correction));
465 if (!tc_locked[sensor_correction.
gyro_mapping[uorb_index]]) {
466 tc_locked[sensor_correction.
gyro_mapping[uorb_index]] =
true;
472 for (
unsigned axis_index = 0; axis_index < 3; axis_index++) {
474 (void)sprintf(str,
"TC_G%u_X0_%u", sensor_correction.
gyro_mapping[uorb_index], axis_index);
478 if (axis_index == 0) {
481 }
else if (axis_index == 1) {
484 }
else if (axis_index == 2) {
501 (void)sprintf(str,
"CAL_GYRO%u_XOFF", uorb_index);
503 (void)sprintf(str,
"CAL_GYRO%u_YOFF", uorb_index);
505 (void)sprintf(str,
"CAL_GYRO%u_ZOFF", uorb_index);
508 (void)sprintf(str,
"CAL_GYRO%u_ID", uorb_index);
#define CAL_ERROR_SENSOR_MSG
static orb_advert_t * mavlink_log_pub
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
Gyroscope driver interface.
static const char * sensor_name
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
gyro scaling factors; Vout = (Vin * Vscale) + Voffset
__EXPORT int param_set_no_notification(param_t param, const void *val)
Set the value of a parameter, but do not notify the system about the change.
Common calibration messages.
int do_gyro_calibration(orb_advert_t *mavlink_log_pub)
int orb_priority(int handle, int32_t *priority)
int gyro_sensor_sub[max_gyros]
Airspeed sensor calibration routine.
#define CAL_ERROR_RESET_CAL_MSG
static int32_t device_id[max_accel_sens]
High-resolution timer with callouts and timekeeping.
Global flash based parameter store.
void calibrate_cancel_unsubscribe(int cmd_sub)
Called to cancel the subscription to the cancel command.
int orb_subscribe(const struct orb_metadata *meta)
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Commander helper functions definitions.
static constexpr unsigned max_gyros
#define GYRO_BASE_DEVICE_PATH
Data passed to calibration worker routine.
#define CAL_ERROR_APPLY_CAL_MSG
int orb_unsubscribe(int handle)
#define CAL_QGC_STARTED_MSG
static calibrate_return gyro_calibration_worker(int cancel_sub, void *data)
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
int32_t device_id_primary
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
int orb_group_count(const struct orb_metadata *meta)
Get the number of published instances of a topic group.
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
__EXPORT void param_notify_changes(void)
Notify the system about parameter changes.
bool calibrate_cancel_check(orb_advert_t *mavlink_log_pub, int cancel_sub)
Used to periodically check for a cancel command.
int orb_check(int handle, bool *updated)
int orb_subscribe_multi(const struct orb_metadata *meta, unsigned instance)
int sensor_correction_sub
#define GYROIOCSSCALE
set the gyro scaling constants to (arg)
#define calibration_log_critical(_pub, _text,...)
int32_t device_id[max_gyros]
sensor_gyro_s gyro_report_0
struct gyro_calibration_s gyro_scale[max_gyros]
#define CAL_QGC_PROGRESS_MSG
#define CAL_QGC_FAILED_MSG
int calibrate_cancel_subscribe()
Called at the beginning of calibration in order to subscribe to the cancel command.
orb_advert_t * mavlink_log_pub
#define calibration_log_info(_pub, _text,...)