9/12 AIL Regulate

Dependencies:   HCSR04_2 MPU6050_2 mbed SDFileSystem3

Fork of Autoflight2018_2 by 航空研究会

Committer:
HARUKIDELTA
Date:
Fri Sep 07 04:11:48 2018 +0000
Revision:
0:17f575135219
SBUS_READ

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HARUKIDELTA 0:17f575135219 1 /**
HARUKIDELTA 0:17f575135219 2 * BMP280 Combined humidity and pressure sensor library
HARUKIDELTA 0:17f575135219 3 *
HARUKIDELTA 0:17f575135219 4 * @author Toyomasa Watarai
HARUKIDELTA 0:17f575135219 5 * @version 1.0
HARUKIDELTA 0:17f575135219 6 * @date 06-April-2015
HARUKIDELTA 0:17f575135219 7 *
HARUKIDELTA 0:17f575135219 8 * Library for "BMP280 temperature, humidity and pressure sensor module" from Switch Science
HARUKIDELTA 0:17f575135219 9 * https://www.switch-science.com/catalog/2236/
HARUKIDELTA 0:17f575135219 10 *
HARUKIDELTA 0:17f575135219 11 * For more information about the BMP280:
HARUKIDELTA 0:17f575135219 12 * http://ae-bst.resource.bosch.com/media/products/dokumente/BMP280/BST-BMP280_DS001-10.pdf
HARUKIDELTA 0:17f575135219 13 */
HARUKIDELTA 0:17f575135219 14
HARUKIDELTA 0:17f575135219 15 #include "mbed.h"
HARUKIDELTA 0:17f575135219 16 #include "BMP280.h"
HARUKIDELTA 0:17f575135219 17
HARUKIDELTA 0:17f575135219 18 BMP280::BMP280(PinName sda, PinName scl, char slave_adr)
HARUKIDELTA 0:17f575135219 19 :
HARUKIDELTA 0:17f575135219 20 i2c_p(new I2C(sda, scl)),
HARUKIDELTA 0:17f575135219 21 i2c(*i2c_p),
HARUKIDELTA 0:17f575135219 22 address(slave_adr),
HARUKIDELTA 0:17f575135219 23 t_fine(0)
HARUKIDELTA 0:17f575135219 24 {
HARUKIDELTA 0:17f575135219 25 initialize();
HARUKIDELTA 0:17f575135219 26 }
HARUKIDELTA 0:17f575135219 27
HARUKIDELTA 0:17f575135219 28 BMP280::BMP280(I2C &i2c_obj, char slave_adr)
HARUKIDELTA 0:17f575135219 29 :
HARUKIDELTA 0:17f575135219 30 i2c_p(NULL),
HARUKIDELTA 0:17f575135219 31 i2c(i2c_obj),
HARUKIDELTA 0:17f575135219 32 address(slave_adr),
HARUKIDELTA 0:17f575135219 33 t_fine(0)
HARUKIDELTA 0:17f575135219 34 {
HARUKIDELTA 0:17f575135219 35 initialize();
HARUKIDELTA 0:17f575135219 36 }
HARUKIDELTA 0:17f575135219 37
HARUKIDELTA 0:17f575135219 38 BMP280::~BMP280()
HARUKIDELTA 0:17f575135219 39 {
HARUKIDELTA 0:17f575135219 40 if (NULL != i2c_p)
HARUKIDELTA 0:17f575135219 41 delete i2c_p;
HARUKIDELTA 0:17f575135219 42 }
HARUKIDELTA 0:17f575135219 43
HARUKIDELTA 0:17f575135219 44 void BMP280::initialize()
HARUKIDELTA 0:17f575135219 45 {
HARUKIDELTA 0:17f575135219 46 char cmd[18];
HARUKIDELTA 0:17f575135219 47
HARUKIDELTA 0:17f575135219 48 cmd[0] = 0xf2; // ctrl_hum
HARUKIDELTA 0:17f575135219 49 cmd[1] = 0x01; // Humidity oversampling x1
HARUKIDELTA 0:17f575135219 50 i2c.write(address, cmd, 2);
HARUKIDELTA 0:17f575135219 51
HARUKIDELTA 0:17f575135219 52 cmd[0] = 0xf4; // ctrl_meas
HARUKIDELTA 0:17f575135219 53 cmd[1] = 0x27; // Temparature oversampling x1, Pressure oversampling x1, Normal mode
HARUKIDELTA 0:17f575135219 54 i2c.write(address, cmd, 2);
HARUKIDELTA 0:17f575135219 55
HARUKIDELTA 0:17f575135219 56 cmd[0] = 0xf5; // config
HARUKIDELTA 0:17f575135219 57 cmd[1] = 0xa0; // Standby 1000ms, Filter off
HARUKIDELTA 0:17f575135219 58 i2c.write(address, cmd, 2);
HARUKIDELTA 0:17f575135219 59
HARUKIDELTA 0:17f575135219 60 cmd[0] = 0x88; // read dig_T regs
HARUKIDELTA 0:17f575135219 61 i2c.write(address, cmd, 1);
HARUKIDELTA 0:17f575135219 62 i2c.read(address, cmd, 6);
HARUKIDELTA 0:17f575135219 63
HARUKIDELTA 0:17f575135219 64 dig_T1 = (cmd[1] << 8) | cmd[0];
HARUKIDELTA 0:17f575135219 65 dig_T2 = (cmd[3] << 8) | cmd[2];
HARUKIDELTA 0:17f575135219 66 dig_T3 = (cmd[5] << 8) | cmd[4];
HARUKIDELTA 0:17f575135219 67
HARUKIDELTA 0:17f575135219 68 DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
HARUKIDELTA 0:17f575135219 69
HARUKIDELTA 0:17f575135219 70 cmd[0] = 0x8E; // read dig_P regs
HARUKIDELTA 0:17f575135219 71 i2c.write(address, cmd, 1);
HARUKIDELTA 0:17f575135219 72 i2c.read(address, cmd, 18);
HARUKIDELTA 0:17f575135219 73
HARUKIDELTA 0:17f575135219 74 dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
HARUKIDELTA 0:17f575135219 75 dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
HARUKIDELTA 0:17f575135219 76 dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
HARUKIDELTA 0:17f575135219 77 dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
HARUKIDELTA 0:17f575135219 78 dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
HARUKIDELTA 0:17f575135219 79 dig_P6 = (cmd[11] << 8) | cmd[10];
HARUKIDELTA 0:17f575135219 80 dig_P7 = (cmd[13] << 8) | cmd[12];
HARUKIDELTA 0:17f575135219 81 dig_P8 = (cmd[15] << 8) | cmd[14];
HARUKIDELTA 0:17f575135219 82 dig_P9 = (cmd[17] << 8) | cmd[16];
HARUKIDELTA 0:17f575135219 83
HARUKIDELTA 0:17f575135219 84 DEBUG_PRINT("dig_P = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9);
HARUKIDELTA 0:17f575135219 85
HARUKIDELTA 0:17f575135219 86 /* cmd[0] = 0xA1; // read dig_H regs
HARUKIDELTA 0:17f575135219 87 i2c.write(address, cmd, 1);
HARUKIDELTA 0:17f575135219 88 i2c.read(address, cmd, 1);
HARUKIDELTA 0:17f575135219 89 cmd[1] = 0xE1; // read dig_H regs
HARUKIDELTA 0:17f575135219 90 i2c.write(address, &cmd[1], 1);
HARUKIDELTA 0:17f575135219 91 i2c.read(address, &cmd[1], 7);
HARUKIDELTA 0:17f575135219 92
HARUKIDELTA 0:17f575135219 93 dig_H1 = cmd[0];
HARUKIDELTA 0:17f575135219 94 dig_H2 = (cmd[2] << 8) | cmd[1];
HARUKIDELTA 0:17f575135219 95 dig_H3 = cmd[3];
HARUKIDELTA 0:17f575135219 96 dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
HARUKIDELTA 0:17f575135219 97 dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
HARUKIDELTA 0:17f575135219 98 dig_H6 = cmd[7];
HARUKIDELTA 0:17f575135219 99
HARUKIDELTA 0:17f575135219 100 DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6);
HARUKIDELTA 0:17f575135219 101 */
HARUKIDELTA 0:17f575135219 102 }
HARUKIDELTA 0:17f575135219 103
HARUKIDELTA 0:17f575135219 104 float BMP280::getTemperature()
HARUKIDELTA 0:17f575135219 105 {
HARUKIDELTA 0:17f575135219 106 uint32_t temp_raw;
HARUKIDELTA 0:17f575135219 107 float tempf;
HARUKIDELTA 0:17f575135219 108 char cmd[4];
HARUKIDELTA 0:17f575135219 109
HARUKIDELTA 0:17f575135219 110 cmd[0] = 0xfa; // temp_msb
HARUKIDELTA 0:17f575135219 111 i2c.write(address, cmd, 1);
HARUKIDELTA 0:17f575135219 112 i2c.read(address, &cmd[1], 3);
HARUKIDELTA 0:17f575135219 113
HARUKIDELTA 0:17f575135219 114 temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
HARUKIDELTA 0:17f575135219 115
HARUKIDELTA 0:17f575135219 116 int32_t temp;
HARUKIDELTA 0:17f575135219 117
HARUKIDELTA 0:17f575135219 118 temp =
HARUKIDELTA 0:17f575135219 119 (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
HARUKIDELTA 0:17f575135219 120 ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
HARUKIDELTA 0:17f575135219 121
HARUKIDELTA 0:17f575135219 122 t_fine = temp;
HARUKIDELTA 0:17f575135219 123 temp = (temp * 5 + 128) >> 8;
HARUKIDELTA 0:17f575135219 124 tempf = (float)temp;
HARUKIDELTA 0:17f575135219 125
HARUKIDELTA 0:17f575135219 126 return (tempf/100.0f);
HARUKIDELTA 0:17f575135219 127 }
HARUKIDELTA 0:17f575135219 128
HARUKIDELTA 0:17f575135219 129 float BMP280::getPressure()
HARUKIDELTA 0:17f575135219 130 {
HARUKIDELTA 0:17f575135219 131 uint32_t press_raw;
HARUKIDELTA 0:17f575135219 132 float pressf;
HARUKIDELTA 0:17f575135219 133 char cmd[4];
HARUKIDELTA 0:17f575135219 134
HARUKIDELTA 0:17f575135219 135 cmd[0] = 0xf7; // press_msb
HARUKIDELTA 0:17f575135219 136 i2c.write(address, cmd, 1);
HARUKIDELTA 0:17f575135219 137 i2c.read(address, &cmd[1], 3);
HARUKIDELTA 0:17f575135219 138
HARUKIDELTA 0:17f575135219 139 press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
HARUKIDELTA 0:17f575135219 140
HARUKIDELTA 0:17f575135219 141 int32_t var1, var2;
HARUKIDELTA 0:17f575135219 142 uint32_t press;
HARUKIDELTA 0:17f575135219 143
HARUKIDELTA 0:17f575135219 144 var1 = (t_fine >> 1) - 64000;
HARUKIDELTA 0:17f575135219 145 var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
HARUKIDELTA 0:17f575135219 146 var2 = var2 + ((var1 * dig_P5) << 1);
HARUKIDELTA 0:17f575135219 147 var2 = (var2 >> 2) + (dig_P4 << 16);
HARUKIDELTA 0:17f575135219 148 var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
HARUKIDELTA 0:17f575135219 149 var1 = ((32768 + var1) * dig_P1) >> 15;
HARUKIDELTA 0:17f575135219 150 if (var1 == 0) {
HARUKIDELTA 0:17f575135219 151 return 0;
HARUKIDELTA 0:17f575135219 152 }
HARUKIDELTA 0:17f575135219 153 press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
HARUKIDELTA 0:17f575135219 154 if(press < 0x80000000) {
HARUKIDELTA 0:17f575135219 155 press = (press << 1) / var1;
HARUKIDELTA 0:17f575135219 156 } else {
HARUKIDELTA 0:17f575135219 157 press = (press / var1) * 2;
HARUKIDELTA 0:17f575135219 158 }
HARUKIDELTA 0:17f575135219 159 var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
HARUKIDELTA 0:17f575135219 160 var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
HARUKIDELTA 0:17f575135219 161 press = (press + ((var1 + var2 + dig_P7) >> 4));
HARUKIDELTA 0:17f575135219 162
HARUKIDELTA 0:17f575135219 163 pressf = (float)press;
HARUKIDELTA 0:17f575135219 164 return (pressf/100.0f);
HARUKIDELTA 0:17f575135219 165 }