-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_no_camera.ps1
126 lines (104 loc) · 4.27 KB
/
run_no_camera.ps1
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
#########################################################################################
# Copyright (C) 2017 FRC Team 1736 Robot Casserole - www.robotcasserole.org
#########################################################################################
# Non-legally-binding statement from Team 1736:
# Thank you for taking the time to read through our software! We hope you
# find it educational and informative!
# Please feel free to snag our software for your own use in whatever project
# you have going on right now! We'd love to be able to help out! Shoot us
# any questions you may have, all our contact info should be on our website
# (listed above).
# If you happen to end up using our software to make money, that is wonderful!
# Robot Casserole is always looking for more sponsors, so we'd be very appreciative
# if you would consider donating to our club to help further STEM education.
#########################################################################################
#
# run_no_camera.ps1 - Opens the JeVois serial port, starts the vision algorithm,
# and dumps any results to console
#
# Algorithm:
# 1) Search all COM ports for JeVois (unopened and responds correctly to ping)
# 2) Run commands to start vision processing (setmaping, streamon)
# 3) Dump all output to the console
#########################################################################################
#########################################################################################
$jevois_port_name = ""
# Get all port names present on the PC
$port_name_list = [System.IO.Ports.SerialPort]::getportnames()
#Strategy: for each port on the system, attempt to open it and send "ping".
# if it responds with ALIVE, we assume we have found the JeVois.
foreach($port_name in $port_name_list)
{
echo "[serial_listen] Checking $port_name"
try {
#Try to open & configure the port
$port= new-Object System.IO.Ports.SerialPort $port_name,115200,None,8,one
$port.Open()
$port.ReadTimeout = 500
}
catch {
echo "[serial_listen] Failed to open and configure $port_name"
if($port.IsOpen){
$port.Close()
}
continue
}
try {
#Assuming the port open works, send ping.
# Note if we send this to a poorly-implemented device that is not a JeVois,
# we may cause it to have bad behavior. Hopefully that will not be the case.
# If it is for you, just kill off part 1 of this code and hardcode the
# JeVois COM port.
$port.WriteLine("ping")
$response = $port.ReadLine()
if(($response -like "ALIVE*") -or ($response -like "{*}*")-or ($response -like "OK*"))
{
#Expect that a real jevois will respond with ALIVE, OK, or a packet.
$jevois_port_name = $port_name
echo "[serial_listen] Found JeVois on $jevois_port_name"
break
}
else {
echo "[serial_listen] Incorrect Response from $port_name"
}
$port.Close();
} catch {
echo "[serial_listen] Failed to get proper response on $port_name"
if($port.IsOpen){
$port.Close()
}
continue
}
}
if($jevois_port_name -eq ""){
Write-Error "[serial_listen] Failed to open JeVois serial port - is it plugged in and not already open?"
exit -1
}
echo "[serial_listen] Jevois Location complete"
#Port will already be open from above code
#adjust the read timeout to be nicer, since non-ping commands might take longer to execute.
$port.ReadTimeout = 1000
echo "[serial_listen] Starting serial listening. Press Q to quit."
$port.WriteLine("setmapping 2")
$port.WriteLine("streamon")
while($true){
try{
echo $port.ReadLine()
} catch {
# Start-Sleep -m 50
}
# Check if ctrl+C was pressed and quit if so.
if ([console]::KeyAvailable) {
$key = [system.console]::readkey($true)
if (($key.key -eq "Q")) {
echo "Quitting, user pressed Q..."
break
}
}
}
$port.WriteLine("streamoff")
if($port.IsOpen){
echo "[serial_listen] Closing port..."
$port.Close()
}
pause