This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshrek_vision.py
executable file
·70 lines (52 loc) · 1.85 KB
/
shrek_vision.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
#!/usr/bin/env python
""" shrek_vision.py - team 1165 pixycam
Take pixycam vector structure and put values into NetworkTables on rio.
"""
import time
import pixy
from ctypes import *
from pixy import *
from networktables import NetworkTables
import logging
class Vector (Structure):
_fields_ = [
("m_x0", c_uint),
("m_y0", c_uint),
("m_x1", c_uint),
("m_y1", c_uint),
("m_index", c_uint),
("m_flags", c_uint) ]
class IntersectionLine (Structure):
_fields_ = [
("m_index", c_uint),
("m_reserved", c_uint),
("m_angle", c_uint) ]
def main():
# Initialize the pixycam and select the line following program.
pixy.init ()
pixy.change_prog ("line")
# NetworkTables requires logging module
logging.basicConfig(level=logging.DEBUG)
# Connect to network tables server on the IP address of the Rio.
NetworkTables.initialize(server='10.11.65.2')
# Connect to SmartDashboard
smart_dash = NetworkTables.getTable('SmartDashboard')
# Initialize a Vector Object with a (very generous) 10 slots
# (We ever only want to pay attention to the first vector anyway.)
vectors = VectorArray(10)
# loop forever. Not very pythonic, but it's what we need.
while True:
# get all of the current features (what the pixycam sees)
line_get_all_features ()
# fetch the top 10 vectors from pixy
vector_count = line_get_vectors (10, vectors)
# If there are any vectors, v_count will be greater than zero.
if vector_count > 0:
smart_dash.putNumber('Pixy X0', vectors[0].m_x0 )
smart_dash.putNumber('Pixy Y0', vectors[0].m_y0 )
smart_dash.putNumber('Pixy X1', vectors[0].m_x1 )
smart_dash.putNumber('Pixy Y1', vectors[0].m_y1 )
time.sleep(1)
## End of main()
if __name__ == "__main__":
main()