Test for MS5803 and MS5837 pressure sensors (pressure and temp readings) and QEI testing for encoder.

Dependencies:   MS5803 QEI mbed

Files at this revision

API Documentation at this revision

Comitter:
alex93
Date:
Sun Mar 20 03:05:25 2016 +0000
Commit message:
Test for MS5803 and MS5837 pressure sensors (pressure and temp readings) and QEI testing for encoder.

Changed in this revision

MS5803.lib Show annotated file Show diff for this revision Revisions of this file
MS5837/MS5837.cpp Show annotated file Show diff for this revision Revisions of this file
MS5837/MS5837.h Show annotated file Show diff for this revision Revisions of this file
QEI.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f8bc804eadbc MS5803.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MS5803.lib	Sun Mar 20 03:05:25 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/jetfishteam/code/MS5803/#ff505486c804
diff -r 000000000000 -r f8bc804eadbc MS5837/MS5837.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MS5837/MS5837.cpp	Sun Mar 20 03:05:25 2016 +0000
@@ -0,0 +1,100 @@
+#include <stdlib.h>
+#include "MS5837.h"
+
+
+/*
+ * Sensor operating function according data sheet
+ */
+
+void MS5837::MS5837Init(void)
+{
+    MS5837Reset();
+    MS5837ReadProm();
+    return;
+}
+
+/* Send soft reset to the sensor */
+void MS5837::MS5837Reset(void)
+{
+    /* transmit out 1 byte reset command */
+    ms5837_tx_data[0] = ms5837_reset;
+    if ( i2c.write( device_address,  ms5837_tx_data, 1 ) );
+    //printf("send soft reset");
+    wait_ms(20);
+}
+
+/* read the sensor calibration data from rom */
+void MS5837::MS5837ReadProm(void)
+{
+    uint8_t i,j;
+    for (i=0; i<8; i++) {
+        j = i;
+        ms5837_tx_data[0] = ms5837_PROMread + (j<<1);
+        if ( i2c.write( device_address,  ms5837_tx_data, 1 ) );
+        if ( i2c.read( device_address,  ms5837_rx_data, 2 ) );
+        C[i]   = ms5837_rx_data[1] + (ms5837_rx_data[0]<<8);
+    }
+}
+
+/* Start the sensor pressure conversion */
+void MS5837::MS5837ConvertD1(void)
+{
+    ms5837_tx_data[0] = ms5837_convD1;
+    if ( i2c.write( device_address,  ms5837_tx_data, 1 ) );
+}
+
+/* Start the sensor temperature conversion */
+void MS5837:: MS5837ConvertD2(void)
+{
+    ms5837_tx_data[0] = ms5837_convD2;
+    if ( i2c.write( device_address,  ms5837_tx_data, 1 ) );
+}
+
+/* Read the previous started conversion results */
+int32_t MS5837::MS5837ReadADC(void)
+{
+    int32_t adc;
+    wait_ms(150);
+    ms5837_tx_data[0] = ms5837_ADCread;
+    if ( i2c.write( device_address,  ms5837_tx_data, 1 ) );
+    if ( i2c.read( device_address,  ms5837_rx_data, 3 ) );
+    adc = ms5837_rx_data[2] + (ms5837_rx_data[1]<<8) + (ms5837_rx_data[0]<<16);
+    return (adc);
+}
+
+/* return the results */
+float MS5837::MS5837_Pressure (void)
+{
+    return P_MS5837;
+}
+float MS5837::MS5837_Temperature (void)
+{
+    return T_MS5837;
+}
+
+/* Sensor reading and calculation procedure */
+void MS5837::Barometer_MS5837(void)
+{
+    int32_t dT, temp;
+    int64_t OFF, SENS, press;
+
+    //no need to do this everytime!
+    //MS5837Reset();                 // reset the sensor
+    //MS5837ReadProm();             // read the calibration values
+    
+    
+    MS5837ConvertD1();             // start pressure conversion
+    D1 = MS5837ReadADC();        // read the pressure value
+    MS5837ConvertD2();             // start temperature conversion
+    D2 = MS5837ReadADC();         // read the temperature value
+
+    /* calculation according MS5837-01BA data sheet DA5837-01BA_006 */
+    dT       = D2 - (C[5]* 256);
+    OFF      = (int64_t)C[2] * (1<<16) + ((int64_t)dT * (int64_t)C[4]) / (1<<7);
+    SENS     = (int64_t)C[1] * (1<<15) + ((int64_t)dT * (int64_t)C[3]) / (1<<8);
+
+    temp     = 2000 + (dT * C[6]) / (1<<23);
+    T_MS5837 = (float) temp / 100.0f;                 // result of temperature in deg C in this var
+    press    = (((int64_t)D1 * SENS) / (1<<21) - OFF) / (1<<13);
+    P_MS5837 = (float) press / 10.0f;                 // result of pressure in mBar in this var
+}
diff -r 000000000000 -r f8bc804eadbc MS5837/MS5837.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MS5837/MS5837.h	Sun Mar 20 03:05:25 2016 +0000
@@ -0,0 +1,64 @@
+#include "mbed.h"
+
+#ifndef MS5837_H
+#define MS5837_H
+
+#define MS5837_RX_DEPTH 3 //
+#define MS5837_TX_DEPTH 2 //
+
+// choose your connection here
+#define ms5837_addr_no_CS  0x76 //0b1110110
+
+#define ms5837_reset       0x1E // Sensor Reset
+
+#define ms5837_convD1_256  0x40 // Convert D1 OSR 256
+#define ms5837_convD1_512  0x42 // Convert D1 OSR 512
+#define ms5837_convD1_1024 0x44 // Convert D1 OSR 1024
+#define ms5837_convD1_2048 0x46 // Convert D1 OSR 2048
+#define ms5837_convD1_4096 0x48 // Convert D1 OSR 4096
+#define ms5837_convD1_8192 0x4A // Convert D1 OSR 8192
+
+#define ms5837_convD1 ms5837_convD1_4096 // choose your sampling rate here
+
+#define ms5837_convD2_256  0x50 // Convert D2 OSR  256
+#define ms5837_convD2_512  0x52 // Convert D2 OSR  512
+#define ms5837_convD2_1024 0x54 // Convert D2 OSR 1024
+#define ms5837_convD2_2048 0x56 // Convert D2 OSR 2048
+#define ms5837_convD2_4096 0x58 // Convert D2 OSR 4096
+#define ms5837_convD2_8192 0x5A // Convert D2 OSR 8192
+
+#define ms5837_convD2 ms5837_convD2_4096 // choose your sampling rate here
+
+#define ms5837_ADCread     0x00 // read ADC command
+#define ms5837_PROMread    0xA0 // read PROM command base address
+
+class MS5837{
+private:
+    int D1, D2, Temp, C[8];
+    float T_MS5837, P_MS5837;
+    /* Data buffers */
+    char ms5837_rx_data[MS5837_RX_DEPTH];
+    char ms5837_tx_data[MS5837_TX_DEPTH];
+
+public:
+    MS5837 (PinName sda, PinName scl,
+            char ms5837_addr = ms5837_addr_no_CS  )
+            : i2c( sda, scl ), device_address( ms5837_addr << 1 ) {
+    }
+    void MS5837Init(void);
+    void MS5837Reset(void);
+    void MS5837ReadProm(void);
+    void MS5837ConvertD1(void);
+    void MS5837ConvertD2(void);
+    int32_t MS5837ReadADC(void);
+    float MS5837_Pressure (void);
+    float MS5837_Temperature (void);
+    void Barometer_MS5837(void);
+
+
+private:
+    I2C     i2c;
+    char    device_address;
+
+};
+#endif
diff -r 000000000000 -r f8bc804eadbc QEI.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QEI.lib	Sun Mar 20 03:05:25 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/aberk/code/QEI/#5c2ad81551aa
diff -r 000000000000 -r f8bc804eadbc main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Mar 20 03:05:25 2016 +0000
@@ -0,0 +1,97 @@
+/*********************************************************************************
+* This code initializes the MS5803 and MS5837 pressure sensors on pins used
+* during testing (in March 2016 version of fish, the Open ROV IMU/Pressure Sensor
+* is connected to p28/p27, whereas this code uses both p28/p27 and p9/p10). It
+* constantly prints out the pressure and temperature readings of the two sensors.
+* It also establishes a QEI object on pins 25 and 24 from the encoder and
+* constantly prints the revolutions.
+*********************************************************************************/
+
+#include "mbed.h"
+#include "MS5803.h"
+#include "MS5837.h"
+#include "QEI.h"
+using namespace std;
+
+Serial pc(USBTX, USBRX);
+
+// Motor Driver
+AnalogIn pot(p15);
+PwmOut enable(p26);
+
+// Hall Sensor
+InterruptIn hallVoltage(p8);
+DigitalOut led2(LED2);
+
+// QEI
+QEI wheel(p25, p24, NC, 12); // 12=counts per revolution of motor shaft for the encoder
+
+void flip() {
+    led2 = !led2;
+}
+
+int main() {
+    // set serial transfer rate
+    pc.baud(9600);
+
+    // Initialize big pressure sensor
+    MS5803 big_sensor = MS5803(p28, p27, ms5803_addrCH);
+    big_sensor.MS5803Init();
+    float pressure_big;
+    float temp_big;
+    
+    // Initialize small pressure sensor
+    MS5837 small_sensor = MS5837(p9, p10, ms5837_addr_no_CS);
+    small_sensor.MS5837Init();
+    float pressure_small;
+    float temp_small;
+
+    // Motor & QEI initialization
+    wheel.reset();
+    enable.period(.000001);
+    int count = 0;
+    int previousHallVoltage = 0;
+
+    while(1) {
+        hallVoltage.rise(&flip);
+        hallVoltage.fall(&flip);
+        
+        // Update big pressure sensor
+        big_sensor.Barometer_MS5803();
+        pressure_big = big_sensor.MS5803_Pressure();
+        temp_big = big_sensor.MS5803_Temperature();
+        pc.printf("Big Sensor Pressure: %f\n", pressure_big);
+        pc.printf("Big Sensor Temperature: %f\n", temp_big);
+        
+        // Update small pressure sensor
+        small_sensor.Barometer_MS5837();
+        pressure_small = small_sensor.MS5837_Pressure();
+        temp_small = small_sensor.MS5837_Temperature();
+        pc.printf("Small Sensor Pressure: %f\n", pressure_small);
+        pc.printf("Small Sensor Temperature: %f\n\n", temp_small);
+        
+//        // Simply feedback loop for speed, changing values every 5 revolutions as detected by hall sensor
+//        if (count < 5) {
+//            enable = pot; //PWM enable output is proportional to analog pot input
+//        } else if (count < 10) {
+//            enable = pot/2;
+//        } else {
+//            enable = pot;
+//            count = 0;
+//        }
+//        if (previousHallVoltage==1 && hallVoltage==0) {
+//            count += 1;
+//            pc.printf("\n\n----END OF REVOLUTION----\nNumber of pulses: %i\n", wheel.getPulses());
+//            wheel.reset();
+//        }
+//        if (hallVoltage) {
+//            led2 = 1;
+//            previousHallVoltage = 1;
+//        } else {
+//            led2 = 0;
+//            previousHallVoltage = 0;
+//        }
+
+        pc.printf("Pulses is: %i\n", wheel.getPulses());
+    }
+}
diff -r 000000000000 -r f8bc804eadbc mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Mar 20 03:05:25 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa
\ No newline at end of file