-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
154 lines (129 loc) · 3.5 KB
/
util.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import struct
import termios, tty
import sys
import Jetson.GPIO as GPIO
from picovoice import Picovoice
import pyaudio
import os, time
import threading
import signal
access_key = "" # AccessKey obtained from Picovoice Console
keyword_path = '/home/jetbot/Downloads/computer_en_jetson_v2_0_0.ppn'
context_path = '/home/jetbot/Downloads/switch_en_jetson_v2_0_0.rhn'
GPIO.setwarnings(False)
auto=False
left_en = 21
right_en = 20
motorLeft = 16
motorRight = 19
pin_1 = 25
pin_2 = 8
def init_gpio():
print("in init func now")
GPIO.setmode(GPIO.BCM)
GPIO.setup(motorLeft,GPIO.OUT)
GPIO.setup(motorRight,GPIO.OUT)
GPIO.setup(left_en,GPIO.OUT)
GPIO.setup(right_en,GPIO.OUT)
GPIO.setup(pin_1, GPIO.OUT)
GPIO.setup(pin_2, GPIO.OUT)
GPIO.output(pin_1, False)
GPIO.output(pin_2, False)
GPIO.output(motorLeft, False)
GPIO.output(motorRight, False)
GPIO.output(left_en, False)
GPIO.output(right_en, False)
def back():
print("moving back!!!!!!")
GPIO.output(motorLeft, False)
GPIO.output(left_en, True)
GPIO.output(motorRight, False)
GPIO.output(right_en, True)
def right():
GPIO.output(motorLeft, False)
GPIO.output(left_en, True)
def left():
GPIO.output(motorLeft, True)
GPIO.output(left_en, True)
def forward():
GPIO.output(motorLeft, True)
GPIO.output(left_en, True)
GPIO.output(right_en, True)
GPIO.output(motorRight, True)
def zoom():
GPIO.output(pin_2, True)
time.sleep(2.5)
GPIO.output(pin_2, False)
def zoom_out():
GPIO.output(pin_1, True)
time.sleep(2.5)
GPIO.output(pin_1, False)
def stop():
GPIO.output(left_en, False)
GPIO.output(right_en, False)
#Define a function to get the pressed key
def getch():
#Get standard input file descriptor
fd = sys.stdin.fileno()
#Get terminal attributes of file descriptor
old_settings = termios.tcgetattr(fd)
#Make it unnecessary to press the enter key
tty.setcbreak(fd)
#Receive characters typed on the keyboard
ch = sys.stdin.read(1)
#Restore the terminal attributes of the file descriptor
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
#ch(character =In this case the pressed key)return it
return ch
def wake_word_callback():
print("[wake word]")
def inference_callback(inference):
global auto
if inference.is_understood:
if inference.intent == 'start':
auto = True
elif inference.intent == 'stop':
auto = False
def controls(direction):
while auto:
if auto:
direction=getch()
print(direction)
else:
stop()
return
if direction == 'l':
left()
if direction == 'r':
right()
if direction == 'f':
forward()
if direction == 'b':
back()
direction=''
time.sleep(2)
stop()
stop()
def listen_for_voice():
print("inside listen for audio")
while True:
pcm = audio_stream.read(pv.frame_length)
audio_frame = struct.unpack_from("h" * pv.frame_length, pcm)
pv.process(audio_frame)
def use_controls(opt=None):
while True:
try:
if auto:
print("testsing")
direction=getch()
controls(direction)
# ctrl+c catch here
except KeyboardInterrupt:
break
def handler(signum, frame):
print("interupt detected")
thread.join()
thread_eile.join()
stop()
GPIO.cleanup()
exit(1)