IRラジコン作成回答

ruby hardware programming

IRラジコン作成回答

注意

リモコン内のボタンの区別はできません。 操作の数の5つのリモコンを用意する必要があります。 ラズベリーパイに重たい動作をさせている場合、赤外線受信がうまくいかなくなります。 不要な処理は起動していない状態で実施してください。

赤外線読み取りメソッドを追加します。

def read_ir(ir_receiver, signal)
  # puts "IR read start ... "
  captureTimings = []
  pre_time = Time.now
  while Time.now - pre_time < RECORDING_TIMEOUT
    if ir_receiver.read != signal
      now_time = Time.now
      captureTimings << now_time - pre_time
      pre_time = now_time
      signal ^= 1
    end
  end
  
  # 先頭のタイミングは削除
  # captureTimings.shift
  captureTimings = captureTimings.map { |timing| (timing*1000000).floor}
  puts "receive result:"
  puts "[#{captureTimings.join(',')}]"
  puts "IR read complete."
  
  return captureTimings
end

赤外線監視メソッドを追加します。

def watch_ir()
  ir_receiver = PiPiper::Pin.new :pin => 4

  puts "watch IR Changed ..."
  before_signal = ir_receiver.read
  loop do
    signal = ir_receiver.read
    if signal != before_signal
      # puts "signal changed."
      captureTimings = read_ir(ir_receiver, signal)
      mortor_order =  convert_ir_to_mortor_order(captureTimings)
      if mortor_order != ""
        send_request(mortor_order)
      end

      puts "watch IR Changed ..."
    end

    before_signal = signal
    # sleep 0.000000001
  end
end

前回と値が異なる場合は、赤外線受信されたと判断し 赤外線読み取りメソッドを呼び出します。 OnOffの周期の時間差を記録する仕組み上 赤外線読み取りまでにインターバルを挟む動作コンソールログ出力などはしないようにしています。

各赤外線学習

赤外線の学習を行います。

以下コマンドを実行します。

cd ~/IRMortorControl
rbenv sudo bundle exec ruby ir_mortor_control.rb

赤外線送信します。

以下のような画面が表示されるため、receive result:~IR read complete. までをコピーします。

watch IR Changed ...
receive result:
[1963,997,5491,996,1590,530,655,342,534,531,533,757,276,467,538,463,533,533,529,468,533,468,631,406,535,531,532,497,533,469,533,496,537,533,468,535,531,466,1602,467,533,467,562,465,601,466,531,466,976,87,538,467,536,469,599,467,556,468,532,468,535,534,561,463,501,527,530,467,1578,470,577,463,530,473,1579,510,1578,448,568,468,1560,542,467,533,593,11203,11078,494,552,481,553,481,554,482,547,416,619,417,551,548,485,488,546,530,547,471,546,491,547,448,517,531,541,449,588,449,556,460,597,444,1562,521,526,445,589,463,520,524,519,530,531,445,590,442,615,445,525,513,527,451,598,440,592,444,533,515,514,450,597,464,1547,536,531,517,522,437,1649,447,1578,442,587,451,1617,460,530,445,601,11267,2009,955,5499,1034,1556,442,582,444,593,520,512,510,538,441,596,438,603,455,520,515,526,454,602,441,580,441,526,523,515,517,518,454,590,445,578,443,810,229,1553,522,586,438,584,447,520,517,509,532,512,455,592,446,578,438,591,439,598,430,596,439,578,436,584,441,591,503,499,506,1606,439,589,428,592,1177,875,473,1534,470,602,660,1329,536,538,468,534]
IR read complete.

各赤外線設定

各モーター起動の赤外線学習内容に学習した赤外線を上書きします。

# 前進   
FORWARD_TIMINGS = [335,465,339,476,340,407,406,637,139,468,335,463,401,372,409,405,341,499,339,464,334,469,536,276,406,408,333,467,336,588,206,469,334,431....]
# 左     
LEFT_TIMINGS = [399,401,650,989,569,275,405,405,403,1214,467,338,453,337,481,332,467,402,406,403,405,1203,463,335,500,340,456,1186,430,1191,407,1210,467,1189,405,411,406,420,401,400,463,499,276,407,408,407,435,336,464....]
# 右     
RIGHT_TIMINGS = [9,533,465,538,470,700,337,1601,472,537,583,469,467,533,466,600,469,532,469,535,467,600,468,535,510,497,539,541,472,539,514,487,593,472,467,533,467,589,465,529,465,1594,767,283,470,1588,436,1586,496,535,459,1632,471,510,468,604....]
# バック  
BACK_TIMINGS = [9,533,465,538,470,700,337,1601,472,537,583,469,467,533,466,600,469,532,469,535,467,600,468,535,510,497,539,541,472,539,514,487,593,472,467,533,467,589,465,529,465,1594,767,283,470,1588,436,1586,496,535,459,1632,471,510,468,604....]
# ブレーキ 
BREAKE_TIMINGS = [542,1757,525,600,476,1788,524,596,458,1789,517,656,457,658,463,772,337,657,527,1710,529,654,464,1787,488,652,460,1761,655,1634,459,1768,528,1740,495,40183,8975,2307,531,96622,9163,2135,466....]

動作確認

動作確認をします。ターミナルを2つ立ち上げます。

1つ目

cd ~/IRMortorControl
rbenv sudo bundle exec rails ir_mortorcontrol_server:wake_up

2つ目

cd ~/IRMortorControl
rbenv sudo bundle exec ruby ir_mortor_control.rb

リモコンを送信し、割り当てた学習内容でモーターが想定通りに動くことを確認します。

回答ソースの例はこちら