void AKM8973_2_6_29::calibrate_magnetometer_analog_helper(float val, int i) { const float ANALOG_MAX = 126.0f; const float BOUND_MAX = 240.0f; /* The rate of forgetting encountering the minimum or maximum bound. * Keeping this fairly large to make it less likely that analog gain * gets adjusted by mistake. */ const float CALIBRATE_DECAY = 0.1f; /* Autoadjust analog parameters */ if (val > ANALOG_MAX || val < -ANALOG_MAX) { analog_offset[i] += val > ANALOG_MAX ? -1 : 1; LOGI("Adjusting magnetometer axis %d to %d because of value %f", i, analog_offset[i], val); calibrate_analog_apply(); /* The other axes are OK */ rc_min[i] = 0; rc_max[i] = 0; return; } /* If recorded digital bounds get close to completely used, * we risk having to constantly adjust the analog gain. We * should be able to detect this happening as user rotates the * device. */ if (rc_max[i] - rc_min[i] > BOUND_MAX && fixed_magnetometer_gain > 0) { fixed_magnetometer_gain -= 1; LOGI("Adjusting magnetometer gain to %d", fixed_magnetometer_gain); calibrate_analog_apply(); /* Bounds will change on all axes. */ for (int j = 0; j < 3; j ++) { rc_min[j] = 0; rc_max[j] = 0; } return; } rc_min[i] += CALIBRATE_DECAY; rc_max[i] -= CALIBRATE_DECAY; /* minimum value seen */ if (rc_min[i] > val) { rc_min[i] = val; } /* maximum value seen */ if (rc_max[i] < val) { rc_max[i] = val; } }
AKM8973_2_6_29::AKM8973_2_6_29(int gain) : index(0), fixed_magnetometer_gain(15), magnetometer(600) { mbuf[0] = mbuf[1] = Vector(); magnetometer_gain = gain; fd = open("/dev/akm8973_daemon", O_RDONLY); SUCCEED(fd != -1); SUCCEED(ioctl(fd, ECS_IOCTL_RESET, NULL) == 0); calibrate_analog_apply(); }
AK8973B::AK8973B(int dgain, int again) : magnetometer(600) { mbuf = Vector(); magnetometer_gain = dgain; fixed_magnetometer_gain = again; //starting offset analog_offset[0]=-3; analog_offset[1]=-3; analog_offset[2]=-5; fd = open("/dev/akm8973_daemon", O_RDONLY); SUCCEED(fd != -1); SUCCEED(ioctl(fd, ECS_IOCTL_RESET, NULL) == 0); calibrate_analog_apply(); }