/**
 * @brief Read magnetometer heading/direction data.
 *
 * This function obtains magnetometer data for all three axes of the Honeywell
 * device.  The data is read from six device registers using a multi-byte
 * bus transfer.  The 10-bit raw results are then assembled from the two
 * register values for each axis, including extending the sign bit, to
 * form a signed 32-bit value.
 *
 * Along with the actual sensor data, the LSB byte contains a "new" flag
 * indicating if the data for this axis has been updated since the last
 * time the axis data was read.  Reading either LSB or MSB data will
 * clear this flag.
 *
 * @param   hal     Address of an initialized sensor device descriptor.
 * @param   data    The address of a vector storing sensor axis data.
 * @return  bool    true if the call succeeds, else false is returned.
 */
static bool hmc5883l_get_heading(sensor_hal_t *hal, sensor_data_t *data)
{
	/* Get magnetic field measurements & test for data overflow. */
	vector3_t mag_data;

	bool result = hmc5883l_get_data(hal, &mag_data);

	if (result) {
		/* Apply sensitivity adjustment to data */
		hmc5883l_apply_sensitivity(&mag_data);

		/* Apply measurement offsets to data */
		hmc5883l_apply_offset(&mag_data);

		/* Calculate direction, inclination, and field strength. */
		scalar_t theta;    /* direction angle (degrees) */
		scalar_t delta;    /* inclination angle (degrees) */
		scalar_t strength; /* magnetic field intensity */

		result = field_direction(&mag_data, &theta, &delta, &strength);

		if (result) {
			strength *= ((scalar_t)GAUSS_TO_MICRO_TESLA /
					scale_table[dev_range]);

			data->heading.direction   = (int32_t)theta;
			data->heading.inclination = (int32_t)delta;
			data->heading.strength    = (int32_t)strength;
		}
	}

	return result;
}
Beispiel #2
0
/**
 * @brief Read magnetometer heading/direction data.
 *
 * This function obtains magnetometer data for all three axes of the AKM
 * device.  The data is read from six device registers using a multi-byte
 * bus transfer.  The 10-bit raw results are then assembled from the two
 * register values for each axis, including extending the sign bit, to
 * form a signed 32-bit value.
 *
 * Along with the actual sensor data, the LSB byte contains a "new" flag
 * indicating if the data for this axis has been updated since the last
 * time the axis data was read.  Reading either LSB or MSB data will
 * clear this flag.
 *
 * @param   hal     Address of an initialized sensor hardware descriptor.
 * @param   data    The address of a vector storing sensor axis data.
 * @return  bool    true if the call succeeds, else false is returned.
 */
static bool ak8975_get_heading(sensor_hal_t *hal, sensor_data_t *data)
{
	bool result = false;

	/* Get magnetic field measurements & test for data overflow. */
	vector3_t mag_data;

	if (ak8975_get_data(hal, AK8975_SINGLE_MODE, &mag_data) &&
			(!ak8975_check_overflow(&mag_data))) {
		/* Apply stored measurement offsets to data. */
		ak8975_apply_offset(&mag_data);

		/* Calculate direction, inclination, and field strength. */
		scalar_t theta;    /* direction angle (degrees) */
		scalar_t delta;    /* inclination angle (degrees) */
		scalar_t strength; /* magnetic field intensity */

		result = field_direction(&mag_data, &theta, &delta, &strength);

		if (result) {
			strength *= (scalar_t)MICRO_TESLA_PER_COUNT;

			data->heading.direction   = (int32_t)theta;
			data->heading.inclination = (int32_t)delta;
			data->heading.strength    = (int32_t)strength;
		}
	}

	return result;
}