https:\/\/github.com\/jarzebski\/Arduino-INA226\/blob\/master\/INA226.cpp<\/a><\/p>\n0.025\u03a9 = 0.00512\/(0.025*0.0001)=2048 = 0x08 0x00<\/p>\n
<\/p>\n
0x05 \u306e\u88dc\u6b63\u30ec\u30b8\u30b9\u30bf\u3092\u898b\u3066\u307f\u308b\u3068\u3001<\/p>\n
# i2cget -y 1 0x40 0x05 w
\n
0x0008<\/p>\n
\u3068\u3044\u3046\u5024\u304c\u5e30\u3063\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n
<\/p>\n
\u3055\u3066\u3001\u305d\u3093\u306a\u611f\u3058\u3067\u3068\u308a\u3042\u3048\u305a\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n
#!\/usr\/bin\/env ruby\n# -*- coding: utf-8 -*-\n \nIOCTL_I2C_SLAVE = 0x0703\n \n# Script for power meter using the following parts\n# - Power Meter module (IC: INA226)\n\n# # i2cdetect -y 1\n# 0 1 2 3 4 5 6 7 8 9 a b c d e f\n# 00: -- -- -- -- -- -- -- -- -- -- -- -- --\n# 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n# 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n# 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n# 40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n# 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n# 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n# 70: -- -- -- -- -- -- -- --\n\nclass PowerSenseor\n def initialize(i2c_bus=1, dev_addr=0x40)\n @i2c = File.open(sprintf('\/dev\/i2c-%d', i2c_bus), 'rb+')\n @i2c.ioctl(IOCTL_I2C_SLAVE, dev_addr)\n\n @dev_addr = dev_addr\n @v_val = 0\n @c_val = 0\n @p_val = 0\n\n # shunt resistor = 0.002\u03a9 = 0.00512\/(0.002*0.001)=2560 = 0x0a 0x00\n # 0.025\u03a9 = 0.00512\/(0.025*0.0001)=2048 = 0x08 0x00\n # \u5185\u90e8\u30ec\u30b8\u30b9\u30bf[0x04] (Current) = \u5185\u90e8\u30ec\u30b8\u30b9\u30bf[0x01]\u00d7\u5185\u90e8\u30ec\u30b8\u30b9\u30bf[0x05] \uff0f 2048\n exec_cmd("i2cset -y 1 0x#{dev_addr.to_s(16)} 0x05 0x08 0x00 i")\n # conversion time = 332us, number of average = 16\n exec_cmd("i2cset -y 1 0x#{dev_addr.to_s(16)} 0x00 0x04 0x97 i")\n end\n\n def sense\n # i2cset\/i2cget \u306f 10ms \u30aa\u30fc\u30c0\u30fc\u306e\u6642\u9593\u3092\u6d88\u8cbb\u3059\u308b\u306e\u3067\u3053\u3053\u3067\u306f\u4f7f\u7528\u3057\u306a\u3044 \n @i2c.write(0x02)\n @v_val = conver_signed(@i2c.read(2))\n\n # 0x04 \u96fb\u6d41\u30ec\u30b8\u30b9\u30bf\n @i2c.write(0x04)\n @c_val = conver_signed(@i2c.read(2))\n\n # 0x03 Power\u30ec\u30b8\u30b9\u30bf\n @i2c.write(0x03)\n @p_val = conver_signed(@i2c.read(2))\n end\n\n def conver_signed(bytes)\n # convert endian\n return bytes.unpack('n').pack('S').unpack('s')[0].abs\n end\n\n def get_voltage\n return calc_voltage(@v_val)\n end\n\n def get_current\n return calc_current(@c_val)\n end\n\n def get_power\n return calc_power(@p_val)\n end\n\n def exec_cmd(cmd)\n val=`#{cmd} 2> \/dev\/null`\n raise StandardError, "FAIL: #{cmd}" unless $?.success?\n return val\n end\n\n def calc_voltage(v_val)\n return v_val * 1.25 \/ 1000.0\n end\n\n # 0x04 \u96fb\u6d41\u30ec\u30b8\u30b9\u30bf\n def calc_current(c_val)\n return c_val \/ 10\n end\n\n # 0x03 Power\u30ec\u30b8\u30b9\u30bf\n def calc_power(p_val)\n # return p_val * 0.025\n return p_val * 0.001\n end\nend\n\nrequire 'optparse'\nparams = ARGV.getopts('lq')\n\ndata_list = []\n\nSignal.trap(:INT){\n if params['l'] then\n printf("time,voltage,current,power\\n")\n data_list.each{|data|\n printf("%10d,%.3f,%.3f,%.3f\\n", data[0], data[1], data[2], data[3])\n }\n end\n exit(0)\n}\n\nsensor = PowerSenseor.new\nrequire "date"\n\nstart_time = Time.now\ni = 0\nwhile true\n sensor.sense\n v = sensor.get_voltage\n c = sensor.get_current\n p = sensor.get_power\n\n dt = DateTime.now\n print "--------- "\n print(dt.strftime("%Y\/%m\/%d, %H:%M:%S"), "\\n")\n\n print v\n print " V\\n"\n print c\n print " mA\\n"\n print p\n print " W\\n"\n\n if ((i & 0x7F) == 0) then\n\n else\n sleep 0.5\n end\n i = (i & 0xff) + 1\nend<\/pre>\n <\/p>\n
\u25bc\u307e\u3068\u3081<\/p>\n
\u30fb0x05 \u306b\u30b7\u30e3\u30f3\u30c8\u62b5\u6297\u5024\u3092\u5165\u308c\u305f\u88dc\u6b63\u5024\u3092\u66f8\u304d\u8fbc\u3080<\/p>\n
\u30fb\u66f8\u304d\u8fbc\u3080\u6700\u306f\u300116\u9032\u3092\u30ea\u30c8\u30eb\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u306b\u3057\u3066\u66f8\u304d\u8fbc\u3080<\/p>\n
\u30fb0x04 \u306fPower\u30ec\u30b8\u30b9\u30bf\u3067\u30010x04 \u306f\u96fb\u6d41\u30ec\u30b8\u30b9\u30bf\u3002<\/p>\n
\u30fbLSB \u3063\u3066\u306a\u306b\uff1f<\/p>\n
\u30fb\u3061\u3063\u3053\u3044\u4eca\u4f7f\u3063\u3066\u3044\u308b\u30d5\u30a1\u30f3\u306f\u3001150mA \u304f\u3089\u3044\u4f7f\u3063\u3066\u3044\u30660.3W \u307b\u3069<\/p>\n
\u30fbc \u306e\u30b5\u30f3\u30d7\u30eb\u306f\u3059\u3050\u306b\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u3051\u3069\u3082\u3001\u3069\u3046\u3093\u306a\u611f\u3058\u306b\u306a\u308b\u3093\u3060\u308d\u3046\uff1f<\/p>\n
\u30fb\u81ea\u5206\u3067\u4f5c\u3063\u305f\u30cf\u30fc\u30c9\u304c\u52d5\u3044\u3066\u3046\u308c\u3057\u3043\uff01<\/p>\n
\u30fb\u30ec\u30b8\u30b9\u30bf\u3063\u3066\u3001\u3069\u3046\u3044\u3046\u56de\u8def\u306a\u306e\uff1f \u3069\u3046\u3044\u3046\u4ed5\u7d44\u307f\u3067\u899a\u3048\u3066\u3044\u308b\u3093\u3060\u308d\u3046\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"
PCB \u304c\u7d44\u307f\u3042\u304c\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u3092 RasPi \u3067 i2c \u3059\u308b\u30b5\u30f3\u30d7\u30eb\u3092\u30b0\u30b0\u3063\u3066\u307f\u305f\u3089\u3001\u3044\u304f\u3064\u304b\u3042\u308a\u307e\u3057\u305f\u3002 RasPi \u306f\u3001i2c \u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30ed\u30fc\u30c9\u3057\u3066\u304a\u304d\u307e\u3057\u305f\u3002RasPi \u3067i2c \u306f\u521d\u3081\u3066\u4f7f […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","footnotes":""},"categories":[6,40,63,64,69],"tags":[],"acf":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"JunkHack","author_link":"https:\/\/hack.gpl.jp\/author\/junkhack\/"},"uagb_comment_info":0,"uagb_excerpt":"PCB \u304c\u7d44\u307f\u3042\u304c\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u3092 RasPi \u3067 i2c \u3059\u308b\u30b5\u30f3\u30d7\u30eb\u3092\u30b0\u30b0\u3063\u3066\u307f\u305f\u3089\u3001\u3044\u304f\u3064\u304b\u3042\u308a\u307e\u3057…","_links":{"self":[{"href":"https:\/\/hack.gpl.jp\/wp-json\/wp\/v2\/posts\/2049"}],"collection":[{"href":"https:\/\/hack.gpl.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hack.gpl.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hack.gpl.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hack.gpl.jp\/wp-json\/wp\/v2\/comments?post=2049"}],"version-history":[{"count":0,"href":"https:\/\/hack.gpl.jp\/wp-json\/wp\/v2\/posts\/2049\/revisions"}],"wp:attachment":[{"href":"https:\/\/hack.gpl.jp\/wp-json\/wp\/v2\/media?parent=2049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hack.gpl.jp\/wp-json\/wp\/v2\/categories?post=2049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hack.gpl.jp\/wp-json\/wp\/v2\/tags?post=2049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}