{"id":2049,"date":"2016-01-25T02:24:33","date_gmt":"2016-01-24T17:24:33","guid":{"rendered":"https:\/\/hack.gpl.jp\/?p=2049"},"modified":"2016-01-25T02:24:33","modified_gmt":"2016-01-24T17:24:33","slug":"post-2049","status":"publish","type":"post","link":"https:\/\/hack.gpl.jp\/2016\/01\/25\/post-2049.html","title":{"rendered":"INA226 \u3092 RasPi \u306e ruby \u3067i2c"},"content":{"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<\/p>\n

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\u3044\u307e\u3059\u3002\u7121\u4e8b\u306b\u51fa\u6765\u308b\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n

# lsmod\nModule                  Size  Used by\n\u2605i2c_dev                 6027  0 \ncfg80211              386508  0 \nrfkill                 16651  1 cfg80211\nrpcsec_gss_krb5        20958  0 \nnfsd                  263569  2 \nsnd_bcm2835            18649  0 \nsnd_pcm                73475  1 snd_bcm2835\nsnd_seq                53078  0 \nsnd_seq_device          5628  1 snd_seq\nsnd_timer              17784  2 snd_pcm,snd_seq\nsnd                    51038  5 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device\n\u2605i2c_bcm2708             4990  0 \njoydev                  8879  0 \nspi_bcm2708             5137  0 \nevdev                   9950  2 \nuio_pdrv_genirq         2958  0 \nuio                     8119  1 uio_pdrv_genirq<\/pre>\n

 <\/p>\n

\u5148\u8f29\u305f\u3061\u304c\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u66f8\u3044\u3066\u3044\u308b\u306f\u305a\u306a\u306e\u3067\u3001\u63a2\u3057\u305f\u3068\u3053\u308d\u3001python \u3067\u3084\u308b\u306e\u3068\u304b\u3001ruby \u3067\u3084\u308b\u306e\u3068\u304b\u304c\u3059\u3050\u898b\u3064\u304b\u308a\u307e\u3057\u305f\u3002<\/p>\n

\u3068\u308a\u3042\u3048\u305a\u3001\u52d5\u4f5c\u78ba\u8a8d\u3057\u305f\u3044\u306e\u3067\u4ee5\u4e0b\u306e\u30b5\u30a4\u30c8\u306e\u30b3\u30fc\u30c9\u3092\u53c2\u8003\u306b\u5fc5\u8981\u306a\u90e8\u5206\u3060\u3051\u4f7f\u308f\u305b\u3066\u3082\u3089\u3044\u307e\u3057\u305f\u3002<\/p>\n

\n

Rabbit Note
\n
BeagleBone Black \u3067\u4f5c\u308b\u30ed\u30ae\u30f3\u30b0\u6a5f\u80fd\u4ed8\u304d\u96fb\u529b\u8a08 (\u30bd\u30d5\u30c8\u7de8)<\/a><\/p>\n<\/blockquote>\n

BeagleBone Black(BBB\u3068\u7565\u3059\u308b\u3088\u3046\u3067\u3059)\u3067\u306e\u74b0\u5883\u3067\u3059\u304c\u3001\u5927\u5909\u53c2\u8003\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n

\u4f7f\u308f\u305b\u3066\u3044\u305f\u3060\u3044\u305f\u30b3\u30fc\u30c9\u306f\u6700\u4e0b\u90e8\u306b\u4ed8\u3051\u3066\u304a\u304d\u307e\u3059\u3002\u3067\u3001\u914d\u7dda\u306f\u5199\u771f\u306e\u3088\u3046\u306b\u3061\u3087\u3063\u3068\u96d1\u3067\u3059\u304c\u914d\u7dda\u3002\u8ca0\u8377\u306b\u306f\u3001\u3061\u3063\u3055\u3044\u30d5\u30a1\u30f3\u3092\u3064\u3051\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n

\"SingleShot0023\"<\/a> \u30b9\u30ec\u30fc\u30d6\u30a2\u30c9\u30ec\u30b9\u306f\u3001\u4f55\u3082\u8a2d\u5b9a\u3057\u306a\u3044\u3068\u30010x40 \u304c\u8a2d\u5b9a\u3055\u308c\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n

# i2cdetect -y 1\n     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\n00:          -- -- -- -- -- -- -- -- -- -- -- -- -- \n10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \n20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \n30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \n40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \n50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \n60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \n70: -- -- -- -- -- -- -- --  <\/pre>\n

 <\/p>\n

\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u8d77\u52d5\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

# .\/test.ruby\n--------- 2016\/01\/25, 01:33:22\n4.78125 V\n151 mA\n0.29 W\n--------- 2016\/01\/25, 01:33:22\n4.78125 V\n151 mA\n0.289 W\n--------- 2016\/01\/25, 01:33:23\n4.78875 V\n152 mA\n0.291 W\n--------- 2016\/01\/25, 01:33:23\n4.7875 V\n151 mA\n0.29 W\n--------- 2016\/01\/25, 01:33:24\n4.7875 V\n150 mA\n0.289 W\n--------- 2016\/01\/25, 01:33:24\n4.7875 V\n151 mA\n0.29 W\n--------- 2016\/01\/25, 01:33:25\n4.79 V\n152 mA\n0.292 W<\/pre>\n

\u8ca0\u8377\u306bLED \u3092\u70b9\u3051\u3066\u307f\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u3067\u3059\u3002<\/p>\n

# .\/test.ruby \n--------- 2016\/01\/25, 01:35:31\n4.88 V\n2 mA\n0.005 W\n--------- 2016\/01\/25, 01:35:31\n4.88 V\n2 mA\n0.005 W\n--------- 2016\/01\/25, 01:35:31\n4.9125 V\n2 mA\n0.005 W\n--------- 2016\/01\/25, 01:35:32\n4.91375 V\n2 mA\n0.005 W\n--------- 2016\/01\/25, 01:35:32\n4.91375 V\n2 mA\n0.005 W<\/pre>\n

 <\/p>\n

\u307e\u3060\u3042\u307e\u308a\u3061\u3083\u3093\u3068\u7406\u89e3\u3057\u3066\u3044\u307e\u305b\u3093\u304c\u3001\u5185\u90e8\u306e\u30ec\u30b8\u30b9\u30bf\u306e ox05 \u306b\u3001\u30ad\u30e3\u30ea\u30d6\u30ec\u30fc\u30b7\u30e7\u30f3\u3059\u308b\u8a2d\u5b9a\u5024\u3092\u5165\u308c\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n

\u4ee5\u4e0b\u306e C \u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u304b\u3089\u3001\u30b7\u30e3\u30f3\u30c8\u62b5\u6297\u306b0.025R \u3092\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a08\u7b97\u3057\u305f16\u9032\u6570\u3092\u5165\u308c\u308c\u3070\u3088\u3044\u3088\u3046\u3067\u3059\u3002\u66f8\u304d\u8fbc\u3080\u3068\u304d\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u30ea\u30c8\u30eb\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u306b\u5909\u63db\u3057\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n

https:\/\/github.com\/jarzebski\/Arduino-INA226\/blob\/master\/INA226.cpp<\/a><\/p>\n

0.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}]}}