Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hi, I need help for the ardunio code. now I am using the MPU 9250, here is the code which I am using. #include #ifdef

hi, I need help for the ardunio code. now I am using the MPU 9250, here is the code which I am using.

#include
#ifdef _ESP32_HAL_I2C_H_
#define SDA_PIN 26
#define SCL_PIN 25
#endif
#define CALIB_SEC 20
MPU9250 mySensor;
uint8_t sensorId;
float mDirection, mX, mY, mZ;
void setup() {
while(!Serial);
Serial.begin(115200);
Serial.println("started");
#ifdef _ESP32_HAL_I2C_H_ // For ESP32
Wire.begin(SDA_PIN, SCL_PIN); // SDA, SCL
#else
Wire.begin();
#endif
mySensor.setWire(&Wire);
mySensor.beginMag();
sensorId = mySensor.readId();
float magXMin, magXMax, magYMin, magYMax, magZ, magZMin, magZMax;
Serial.println("Start scanning values of magnetometer to get offset values.");
Serial.println("Rotate your device for " + String(CALIB_SEC) + " seconds.");
setMagMinMaxAndSetOffset(&mySensor, CALIB_SEC);
Serial.println("Finished setting offset values.");
}
void setMagMinMaxAndSetOffset(MPU9250* sensor, int seconds) {
unsigned long calibStartAt = millis();
float magX, magXMin, magXMax, magY, magYMin, magYMax, magZ, magZMin, magZMax;
sensor->magUpdate();
magXMin = magXMax = sensor->magX();
magYMin = magYMax = sensor->magY();
magZMin = magZMax = sensor->magZ();
while(millis() - calibStartAt
delay(100);
sensor->magUpdate();
magX = sensor->magX();
magY = sensor->magY();
magZ = sensor->magZ();
if (magX > magXMax) magXMax = magX;
if (magY > magYMax) magYMax = magY;
if (magZ > magZMax) magZMax = magZ;
if (magX
if (magY
if (magZ
}
sensor->magXOffset = - (magXMax - magXMin) / 2;
sensor->magYOffset = - (magYMax - magYMin) / 2;
sensor->magZOffset = - (magZMax - magZMin) / 2;
}
void loop() {
Serial.println("sensorId: " + String(sensorId));
mySensor.magUpdate();
mX = mySensor.magX();
mY = mySensor.magY();
mZ = mySensor.magZ();
mDirection = mySensor.magHorizDirection();
Serial.println("mySensor.magXOffset = " + String(mySensor.magXOffset) + ";");
Serial.println("mySensor.maxYOffset = " + String(mySensor.magYOffset) + ";");
Serial.println("mySensor.magZOffset = " + String(mySensor.magZOffset) + ";");
Serial.println("magX: " + String(mX));
Serial.println("maxY: " + String(mY));
Serial.println("magZ: " + String(mZ));
Serial.println("horizontal direction: " + String(mDirection));
Serial.println("at " + String(millis()) + "ms");
Serial.println(""); // Add an empty line
delay(500);
}

this is what showing on the ardunio monitor, I just show the compass code, not all the code, so miss the depth etc things image text in transcribednow, i want to ask, how to do the coding, can let the data showing becomes direction and angle, such as south east 60 degree, north west 135 degree. thank you

here here 16:40:10 13/10/16here Pressure: 1011.00 mbar Temperature: 18.96 deg C Depth: -0.02 m Altitude: 18.74 m above mean sea level sensorId: 0 gyroX: 0.00 gyroy: 0.00 gyroZ: 0.00 magX: -18.94 maxy: -8.37 magZ: 5.74 horizontal direction: -113.84 at 388804ms here here 16:40:10 13/10/16here Pressure: Temperature: 18.97 deg (C Depth: -0.02 m Altitude: 18.74 m above mean sea level sensorId: 0 gyroX: 0.00 gyroY: 0.00 gyroZ: 0.00 magX: -17.75 maxY: -7.17 1011.00 mbar Autoscroll Show timestamp here here 16:40:10 13/10/16here Pressure: 1011.00 mbar Temperature: 18.96 deg C Depth: -0.02 m Altitude: 18.74 m above mean sea level sensorId: 0 gyroX: 0.00 gyroy: 0.00 gyroZ: 0.00 magX: -18.94 maxy: -8.37 magZ: 5.74 horizontal direction: -113.84 at 388804ms here here 16:40:10 13/10/16here Pressure: Temperature: 18.97 deg (C Depth: -0.02 m Altitude: 18.74 m above mean sea level sensorId: 0 gyroX: 0.00 gyroY: 0.00 gyroZ: 0.00 magX: -17.75 maxY: -7.17 1011.00 mbar Autoscroll Show timestamp

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions