Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Soyoon
Date:
Tue Jul 19 15:47:17 2016 +0000
Commit message:
Robo Frien

Changed in this revision

Barometer.cpp Show annotated file Show diff for this revision Revisions of this file
Barometer.h 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 9f0027e4c618 Barometer.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Barometer.cpp	Tue Jul 19 15:47:17 2016 +0000
@@ -0,0 +1,120 @@
+#include "mbed.h"
+#include "Barometer.h"
+ 
+#define WEATHER_Barometer 0xee
+#define xpow(x, y) ((long)1 << y)
+ 
+
+Barometer::Barometer (PinName p_sda, PinName p_scl, Barometer_oss p_oss) : i2c(p_sda, p_scl) {
+    init(p_oss);
+}
+ 
+Barometer::Barometer (I2C& p_i2c, Barometer_oss p_oss) : i2c(p_i2c) { 
+    init(p_oss);
+}
+ 
+
+float Barometer::get_temperature() {
+    return temperature;
+}
+ 
+float Barometer::get_pressure() {
+    return pressure;
+}
+ 
+void Barometer::update () {
+    long t, p, ut, up, x1, x2, x3, b3, b5, b6;
+    unsigned long b4, b7;
+ 
+    twi_writechar(WEATHER_Barometer, 0xf4, 0x2e);
+    wait(0.01);
+    ut = twi_readshort(WEATHER_Barometer, 0xf6);
+ 
+    twi_writechar(WEATHER_Barometer, 0xf4, 0x34 | (oss << 6));
+    wait(0.05);
+    up = twi_readlong(WEATHER_Barometer, 0xf6) >> (8 - oss);
+ 
+    x1 = (ut - ac6) * ac5 / xpow(2, 15);
+    x2 = (long)mc * xpow(2, 11) / (x1 + md);
+    b5 = x1 + x2;
+    t = (b5 + 8) / xpow(2, 4);
+    temperature = (float)t / 10.0;
+ 
+    b6 = b5 - 4000;
+    x1 = (b2 * (b6 * b6 / xpow(2, 12))) / xpow(2, 11);
+    x2 = ac2 * b6 / xpow(2, 11);
+    x3 = x1 + x2;
+    b3 = ((((unsigned long)ac1 * 4 + x3) << oss) + 2) / 4;
+    x1 = ac3 * b6 / xpow(2, 13);
+    x2 = (b1 * (b6 * b6 / xpow(2, 12))) / xpow(2, 16);
+    x3 = ((x1 + x2) + 2) / xpow(2, 2);
+    b4 = ac4 * (unsigned long)(x3 + 32768) / xpow(2, 15);
+    b7 = ((unsigned long)up - b3) * (50000 >> oss);
+    if (b7 < (unsigned long)0x80000000) {
+        p = (b7 * 2) / b4;
+    } else {
+        p = (b7 / b4) * 2;
+    }
+    x1 = (p / xpow(2, 8)) * (p / xpow(2, 8));
+    x1 = (x1 * 3038) / xpow(2, 16);
+    x2 = (-7357 * p) / xpow(2, 16);
+    p = p + (x1 + x2 + 3791) / xpow(2, 4);
+    pressure = (float)p / 100.0;
+}
+ 
+void Barometer::init (Barometer_oss p_oss) {
+    ac1 = twi_readshort(WEATHER_Barometer, 0xaa);
+    ac2 = twi_readshort(WEATHER_Barometer, 0xac);
+    ac3 = twi_readshort(WEATHER_Barometer, 0xae);
+    ac4 = twi_readshort(WEATHER_Barometer, 0xb0);
+    ac5 = twi_readshort(WEATHER_Barometer, 0xb2);
+    ac6 = twi_readshort(WEATHER_Barometer, 0xb4);
+    b1 = twi_readshort(WEATHER_Barometer, 0xb6);
+    b2 = twi_readshort(WEATHER_Barometer, 0xb8);
+    mb = twi_readshort(WEATHER_Barometer, 0xba);
+    mc = twi_readshort(WEATHER_Barometer, 0xbc);
+    md = twi_readshort(WEATHER_Barometer, 0xbe);
+    oss = p_oss;
+}
+ 
+unsigned short Barometer::twi_readshort (int id, int addr) {
+    unsigned short i;
+ 
+    i2c.start();
+    i2c.write(id);
+    i2c.write(addr);
+ 
+    i2c.start();
+    i2c.write(id | 1);
+    i = i2c.read(1) << 8;
+    i |= i2c.read(0);
+    i2c.stop();
+ 
+    return i;
+}
+ 
+unsigned long Barometer::twi_readlong (int id, int addr) {
+    unsigned long i;
+ 
+    i2c.start();
+    i2c.write(id);
+    i2c.write(addr);
+ 
+    i2c.start();
+    i2c.write(id | 1);
+    i = i2c.read(1) << 16;
+    i |= i2c.read(1) << 8;
+    i |= i2c.read(0);
+    i2c.stop();
+ 
+    return i;
+}
+ 
+void Barometer::twi_writechar (int id, int addr, int dat) {
+ 
+    i2c.start();
+    i2c.write(id);
+    i2c.write(addr);
+    i2c.write(dat);
+    i2c.stop();
+}
\ No newline at end of file
diff -r 000000000000 -r 9f0027e4c618 Barometer.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Barometer.h	Tue Jul 19 15:47:17 2016 +0000
@@ -0,0 +1,39 @@
+#ifndef Barometer_H
+#define Barometer_H
+#include "mbed.h"
+ 
+
+enum Barometer_oss {
+    Barometer_oss1 = 0, ///< ultra low power (1 time)
+    Barometer_oss2 = 1, ///< standard (2 times)
+    Barometer_oss4 = 2, ///< high resolution (4 times)
+    Barometer_oss8 = 3  ///< ultra high resolution (8 times)
+};
+ 
+
+class Barometer {
+public:
+    Barometer(PinName p_sda, PinName p_scl, Barometer_oss p_oss = Barometer_oss1);
+    Barometer(I2C& p_i2c, Barometer_oss p_oss = Barometer_oss1);
+ 
+    float get_temperature();
+    float get_pressure();
+    void update();
+ 
+protected:
+    void init(Barometer_oss);
+    unsigned short twi_readshort (int, int);
+    unsigned long twi_readlong (int, int);
+    void twi_writechar (int, int, int);
+ 
+    I2C i2c;
+    float temperature;
+    float pressure;
+ 
+private:
+ 
+    short ac1, ac2, ac3, b1, b2, mb, mc, md, oss;
+    unsigned short ac4, ac5, ac6;
+};
+ 
+#endif
\ No newline at end of file
diff -r 000000000000 -r 9f0027e4c618 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jul 19 15:47:17 2016 +0000
@@ -0,0 +1,38 @@
+#include "mbed.h"
+#include "LocalFileSystem.h"
+#include "Barometer.h"
+#include "math.h"
+
+Serial pc(USBTX, USBRX);
+Barometer barometer(p9, p10);
+Serial AHRS(p13, p14);
+
+LocalFileSystem local("local");
+int i=0;
+float p = 0.0f, t = 0.0f, alt = 0.0f;
+float roll,pitch,yaw,accx,accy,accz;
+
+int file_no=0;
+char filename[256];
+
+int main() {
+    AHRS.baud(9600);
+    sprintf(filename, "/local/out%d.txt", file_no);    // Open "tem%d.txt" on the local file system for writing
+    FILE *fp;
+    fp = fopen(filename, "r");
+    while(fp){ ////File!!!
+        fclose(fp);
+        file_no ++;
+        sprintf(filename, "/local/out%d.txt", file_no);    // Open "tem%d.txt" on the local file system for writing
+        fp = fopen(filename, "r");
+        pc.printf("File Yes !! \r\n");
+    }
+    fp = fopen(filename, "w");    
+    for(i=0;i<=50;i++) {
+        while(AHRS.getc() != '\n')
+        AHRS.scanf("*%f,%f,%f,%f,%f,%f\n", &roll, &pitch, &yaw, &accx,&accy,&accz);
+        fprintf(fp, "roll:%f yaw:%f\r\n",roll, yaw);
+        pc.printf("%.2f,%.2f,%.2f,%f,%f,%f \r\n", roll, pitch, yaw,accx,accy, accz);
+    }
+    fclose(fp);
+}
\ No newline at end of file
diff -r 000000000000 -r 9f0027e4c618 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Jul 19 15:47:17 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6c34061e7c34
\ No newline at end of file