
1月27日にせんだいメディアテークで催された「オープンソースソフトウェアの世界」というイベントに行ってきました。内容は、IPAの三浦広志氏とRubyの作者であるまつもとゆきひろ氏(Matz)の講演。
買ったばかりの、というか出版されたばかりのオライリーのRuby本にサインしていただきました。
軽く緊張してて、帽子被ったままサインを貰いに行ったのは良い(?)思い出。
$ ruby
hoge = 12
puts hoge #ここまで入力してCtrl + D
12 #実行結果の表示が出てくれる
$
#/usr/bin/env ruby
loop do
print "\r・.・ y"
$stdout.flush
sleep 0.25
print "\r・.・_"
$stdout.flush
sleep 0.25
end
$KCODE = "SUGEEEEEEEEE"要するに最初の1文字だけが'e','s','u','n'(及びそれぞれの大文字)なら、代入する文字列は何でも良いのだ。そして、これら以外のものを代入すると、$KCODEは"NONE"になる。
@motor_canon = NXTComm::MOTOR_C
def canon_fire
@nxt.set_output_state(
@motor_canon,
100,
NXTComm::MOTORON,
NXTComm::REGULATION_MODE_IDLE,
0,
NXTComm::MOTOR_RUN_STATE_RUNNING,
360
)
end
require 'sdl'
require 'rubygems'
require 'nxt_comm'
# control motors methods
# com is boolean. true/false = go/stop
def motor_right(com, pow)
if com
@nxt.set_output_state(
@motor_right,
pow,
NXTComm::MOTORON,
NXTComm::REGULATION_MODE_IDLE,
0,
NXTComm::MOTOR_RUN_STATE_RUNNING,
0
)
else
@nxt.set_output_state(@motor_right, 0, 0, 0, 0, 0, 0)
end
end
def motor_left(com, pow)
if com
@nxt.set_output_state(
@motor_left,
pow,
NXTComm::MOTORON,
NXTComm::REGULATION_MODE_IDLE,
0,
NXTComm::MOTOR_RUN_STATE_RUNNING,
0
)
else
@nxt.set_output_state(@motor_left, 0, 0, 0, 0, 0, 0)
end
end
#MoveMethods
def goAhead(com)
if com
puts "goAhead"
motor_right(true, -100)
motor_left(true, -100)
else
puts "goAhead stop"
motor_right(false, 0)
motor_left(false, 0)
end
end
def goBack(com)
if com
puts "goBack"
motor_right(true, 100)
motor_left(true, 100)
else
puts "goBack stop"
motor_right(false, 0)
motor_left(false, 0)
end
end
def rotateRight(com)
if com
puts "rotateRight"
motor_right(true, 85)
motor_left(true, -85)
else
puts "rotateRight stop"
motor_right(false, 0)
motor_left(false, 0)
end
end
def rotateLeft(com)
if com
puts "rotateLeft"
motor_right(true, -85)
motor_left(true, 85)
else
puts "rotateLeft stop"
motor_right(false, 0)
motor_left(false, 0)
end
end
#main
begin
@nxt = NXTComm.new("/dev/rfcomm0")
@motor_right = NXTComm::MOTOR_A
@motor_left = NXTComm::MOTOR_B
rescue
puts "Bluetooth Connection failed"
exit
end
puts "Connected to NXT"
SDL.init(SDL::INIT_VIDEO)
screen = SDL.setVideoMode(320, 240, 16, SDL::SWSURFACE)
puts "press ESC to exit"
while true
begin
case event = SDL::Event2.poll
when SDL::Event2::Quit
exit
when SDL::Event2::KeyDown
if event.sym == SDL::Key::UP
goAhead(true)
end
if event.sym == SDL::Key::DOWN
goBack(true)
end
if event.sym == SDL::Key::RIGHT
rotateRight(true)
end
if event.sym == SDL::Key::LEFT
rotateLeft(true)
end
when SDL::Event2::KeyUp
if event.sym == SDL::Key::UP
goAhead(false)
end
if event.sym == SDL::Key::DOWN
goBack(false)
end
if event.sym == SDL::Key::RIGHT
rotateRight(false)
end
if event.sym == SDL::Key::LEFT
rotateLeft(false)
end
if event.sym == SDL::Key::ESCAPE
puts "bye"
exit
end
end
rescue
puts "Error"
exit
end
end
exit
余計な部分もあるが、まだ少し弄くろうと思ってるので分かり易さ重視。$ hcitool scanを実行してNXTのMACアドレスを表示させる。
rfcomm0 {Bluetoothデーモンを再起動。
bind yes;
device MACアドレス;
channel 1;
comment "NXT";
}
/etc/init.d/bluetooth restartPC側から、もしくはNXT側からBluetooth接続要求を出して接続をペアリングする。ペアリングは上のサ行の前にやっても良かったかもしれない。
$ hcitool scanで機器を検索し、MACアドレスを表示させる。
$ sudo rfcomm bind 1 MACアドレス チャンネル番号で、MACアドレスで指定した機器への/dev/rfcomm1というデバイスファイルが出来る。チャンネル番号は省略して良い。省略すると自動で1が割り当てられる。
$ rfcomm何もオプションを指定せずにrfcommコマンドを叩くと、現在登録している機器の情報が表示される。
$ sudo rfcomm releace rfcomm1