Dependencies:   ChaNFSSD mbed BMP085 SHT2x

Files at this revision

API Documentation at this revision

Comitter:
tosihisa
Date:
Fri Jan 13 10:15:08 2012 +0000
Parent:
0:6089ae824f06
Child:
2:f546aaa0e1d5
Commit message:
add 5-sec wait before using CO2-sensor

Changed in this revision

AD7994.cpp Show annotated file Show diff for this revision Revisions of this file
AD7994.h Show annotated file Show diff for this revision Revisions of this file
SHT25.cpp Show annotated file Show diff for this revision Revisions of this file
SHT25.h Show annotated file Show diff for this revision Revisions of this file
libT/mbed/libT_getAcc.c Show annotated file Show diff for this revision Revisions of this file
libT/mbed/tserialbuffer.h Show annotated file Show diff for this revision Revisions of this file
libT/portable/NMEA_parse.c Show annotated file Show diff for this revision Revisions of this file
libT/portable/NMEA_parse.h Show annotated file Show diff for this revision Revisions of this file
libT/portable/UBXPacket.c Show annotated file Show diff for this revision Revisions of this file
libT/portable/UBXPacket.h Show annotated file Show diff for this revision Revisions of this file
libT/portable/tringbuffer.h Show annotated file Show diff for this revision Revisions of this file
libT/portable/tversion.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
modbus_crc.c Show annotated file Show diff for this revision Revisions of this file
--- a/AD7994.cpp	Mon Dec 26 15:58:32 2011 +0000
+++ b/AD7994.cpp	Fri Jan 13 10:15:08 2012 +0000
@@ -1,3 +1,7 @@
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
 
 #include "mbed.h"
 #include "AD7994.h"
--- a/AD7994.h	Mon Dec 26 15:58:32 2011 +0000
+++ b/AD7994.h	Fri Jan 13 10:15:08 2012 +0000
@@ -1,3 +1,7 @@
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
 
 #ifndef __AD7994_H  /* } */
 
--- a/SHT25.cpp	Mon Dec 26 15:58:32 2011 +0000
+++ b/SHT25.cpp	Fri Jan 13 10:15:08 2012 +0000
@@ -1,3 +1,7 @@
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
 
 #include "mbed.h"
 #include "SHT25.h"
--- a/SHT25.h	Mon Dec 26 15:58:32 2011 +0000
+++ b/SHT25.h	Fri Jan 13 10:15:08 2012 +0000
@@ -1,3 +1,7 @@
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
 
 #ifndef __SHT25_H  /* } */
 
--- a/libT/mbed/libT_getAcc.c	Mon Dec 26 15:58:32 2011 +0000
+++ b/libT/mbed/libT_getAcc.c	Fri Jan 13 10:15:08 2012 +0000
@@ -1,4 +1,7 @@
-
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
 
 extern "C" {
 static int libT_getAccTOmV(unsigned long Val,long *mV);
--- a/libT/mbed/tserialbuffer.h	Mon Dec 26 15:58:32 2011 +0000
+++ b/libT/mbed/tserialbuffer.h	Fri Jan 13 10:15:08 2012 +0000
@@ -1,3 +1,7 @@
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
 
 #ifndef __TSERIALBUFFER_H
 #define __TSERIALBUFFER_H
--- a/libT/portable/NMEA_parse.c	Mon Dec 26 15:58:32 2011 +0000
+++ b/libT/portable/NMEA_parse.c	Fri Jan 13 10:15:08 2012 +0000
@@ -1,3 +1,7 @@
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
 
 #include "NMEA_parse.h"
 
--- a/libT/portable/NMEA_parse.h	Mon Dec 26 15:58:32 2011 +0000
+++ b/libT/portable/NMEA_parse.h	Fri Jan 13 10:15:08 2012 +0000
@@ -1,3 +1,7 @@
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
 
 #ifndef _NMEA_PARSE_H
 #define _NMEA_PARSE_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libT/portable/UBXPacket.c	Fri Jan 13 10:15:08 2012 +0000
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+#include "UBXPacket.h"
+
+void UBXPacket_CalcSum(struct UBXPacket_s *info,int c)
+{
+    *(info->sum + 0) = *(info->sum + 0) + ((unsigned char)c);
+    *(info->sum + 1) = *(info->sum + 1) + *(info->sum + 0);
+}
+
+int UBXPacket_Parse(struct UBXPacket_s *info,int c)
+{
+    if(info->cjobst == 0){
+        info->cjobst = (c == 0xB5) ? 1 /* OK */ : 0 /*Err*/;
+    } else if(info->cjobst == 1){
+        info->cjobst = (c == 0x62) ? 2 /* OK */ : 0 /*Err*/;
+        info->sum[0] = info->sum[1] = 0;
+    } else if(info->cjobst == 2){
+        UBXPacket_CalcSum(info,c);
+        info->cls = (unsigned char)c;
+        info->cjobst++;
+    } else if(info->cjobst == 3){
+        UBXPacket_CalcSum(info,c);
+        info->id = (unsigned char)c;
+        info->cjobst++;
+    } else if(info->cjobst == 4){
+        UBXPacket_CalcSum(info,c);
+        info->len = (unsigned char)c;
+        info->cjobst++;
+    } else if(info->cjobst == 5){
+        UBXPacket_CalcSum(info,c);
+        info->len |= (((unsigned short)c) << 8);
+        info->idx = 0;
+        info->cjobst++;
+    } else if(info->cjobst == 6){
+        UBXPacket_CalcSum(info,c);
+        info->body[info->idx] = (unsigned char)c;
+        info->idx++;
+        if(info->idx >= info->len){
+            info->cjobst = 7;
+        }
+    } else if(info->cjobst == 7){
+        info->cjobst = (c == info->sum[0]) ? 8 /* OK */ : 0 /*Err*/;
+    } else if(info->cjobst == 8){
+        info->cjobst = (c == info->sum[1]) ? 100 /* OK */ : 0 /*Err*/;
+    }
+    return info->cjobst;
+}
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libT/portable/UBXPacket.h	Fri Jan 13 10:15:08 2012 +0000
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+#ifndef __UBXPACKET_H__ /* { */
+
+struct UBXPacket_s {
+    unsigned char cls;
+    unsigned char id;
+    unsigned short len;
+    unsigned char body[1024];
+    unsigned char sum[2];
+    int cjobst;
+    int idx;
+};
+
+void UBXPacket_CalcSum(struct UBXPacket_s *info,int c);
+int UBXPacket_Parse(struct UBXPacket_s *info,int c);
+
+#endif  /* __UBXPACKET_H__ } */
--- a/libT/portable/tringbuffer.h	Mon Dec 26 15:58:32 2011 +0000
+++ b/libT/portable/tringbuffer.h	Fri Jan 13 10:15:08 2012 +0000
@@ -1,3 +1,7 @@
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
 
 #ifndef __TRINGBUFFER_H
 #define __TRINGBUFFER_H
--- a/libT/portable/tversion.h	Mon Dec 26 15:58:32 2011 +0000
+++ b/libT/portable/tversion.h	Fri Jan 13 10:15:08 2012 +0000
@@ -1,3 +1,7 @@
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
 
 #ifndef __TVERSION_H
 #define __TVERSION_H
--- a/main.cpp	Mon Dec 26 15:58:32 2011 +0000
+++ b/main.cpp	Fri Jan 13 10:15:08 2012 +0000
@@ -1,3 +1,8 @@
+/*
+ * このソフトウェアは、大阪市立大学と京都大学生存圏ミッション研究の共同研究の成果物です。
+ * ソースコードそのもののライセンスは、各ソースコードのライセンスに沿って公開します。
+ * 2012-1-4 Toshihisa T
+ */
 #include "mbed.h"
 #include "NMEA_parse.h"
 #include "SDFileSystem.h"
@@ -8,10 +13,12 @@
 #include "AD7994.h"
 //#include "TextLCD.h"
 #include "TextLCD_20X4.h"
+#include "UBXPacket.h"
 
 using namespace libT;
 
-//#define HAVE_CO2
+#define _CO2_TEST
+//#define _I2C_TEST
 
 #define _USE_FS_NAME "sd"
 
@@ -22,21 +29,25 @@
 InterruptIn gps_pps(p26);
 tSerialBuffer gps(p28,p27);
 DigitalOut  gps_pps_led(LED2);
-//I2C i2c(p9, p10);        // sda, scl
+#ifdef _I2C_TEST /* { */
+I2C i2c(p9, p10);        // sda, scl
+#endif  /* } */
 BMP085 bmp085(p9, p10);
 //SHT25 sht25(p9, p10);
 //SHT2x sht25(p9,p10);
 AD7994 ad7994(p9,p10);
 TextLCD_20X4 lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
 
-//SDFileSystem sd(p5, p6, p7, p8, _USE_FS_NAME);
+SDFileSystem sd(p5, p6, p7, p8, _USE_FS_NAME);
 
 DigitalOut myled(LED1);
 
-#ifdef  HAVE_CO2
+#ifdef  _CO2_TEST
 tSerialBuffer CO2(p13,p14);
 #endif
 
+struct UBXPacket_s UBXPacket;
+
 int pps_count = 0;
 void gps_pps_rise()
 {
@@ -73,7 +84,7 @@
     fclose(fp);
 }
 
-#ifdef  HAVE_CO2    /* { */
+#ifdef  _CO2_TEST    /* { */
 int CO2_Read(unsigned short *val)
 {
     unsigned char sbuf[] = { 0xFE, 0x04, 0x00, 0x03, 0x00, 0x01, 0xD5, 0xC5 };
@@ -90,12 +101,20 @@
         while(!CO2.readable()){}
         rbuf[i] = CO2.getc();
     }
+    if(rbuf[0] != 0xFE){
+        return -4;
+    }
+    if(rbuf[1] != 0x04){
+        return -1;
+    }
+    if(rbuf[2] != 0x02){
+        return -2;
+    }
     crc = rbuf[6];
     crc = (crc << 8) | rbuf[5];
     if(crc != modbus_CRC(rbuf,5)){
-        return -1;
+        return -3;
     }
-
     *val = rbuf[3];
     *val = (*val << 8) | rbuf[4];
     return 0;
@@ -104,21 +123,66 @@
 
 // BMP085 0xee
 // AD9774 0x44
-#if 0
+#ifdef _I2C_TEST /* { */
 int i2c_found() {
-    printf("Searching for I2C devices...\n");
-
     int count = 0;
+    lcd.locate(0,2);
+    lcd.printf("%-19s","I2C Dev:");
+    lcd.locate(8,2);
     for (int address=0; address<256; address+=2) {
         if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
-            printf(" - I2C device found at address 0x%02X\n", address);
+            lcd.printf("%02X ",address);
             count++;
         }
     }
-    printf("%d devices found\n", count);
+    lcd.printf("\n");
+    lcd.printf("%d devices found", count);
     return count;
 }
-#endif
+#endif /* } */
+
+unsigned char NMEA_CalcSum(unsigned char *str,int len)
+{
+    unsigned char sum = 0;
+    int i;
+    for(i = 0;i < len;i++){
+        sum = sum ^ (*(str + i));
+    }
+    return sum;
+}
+
+void UBX_CalcSum(unsigned char *str,int len,unsigned char *sum)
+{
+    int i;
+    *(sum + 0) = *(sum + 1) = 0;
+    for(i = 0;i < len;i++){
+        *(sum + 0) = *(sum + 0) + *(str+i);
+        *(sum + 1) = *(sum + 1) + *(sum + 0);
+    }
+}
+
+int UBX_WaitAck(struct UBXPacket_s *info)
+{
+    UBXPacket.cjobst = 0;
+
+    while(1){
+        while(gps.readable()) {
+            if(UBXPacket_Parse(&UBXPacket,gps.getc()) == 100){
+                if((UBXPacket.cls == 0x05) && (UBXPacket.id == 0x01)){
+                    return 1; /* ACK */
+                } else if((UBXPacket.cls == 0x05) && (UBXPacket.id == 0x00)){
+                    return 0; /* NAK */
+                } else {
+                    UBXPacket.cjobst = 0;
+                }
+            }
+        }
+    }
+    return -1;
+}
+
+unsigned char wbuf[1024];
+int widx;
 
 int main() {
     int ret = 0;
@@ -127,6 +191,9 @@
     int userRegister;
     unsigned short CO2_val;
     unsigned long scanCount = 0;
+    FILE *fp;
+    char *logname = "/" _USE_FS_NAME "/ENVLOG.TXT";
+    char c;
  
     debug.format(8,Serial::None,1);
     debug.baud(115200);
@@ -137,23 +204,76 @@
     //lcd.cls();
     lcd.locate(0,0);
     lcd.printf("ENV Logger \"V1\"");
+#ifdef  _CO2_TEST
+    wait(5.0);
+#endif
  
     //logFile_Init();
     //FileWriteTest();
 
+    UBXPacket.cjobst = 0;
+
     gps_pps.rise(gps_pps_rise);
     gps.format(8,Serial::None,1);
     gps.baud(9600);
-    gps.recvStart();
 
-    //�P���Z�b�g�C�O���Z�b�g�J��
     gps_reset = 1;
     wait(0.5);
     gps_reset = 0;
 
-    ad7994.Start();
+#if 0
+    wait(0.5);
+    if(1){
+        //unsigned char modeStr[] = "PUBX,41,1,0007,0003,115200,0";
+        unsigned char modeStr[] = "PUBX,41,1,0007,0001,115200,0";
+        unsigned char sum = NMEA_CalcSum(modeStr,strlen((char *)modeStr));
+        debug.printf("SEND:[%s](0x%02X)\n",modeStr,sum);
+        gps.printf("$%s*%02X%c%c",modeStr,sum,0x0d,0x0a);
+
+        debug.printf("CHG BAUD:115200\n");
+        gps.format(8,Serial::None,1);
+        gps.baud(115200);
+    }
+
+    gps.recvStart();
+    wait(0.5);
 
-#ifdef  HAVE_CO2
+    if(1){
+        unsigned char chkStr[][11] = {
+            { 0xB5,0x62,0x06,0x01,0x03,0x00,0x02,0x10,0x02,0xFF,0xFF }, //RXM-RAW (0x02 0x10)
+            { 0xB5,0x62,0x06,0x01,0x03,0x00,0x02,0x30,0x02,0xFF,0xFF }, //RXM-ALM (0x02 0x30)
+            { 0xB5,0x62,0x06,0x01,0x03,0x00,0x02,0x31,0x02,0xFF,0xFF }, //RXM-EPH (0x02 0x31)
+            //{ 0xB5,0x62,0x06,0x01,0x03,0x00,0x02,0x41,0x02,0xFF,0xFF },
+            { 0xB5,0x62,0x06,0x01,0x03,0x00,0x02,0x11,0x02,0xFF,0xFF }, //RXM-SFRB (0x02 0x11)
+            { 0xB5,0x62,0x06,0x01,0x03,0x00,0x02,0x20,0x02,0xFF,0xFF }, //RXM-SVSI (0x02 0x20)
+            { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF }, //END
+        };
+        int i,j;
+
+        for(j=0;chkStr[j][0] != 0x00;j++){
+            UBX_CalcSum(&chkStr[j][2],7,&chkStr[j][9]);
+            for(i = 0; i < sizeof(chkStr[0]);i++){
+                gps.putc(chkStr[j][i]);
+            }
+            debug.printf("%d : SET UBX Rate : %s\n",j,(UBX_WaitAck(&UBXPacket) == 1) ? "ACK" : "NAK");
+       }
+    }
+    UBXPacket.cjobst = 0;
+#endif
+
+#if 0
+    debug.printf("open(%s) :", logname);
+    if((fp = fopen(logname,"a+w")) == NULL){
+        printf("NG\n");
+    } else {
+        printf("OK\n");
+    }
+    widx = 0;
+#endif
+
+//  ad7994.Start();
+
+#ifdef  _CO2_TEST
     CO2.format(8,Serial::None,1);
     CO2.baud(9600);
     CO2.recvStart();
@@ -167,16 +287,38 @@
     while(1) {
         //lcd.cls();
         lcd.locate(0,1);
-        lcd.printf("SCAN : %-10ld\n",scanCount);
+        lcd.printf("SCAN: %-10ld",scanCount);
 
 #if 0
         while(gps.readable()) {
-            debug.printf("%c",gps.getc());
-            //ret = libNMEA_Parse1Char(gps.getc(),&sts);
+            c = gps.getc();
+            if(UBXPacket_Parse(&UBXPacket,c) == 100){
+                debug.printf("%ld : GET UBX Packet (Class=0x%02X,ID=0x%02X,LEN=%d)\n",
+                            scanCount,
+                            UBXPacket.cls,
+                            UBXPacket.id,
+                            UBXPacket.len );
+                UBXPacket.cjobst = 0;
+                if(scanCount > 120){
+                    if(widx >= 0){
+                        fwrite(&wbuf,sizeof(wbuf[0]),widx,fp);
+                        widx = 0;
+                    }
+                    fclose(fp);
+                }
+            }
+            if(fp != NULL){
+                wbuf[widx] = c;
+                widx++;
+                if(widx >= sizeof(wbuf)){
+                    fwrite(&wbuf,sizeof(wbuf[0]),widx,fp);
+                    widx = 0;
+                }
+            }
         }
 #endif
 
-#if 1
+#if 0
         bmp085.update();
         p = bmp085.get_pressure();
         t = bmp085.get_temperature();
@@ -192,29 +334,38 @@
         debug.printf("SHT25 = p:%6.2f C / t:%6.2f RH\n", p, t);
         sht25.SHT2x_SoftReset();
 #endif
+
 #if 0
         sht25.SoftReset();
         printf("SHT25 = %f\n",sht25.get_temperature());
 #endif
-        //i2c_found();
+
+#ifdef _I2C_TEST /* { */
+        i2c_found();
+#endif  /* } */
 
-#ifdef  HAVE_CO2
-        if(CO2_Read(&CO2_val) == 0){
-            debug.printf("CO2 OK : %d (0x%04x) ppm\n",CO2_val,CO2_val);
+#ifdef  _CO2_TEST
+        lcd.locate(0,2);
+        ret = CO2_Read(&CO2_val);
+        if(ret == 0){
+            lcd.printf("CO2:%5dppm\n",CO2_val);
         } else {
-            debug.printf("CO2 NG\n");
+            lcd.printf("CO2:NG(%d)  \n",ret);
         }
 #endif
+
+#if 0
         ad7994.update();
         lcd.locate(0,3);
         lcd.printf("A/D:%04X/%04X",
                      ad7994.readChn(0),
                      ad7994.readChn(1));
+#endif
 
         myled = 1;
-        wait(0.2);
+        wait(0.5);
         myled = 0;
-        wait(0.2);
+        wait(0.5);
 
         scanCount++;
     }
--- a/modbus_crc.c	Mon Dec 26 15:58:32 2011 +0000
+++ b/modbus_crc.c	Fri Jan 13 10:15:08 2012 +0000
@@ -1,3 +1,7 @@
+/*
+ * Copyright (c) 2011 Toshihisa T
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
 
 static const unsigned short _modbus_CRCTab[256] = {
     0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,    /* 0x00-0x07 */
@@ -57,7 +61,7 @@
     calc_crc = modbus_CRC(testbuf,sizeof(testbuf));
     printf("CRC:0x%04x\n",calc_crc);
 
-    /* modbus crc16 ‚́A‰ºƒoƒCƒg‚©‚ç‘—M‚·‚邱‚Æ */
+    /* modbus crc16 �́A���o�C�g���瑗�M���邱�� */
     return 0;
 }