#include #include #include #define MPU9250_ADDR 0x68 MPU9250_WE MPU9250 = MPU9250_WE(MPU9250_ADDR); #define SEALEVELPRESSURE_HPA (1019.7) // Change to current reported reading Adafruit_BME280 altimeter; float net_gravity_accel; void setup() { Serial.begin(115200); Wire.begin(); if (!MPU9250.init()) { Serial.println("MPU9520 is not responding"); } else { Serial.println("MPU9520 is connected"); } if (!MPU9250.initMagnetometer()) { Serial.println("Magnetometer is not responding"); } else { Serial.println("Magnetometer is connected"); } /*******************************************************************************************************/ /*MPU9250 General Settings*/ //MPU9250.autoOffsets(); // Lay the sensor on a level surface and call this function /* Sample rate divider divides the output rate of the gyroscope and accelerometer. * Sample rate = Internal sample rate / (1 + divider) * It can only be applied if the corresponding DLPF is enabled and 0 60) { uncorrected_data.angles.x = MPU9250.getPitch(); } if (uncorrected_data.angles.y > 60) { uncorrected_data.angles.y = MPU9250.getRoll(); }*/ //float net_gravity_accel = sqrt(uncorrected_data.accel.x*uncorrected_data.accel.x + uncorrected_data.accel.y*uncorrected_data.accel.y + uncorrected_data.accel.z*uncorrected_data.accel.z); xyzFloat corrected_accel; corrected_accel.x = uncorrected_data.accel.x - (net_gravity_accel*sin(uncorrected_data.angles.x*0.0174532)); corrected_accel.x = round_decimals(corrected_accel.x, 2); corrected_accel.y = uncorrected_data.accel.y - (net_gravity_accel*sin(uncorrected_data.angles.y*0.0174532)); corrected_accel.y = round_decimals(corrected_accel.y, 2); corrected_accel.z = uncorrected_data.accel.z - (net_gravity_accel*sin(uncorrected_data.angles.z*0.0174532)); corrected_accel.z = round_decimals(corrected_accel.z, 2); Serial.println("Acceleration without Gravity in m/s/s (x,y,z):"); Serial.print(corrected_accel.x); Serial.print(" "); Serial.print(corrected_accel.y); Serial.print(" "); Serial.print(corrected_accel.z); Serial.print(" "); Serial.println(""); return corrected_accel; } xyzFloat get_average_accel(int num_samples, int millis_delay) { /* * This function helps reduced the inaccuracy of the accelerometer by averaging readings * It returns an xyzFloat struct as defined in the MPU9250 library * num_samples is the number of readings from the accelerometer to take * millis_delay is the delay in between readings */ xyzFloat reading; // Temporary struct to store each reading float x_sum, y_sum, z_sum; // Floats to tally up the total value of the readings to then divide by the total number of readings for (int i = 0; i < num_samples; i++) { reading = MPU9250.getAccValues(); x_sum += reading.x; y_sum += reading.y; z_sum += reading.z; delay(millis_delay); } xyzFloat average_readings; // xyzFloat struct to store and return the averages average_readings.x = x_sum / num_samples; average_readings.y = y_sum / num_samples; average_readings.z = z_sum / num_samples; return average_readings; } float get_net_gravity_accel() { xyzFloat accel = get_median_accel(50, 20); // Get the median of 50 measurements with 20ms between them return sqrt(accel.x*accel.x + accel.y*accel.y + accel.z*accel.z); } float get_mag_heading(xyzFloat mag_readings) { return atan2(mag_readings.y*0.01, mag_readings.x*0.01)*180/3.14169; } float round_decimals(float num, int places) { // num is the original number to be rounded // places is the number of figures after the decimal place to keep float new_num = num * pow(10, places); new_num += (0.5 * pow(10, -1*(places-1))); return float(int(new_num) / pow(10, places)); } void round_to_zero(float *num, float *margin) { // num is the original number to be rounded to zero if the number is within an absolute distance "margin" away from zero if (*num >= -1*(*margin) && *num <= *margin) { *num = 0.0; } /*How to call it float number = 0.07; float margin = 0.05; round_to_zero(&number, &margin); Serial.println(number); */ }