-
Notifications
You must be signed in to change notification settings - Fork 3
/
commands.py
65 lines (56 loc) · 2 KB
/
commands.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
#!/usr/bin/env python3
import argparse
import json
import serial
import time
from vosk import Model, KaldiRecognizer
command_triggered = {
1: False, # Forward
0: False, # Backward
2: False, # Stop
3: False, # Left
4: False # Right
}
state = "stop" # Initial state
previous_state = state # Variable to track the previous state
cooldown_period = 4 # seconds
# Establish serial connection to Arduino
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.flush()
def handle_command(result_text):
global state, command_triggered, last_action_time
if "front" in result_text or "forward" in result_text:
if not command_triggered[1]:
ser.write(b'F')
state = "running"
last_action_time = time.time()
command_triggered[1] = True
elif "back" in result_text or "backward" in result_text:
if not command_triggered[0]:
ser.write(b'B')
state = "running"
last_action_time = time.time()
command_triggered[0] = True
elif "stop" in result_text:
if not command_triggered[2]:
ser.write(b'S')
state = "stop"
last_action_time = time.time()
command_triggered[2] = True
elif "lift" in result_text or "left" in result_text:
if not command_triggered[3]:
ser.write(b'L')
state = "running"
last_action_time = time.time()
command_triggered[3] = True
elif "right" in result_text:
if not command_triggered[4]:
ser.write(b'R')
state = "running"
last_action_time = time.time()
command_triggered[4] = True
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Command handling script")
parser.add_argument("-r", "--result-text", type=str, help="Result text from the recognizer")
args = parser.parse_args()
handle_command(args.result_text)