lib pm2
Dependents: TestPMLIB TurtleBot_PM
Fork of PowerMon by
Revision 1:7a6a568c5c34, committed 2018-02-25
- Comitter:
- sippasaeng
- Date:
- Sun Feb 25 09:10:18 2018 +0000
- Parent:
- 0:6615da7486d0
- Commit message:
- Rev1.01 25FEB,18;
Changed in this revision
PowerMon.h | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show diff for this revision Revisions of this file |
diff -r 6615da7486d0 -r 7a6a568c5c34 PowerMon.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PowerMon.h Sun Feb 25 09:10:18 2018 +0000 @@ -0,0 +1,89 @@ + +#define i2c_sda D14 +#define i2c_scl D15 + +#define SourceVH 0x58 +#define SourceVL 0x59 +#define SenseVH 0x54 +#define SenseVL 0x55 +#define PowerH 0x5B +#define PowerL 0x5C + +I2C i2c_PM(i2c_sda, i2c_scl); + +class powerMon{ + private: + int w_addr,r_addr; + + public: + powerMon(int addr_sel){ + switch(addr_sel) + { + case 0: w_addr=0x98; break; + case 100: w_addr=0x9A; break; + case 180: w_addr=0x9C; break; + case 300: w_addr=0x9E; break; + case 430: w_addr=0x90; break; + case 560: w_addr=0x92; break; + case 750: w_addr=0x94; break; + case 1270: w_addr=0x96; break; + case 1600: w_addr=0x50; break; + case 2000: w_addr=0x52; break; + case 2700: w_addr=0x54; break; + case 3600: w_addr=0x56; break; + case 5600: w_addr=0x58; break; + case 9100: w_addr=0x58; break; + case 20000: w_addr=0x5A; break; + case -1: w_addr=0x30; break; + } + r_addr=w_addr+0x01; + + } +double V_Sense(){ + double volt=0,mult_fac=1,offset=0; + char buffer[2]; + buffer[0]=SenseVL; + i2c_PM.write(r_addr,buffer, 1,true); + i2c_PM.read(r_addr, buffer,1); + volt=buffer[0]/16; + buffer[0]=SenseVH; + i2c_PM.write(r_addr,buffer, 1,true); + i2c_PM.read(r_addr, buffer,1); + volt += (buffer[0] & 0x7f)*16; + volt = (volt*mult_fac)+offset; + return (buffer[0]&0x80)?-volt:volt; +} +double V_Source(){ + double volt=0; + char buffer[2]; + buffer[0]=SourceVL; + i2c_PM.write(r_addr,buffer, 1,true); + i2c_PM.read(r_addr, buffer,1); + volt=buffer[0]/32; + buffer[0]=SourceVH; + i2c_PM.write(r_addr,buffer, 1,true); + i2c_PM.read(r_addr, buffer,1); + volt += buffer[0]*8; + + return (volt/2048.0000)*23.9883; +} +double Current(){ + return (V_Sense()/2047.0000)*(8.0000);} +long Power_Ratio(){ + long power_r=0; + char buffer[2]; + buffer[0]=PowerL; + i2c_PM.write(r_addr,buffer, 1,true); + i2c_PM.read(r_addr, buffer,1); + power_r=buffer[0]; + buffer[0]=PowerH; + i2c_PM.write(r_addr,buffer, 1,true); + i2c_PM.read(r_addr, buffer,1); + power_r += buffer[0]*256; + + return power_r;} +double Power(){ + return (Power_Ratio()/65535.0000)*(8.0000*23.9883); + } + +}; \ No newline at end of file
diff -r 6615da7486d0 -r 7a6a568c5c34 main.cpp --- a/main.cpp Sun Feb 25 09:06:52 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,89 +0,0 @@ - -#define i2c_sda D14 -#define i2c_scl D15 - -#define SourceVH 0x58 -#define SourceVL 0x59 -#define SenseVH 0x54 -#define SenseVL 0x55 -#define PowerH 0x5B -#define PowerL 0x5C - -I2C i2c_PM(i2c_sda, i2c_scl); - -class powerMon{ - private: - int w_addr,r_addr; - - public: - powerMon(int addr_sel){ - switch(addr_sel) - { - case 0: w_addr=0x98; break; - case 100: w_addr=0x9A; break; - case 180: w_addr=0x9C; break; - case 300: w_addr=0x9E; break; - case 430: w_addr=0x90; break; - case 560: w_addr=0x92; break; - case 750: w_addr=0x94; break; - case 1270: w_addr=0x96; break; - case 1600: w_addr=0x50; break; - case 2000: w_addr=0x52; break; - case 2700: w_addr=0x54; break; - case 3600: w_addr=0x56; break; - case 5600: w_addr=0x58; break; - case 9100: w_addr=0x58; break; - case 20000: w_addr=0x5A; break; - case -1: w_addr=0x30; break; - } - r_addr=w_addr+0x01; - - } -double V_Sense(){ - double volt=0,mult_fac=1,offset=0; - char buffer[2]; - buffer[0]=SenseVL; - i2c_PM.write(r_addr,buffer, 1,true); - i2c_PM.read(r_addr, buffer,1); - volt=buffer[0]/16; - buffer[0]=SenseVH; - i2c_PM.write(r_addr,buffer, 1,true); - i2c_PM.read(r_addr, buffer,1); - volt += (buffer[0] & 0x7f)*16; - volt = (volt*mult_fac)+offset; - return (buffer[0]&0x80)?-volt:volt; -} -double V_Source(){ - double volt=0; - char buffer[2]; - buffer[0]=SourceVL; - i2c_PM.write(r_addr,buffer, 1,true); - i2c_PM.read(r_addr, buffer,1); - volt=buffer[0]/32; - buffer[0]=SourceVH; - i2c_PM.write(r_addr,buffer, 1,true); - i2c_PM.read(r_addr, buffer,1); - volt += buffer[0]*8; - - return (volt/2048.0000)*23.9883; -} -double Current(){ - return (V_Sense()/2047.0000)*(8.0000);} -long Power_Ratio(){ - long power_r=0; - char buffer[2]; - buffer[0]=PowerL; - i2c_PM.write(r_addr,buffer, 1,true); - i2c_PM.read(r_addr, buffer,1); - power_r=buffer[0]; - buffer[0]=PowerH; - i2c_PM.write(r_addr,buffer, 1,true); - i2c_PM.read(r_addr, buffer,1); - power_r += buffer[0]*256; - - return power_r;} -double Power(){ - return (Power_Ratio()/65535.0000)*(8.0000*23.9883); - } - -}; \ No newline at end of file