- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2018-07-02T16:39:56+09:00","default:aiy_gadget","aiy_gadget")
#author("2018-07-02T23:54:21+09:00","default:aiy_gadget","aiy_gadget")
[[AIY_Gadget]]
#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