revisions to pressure and magnetometer

Dependencies:   HMC5883L IMU MPU6050 MS5803 MS5837 mbed-rtos mbed sensor

Fork of sensor_library_test by Andrew Olguin

Revision:
3:6ca88b9c44c1
Parent:
1:cea9d83b8636
Child:
4:60340d6eed1f
--- a/main.cpp	Sun Jun 29 21:53:23 2014 +0000
+++ b/main.cpp	Mon Jul 25 00:51:01 2016 +0000
@@ -1,224 +1,112 @@
 
-/* MPU6050 Basic Example Code
- by: Kris Winer
- date: May 1, 2014
- license: Beerware - Use this code however you'd like. If you 
- find it useful you can buy me a beer some time.
- 
- Demonstrate  MPU-6050 basic functionality including initialization, accelerometer trimming, sleep mode functionality as well as
- parameterizing the register addresses. Added display functions to allow display to on breadboard monitor. 
- No DMP use. We just want to get out the accelerations, temperature, and gyro readings.
- 
- SDA and SCL should have external pull-up resistors (to 3.3V).
- 10k resistors worked for me. They should be on the breakout
- board.
- 
- Hardware setup:
- MPU6050 Breakout --------- Arduino
- 3.3V --------------------- 3.3V
- SDA ----------------------- A4
- SCL ----------------------- A5
- GND ---------------------- GND
- 
-  Note: The MPU6050 is an I2C sensor and uses the Arduino Wire library. 
- Because the sensor is not 5V tolerant, we are using a 3.3 V 8 MHz Pro Mini or a 3.3 V Teensy 3.1.
- We have disabled the internal pull-ups used by the Wire library in the Wire.h/twi.c utility file.
- We are also using the 400 kHz fast I2C mode by setting the TWI_FREQ  to 400000L /twi.h utility file.
+/* 
+Connections information:
+MPU to stm32F401RE NUCLEO Connection information
+VCC -> 3.3V
+SCL -> D15
+SDA -> D14
+AD0 -> high
+
+HMC5883L
+VCC -> 3.3V
+SCL -> D7
+SDA -> D5
+
+MS5803
+VCC -> 3.3V
+SCL -> D7
+SDA -> D5
+
+All grounds go to common ground
  */
  
 #include "mbed.h"
-#include "MPU6050.h"
-#include "N5110.h"
+#include "sensor.h"
 
-// Using NOKIA 5110 monochrome 84 x 48 pixel display
-// pin 9 - Serial clock out (SCLK)
-// pin 8 - Serial data out (DIN)
-// pin 7 - Data/Command select (D/C)
-// pin 5 - LCD chip select (CS)
-// pin 6 - LCD reset (RST)
-//Adafruit_PCD8544 display = Adafruit_PCD8544(9, 8, 7, 5, 6);
+int main() {
+    sensor_init();
+    while (1) {
+        sensor_update();
+        print_data();
+        //sensor_msg();    
+    }
+}
 
-float sum = 0;
-uint32_t sumCount = 0;
+/*#include "MPU6050.h"
+#include "MS5803.h"
+#include "MS5837.h"
+#include "IMU.h"
+#include "HMC5883L.h"
 
-   MPU6050 mpu6050;
-   
-   Timer t;
+MPU6050 mpu6050;
+DigitalOut led1(LED1);
+//MS5837 pressure_sensor_1 = MS5803(I2C_SDA, I2C_SCL);
+//MS5837 pressure_sensor_2 = MS5803(PB_4, PA_8);
 
-   Serial pc(USBTX, USBRX); // tx, rx
+HMC5883L compass(PB_4, PA_8);
+MS5837 pressure_sensor_1 = MS5837(I2C_SDA, I2C_SCL, ms5837_addr_no_CS);
+MS5837 pressure_sensor_2 = MS5837(PB_4, PA_8, ms5837_addr_no_CS);
 
-   //        VCC,   SCE,  RST,  D/C,  MOSI,S CLK, LED
-   N5110 lcd(PA_8, PB_10, PA_9, PA_6, PA_7, PA_5, PC_7);
-        
+//Serial pc(USBTX, USBRX);
+
 int main()
 {
-  pc.baud(9600);  
-
-  //Set up I2C
-  i2c.frequency(400000);  // use fast (400 kHz) I2C   
-  
-  t.start();        
-  
-  lcd.init();
-  lcd.setBrightness(0.05);
-  
+    //Test Compass
+    //pc.printf("Compass mode: %b\n", compass.getMode());
+    int16_t mag[3] = {0};
+    
+    printf("Begining Initialization\n");
+    
+    int error_cnt = 0;
     
-  // Read the WHO_AM_I register, this is a good test of communication
-  uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050);  // Read WHO_AM_I register for MPU-6050
-  pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r");
-  
-  if (whoami == 0x68) // WHO_AM_I should always be 0x68
-  {  
-    pc.printf("MPU6050 is online...");
-    wait(1);
-    lcd.clear();
-    lcd.printString("MPU6050 OK", 0, 0);
-
+    float depth;
+    float pressure1, depth1;
+    float pressure2, depth2;
     
-    mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values
-    pc.printf("x-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[0]); pc.printf("% of factory value \n\r");
-    pc.printf("y-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[1]); pc.printf("% of factory value \n\r");
-    pc.printf("z-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[2]); pc.printf("% of factory value \n\r");
-    pc.printf("x-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[3]); pc.printf("% of factory value \n\r");
-    pc.printf("y-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[4]); pc.printf("% of factory value \n\r");
-    pc.printf("z-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[5]); pc.printf("% of factory value \n\r");
-    wait(1);
-
-    if(SelfTest[0] < 1.0f && SelfTest[1] < 1.0f && SelfTest[2] < 1.0f && SelfTest[3] < 1.0f && SelfTest[4] < 1.0f && SelfTest[5] < 1.0f) 
-    {
-    mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration
-    mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers  
-    mpu6050.initMPU6050(); pc.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
+    pressure_sensor_1.MS5837Init();
+    pressure_sensor_2.MS5837Init();
+    pc.printf("Initialized begining readings\n");
 
-    lcd.clear();
-    lcd.printString("MPU6050", 0, 0);
-    lcd.printString("pass self test", 0, 1);
-    lcd.printString("initializing", 0, 2);  
-    wait(2);
-       }
-    else
-    {
-    pc.printf("Device did not the pass self-test!\n\r");
- 
-       lcd.clear();
-       lcd.printString("MPU6050", 0, 0);
-       lcd.printString("no pass", 0, 1);
-       lcd.printString("self test", 0, 2);      
-      }
-    }
-    else
-    {
-    pc.printf("Could not connect to MPU6050: \n\r");
-    pc.printf("%#x \n",  whoami);
- 
-    lcd.clear();
-    lcd.printString("MPU6050", 0, 0);
-    lcd.printString("no connection", 0, 1);
-    lcd.printString("0x", 0, 2);  lcd.setXYAddress(20, 2); lcd.printChar(whoami);
- 
-    while(1) ; // Loop forever if communication doesn't happen
-  }
-
-
-
- while(1) {
-  
-  // If data ready bit set, all data registers have new data
-  if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) {  // check if data ready interrupt
-    mpu6050.readAccelData(accelCount);  // Read the x/y/z adc values
-    mpu6050.getAres();
+    IMUinit(mpu6050);
     
-    // Now we'll calculate the accleration value into actual g's
-    ax = (float)accelCount[0]*aRes - accelBias[0];  // get actual g value, this depends on scale being set
-    ay = (float)accelCount[1]*aRes - accelBias[1];   
-    az = (float)accelCount[2]*aRes - accelBias[2];  
-   
-    mpu6050.readGyroData(gyroCount);  // Read the x/y/z adc values
-    mpu6050.getGres();
- 
-    // Calculate the gyro value into actual degrees per second
-    gx = (float)gyroCount[0]*gRes; // - gyroBias[0];  // get actual gyro value, this depends on scale being set
-    gy = (float)gyroCount[1]*gRes; // - gyroBias[1];  
-    gz = (float)gyroCount[2]*gRes; // - gyroBias[2];   
-
-    tempCount = mpu6050.readTempData();  // Read the x/y/z adc values
-    temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade
-   }  
-   
-    Now = t.read_us();
-    deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
-    lastUpdate = Now;
-    
-    sum += deltat;
-    sumCount++;
-    
-    if(lastUpdate - firstUpdate > 10000000.0f) {
-     beta = 0.04;  // decrease filter gain after stabilized
-     zeta = 0.015; // increasey bias drift gain after stabilized
-    }
-    
-   // Pass gyro rate as rad/s
-    mpu6050.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f);
-
-    // Serial print and/or display at 0.5 s rate independent of data rates
-    delt_t = t.read_ms() - count;
-    if (delt_t > 500) { // update LCD once per half-second independent of read rate
-
-    pc.printf("ax = %f", 1000*ax); 
-    pc.printf(" ay = %f", 1000*ay); 
-    pc.printf(" az = %f  mg\n\r", 1000*az); 
+    while(1) {
+        pressure_sensor_1.Barometer_MS5837();
+        pressure_sensor_2.Barometer_MS5837();
+        pressure1 = pressure_sensor_1.MS5837_Pressure();
+        pressure2 = pressure_sensor_2.MS5837_Pressure();
+        
+        //Do some error checking
+        if ((pressure1 > 900 && pressure1 < 3000) && (pressure2 > 900 && pressure2 < 3000)) {
+            depth1 = pressure_sensor_1.depth();
+            depth2 = pressure_sensor_2.depth();
+            
+            depth = (depth1 + depth2) / 2;
+            error_cnt = 0;
+        } else if (pressure2 > 900 && pressure2 < 3000) {
+            depth = pressure_sensor_2.depth();
+            
+            error_cnt = 0;
+        } else if (pressure1 > 900 && pressure1 < 3000) {
+            depth = pressure_sensor_1.depth();
+            error_cnt = 0;
+        } else {
+            error_cnt++;
+            if (error_cnt > 50) {
+                //do something
+                pc.printf("Pressure sensor error");   
+            }
+        }
+        pc.printf("Depth is: %f\n", depth);
+        
+        //IMU Read
+        IMUUpdate(mpu6050);
 
-    pc.printf("gx = %f", gx); 
-    pc.printf(" gy = %f", gy); 
-    pc.printf(" gz = %f  deg/s\n\r", gz); 
-    
-    pc.printf(" temperature = %f  C\n\r", temperature); 
-    
-    pc.printf("q0 = %f\n\r", q[0]);
-    pc.printf("q1 = %f\n\r", q[1]);
-    pc.printf("q2 = %f\n\r", q[2]);
-    pc.printf("q3 = %f\n\r", q[3]);      
-    
-    lcd.clear();
-    lcd.printString("MPU6050", 0, 0);
-    lcd.printString("x   y   z", 0, 1);
-    lcd.setXYAddress(0, 2); lcd.printChar((char)(1000*ax));
-    lcd.setXYAddress(20, 2); lcd.printChar((char)(1000*ay));
-    lcd.setXYAddress(40, 2); lcd.printChar((char)(1000*az)); lcd.printString("mg", 66, 2);
-    
-    
-  // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
-  // In this coordinate system, the positive z-axis is down toward Earth. 
-  // Yaw is the angle between Sensor x-axis and Earth magnetic North (or true North if corrected for local declination, looking down on the sensor positive yaw is counterclockwise.
-  // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
-  // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
-  // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
-  // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
-  // applied in the correct order which for this configuration is yaw, pitch, and then roll.
-  // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
-    yaw   = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]);   
-    pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
-    roll  = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]), q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
-    pitch *= 180.0f / PI;
-    yaw   *= 180.0f / PI; 
-    roll  *= 180.0f / PI;
-
-//    pc.printf("Yaw, Pitch, Roll: \n\r");
-//    pc.printf("%f", yaw);
-//    pc.printf(", ");
-//    pc.printf("%f", pitch);
-//    pc.printf(", ");
-//    pc.printf("%f\n\r", roll);
-//    pc.printf("average rate = "); pc.printf("%f", (sumCount/sum)); pc.printf(" Hz\n\r");
-
-     pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll);
-     pc.printf("average rate = %f\n\r", (float) sumCount/sum);
- 
-    myled= !myled;
-    count = t.read_ms(); 
-    sum = 0;
-    sumCount = 0; 
-}
-}
- 
- }
\ No newline at end of file
+        //Compass read
+        compass.getXYZ(mag);
+        pc.printf("Mag values: %d, %d, %d\n", mag[0], mag[1], mag[2]); 
+        pc.printf("Heading: %f\n",compass.getHeadingXYDeg());  
+        
+        led1 = !led1;
+        //Thread::wait(500);
+    }
+}*/
\ No newline at end of file