Ruby on Railsのシステム開発
< Ruby標準csv遅... | メイン | 下書き >
こんにちは。
操縦画面については、こんな感じ。
動画動作している様子と仕組みあたりの概要については、以下の動画を見るのがてっとりばやいかも。
http://jp.youtube.com/watch?v=-n3XV6GGdWY
http://www.nicovideo.jp/watch/sm2432398
動作環境動作環境は MacOSX 10.5 ですが、 rubyとgainerの接続ができれば、他のOSでも動作すると思います。
GainerというIOモジュールを使って実現しています。
参考書籍GainerとチョロQの接続方法については下記の書籍が詳しいです。
config/environments/development.rb以下、ソースについて解説していきます。
config/environments/development.rb あたりの最後に以下のような記述を追加してください。
require 'gainer'
require 'gainer' は、rubyからgainerを扱うためのgemです。gem install しておいてください。
top_controller.rb
require 'gainer' class TopController < ApplicationController def frontleft $gainer.digital_output[0] = false $gainer.digital_output[1] = true $gainer.digital_output[2] = false $gainer.digital_output[3] = true end def front $gainer.digital_output[0] = false $gainer.digital_output[1] = true $gainer.digital_output[2] = true $gainer.digital_output[3] = true end def frontright $gainer.digital_output[0] = false $gainer.digital_output[1] = true $gainer.digital_output[2] = true $gainer.digital_output[3] = false end def stop $gainer.digital_output[0] = true $gainer.digital_output[1] = true $gainer.digital_output[2] = false $gainer.digital_output[3] = false end
def backleft gainerのIOポートから入出力するためのgemがあり、これを使うと上記のように簡単にgainerを扱うことができます.l
difital_output[0] = true などとすることで、gainerのデジタル出力を操作することができます。 index.rhtml <html> <head> <%= javascript_include_tag :defaults %> <script type="text/javascript"> function keyput(event){ switch(event.keyCode){ case 81: key_q(); break; case 87: key_w(); break; case 69: key_e(); break; case 90: key_z(); break; case 88: key_x(); break; case 67: key_c(); break; } } function key_q(){ new Ajax.Request('/top/frontleft', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('5c2a0299f9f219731bb72866e9831ce211999fad')}); return false; } function key_w(){ new Ajax.Request('/top/front', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + enc odeURIComponent('5c2a0299f9f219731bb72866e9831ce211999fad')}); return false; } function key_e(){ new Ajax.Request('/top/frontright', {asynchronous:true, evalScripts:true, parameters:'authenticity_token= ' + encodeURIComponent('5c2a0299f9f219731bb72866e9831ce211999fad')}); return false; } function key_z(){ new Ajax.Request('/top/backleft', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('5c2a0299f9f219731bb72866e9831ce211999fad')}); return false; }
function key_x(){ <body>
ustreamのブログパーツを張りつけて映像配信を実現しています。 JavaScriptでキーイベントを取得して、AJAXでサーバー側にリクエストを飛ばしています。 なのでustreamによる映像配信がページ遷移で途絶えることはありません。
回路回路については先ほどの書籍と、以下の写真を参考にしてみて下さい。 使用しているリレーは941H-2C-5Dというもの。 秋葉原の秋月電子にて1個100円で購入しました。
リモコンリモコン側については以下のような感じ。
FirebugとAJAXFireBugを使うと、AJAXによるリクエストの様子が見れます。
FirebugとAJAX操縦中のサーバー側のログについては、こんな感じ。
まとめ以上、RailsでチョロQを操縦するハックについてでした。 この仕組みを使えば、色々と楽しそうなことができそうですね。 他にも面白いネタを考えています。次回もお楽しみに!
Hacked by id:akio0911 個人サイトhttp://d.hatena.ne.jp/akio0911/
トラックバック URL この記事にコメントする |
||||