Raspberry PiB+ & Pi2


Raspberry Piを使って、音声命令でキャタピラを操作(3)

キャタピラのコントロールプログラムの設定



次のように設定して、キャタピラのコントロールプログラムをPythonで作成し操作します。
 ・WiringPiをインストール。
 ・「julius」、「GPIO」、「jtalk」をつなぐPythonで記述したプログラムを作成。
 ・プログラムの自動起動を設定。



  WiringPiのインストール


RaspberryPiのGPIOピンのコントロールを「WiringPi」を使ってコントロールします。

1.次のコマンドを実行して、WiringPiをインストールします。

  $ cd
  $ sudo apt-get install git-core
  $ git clone git://git.drogon.net/wiringPi
  $ cd wiringPi/
  $ ./build


2.次のコマンドを実行して、インストールを確認します。

  $ gpio -v

 バージョン番号が表示されれば、OKです。




  Pythonによるプログラム


juliusサーバーから情報を受け取るクライアントを作成します。

「julius」、「GPIO」、「jtalk」をつなぐPythonで記述したプログラム「vremocon.py」を 作成します。
1.次のコマンドを実行して、「vremocon.py」を作成

  $ cd
  $ nano vremocon.py


2.「vremocon.py」の内容は、次の通りです。

# -*- coding: utf-8 -*-

import socket
import sys
import select
import os
import threading
host = "localhost"
port = 10500
bufsize = 1024
timer_length = 120.0
standby = False

os.system("gpio -g mode 8 out")
os.system("gpio -g mode 7 out")
os.system("gpio -g mode 23 out")
os.system("gpio -g mode 24 out")
os.system("/home/pi/jtalk.sh 音声リモコンを起動しました")

def standbyoff():
  global standby
  if standby == True:
    os.system("/home/pi/jtalk.sh 待機モードを解除します")
    standby = False
    stop()
    
def stop():
  os.system("gpio -g write 8 0")
  os.system("gpio -g write 7 0")
  os.system("gpio -g write 23 0")
  os.system("gpio -g write 24 0")
  
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))

while True:
  inputready, outputready, exceptrdy = select.select([client_socket], [],[])
  for s in inputready:
    if s == client_socket:
      message = client_socket.recv(bufsize)
      print "受診したメッセージ : " + message
      if message == "":
        print 'クライアントの実行を停止します'
        break
      if standby == True:
        if "止まれ" in message:
          os.system("amixer set Capture nocap")
          os,system("/home/pi/jtalk.sh 止まります")
          os.system("amixer set Capture cap")
          stop()
        if "前進" in message:
          stop()
          os.system("amixer set Capture nocap")
          os,system("/home/pi/jtalk.sh 前進します")
          os.system("amixer set Capture cap")
          os.system("gpio -g write 8 1")
          os.system("gpio -g write 7 0")
          os.system("gpio -g write 23 1")
          os.system("gpio -g write 24 0")
        if "後進" in message:
          stop()
          os.system("amixer set Capture nocap")
          os,system("/home/pi/jtalk.sh ばっくします")
          os.system("amixer set Capture cap")
          os.system("gpio -g write 8 0")
          os.system("gpio -g write 7 1")
          os.system("gpio -g write 23 0")
          os.system("gpio -g write 24 1")
        if "左前" in message:
          stop()
          os.system("amixer set Capture nocap")
          os,system("/home/pi/jtalk.sh 前進で左に曲がります")
          os.system("amixer set Capture cap")
          os.system("gpio -g write 8 0")
          os.system("gpio -g write 7 0")
          os.system("gpio -g write 23 1")
          os.system("gpio -g write 24 0")
        if "左後" in message:
          stop()
          os.system("amixer set Capture nocap")
          os,system("/home/pi/jtalk.sh ばっくで左に曲がります")
          os.system("amixer set Capture cap")
          os.system("gpio -g write 8 0")
          os.system("gpio -g write 7 0")
          os.system("gpio -g write 23 0")
          os.system("gpio -g write 24 1")
        if "右前" in message:
          stop()
          os.system("amixer set Capture nocap")
          os,system("/home/pi/jtalk.sh 前進で右に曲がります")
          os.system("amixer set Capture cap")
          os.system("gpio -g write 8 1")
          os.system("gpio -g write 7 0")
          os.system("gpio -g write 23 0")
          os.system("gpio -g write 24 0")
        if "右後" in message:
          stop()
          os.system("amixer set Capture nocap")
          os,system("/home/pi/jtalk.sh ばっくで右に曲がります")
          os.system("amixer set Capture cap")
          os.system("gpio -g write 8 0")
          os.system("gpio -g write 7 1")
          os.system("gpio -g write 23 0")
          os.system("gpio -g write 24 0")
        if "受付モードOFF" in message:
          standby = False
          os,system("/home/pi/jtalk.sh 待機モードを解除します")
        if "受付モードON" in message:
          standby = False
          os,system("/home/pi/jtalk.sh 既に待機中です")
    elif "受付モードON" in message:
      os,system("/home/pi/jtalk.sh 待機モードに入ります")
      standby = True
      t = threading.Timer(timer_length, standbyoff)
      t.start()
      
