#author("2018-10-25T12:33:36+09:00","default:aiy_gadget","aiy_gadget")
#author("2018-10-25T14:47:51+09:00","default:aiy_gadget","aiy_gadget")
[[AIY_Gadget]]

* Google AIY Voice Kitを利用した音声制御おもちゃ (ForkLift)[#s8217d01]
- 
#youtube(XgzDTKNBak4&t=14s)
#youtube(LmaOuKzHcio&t=24s)
- IoTLT広島 vol.8 におけるAIY_Gadgetの説明
-- https://www.slideshare.net/takashiyamanoue/aiy-voice-kit-neopixels
- Overview
-- &ref(AIY_Gadget/overview.png,30%);
** Google AIY Voice Kit [#uadefc15]
- Google の人工知能を使った音声認識システムを使って, 自分で音声認識応用システムを作るために Kitです。
- https://aiyprojects.withgoogle.com/voice/
** 作りかた... The Voice Controlled ForkLift の場合 [#m79daccd]
+ Google AIY Voice Kit とRaspberry Pi 3とmicro-SD(16GB以上)を買ってきます。
+ Kitに添付された英文のThe MagPi AIY Projects にしたがって、Google Speaker をそのまま最後まで作ります。
++ 箱を組み立てる必要はないのですが、動作確認のため、MagPiに記載された通りに最後まで作ってPythonのプログラムの動作確認を行うことをお勧めします。
++ 注意
+++ マニュアル P. 38, STEP 06, "the LED inside the arched button should both indicate the device is running." ... ボタンのLEDは点灯しません. ... https://www.raspberrypi.org/magpi/issues/essentials-aiy-v1/
++ 参考: https://kureuetan.com/web/raspberrypi/4346/ 
+ 
+ AIY Voice Kit で作った Google Speakerで、MagPi の chapter six の step 01 から step 07 まで、および、chapter seven の step 01 の backup first, step 02 の expect phrase, step 03 の handle phrase を一つずつ理解しながら実行し、p. 55 の cloudspeech_demo.py を作成し動作確認します。部品が手に入れば、step 04, step 05, step 06を実行して、led_demo.py を作成し, 実行します。p.59 のshutdown_demo.py を作成し動作確認します.
++ MagPi P.65の server_demo.py は servo_demo.py の間違いです。servoモータがあれば、これもテストしておくと良いです。
+ Arduino Nano と タミヤ3チャンネルリモコン フォークリフト工作セット
とブレッドボードとモータードライバ(Hブリッジ)を3個とジャンプワイヤの束を買ってきます。
++ https://store.arduino.cc/usa/arduino-nano
+ Arduino Nano 1個と タミヤ3チャンネルリモコン フォークリフト工作セット1個
とブレッドボードとモータードライバ(Hブリッジ)を3個とジャンプワイヤの束1束を買ってきます。
++ Arduino Nano: https://store.arduino.cc/usa/arduino-nano
+++ Arduino Nanoのバージョンに注意. 3.0だとダメです。Whats Next Purple だとちゃんと動きました。
+++ http://bizright-blog.tumblr.com/post/120520808729/arduino-nano30の落とし穴にはまったお話戸嶋
++ https://www.tamiya.com/japan/products/70115/index.html
++ http://akizukidenshi.com/catalog/g/gK-06375/
++ タミヤ3チャンネルリモコン フォークリフト工作セット: https://www.tamiya.com/japan/products/70115/index.html
++ モータードライバ(Hブリッジ) http://akizukidenshi.com/catalog/g/gK-06375/
+ AIY Voice Kit のRaspberry Pi (以後 Pi)に, Arduino のIDEをインストールします。
++ https://make.kosakalab.com/make/electronic-work/inst_arduino-ide_raspi/
+++ sudo apt-get install arduino では動かないので、注意。
+ PiのUSBコネクタの一つとArduino Nano をUSBケーブルで接続します。
+ PiのArduino IDEの[Tool]の[Borad]でArduino Nano を選び、[Tool]の[Port]で /dev/ttyUSB0 を選びます。
+ Arduino Nano で IDEのsketch の例の中のBasicのBlink(Lチカ)が動作することを確認します。ここで使うLEDはNano に最初から付いているLEDです。
+ Adafruit のNeoPixels のページを参考にして、Arduino Nano と NeoPixels Strip を接続し, サンプルプログラムを動かして動作確認します。
++ https://learn.adafruit.com/adafruit-neopixel-uberguide/the-magic-of-neopixels
+ 以下の arduino の sketch を、Raspberry Pi の arduino のIDEで作成し、arduino nano に書き込みます。
#code(python){{
#!/usr/bin/env python3
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A demo of the Google CloudSpeech recognizer."""

import aiy.audio
import aiy.cloudspeech
import aiy.voicehat
import serial
import time
import subprocess

def main():
  recognizer = aiy.cloudspeech.get_recognizer()
  recognizer.expect_phrase('turn on the light')
  recognizer.expect_phrase('turn off the light')
  recognizer.expect_phrase('blink')
  recognizer.expect_phrase('go')
  recognizer.expect_phrase('forward')
  recognizer.expect_phrase('back')
  recognizer.expect_phrase('left')
  recognizer.expect_phrase('right')
  recognizer.expect_phrase('up')
  recognizer.expect_phrase('down')
  recognizer.expect_phrase('stop')
  recognizer.expect_phrase('goodbye')
  recognizer.expect_phrase('shutdown')

  button = aiy.voicehat.get_button()
  led = aiy.voicehat.get_led()
  aiy.audio.get_recorder().start()
  ser=serial.Serial('/dev/ttyUSB0',9600)
  time.sleep(2)
  aiy.audio.say('Hello, I\'m listening.')

  while True:
    # print('Press the button and speak')
    # button.wait_for_press()
    print('Listening...')
    text = recognizer.recognize()
    if not text:
      print('Sorry, I did not hear you.')
      aiy.audio.say('Sorry, I did not hear you.')
    else:
      print('You said "', text, '"')
      aiy.audio.say('you said '+text+'.')
      if 'turn on the light' in text:
        led.set_state(aiy.voicehat.LED.ON)
      elif 'turn off the light' in text:
        led.set_state(aiy.voicehat.LED.OFF)
      elif 'blink' in text:
        led.set_state(aiy.voicehat.LED.BLINK)
      elif 'stop' in text:
        ser.write(b"stop;")
      elif 'goodbye' in text:
        os._exit(0)
      elif 'shutdown' in text:
        subprocess.call(["sudo","shutdown","-h","now"])
      elif 'forward' in text:
        ser.write(b"go;")
      elif 'go' in text:
        ser.write(b"go;")
      elif 'back' in text:
        ser.write(b"back;")
      elif 'left' in text:
        ser.write(b"left;")
      elif 'right' in text:
        ser.write(b"right;")
      elif 'up' in text:
        ser.write(b"up;")
      elif 'down' in text:
        ser.write(b"down;")

if __name__ == '__main__':
    main()
}}

#code(C){{
int w=60;
int d=20;
int c;
String str;

void setup() {

  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  digitalWrite(2,LOW);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
  Serial.begin(9600);
  c=0;
  str="stop"; 
}

void loop() {

  if(Serial.available()>0){
    str = Serial.readStringUntil(';');
    Serial.print(str); //for debug, echo back
    c=0;
  }
  if(str.indexOf("stop")==0){
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(7,LOW);
    digitalWrite(8,LOW);
  }
  else if(str.indexOf("go")==0){
    // digitalWrite(2,HIGH);
    // analogWrite(2,level);
    if(c<d) digitalWrite(2,HIGH); else digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    // digitalWrite(4,HIGH);
    // analogWrite(4,level);
    if(c<d) digitalWrite(4,HIGH); else digitalWrite(4,LOW);
    digitalWrite(5,LOW);
  }
  else if(str.indexOf("back")==0){
    digitalWrite(2,LOW);
    // digitalWrite(3,HIGH);
    // analogWrite(3,level);
    if(c<d) digitalWrite(3,HIGH); else digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    // digitalWrite(5,HIGH);
    // analogWrite(5,level);
    if(c<d) digitalWrite(5,HIGH); else digitalWrite(5,LOW);
  }
  else if(str.indexOf("left")==0){
    // digitalWrite(2,HIGH);
    // analogWrite(2,level);
    if(c<d) digitalWrite(2,HIGH); else digitalWrite(2,LOW);
    digitalWrite(3,LOW);
  }
  else if(str.indexOf("right")==0){
    // digitalWrite(4,HIGH);
    // analogWrite(4,level);
    if(c<d) digitalWrite(4,HIGH); else digitalWrite(4,LOW);
    digitalWrite(5,LOW);
  }
  else if(str.indexOf("up")==0){
    // digitalWrite(7,HIGH);
    // analogWrite(7,level);
    if(c<d) digitalWrite(7,HIGH); else digitalWrite(7,LOW);
    digitalWrite(8,LOW);
  }
  else if(str.indexOf("down")==0){
    digitalWrite(7,LOW);
    // digitalWrite(8,HIGH);
    // analogWrite(8,level);
    if(c<d) digitalWrite(8,HIGH); else digitalWrite(8,LOW);
  }
  delay(50);
  if(c>d) {
    c=0; 
    str="stop";
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(7,LOW);
    digitalWrite(8,LOW); 
  }
  else c++; 
}
}}

----
start-forklift.sh

#code(python){{
#!/bin/bash --rcfile

source /etc/bash.bashrc
source /home/pi/.bashrc

cat /etc/aiyprojects.info

cd /home/pi/AIY-projects-python
source /home/pi/AIY-projects-python/env/bin/activate
cd /home/pi/AIY-projects-python/src/aiy-forklift
python3 forklift-ex1.py

}}
----
forklift.service

#code(python){{
[Unit]
Description=FORKLIFT
After=syslog.target

[Service]
Type=simple
WorkingDirectory=/home/pi/AIY-projects-python/src/aiy-forklift
ExecStart=/bin/bash /home/pi/AIY-projects-python/src/aiy-forklift/start-forklift.sh
Restart = always
User = pi

[Install]
WantedBy = multi-user.target

}}
----
#counter


トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS