EIC / Mbed 2 deprecated INA226_monitoring

Dependencies:   INA226 TextLCD mbed

Fork of INA226TEST by Toshihisa T

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*前バージョンからの変更点
00002 ・シリアル通信モードでもオフセットの初期値を取得するように変更
00003 ・シリアル通信モードで最初に送られるデータが(0.0, 0.0, 0,0)だったのを、
00004 センサから取得した値を送るように変更
00005 */
00006 #include "mbed.h"
00007 #include "TextLCD.h"
00008 #include "INA226.hpp"
00009 //#include "limits.h"
00010 
00011 #define VCC 3.3//抵抗R1に供給する電圧[V]
00012 #define R1 10000.0//抵抗R1の値[Ω]
00013 #define GAUGE_STEP 17//テキストディスプレイモード:ゲージの段階数
00014 #define GAUGE_MAX 100.0//テキストディスプレイモード:ゲージ最大状態の値[Ω]
00015 #define GAUGE_NOTIFY 50.0//テキストディスプレイモード:音を鳴らし始める値[Ω]
00016 #define INTERVAL_SERIAL 0.5//シリアル通信モード:データを収集する間隔[秒]
00017 #define INTERVAL_TEXTLCD 0.1//テキストディスプレイモード:データを収集する間隔[秒]
00018 #define SAMPLES 10//一回の測定で取得するデータの数(これらのデータの平均を使用)
00019 
00020 Serial pc(USBTX,USBRX);//tx,rx :シリアル通信
00021 TextLCD dsp_t(p24, p23, p22, p19, p21, p20);// rs, e, d4-d7 :テキストディスプレイ
00022 //LocalFileSystem local("local");//内部ストレージ
00023 AnalogIn sensor(p18);
00024 InterruptIn setoffset(p17);//テキストディスプレイモード:オフセット設定
00025 InterruptIn mode(p16);//テキストディスプレイモード:ゲージ←→数値詳細  切替
00026 PwmOut speaker(p25);
00027 //Timer tim;
00028 DigitalOut led[] = {LED1, LED2};
00029 I2C i2c(p28,p27);
00030 INA226 VCmonitor(i2c);
00031 
00032 float resistance, offset;
00033 bool detailmode = false;
00034 volatile bool serialmode = false;
00035 
00036 void gaugeOffset();//ゲージのオフセットを設定
00037 void changeMode();//ゲージモードと測定値の詳細表示の切り替え
00038 void isrRx();//受信イベント
00039 
00040 unsigned short val;
00041 double V,C;
00042 int count = 1;
00043 
00044 int main()
00045 {
00046     pc.baud(115200);
00047     VCmonitor.rawWrite(0x00,0x4EDF);
00048     if(!VCmonitor.isExist()) {
00049         pc.printf("VCmonitor NOT FOUND\r\n");
00050         while(1) {}
00051     }
00052     pc.printf("VCmonitor FOUND\r\n");
00053 
00054     val = 0;
00055     if(VCmonitor.rawRead(0x00,&val) != 0) {
00056         pc.printf("VCmonitor READ ERROR\r\n");
00057         while(1) {}
00058     }
00059     pc.printf("VCmonitor Reg 0x00 : 0x%04x\r\n",val);
00060 
00061     VCmonitor.setCurrentCalibration(0x0800);
00062     /*tim.start();
00063     led[0] = led[1] = 1;
00064 
00065     while(serialmode == false && tim.read() < SERIAL_WAIT) {
00066         if(pc.readable()) {//通信相手からデータが送られてきた時
00067             serialmode = true;
00068             break;
00069         }
00070         wait(0.5);
00071     }
00072 
00073     led[0] = led[1] = */
00074     resistance = offset = 0;
00075     if((VCmonitor.getVoltage(&V) == 0) && (VCmonitor.getCurrent(&C) == 0))
00076         offset = (float) 10*V/C;
00077     //tim.stop();
00078     //FILE *fp = NULL;
00079     pc.attach(isrRx,Serial::RxIrq);//割り込みハンドラ登録
00080 BUNKI:
00081     if(serialmode == true) {//シリアル通信モード
00082         led[0] = 0;
00083         led[1] = 1;
00084         //if(fp!=NULL)fclose(fp);
00085         dsp_t.cls();
00086         dsp_t.printf("Serial mode");
00087 
00088         float voltage = 0;
00089         for(int i = 0; i < SAMPLES; i++)voltage += sensor.read();
00090         voltage = voltage * 3.3 / SAMPLES;
00091         resistance = R1 / (VCC / voltage - 1);
00092         pc.printf("%f,%f,%f\n", voltage, resistance, offset);
00093         //pc.printf("%f,%f,%f\n", 0.0, 0.0, 0.0);
00094 
00095         while(1)if(serialmode == false)goto BUNKI;
00096     } else {//テキストディスプレイモード
00097 
00098         float t = 1.0/261.626;//「ド」の音(C4)の周期
00099         float a = (t-(t/2/2))/GAUGE_MAX;//C4の周期と2オクターブ上の音の周期の差を(GAUGE_MAX)分割
00100 
00101         led[0] = 1;
00102         led[1] = 0;
00103 
00104         //offset = R1 / (VCC / (sensor.read() * 3.3)- 1);//オフセット初期値の取得
00105         speaker = 0.0;
00106 
00107         speaker.period(0.001);
00108         //tim.reset();
00109         //tim.start();
00110         setoffset.rise(&gaugeOffset);//割り込みハンドラ登録
00111         mode.rise(&changeMode);//割り込みハンドラ登録
00112 
00113         /*
00114                 if((fp = fopen("/local/data.txt", "r")) != NULL) { //data.txtの検出
00115                     if(fgetc(fp)!=EOF) {
00116                         dsp_t.printf("Err:data exists\nPlease move it.");
00117                         return EXIT_FAILURE;
00118                     }
00119                     fclose(fp);
00120                 }
00121         */
00122         while(1) {
00123             if(serialmode == true)goto BUNKI;
00124             /*
00125             static unsigned long long int time_sum = 0;
00126             int now = tim.read_ms();
00127             if(now > 1800000) {
00128                 time_sum += now;
00129                 tim.reset();
00130             }
00131             if(time_sum > ULLONG_MAX - 10000000) {
00132                 printf("Err:time out\nPlease restart now.");
00133                 return EXIT_FAILURE;
00134             }*/
00135             float voltage = 0.0;
00136             for(int i = 0; i < SAMPLES; i++)voltage += sensor.read();
00137             //voltage = voltage * 3.3 / SAMPLES;
00138             //resistance = R1 / (VCC / voltage - 1);
00139             //if((fp = fopen("/local/data.txt", "a")) == NULL)return EXIT_FAILURE;
00140             //fprintf(fp, "%lld %f %f %f\r\n", now + time_sum, voltage, resistance, offset);
00141             //fclose(fp);
00142             if((VCmonitor.getVoltage(&V) == 0) && (VCmonitor.getCurrent(&C) == 0)) {
00143                 voltage = V/1000;
00144                 resistance = 10*V/C;
00145             }
00146             dsp_t.cls();
00147             if(detailmode == false) {
00148                 for(int i = 0; i < (int)((resistance - offset) / (GAUGE_MAX / GAUGE_STEP)) && i < GAUGE_STEP; i++)
00149                     dsp_t.printf("%c", 0xFF);//ゲージ
00150 
00151                 dsp_t.locate(0, 1);
00152 
00153                 if(resistance>=1000000000)dsp_t.printf("R=%3.0fG%c", resistance/1000000000,0xF4);
00154                 else if(resistance>=1000000)dsp_t.printf("R=%3.0fM%c", resistance/1000000,0xF4);
00155                 else if(resistance>=1000)dsp_t.printf("R=%3.0fk%c", resistance/1000,0xF4);
00156                 else dsp_t.printf("R=%4.0f%c", resistance,0xF4);
00157 
00158                 if(offset>=1000000000)dsp_t.printf("  O=%3.0fG%c", offset/1000000000,0xF4);
00159                 else if(offset>=1000000)dsp_t.printf("  O=%3.0fM%c", offset/1000000,0xF4);
00160                 else if(offset>=1000)dsp_t.printf("  O=%3.0fk%c", offset/1000,0xF4);
00161                 else dsp_t.printf("  O=%4.0f%c", offset,0xF4);
00162             } else {
00163                 if(resistance>=1000000000)dsp_t.printf("R=%11.7f G%c", resistance/1000000000,0xF4);
00164                 else if(resistance>=1000000)dsp_t.printf("R=%11.7f M%c", resistance/1000000,0xF4);
00165                 else if(resistance>=1000)dsp_t.printf("R=%11.7f k%c", resistance/1000,0xF4);
00166                 else dsp_t.printf("R=%11.7f  %c", resistance,0xF4);
00167 
00168                 dsp_t.locate(0, 1);
00169 
00170                 if(offset>=1000000000)dsp_t.printf("O=%11.7f G%c", offset/1000000000,0xF4);
00171                 else if(offset>=1000000)dsp_t.printf("O=%11.7f M%c", offset/1000000,0xF4);
00172                 else if(offset>=1000)dsp_t.printf("O=%11.7f k%c", offset/1000,0xF4);
00173                 else dsp_t.printf("O=%11.7f  %c", offset,0xF4);
00174             }
00175 
00176             speaker = 0.5;
00177             if(resistance - offset >= GAUGE_NOTIFY)speaker.period(t-a*(resistance - offset));
00178             else speaker = 0.0;
00179             wait(INTERVAL_TEXTLCD);
00180         }
00181     }
00182 }
00183 
00184 void gaugeOffset()
00185 {
00186     speaker.period(0.01);
00187     speaker = 0.5;
00188     if(detailmode == false) {
00189         dsp_t.locate(0, 0);
00190         dsp_t.printf("################");
00191     }
00192     offset = resistance;
00193     wait(0.5);
00194     dsp_t.cls();
00195     speaker = 0.0;
00196     //speaker.period(0.001);
00197 }
00198 
00199 void changeMode()
00200 {
00201     detailmode = (detailmode == false) ? true : false;
00202 }
00203 
00204 
00205 void isrRx()
00206 {
00207     if(serialmode == false) {
00208         serialmode=true;
00209         return;
00210     }
00211     char ch;
00212     //static float resistance = 0, offset = 0;
00213     ch = pc.getc();
00214 
00215     switch(ch) {
00216 
00217         case 'c'://continue
00218             float voltage = 0;
00219             
00220             for(int i = 0; i < SAMPLES; i++)voltage += sensor.read();
00221             voltage = voltage * 3.3 / SAMPLES;
00222             resistance = R1 / (VCC / voltage - 1);
00223             if((VCmonitor.getVoltage(&V) == 0) && (VCmonitor.getCurrent(&C) == 0)) {
00224                 voltage = (float) V/1000;
00225                 resistance = (float) 10*V/C;
00226 
00227             }
00228             //pc.printf("%f,%f,%f\n", 1.1, 200.0, 0.0);
00229             pc.printf("%f,%f,%f\n", voltage, resistance, offset);
00230             count++;
00231             break;
00232 
00233         case 'o'://offset
00234             offset = resistance;
00235             break;
00236 
00237         case 'q'://quit serial mode
00238             serialmode = false;
00239             return;
00240     }
00241 }