ランプの中身(Ruby on Railsのシステム開発)
ランプの中身(Ruby on Railsのシステム開発)では、株式会社ケイビーエムジェイのRuby on Railsエンジニアが蓄積したノウハウを公開しています。Ruby on Railに関する技術解説や実践的なノウハウなど、開発現場の技術に則したコンテンツを随時追加していきます。 初心者の方でもわかりやすい技術解説を心がけています。リクエスト、ご質問も受け付けいますので、ご気軽にコメントを記述して下さい。

Ruby on Railsブログ投稿者一覧

Ruby on Railsブログ投稿者一覧
  • akio0911の中身(ruby on railsシステム開発)
  • tacchiの中身(Ruby on Railsのシステム開発)
  • zenpouの中身(Ruby on Railsのシステム開発)
  • tnの中身(Ruby on Railsのシステム開発)
  • yoppiの中身(Ruby on Railsのシステム開発)
  • 中平の中身(Ruby on Railsのシステム開発)
  • PHPなら(Ruby on Railsのシステム開発)
  • x5rの中身(Ruby on Railsのシステム開発)
  • O2の中身(Ruby on Railsのシステム開発)
  • mimiの中身(Ruby on Railsのシステム開発)
  • ワイYの中身(Ruby on Railsのシステム開発)
  • kimiの中身(Ruby on Railsのシステム開発)
  • TrinityTの中身(Ruby on Railsのシステム開発)
  • 花吹雪の中身(Ruby on Railsのシステム開発)
  • hondaの中身(Ruby on Railsのシステム開発)

全体のRoR最新ブログ一覧

ssatoのアーカイブ

プロフィール

  • ssato
  • 2008/01/07にRuby, Railsの開発を始めた新米です。

ブログの購読

RSS

< Ruby標準csv遅... |  メイン  |  下書き >

Do Ruby!   Do Ruby!
 
akio0911の中身(ruby

2008.02.25

北海道にあるチョロQを東京からWebブラウザで操縦する方法 on Rails


ブックマークに追加する

こんにちは。
id:akio0911です。


今回はRailsでチョロQを操縦するページを作ってみました。

操縦画面については、こんな感じ。


動画

動作している様子と仕組みあたりの概要については、以下の動画を見るのがてっとりばやいかも。

 

 

 

http://jp.youtube.com/watch?v=-n3XV6GGdWY 

 

 

 

http://www.nicovideo.jp/watch/sm2432398 

 

動作環境

動作環境は MacOSX 10.5 ですが、

rubyとgainerの接続ができれば、他のOSでも動作すると思います。 

 

GainerというIOモジュールを使って実現しています。

 

参考書籍

GainerとチョロQの接続方法については下記の書籍が詳しいです。

 

MASHUP++ (単行本)

 



config/environments/development.rb

以下、ソースについて解説していきます。

 

 

 config/environments/development.rb あたりの最後に以下のような記述を追加してください。

 

require 'gainer'

$gainer = Gainer::Serial.new('/dev/cu.usbserial-A2002mcq')
$gainer.digital_output = 0xf
sleep(1)
$gainer.digital_output = 0
sleep(1)

 

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.digital_output[0] = true
    $gainer.digital_output[1] = false
    $gainer.digital_output[2] = false
    $gainer.digital_output[3] = true
  end

  def back
    $gainer.digital_output[0] = true
    $gainer.digital_output[1] = false
    $gainer.digital_output[2] = true
    $gainer.digital_output[3] = true
  end

  def backright
    $gainer.digital_output[0] = true
    $gainer.digital_output[1] = false
    $gainer.digital_output[2] = true
    $gainer.digital_output[3] = false
  end
end

 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(){
           new Ajax.Request('/top/back', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + en
codeURIComponent('5c2a0299f9f219731bb72866e9831ce211999fad')}); return false;
}
function key_c(){
           new Ajax.Request('/top/backright', {asynchronous:true, evalScripts:true, parameters:'authenticity_token='
 + encodeURIComponent('5c2a0299f9f219731bb72866e9831ce211999fad')}); return false;
}

function keyputup(event){
          new Ajax.Request('/top/stop', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + enc
odeURIComponent('5c2a0299f9f219731bb72866e9831ce211999fad')}); return false;
}

document.onkeydown=keyput;
document.onkeyup=keyputup;

</script>
</head>

<body>
<embed width="416" height="340" flashvars="autoplay=true" src="http://ustream.tv/SxQwT0VkmQeydBJKd2fj1pniqS2xUiuz.us
c" type="application/x-shockwave-flash" allowfullscreen="true" />

<embed width="563" height="266" type="application/x-shockwave-flash" flashvars="channel=#gainer&server=chat1.ustream
.tv" pluginspage="http://www.adobe.com/go/getflashplayer" src="http://ustream.tv/IrcClient.swf" allowfullscreen="tru
e" />

<hr>

<table>

<tr>
<td><%= link_to_remote("left", :url => { :action => "frontleft" }) %></td>
<td><%= link_to_remote("front", :url => { :action => "front" }) %></td>
<td><%= link_to_remote("right", :url => { :action => "frontright" }) %></td>
</tr>

<tr>
<td><%= link_to_remote("left", :url => { :action => "backleft" }) %></td>
<td><%= link_to_remote("back", :url => { :action => "back" }) %></td>
<td><%= link_to_remote("right", :url => { :action => "backright" }) %></td>
</tr>

</table>


</body>

</html>

 

 ustreamのブログパーツを張りつけて映像配信を実現しています。

JavaScriptでキーイベントを取得して、AJAXでサーバー側にリクエストを飛ばしています。

なのでustreamによる映像配信がページ遷移で途絶えることはありません。

 

回路

 回路については先ほどの書籍と、以下の写真を参考にしてみて下さい。

使用しているリレーは941H-2C-5Dというもの。

秋葉原の秋月電子にて1個100円で購入しました。

 

 

リモコン

リモコン側については以下のような感じ。

 

 

 

FirebugとAJAX

FireBugを使うと、AJAXによるリクエストの様子が見れます。

 

 

FirebugとAJAX

操縦中のサーバー側のログについては、こんな感じ。

 

 

まとめ

以上、RailsでチョロQを操縦するハックについてでした。

この仕組みを使えば、色々と楽しそうなことができそうですね。

他にも面白いネタを考えています。次回もお楽しみに!

 

Hacked by id:akio0911

個人サイト

 http://d.hatena.ne.jp/akio0911/

 

コメント (0)  |トラックバック (0)

参考になったらクリック!!⇒

トラックバック URL

この記事にコメントする

ニックネーム:
メールアドレス:
URL:



 
Do Ruby!   Do Ruby!
株式会社ケイビーエムジェイロゴ