AIY_Gadget

٤Ƴ٤Ĥ
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
-
|
|
|
|
|
|
|
|
|
|
|
|
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#!/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()
٤Ƴ٤Ĥ
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 
 
 
 
 
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
 
-
|
-
|
|
|
!
-
|
|
|
|
|
|
!
-
-
|
!
|
-
|
!
|
!
-
|
-
|
!
|
-
|
!
!
-
-
|
!
|
!
-
-
|
!
|
!
-
-
|
!
|
!
-
|
-
|
!
!
|
-
|
|
|
|
|
|
|
|
!
|
!
int w=60;
int d=30;
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++; 
}

Counter: 771, today: 1, yesterday: 0

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS