rubyでwiiバランスボード

ついに、WiiBoardに手がとどいたよ。
sudo apt-get install wminput lswm
で出来た/usr/lib/libcwiid.so.1には、バグがあったみたい。
ソースから、やり直してみた。
sudo apt-get install git
sudo apt-get install automake
sudo apt-get install flex
sudo apt-get install bison
sudo apt-get install libgtk2.0-dev
sudo apt-get install python-dev
してから
git clone https://github.com/abstrakraft/cwiid.git
cd ./cwiid
aclocal
autoconf
./configure
make
sudo make install
だけど、libcwiid.so.1が置き換えられないみたい。
よくわからないから、プログラムと同じフォルダに
libcwiid/libcwiid.so.1.0をコピーして使ってみた。

wiiリモコンと同じで
hcitool scanでバランスボードのIDをてにいれる。
データの使い方は
http://abstrakraft.org/cwiid/ticket/63
のweighdemo.py を参考にした。
プログラムを起動したらボードの裏にある電池の側の赤いボタンを押す。

<こちらにCwiidFFIのモジュール載せときました。>
http://gaziya.exblog.jp/15643259/
+++++++++++++++++++++++++++++++

#!usr/bin/env ruby

require 'rubygems'
require 'ffi'

module CwiidFFI
extend FFI::Library
ffi_lib './libcwiid.so.1.0'

class BalanceCal < FFI::Struct
layout :right_top, [:uint16,3],
:right_bottom, [:uint16,3],
:left_top, [:uint16,3],
:left_bottom, [:uint16,3]
end

class CwiidIrSrc < FFI::Struct
layout :valid, :char,
:pos, [:uint16, 2],
:size, :uint8
end

class BalanceState < FFI::Struct
layout :right_top, :uint16,
:right_bottom, :uint16,
:left_top, :uint16,
:left_bottom, :uint16
end

class CwiidState < FFI::Struct
layout :rpt_mode, :uint8,
:led, :uint8,
:rumble, :uint8,
:battery, :uint8,
:buttons, :uint16,
:acc, [:uint8, 3],
:ir_src, [CwiidIrSrc, 4],
:ext_type, :int,
:ext, BalanceState,
:error, :int
end

attach_function :str2ba,[:string,:pointer],:int
attach_function :cwiid_open,[:pointer,:int],:pointer
attach_function :cwiid_close,[:pointer],:int
attach_function :cwiid_set_led,[:pointer,:int],:int
attach_function :cwiid_get_balance_cal,[:pointer,BalanceCal],:int
attach_function :cwiid_set_rpt_mode,[:pointer,:uint8],:int
attach_function :cwiid_get_state, [:pointer, CwiidState], :int
end

def calc_weight(state,cals)
weight = 0
[:right_top,:right_bottom,:left_top,:left_bottom].each do |sensor|
reading = state[:ext][sensor]
cal = cals[sensor]
if reading < cal[1]
weight += 1700 * (reading - cal[0]) / (cal[1] -cal[0])
else
weight += 1700 * (reading - cal[1]) / (cal[2] -cal[1]) + 1700
end
end
weight
end

address = "00:26:59:39:A0:38"
bdaddr = FFI::MemoryPointer.new(:pointer)
CwiidFFI::str2ba(address, bdaddr)
puts "Put Wiimote in discoverable mode now (press red button)..."
wiimote = CwiidFFI::cwiid_open(bdaddr,0)
balance_cal = CwiidFFI::BalanceCal.new
CwiidFFI::cwiid_get_balance_cal(wiimote,balance_cal)
CwiidFFI::cwiid_set_rpt_mode(wiimote,0x02 | 0x40)
cwiid_state = CwiidFFI::CwiidState.new
CwiidFFI::cwiid_set_led(wiimote,0x01)
100.times do
CwiidFFI::cwiid_get_state(wiimote,cwiid_state)
puts ":buttons => %d" % cwiid_state[:buttons]
puts ":right_top => %d" % cwiid_state[:ext][:right_top]
puts ":right_bottom => %d" % cwiid_state[:ext][:right_bottom]
puts ":left_top => %d" % cwiid_state[:ext][:left_top]
puts ":left_bottom => %d" % cwiid_state[:ext][:left_bottom]
puts "Weight: %dKg.." % (calc_weight(cwiid_state,balance_cal) / 100)
puts "#########################################"
sleep 1
end
CwiidFFI::cwiid_close(wiimote)
[PR]
by gaziya | 2011-03-31 22:51