MPU6050

tools
tools/MPU6050.h
tool

Gestion du capteur inertiel MPU6050 Cette classe encapsule un capteur MPU6050 (accéléromètre 3 axes, gyroscope 3 axes, température). L'initialisation est volontairement séparée de la construction afin de laisser le contrôle total à l'utilisateur.

Méthodes publiques

Méthode Description Paramètres Retour
MPU6050()
MPU6050();
Constructeur Le capteur n'est pas initialisé ici. /
begin()
begin(uint8_t address = 0x69);
Initialise le capteur MPU6050 / address: Adresse I2C du capteur (0x68 si AD0=GND, 0x69 si AD0=VCC) true
true si l'initialisation réussit, false sinon /
getAcceleration()
getAcceleration(float &x, float &y, float &z) const;
Lit l'accélération sur les 3 axes / x: Accélération en X (m/s²)
y: Accélération en Y (m/s²)
z: Accélération en Z (m/s²) /
getGyroscope()
getGyroscope(float &x, float &y, float &z) const;
Lit la vitesse angulaire sur les 3 axes / x: Gyroscope en X (rad/s)
y: Gyroscope en Y (rad/s)
z: Gyroscope en Z (rad/s) /
getTemperature()
getTemperature() const;
Lit la température interne du capteur / Température
Température en degrés Celsius /
setAccelerometerRange()
setAccelerometerRange(mpu6050_accel_range_t range);
Définit la plage de mesure de l'accéléromètre / range: Plage (MPU6050_RANGE_2_G, 4_G, 8_G, 16_G) /
setGyroRange()
setGyroRange(mpu6050_gyro_range_t range);
Définit la plage de mesure du gyroscope / range: Plage (MPU6050_RANGE_250_DEG, 500_DEG, 1000_DEG, 2000_DEG) /
setFilterBandwidth()
setFilterBandwidth(mpu6050_bandwidth_t bandwidth);
Définit la bande passante du filtre passe-bas / bandwidth: Bande passante (MPU6050_BAND_260_HZ ... MPU6050_BAND_5_HZ) /
getStatus()
getStatus() const {
Retourne l'état du capteur. / DEVICE_OK
DEVICE_OK si begin() a réussi, DEVICE_ERROR_INIT sinon. /
calibrate()
calibrate(uint16_t samples = 100);
Calibre les offsets du capteur (à effectuer capteur immobile). / samples: Nombre d'échantillons moyennés pour la calibration (défaut: 100) /

Méthodes privées


            begin(115200);;
println("Erreur d'initialisation du MPU6050 !");;
while (1);;
setAccelerometerRange(MPU6050_RANGE_8_G);;
setGyroRange(MPU6050_RANGE_500_DEG);;
setFilterBandwidth(MPU6050_BAND_21_HZ);;
getAcceleration(ax, ay, az);;
getGyroscope(gx, gy, gz);;
getTemperature();;
print("Accel X: ");;
print(ax);;
println(" m/s²");;
print("Accel Y: ");;
print(ay);;
println(" m/s²");;
print("Accel Z: ");;
print(az);;
println(" m/s²");;
print("Gyro X: ");;
print(gx);;
println(" rad/s");;
print("Gyro Y: ");;
print(gy);;
println(" rad/s");;
print("Gyro Z: ");;
print(gz);;
println(" rad/s");;
print("Temp: ");;
print(temp);;
println(" °C");;
delay(500);;

                    

Variables membres


            si AD0;
return _status;

                    

Code source


        #pragma once

#include <Arduino.h>
#include "../StatusDevice.h"
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>


namespace crepp::tools {

/**
 * @class MPU6050
 * @brief Gestion du capteur inertiel MPU6050
 *
 * Cette classe encapsule un capteur MPU6050 (accéléromètre 3 axes,
 * gyroscope 3 axes, température).
 *
 * L'initialisation est volontairement séparée de la construction
 * afin de laisser le contrôle total à l'utilisateur.
 *
 */
class MPU6050
{

public:
    /**
     * @brief Constructeur
     *
     * Le capteur n'est pas initialisé ici.
     */
    MPU6050();

    /**
     * @brief Destructeur
     */
    ~MPU6050() = default;

    /**
     * @brief Initialise le capteur MPU6050
     * @param address Adresse I2C du capteur (0x68 si AD0=GND, 0x69 si AD0=VCC)
     * @return true si l'initialisation réussit, false sinon
     */
    bool begin(uint8_t address = 0x69);

    /**
     * @brief Lit l'accélération sur les 3 axes
     * @param x Accélération en X (m/s²)
     * @param y Accélération en Y (m/s²)
     * @param z Accélération en Z (m/s²)
     */
    void getAcceleration(float &x, float &y, float &z) const;

    /**
     * @brief Lit la vitesse angulaire sur les 3 axes
     * @param x Gyroscope en X (rad/s)
     * @param y Gyroscope en Y (rad/s)
     * @param z Gyroscope en Z (rad/s)
     */
    void getGyroscope(float &x, float &y, float &z) const;

    /**
     * @brief Lit la température interne du capteur
     * @return Température en degrés Celsius
     */
    float getTemperature() const;

    /**
     * @brief Définit la plage de mesure de l'accéléromètre
     * @param range Plage (MPU6050_RANGE_2_G, 4_G, 8_G, 16_G)
     */
    void setAccelerometerRange(mpu6050_accel_range_t range);

    /**
     * @brief Définit la plage de mesure du gyroscope
     * @param range Plage (MPU6050_RANGE_250_DEG, 500_DEG, 1000_DEG, 2000_DEG)
     */
    void setGyroRange(mpu6050_gyro_range_t range);

    /**
     * @brief Définit la bande passante du filtre passe-bas
     * @param bandwidth Bande passante (MPU6050_BAND_260_HZ ... MPU6050_BAND_5_HZ)
     */
    void setFilterBandwidth(mpu6050_bandwidth_t bandwidth);

    /**
     * @brief Retourne l'état du capteur.
     * @return DEVICE_OK si begin() a réussi, DEVICE_ERROR_INIT sinon.
     */
    StatusDevice getStatus() const { return _status; }

    /**
     * @brief Calibre les offsets du capteur (à effectuer capteur immobile).
     * @param samples Nombre d'échantillons moyennés pour la calibration (défaut: 100)
     */
    void calibrate(uint16_t samples = 100);

private:
    bool _initialized = false;

    float _offsetX = 0.0f;
    float _offsetY = 0.0f;
    float _offsetZ = 0.0f;

    class Adafruit_MPU6050* _mpu;
    StatusDevice _status = StatusDevice::DEVICE_ERROR_INIT;
};

/*
@example

@include
#include <crepp/tools/MPU6050.h>
@end_include

@macro
#define I2C_ADDRESS_MPU6050 0x68
@end_macro

@instance
crepp::tools::MPU6050 mpu;
@end_instance

@isr
@end_isr

@function
@end_function

@setup
  Serial.begin(115200);
  if (!mpu.begin(I2C_ADDRESS_MPU6050)) {
      Serial.println("Erreur d'initialisation du MPU6050 !");
      while (1);
  }
  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
@end_setup

@loop
  float ax, ay, az;
  float gx, gy, gz;
  mpu.getAcceleration(ax, ay, az);
  mpu.getGyroscope(gx, gy, gz);
  float temp = mpu.getTemperature();
  Serial.print("Accel X: "); Serial.print(ax); Serial.println(" m/s²");
  Serial.print("Accel Y: "); Serial.print(ay); Serial.println(" m/s²");
  Serial.print("Accel Z: "); Serial.print(az); Serial.println(" m/s²");
  Serial.print("Gyro X: ");  Serial.print(gx); Serial.println(" rad/s");
  Serial.print("Gyro Y: ");  Serial.print(gy); Serial.println(" rad/s");
  Serial.print("Gyro Z: ");  Serial.print(gz); Serial.println(" rad/s");
  Serial.print("Temp: ");    Serial.print(temp); Serial.println(" °C");
  delay(500);
@end_loop

@end_example
*/


} // namespace crepp::tools