You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm bulding my first project using two MPU9250s and I came across this library through some video I saw on YT.
The problem is that the video was old and the version he was using is the 1.0.2.
However, the code is running great and I'm able to get reading from both Pitch and Roll. The problem is to get the Yaw.
I can't find a way to get readings from the magnetometers, they return 0.
I've tried to update to the most recent version of this library but when I did that I got 0 also on pitch and yaw.
// Initialize BLE
BLEDevice::init("Wrist Sensor"); // Set the device name
// Create BLE Server
pServer = BLEDevice::createServer();
// Define a service UUID
BLEService *pService = pServer->createService(BLEUUID("12345678-1234-1234-1234-1234567890ab"));
// Define a characteristic
pCharacteristic = pService->createCharacteristic(
BLEUUID("abcd1234-ab12-cd34-ef56-abcdef123456"),
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID("12345678-1234-1234-1234-1234567890ab");
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // Min connection interval
pAdvertising->setMinPreferred(0x12); // Max connection interval
BLEDevice::startAdvertising();
Serial.println("Wrist Sensor is advertising...");
}
void addToBuffer(float *buffer, float value) {
for (int i = WINDOW_SIZE - 1; i > 0; i--) {
buffer[i] = buffer[i - 1];
}
buffer[0] = value;
}
float calculateAverage(float *buffer) {
float sum = 0;
for (int i = 0; i < WINDOW_SIZE; i++) {
sum += buffer[i];
}
return sum / WINDOW_SIZE;
}
void loop() {
IMU1.readSensor();
IMU2.readSensor();
Are you sure you're using the MPU-9250? There are a lot of counterfeits or chips substituted with something like the MPU-6500. I would also use the newest version downloaded directly from GitHub and start with one of our examples to see if the chip is reading correctly. That way you can debug from a much more simple example.
Hi,
I'm bulding my first project using two MPU9250s and I came across this library through some video I saw on YT.
The problem is that the video was old and the version he was using is the 1.0.2.
However, the code is running great and I'm able to get reading from both Pitch and Roll. The problem is to get the Yaw.
I can't find a way to get readings from the magnetometers, they return 0.
I've tried to update to the most recent version of this library but when I did that I got 0 also on pitch and yaw.
Here's my code:
`#include <MPU9250.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
TwoWire MainWire = TwoWire(0);
MPU9250 IMU1 = MPU9250(MainWire, 0x68); // First MPU9250 at 0x68
MPU9250 IMU2 = MPU9250(MainWire, 0x69); // Second MPU9250 at 0x69
String sensorData;
BLECharacteristic *pCharacteristic = NULL;
BLEServer *pServer = NULL;
const double xBias = 1624.766973;
const double yBias = -977.863214;
const double zBias = -674.986618;
#define WINDOW_SIZE 5
#define ALPHA 0.1 // Smoothing factor for exponential moving average
float smoothedPitch1 = 0, smoothedRoll1 = 0, smoothedYaw1 = 0;
float smoothedPitch2 = 0, smoothedRoll2 = 0, smoothedYaw2 = 0;
float pitch1Values[WINDOW_SIZE] = { 0 };
float roll1Values[WINDOW_SIZE] = { 0 };
float pitch2Values[WINDOW_SIZE] = { 0 };
float roll2Values[WINDOW_SIZE] = { 0 };
void setup() {
Serial.begin(115200);
// Initialize I2C buses
MainWire.begin(21, 22); // SDA, SCL for the first MPU
IMU1.begin();
IMU2.begin();
IMU1.calibrateAccel();
IMU2.calibrateAccel();
IMU1.calibrateGyro();
IMU2.calibrateGyro();
IMU1.calibrateMag();
IMU2.calibrateMag();
// Initialize BLE
BLEDevice::init("Wrist Sensor"); // Set the device name
// Create BLE Server
pServer = BLEDevice::createServer();
// Define a service UUID
BLEService *pService = pServer->createService(BLEUUID("12345678-1234-1234-1234-1234567890ab"));
// Define a characteristic
pCharacteristic = pService->createCharacteristic(
BLEUUID("abcd1234-ab12-cd34-ef56-abcdef123456"),
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID("12345678-1234-1234-1234-1234567890ab");
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // Min connection interval
pAdvertising->setMinPreferred(0x12); // Max connection interval
BLEDevice::startAdvertising();
Serial.println("Wrist Sensor is advertising...");
}
void addToBuffer(float *buffer, float value) {
for (int i = WINDOW_SIZE - 1; i > 0; i--) {
buffer[i] = buffer[i - 1];
}
buffer[0] = value;
}
float calculateAverage(float *buffer) {
float sum = 0;
for (int i = 0; i < WINDOW_SIZE; i++) {
sum += buffer[i];
}
return sum / WINDOW_SIZE;
}
void loop() {
IMU1.readSensor();
IMU2.readSensor();
// Calculate pitch, roll, and yaw for IMU1
float pitch1 = atan2(IMU1.getAccelY_mss(), IMU1.getAccelZ_mss()) * 180 / PI;
float roll1 = atan2(-IMU1.getAccelX_mss(), sqrt(IMU1.getAccelY_mss() * IMU1.getAccelY_mss() + IMU1.getAccelZ_mss() * IMU1.getAccelZ_mss())) * 180 / PI;
float yaw1 = atan2(IMU1.getMagY_uT(), IMU1.getMagX_uT()) * 180 / PI; // Yaw from magnetometer
// Calculate pitch, roll, and yaw for IMU2
float pitch2 = atan2(IMU2.getAccelY_mss(), IMU2.getAccelZ_mss()) * 180 / PI;
float roll2 = atan2(-IMU2.getAccelX_mss(), sqrt(IMU2.getAccelY_mss() * IMU2.getAccelY_mss() + IMU2.getAccelZ_mss() * IMU2.getAccelZ_mss())) * 180 / PI;
float yaw2 = atan2(IMU2.getMagY_uT(), IMU2.getMagX_uT()) * 180 / PI; // Yaw from magnetometer
// Apply exponential moving average to smooth the values
smoothedPitch1 = ALPHA * pitch1 + (1 - ALPHA) * smoothedPitch1;
smoothedRoll1 = ALPHA * roll1 + (1 - ALPHA) * smoothedRoll1;
smoothedYaw1 = ALPHA * yaw1 + (1 - ALPHA) * smoothedYaw1;
smoothedPitch2 = ALPHA * pitch2 + (1 - ALPHA) * smoothedPitch2;
smoothedRoll2 = ALPHA * roll2 + (1 - ALPHA) * smoothedRoll2;
smoothedYaw2 = ALPHA * yaw2 + (1 - ALPHA) * smoothedYaw2;
// Format data as a string
sensorData = String(smoothedPitch1, 2) + "," + String(smoothedRoll1, 2) + "," + String(smoothedYaw1, 2) + "," + String(smoothedPitch2, 2) + "," + String(smoothedRoll2, 2) + "," + String(smoothedYaw2, 2);
Serial.println("Sending data: " + sensorData);
Serial.print("MagX: ");
Serial.print(IMU1.getMagX_uT() * (100000.0 / 1100.0) + xBias);
Serial.print(", MagY: ");
Serial.print(IMU1.getMagY_uT() * (100000.0 / 1100.0) + yBias);
Serial.print(", MagZ: ");
Serial.println(IMU1.getMagZ_uT() * (100000.0 / 1100.0) + zBias);
Serial.print("MagX: ");
Serial.print(IMU2.getMagX_uT());
Serial.print(", MagY: ");
Serial.print(IMU2.getMagY_uT());
Serial.print(", MagZ: ");
Serial.println(IMU2.getMagZ_uT());
// Notify connected devices
pCharacteristic->setValue(sensorData.c_str());
pCharacteristic->notify();
delay(20);
}`
Can you help me?
Thanks!
The text was updated successfully, but these errors were encountered: