Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed May 30, 2022
2 parents 371445e + 06fa77d commit 3154031
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 74 deletions.
2 changes: 1 addition & 1 deletion docs/source/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ WARN_LOGFILE =
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
# Note: If this tag is empty the current directory is searched.

INPUT = ../../src ../../inc
INPUT = ../../src ../../inc ../../extras

# This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
Expand Down
34 changes: 34 additions & 0 deletions docs/source/advanced-usage/extras.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.. _extras:

Extra Modules
=============

Simple AHRS
"""""""""""

Simple attitude and heading reference system module can return pitch and roll angles using acceleration value. Include ``mpu925x_simple_ahrs.h`` in desired source file and compile ``mpu925x_simple_ahrs.c`` source file with target program.

.. code-block:: c
:caption: Example Code
#include "mpu925x.h"
#include "mpu925x_simple_ahrs.h"
// Create struct instances.
mpu925x_t mpu925x;
mpu925x_simple_ahrs ahrs;
// Initialize driver.
mpu925x_init(&mpu925x, 0);
// Get values.
mpu925x_get_acceleration(&mpu925x);
mpu925x_get_simple_ahrs(&mpu925x, &ahrs);
printf("Pitch: %f, Roll: %f\n", ahrs.pitch, ahrs.roll);
API Reference
^^^^^^^^^^^^^

.. doxygenfile:: mpu925x_simple_ahrs.h
:project: mpu925x-driver
6 changes: 0 additions & 6 deletions docs/source/advanced-usage/fifo.rst

This file was deleted.

2 changes: 1 addition & 1 deletion docs/source/advanced-usage/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ Advanced Usage
accelerometer
gyroscope
magnetometer
fifo
extras
1 change: 1 addition & 0 deletions docs/source/getting-started/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Adding to Your Project
3. Add ``src/mpu925x_core.c``, ``src/mpu925x_settings.c`` and ``src/mpu925x_internals.c`` source files to your project's build toolchain.
4. Provide bus handle, bus read, bus write and delay functions depending on your platform (see: :ref:`porting guide<porting-guide>`).
5. Include ``mpu925x.h`` header to your desired source files.
6. [EXTRAS] Extra modules can be compiled with program if any of the extra functionalities needed. Extra modules are located in ``extras`` directory.

Simple Usage
^^^^^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions examples/stm32_hal_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ int main()

.settings = {
// Other settings
.accelerometer_scale = _2g,
.gyroscope_scale = _250dps,
.orientation = z_minus
.accelerometer_scale = mpu925x_2g,
.gyroscope_scale = mpu925x_250dps,
.orientation = mpu925x_z_minus
}
};

Expand Down
44 changes: 44 additions & 0 deletions extras/mpu925x_simple_ahrs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @file
* @author Ceyhun Şen
* @brief Simple AHRS source file for MPU-925X driver.
* */

/*
* MIT License
*
* Copyright (c) 2022 Ceyhun Şen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* */

#include "mpu925x_simple_ahrs.h"
#include <math.h>

/**
* @brief Get pitch and roll angles.
* @param mpu925x MPU-925X struct pointer.
* @param ahrs Simple AHRS struct pointer.
* */
void mpu925x_get_simple_ahrs(mpu925x_t *mpu925x, mpu925x_simple_ahrs *ahrs)
{
ahrs->pitch = asin(mpu925x->sensor_data.acceleration[0] / sqrt(mpu925x->sensor_data.acceleration[0] * mpu925x->sensor_data.acceleration[0] + mpu925x->sensor_data.acceleration[1] * mpu925x->sensor_data.acceleration[1] + mpu925x->sensor_data.acceleration[2] * mpu925x->sensor_data.acceleration[2])) * 120;

ahrs->roll = atan(mpu925x->sensor_data.acceleration[1] / mpu925x->sensor_data.acceleration[2]) * 120;
}
46 changes: 46 additions & 0 deletions extras/mpu925x_simple_ahrs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @file
* @author Ceyhun Şen
* @brief Simple AHRS header file for MPU-925X driver.
* */

/*
* MIT License
*
* Copyright (c) 2022 Ceyhun Şen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* */

#ifndef __MPU925X_SIMPLE_AHRS_H
#define __MPU925X_SIMPLE_AHRS_H

#include "mpu925x.h"

/**
* @brief Pitch and roll angles for simple AHRS.
* */
typedef struct mpu925x_simple_ahrs {
float pitch;
float roll;
} mpu925x_simple_ahrs;

void mpu925x_get_simple_ahrs(mpu925x_t *mpu925x, mpu925x_simple_ahrs *ahrs);

#endif // __MPU925X_SIMPLE_AHRS_H
3 changes: 0 additions & 3 deletions inc/mpu925x_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ void mpu925x_get_accelerometer_bias(mpu925x_t *mpu925x, int16_t *bias);

void mpu925x_bus_write_preserve(mpu925x_t *mpu925x, uint8_t slave_address, uint8_t reg, uint8_t *buffer, uint8_t size, uint8_t and_sentence);

uint8_t ak8963_bus_read(mpu925x_t *mpu925x, uint8_t read_register, uint8_t *buffer, uint8_t size);
uint8_t ak8963_bus_write(mpu925x_t *mpu925x, uint8_t write_register, uint8_t *buffer, uint8_t size);

#define convert8bitto16bit(x, y) (((x) << 8) | (y))
#define powerof2(x) (1 << (x))

Expand Down
4 changes: 2 additions & 2 deletions src/mpu925x_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void mpu925x_get_magnetic_field_raw(mpu925x_t *mpu925x)
switch (mpu925x->settings.measurement_mode) {
case mpu925x_single_measurement_mode:
case mpu925x_self_test_mode:
ak8963_bus_read(mpu925x, ST1, buffer, 1);
mpu925x->master_specific.bus_read(mpu925x, AK8963_ADDRESS, ST1, buffer, 1);
if ((buffer[0] & 1) != 1) {
return;
}
Expand All @@ -178,7 +178,7 @@ void mpu925x_get_magnetic_field_raw(mpu925x_t *mpu925x)
}

// Read raw data and ST2 overflow register.
ak8963_bus_read(mpu925x, HXL, buffer, 7);
mpu925x->master_specific.bus_read(mpu925x, AK8963_ADDRESS, HXL, buffer, 7);

// Check overflow.
if ((buffer[6] & 0x08) == 0x08) {
Expand Down
65 changes: 11 additions & 54 deletions src/mpu925x_internals.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

/**
* @brief Initialize accelerometer and gyro.
* @param mpu925x Struct that holds sensor data.
* @param mpu925x MPU-925X struct pointer.
* @returns 0 on success, 1 on failure.
* */
uint8_t __mpu925x_init(mpu925x_t *mpu925x)
Expand Down Expand Up @@ -70,15 +70,15 @@ uint8_t __mpu925x_init(mpu925x_t *mpu925x)

/**
* @brief Initialize magnetometer.
* @param mpu925x Struct that holds sensor data.
* @param mpu925x MPU-925X struct pointer.
* @returns 0 on success, 1 on failure.
* */
uint8_t __ak8963_init(mpu925x_t *mpu925x)
{
uint8_t buffer;

// Check WIA register. WIA register should return 0x48.
ak8963_bus_read(mpu925x, WIA, &buffer, 1);
mpu925x->master_specific.bus_read(mpu925x, AK8963_ADDRESS, WIA, &buffer, 1);
if (buffer != 0x48)
return 1;

Expand All @@ -93,7 +93,7 @@ uint8_t __ak8963_init(mpu925x_t *mpu925x)

// Read coefficient data and save it.
uint8_t coef_data[3];
ak8963_bus_read(mpu925x, ASAX, coef_data, 3);
mpu925x->master_specific.bus_read(mpu925x, AK8963_ADDRESS, ASAX, coef_data, 3);
for (uint8_t i = 0; i < 3; i++) {
mpu925x->settings.magnetometer_coefficient[i] = (coef_data[i] - 128) * 0.5 / 128 + 1;
}
Expand All @@ -110,7 +110,7 @@ uint8_t __ak8963_init(mpu925x_t *mpu925x)

/**
* @brief Get acceleration bias.
* @param mpu925x Struct that holds sensor data.
* @param mpu925x MPU-925X struct pointer.
* @param bias 3d array which will hold bias values.
* */
void mpu925x_get_accelerometer_bias(mpu925x_t *mpu925x, int16_t *bias)
Expand All @@ -128,7 +128,7 @@ void mpu925x_get_accelerometer_bias(mpu925x_t *mpu925x, int16_t *bias)

/**
* @brief Write data to bus whilst preserving other bits (not ready for usage).
* @param mpu925x Struct that holds sensor data.
* @param mpu925x MPU-925X struct pointer.
* */
void mpu925x_bus_write_preserve(mpu925x_t *mpu925x, uint8_t slave_address, uint8_t reg, uint8_t *buffer, uint8_t size, uint8_t and_sentence)
{
Expand All @@ -143,51 +143,8 @@ void mpu925x_bus_write_preserve(mpu925x_t *mpu925x, uint8_t slave_address, uint8
}

/**
* @brief Read data from AK8963 as slave of MPU-925X.
* @param mpu925x Struct that holds sensor data.
* @param read_register Read register.
* @param buffer Read data buffer.
* @param size Size of the data that will be read.
* */
uint8_t ak8963_bus_read(mpu925x_t *mpu925x, uint8_t read_register, uint8_t *buffer, uint8_t size)
{
return mpu925x->master_specific.bus_read(mpu925x, AK8963_ADDRESS, read_register, buffer, size);
//~ uint8_t slave_data[3] = {(1 << 7) | AK8963_ADDRESS, read_register, (1 << 7) | size};
//~ mpu925x->master_specific.bus_write(mpu925x, I2C_SLV0_ADDR, slave_data, 3);
//~ mpu925x->master_specific.delay_ms(mpu925x, 1);
//~ return mpu925x->master_specific.bus_read(mpu925x, EXT_SENS_DATA_00, buffer, size);
}

/**
* @brief Write data to AK8963 as a slave of MPU-925X.
* @param mpu925x Struct that holds sensor data.
* @param write_register Write register.
* @param Write data buffer.
* @param size Size of the data that will be written.
* */
uint8_t ak8963_bus_write(mpu925x_t *mpu925x, uint8_t write_register, uint8_t *buffer, uint8_t size)
{
return mpu925x->master_specific.bus_write(mpu925x, AK8963_ADDRESS, write_register, buffer, size);
//~ uint8_t write_data[2];

//~ write_data[0] = 0;
//~ mpu925x->master_specific.bus_write(mpu925x, I2C_SLV0_CTRL, write_data, 1);

//~ for (uint16_t i = 0; i < size; i++) {
//~ write_data[0] = AK8963_ADDRESS;
//~ write_data[1] = write_register + i;
//~ mpu925x->master_specific.bus_write(mpu925x, I2C_SLV4_ADDR, write_data, 2);
//~ write_data[0] = buffer[i];
//~ write_data[1] = 1 << 7;
//~ mpu925x->master_specific.bus_write(mpu925x, I2C_SLV4_DO, write_data, 2);
//~ mpu925x->master_specific.delay_ms(mpu925x, 1);
//~ }
//~ return 0;
}

/**
* @brief Reset MPU-925X.
* @param mpu925x Struct that holds sensor data.
* @brief Reset MPU-925X sensor.
* @param mpu925x MPU-925X struct pointer.
* */
void mpu925x_reset(mpu925x_t *mpu925x)
{
Expand All @@ -197,12 +154,12 @@ void mpu925x_reset(mpu925x_t *mpu925x)
}

/**
* @brief Reset AK8963.
* @param mpu925x Struct that holds sensor data.
* @brief Reset AK8963 sensor.
* @param mpu925x MPU-925X struct pointer.
* */
void ak8963_reset(mpu925x_t *mpu925x)
{
uint8_t buffer = 1;
ak8963_bus_write(mpu925x, CNTL2, &buffer, 1);
mpu925x->master_specific.bus_write(mpu925x, AK8963_ADDRESS, CNTL2, &buffer, 1);
mpu925x->master_specific.delay_ms(mpu925x, 100);
}
8 changes: 4 additions & 4 deletions src/mpu925x_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ void mpu925x_set_magnetometer_measurement_mode(mpu925x_t *mpu925x, mpu925x_magne
// Save measurement mode.
mpu925x->settings.measurement_mode = measurement_mode;

ak8963_bus_read(mpu925x, CNTL1, &buffer, 1);
mpu925x->master_specific.bus_read(mpu925x, AK8963_ADDRESS, CNTL1, &buffer, 1);
buffer &= 0b11110000;

switch (measurement_mode) {
Expand Down Expand Up @@ -360,7 +360,7 @@ void mpu925x_set_magnetometer_measurement_mode(mpu925x_t *mpu925x, mpu925x_magne
break;
}

ak8963_bus_write(mpu925x, CNTL1, &buffer, 1);
mpu925x->master_specific.bus_write(mpu925x, AK8963_ADDRESS, CNTL1, &buffer, 1);

mpu925x->master_specific.delay_ms(mpu925x, 100);
}
Expand All @@ -378,7 +378,7 @@ void mpu925x_set_magnetometer_bit_mode(mpu925x_t *mpu925x, mpu925x_magnetometer_
// Save bit mode.
mpu925x->settings.bit_mode = bit_mode;

ak8963_bus_read(mpu925x, CNTL1, &buffer, 1);
mpu925x->master_specific.bus_read(mpu925x, AK8963_ADDRESS, CNTL1, &buffer, 1);
buffer &= 0b11101111;

switch (bit_mode) {
Expand All @@ -393,7 +393,7 @@ void mpu925x_set_magnetometer_bit_mode(mpu925x_t *mpu925x, mpu925x_magnetometer_
break;
}

ak8963_bus_write(mpu925x, CNTL1, &buffer, 1);
mpu925x->master_specific.bus_write(mpu925x, AK8963_ADDRESS, CNTL1, &buffer, 1);

mpu925x->master_specific.delay_ms(mpu925x, 100);
}

0 comments on commit 3154031

Please sign in to comment.