client_socket.close()
						

3.次のような操作方法になります。
(1)起動後、メイちゃんが「音声リモコンを起動しました」といいます。

(2)次に「スタンバイ」と言うと、メイちゃんが「待機モードに入ります」と返します。

(3)その後、「前」、「後」、「止まれ」、「右前」、「右後」、「左前」、「左後」と言うと 一旦止まり、
  メイちゃんが返事を返して命令通りに動きます。

(4)「ニュートラル」と言うと、「待機モードを解除します」とメイちゃんが返事を返し、 待機モードを解除します。

(5)再度、「スタンバイ」というと(2)のようになります。

(6)待機モードに入ってから2分後に待機モードを自動解除し、モータを停止します。




  「julius」と「vremocon.py」の起動


1.次のコマンドを実行して、起動します。

  $ julius -C ~/julius/julius.conf &

2.jyuriusが起動後、下図のように表示され止まります。

3.Enterキーを押すとプロンプトが表示されます。

4.プロンプトの後に次のコマンドを実行すれば、「vremocon.py」が起動します。

  $ python vremocon.py

5.しばらくするとメイちゃんが「音声リモコンを起動しました」と言い、
(「julius」と「vremocon.py」の起動の2 )の操作をします。
  下図が操作途中の画面です。


6.「Ctrl」+「C」キーを押すと「vremocon.py」のプログラムが停止しプロンプトが表示されます。(下図)


7.「julius」はバックグランドで動いていますので、RaspberryPiのデスクトップ画面でシャットダウンします。




  自動起動を設定


RaspberryPi が起動後、自動的に「julius」と「vremocon.py」が起動するように設定します。

1.スクリプトファイル「rc.local」に以下のスクリプトを追加し自動起動できるようにします。
  $ sudo nano /etc/rc.local

2.「rc.local」ファイルの内容で、最後の「exit 0」の前に以下の内容を追加し保存します。

  su - pi -c 'julius -C /home/pi/julius/julius.conf &'
  sleep 10
  su - pi -c 'python /home/pi/vremocon.py &'
  exit 0


3.RaspberryPi をシャットダウンします。

4.再度、RaspberryPi を起動すると、しばらくしてメイちゃんの声で「音声リモコンを起動しました」 と言います。

5.「スタンバイ」と話しかければ、メイちゃんが「待機モードに入ります」と返します。
  (最初は反応が遅いと思います。)

6.「待機モードに入ります」と返事があれば、音声命令を話しかければキャタピラが反応します。

7.待機モードに入ってから何もしないで2分間たつと「待機モードを解除します」とメイちゃんが 返事をします。

8.終了するときは、PCのリモート操作で、RaspberryPi をシャットダウンします。