Correction de la lib car erreur de format dans les calculs

Committer:
sandwich
Date:
Wed Aug 06 17:27:29 2014 +0000
Revision:
3:77cf75d8a6ce
Parent:
1:a1f3bd291a0c
Child:
4:ff505486c804
added controller class for hysteresis. Video taking works now as do servos , motor control, and IMU. Not using interrupts anymore

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rkk 0:d558dda1e821 1 /*
rkk 0:d558dda1e821 2 Permission is hereby granted, free of charge, to any person obtaining a copy
rkk 0:d558dda1e821 3 of this software and associated documentation files (the "Software"), to deal
rkk 0:d558dda1e821 4 in the Software without restriction, including without limitation the rights
rkk 0:d558dda1e821 5 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
rkk 0:d558dda1e821 6 copies of the Software, and to permit persons to whom the Software is
rkk 0:d558dda1e821 7 furnished to do so, subject to the following conditions:
rkk 0:d558dda1e821 8
rkk 0:d558dda1e821 9 The above copyright notice and this permission notice shall be included in
rkk 0:d558dda1e821 10 all copies or substantial portions of the Software.
rkk 0:d558dda1e821 11
rkk 0:d558dda1e821 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
rkk 0:d558dda1e821 13 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
rkk 0:d558dda1e821 14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
rkk 0:d558dda1e821 15 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
rkk 0:d558dda1e821 16 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
rkk 0:d558dda1e821 17 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
rkk 0:d558dda1e821 18 THE SOFTWARE.
rkk 0:d558dda1e821 19
rkk 1:a1f3bd291a0c 20 * Library for Pressure Sensors of type MS5803-x of MEAS Switzerland (www.meas-spec.com).
sandwich 3:77cf75d8a6ce 21 * The driver uses I2C mode (sensor's Protocol Select (PS) pin pulled to high).
rkk 1:a1f3bd291a0c 22 * MS5803-01BA was successfully tested by Raig Kaufer.
rkk 1:a1f3bd291a0c 23 * MS5803-14BA (Underwater Pressure Sensor 14 bar) was successfully tested by Robert Katzschmann
sandwich 3:77cf75d8a6ce 24 * Other types of MEAS are compatible but not tested
rkk 1:a1f3bd291a0c 25 * Written by Raig Kaufer, distribute freely!
rkk 1:a1f3bd291a0c 26 * Revised by Robert Katzschmann
rkk 0:d558dda1e821 27 */
rkk 0:d558dda1e821 28
rkk 0:d558dda1e821 29 #include <stdlib.h>
rkk 0:d558dda1e821 30 #include "MS5803.h"
rkk 0:d558dda1e821 31
rkk 0:d558dda1e821 32
rkk 0:d558dda1e821 33 /*
rkk 0:d558dda1e821 34 * Sensor operating function according data sheet
rkk 0:d558dda1e821 35 */
rkk 0:d558dda1e821 36
sandwich 3:77cf75d8a6ce 37 void MS5803::MS5803Init(void)
sandwich 3:77cf75d8a6ce 38 {
sandwich 3:77cf75d8a6ce 39 MS5803Reset();
sandwich 3:77cf75d8a6ce 40 MS5803ReadProm();
sandwich 3:77cf75d8a6ce 41 return;
sandwich 3:77cf75d8a6ce 42 }
sandwich 3:77cf75d8a6ce 43
rkk 0:d558dda1e821 44 /* Send soft reset to the sensor */
sandwich 3:77cf75d8a6ce 45 void MS5803::MS5803Reset(void)
sandwich 3:77cf75d8a6ce 46 {
rkk 0:d558dda1e821 47 /* transmit out 1 byte reset command */
rkk 0:d558dda1e821 48 ms5803_tx_data[0] = ms5803_reset;
rkk 0:d558dda1e821 49 if ( i2c.write( device_address, ms5803_tx_data, 1 ) );
sandwich 3:77cf75d8a6ce 50 //printf("send soft reset");
rkk 0:d558dda1e821 51 wait_ms(20);
rkk 0:d558dda1e821 52 }
rkk 0:d558dda1e821 53
rkk 0:d558dda1e821 54 /* read the sensor calibration data from rom */
sandwich 3:77cf75d8a6ce 55 void MS5803::MS5803ReadProm(void)
sandwich 3:77cf75d8a6ce 56 {
rkk 0:d558dda1e821 57 uint8_t i,j;
rkk 0:d558dda1e821 58 for (i=0; i<8; i++) {
rkk 0:d558dda1e821 59 j = i;
rkk 0:d558dda1e821 60 ms5803_tx_data[0] = ms5803_PROMread + (j<<1);
rkk 0:d558dda1e821 61 if ( i2c.write( device_address, ms5803_tx_data, 1 ) );
rkk 0:d558dda1e821 62 if ( i2c.read( device_address, ms5803_rx_data, 2 ) );
rkk 0:d558dda1e821 63 C[i] = ms5803_rx_data[1] + (ms5803_rx_data[0]<<8);
rkk 0:d558dda1e821 64 }
rkk 0:d558dda1e821 65 }
rkk 0:d558dda1e821 66
rkk 0:d558dda1e821 67 /* Start the sensor pressure conversion */
sandwich 3:77cf75d8a6ce 68 void MS5803::MS5803ConvertD1(void)
sandwich 3:77cf75d8a6ce 69 {
rkk 0:d558dda1e821 70 ms5803_tx_data[0] = ms5803_convD1;
rkk 0:d558dda1e821 71 if ( i2c.write( device_address, ms5803_tx_data, 1 ) );
rkk 0:d558dda1e821 72 }
rkk 0:d558dda1e821 73
rkk 0:d558dda1e821 74 /* Start the sensor temperature conversion */
sandwich 3:77cf75d8a6ce 75 void MS5803:: MS5803ConvertD2(void)
sandwich 3:77cf75d8a6ce 76 {
rkk 0:d558dda1e821 77 ms5803_tx_data[0] = ms5803_convD2;
rkk 0:d558dda1e821 78 if ( i2c.write( device_address, ms5803_tx_data, 1 ) );
rkk 0:d558dda1e821 79 }
rkk 0:d558dda1e821 80
sandwich 3:77cf75d8a6ce 81 /* Read the previous started conversion results */
sandwich 3:77cf75d8a6ce 82 int32_t MS5803::MS5803ReadADC(void)
sandwich 3:77cf75d8a6ce 83 {
rkk 0:d558dda1e821 84 int32_t adc;
rkk 0:d558dda1e821 85 wait_ms(150);
rkk 0:d558dda1e821 86 ms5803_tx_data[0] = ms5803_ADCread;
rkk 0:d558dda1e821 87 if ( i2c.write( device_address, ms5803_tx_data, 1 ) );
rkk 0:d558dda1e821 88 if ( i2c.read( device_address, ms5803_rx_data, 3 ) );
rkk 0:d558dda1e821 89 adc = ms5803_rx_data[2] + (ms5803_rx_data[1]<<8) + (ms5803_rx_data[0]<<16);
rkk 0:d558dda1e821 90 return (adc);
rkk 0:d558dda1e821 91 }
rkk 0:d558dda1e821 92
rkk 0:d558dda1e821 93 /* return the results */
sandwich 3:77cf75d8a6ce 94 float MS5803::MS5803_Pressure (void)
sandwich 3:77cf75d8a6ce 95 {
rkk 0:d558dda1e821 96 return P_MS5803;
rkk 0:d558dda1e821 97 }
sandwich 3:77cf75d8a6ce 98 float MS5803::MS5803_Temperature (void)
sandwich 3:77cf75d8a6ce 99 {
rkk 0:d558dda1e821 100 return T_MS5803;
rkk 0:d558dda1e821 101 }
rkk 0:d558dda1e821 102
rkk 0:d558dda1e821 103 /* Sensor reading and calculation procedure */
sandwich 3:77cf75d8a6ce 104 void MS5803::Barometer_MS5803(void)
sandwich 3:77cf75d8a6ce 105 {
rkk 0:d558dda1e821 106 int32_t dT, temp;
rkk 0:d558dda1e821 107 int64_t OFF, SENS, press;
rkk 0:d558dda1e821 108
sandwich 3:77cf75d8a6ce 109 //no need to do this everytime!
sandwich 3:77cf75d8a6ce 110 //MS5803Reset(); // reset the sensor
sandwich 3:77cf75d8a6ce 111 //MS5803ReadProm(); // read the calibration values
sandwich 3:77cf75d8a6ce 112
sandwich 3:77cf75d8a6ce 113
rkk 0:d558dda1e821 114 MS5803ConvertD1(); // start pressure conversion
rkk 0:d558dda1e821 115 D1 = MS5803ReadADC(); // read the pressure value
rkk 0:d558dda1e821 116 MS5803ConvertD2(); // start temperature conversion
rkk 0:d558dda1e821 117 D2 = MS5803ReadADC(); // read the temperature value
rkk 0:d558dda1e821 118
rkk 0:d558dda1e821 119 /* calculation according MS5803-01BA data sheet DA5803-01BA_006 */
rkk 0:d558dda1e821 120 dT = D2 - (C[5]* 256);
rkk 0:d558dda1e821 121 OFF = (int64_t)C[2] * (1<<16) + ((int64_t)dT * (int64_t)C[4]) / (1<<7);
rkk 0:d558dda1e821 122 SENS = (int64_t)C[1] * (1<<15) + ((int64_t)dT * (int64_t)C[3]) / (1<<8);
rkk 0:d558dda1e821 123
rkk 0:d558dda1e821 124 temp = 2000 + (dT * C[6]) / (1<<23);
rkk 0:d558dda1e821 125 T_MS5803 = (float) temp / 100.0f; // result of temperature in deg C in this var
rkk 0:d558dda1e821 126 press = (((int64_t)D1 * SENS) / (1<<21) - OFF) / (1<<15);
rkk 0:d558dda1e821 127 P_MS5803 = (float) press / 100.0f; // result of pressure in mBar in this var
rkk 0:d558dda1e821 128 }