Hello.
I work on my project on Arudino UNO. I have the Bluetooth Classic module HC-06 and MPU-6050. For communication with bluetooth module I use Serial and transfer data via TX (1) and RX (0) pins and all are okey, but when I try to use MPU-6050, when bluetooth is connected to Arduino - MPU blocks transfer of data, what's a problem?
imu.hpp code.
#pragma once
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"
class IMU
{
public:
IMU()
{
Wire.begin();
_device.initialize();
const uint8_t status = _device.dmpInitialize();
if (_device.testConnection() == false || status != 0)
return;
_device.setXAccelOffset(0);
_device.setYAccelOffset(0);
_device.setZAccelOffset(0);
_device.setXGyroOffset(0);
_device.setYGyroOffset(0);
_device.setZGyroOffset(0);
_valid = true;
}
~IMU()
{
_valid = false;
}
inline void calibrate()
{
_calibrated = false;
_device.setDMPEnabled(false);
if (!valid())
return;
_device.CalibrateAccel(CABLIRATION_ITERATIONS);
_device.CalibrateGyro(CABLIRATION_ITERATIONS);
_calibrated = true;
_device.setDMPEnabled(true);
}
inline bool calibrated() const noexcept
{
return _calibrated;
}
inline bool valid() const noexcept
{
return _valid;
}
operator bool() const noexcept
{
return valid();
}
inline Quaternion get_angle() const noexcept
{
if (!valid())
return {};
Quaternion value{};
if (_device.dmpGetCurrentFIFOPacket(_fifoBuffer))
_device.dmpGetQuaternion(&value, _fifoBuffer);
return value;
}
inline VectorInt16 get_accel() const noexcept
{
if (valid())
return {};
VectorInt16 value;
if (_device.dmpGetCurrentFIFOPacket(_fifoBuffer))
_device.dmpGetAccel(&value, _fifoBuffer);
return value;
}
private:
bool _calibrated{false};
bool _valid{false};
MPU6050 _device{0x68};
uint8_t _fifoBuffer[64]{};
static constexpr uint8_t CABLIRATION_ITERATIONS{15};
};
main.ino code
#include "bluetooth.hpp" // in this header I use Serial as default to communication with bluetooth
#include "imu.hpp"
#define BUTTON 7
IMU imu;
BLUETOOTH bluetooth;
void processRequest(const String &data);
void setup()
{
bluetooth.begin(115200); // Sets to Serial the same value
if (bluetooth.is_connected())
Serial.println("Bluetooth is initialized!");
else
Serial.println("Bluetooth is not initialized!");
bluetooth.clear_serial();
}
void loop()
{
buffer = bluetooth.receive();
processRequest(buffer);
}
void processRequest(const String &data)
{
}
bluetooth.hpp
#pragma once
#include <stdint.h>
class BLUETOOTH
{
public:
BLUETOOTH () = default;
~BLUETOOTH () = default;
inline void begin(uint32_t baudrate)
{
_baudrate = baudrate;
_serial.begin(_baudrate);
while (!_serial)
{
// Waiting for the module to start working
}
clear_serial();
_isConnected = true;
}
inline String receive()
{
return _serial.available() ? _serial.readStringUntil('\n') : String{};
}
inline void send(const String &msg)
{
_serial.write(msg.c_str(), msg.length());
}
inline uint32_t get_baudrate() const noexcept
{
return _baudrate;
}
constexpr bool is_connected() const noexcept
{
return _isConnected;
}
inline void clear_serial() noexcept
{
if (_serial.available())
{
_serial.readString();
}
}
private:
uint32_t _baudrate{};
bool _isConnected{false};
static auto& _serial = Serial;
};
That's how I connected MPU to the Arduino.