Skip to content

Commit 4960726

Browse files
author
Alec Miller
committed
MPAE-4022: Python project
1 parent 5edcfd7 commit 4960726

File tree

11 files changed

+732
-0
lines changed

11 files changed

+732
-0
lines changed

cnc_master/.gitignore

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
.hypothesis/
50+
.pytest_cache/
51+
52+
# Translations
53+
*.mo
54+
*.pot
55+
56+
# Django stuff:
57+
*.log
58+
local_settings.py
59+
db.sqlite3
60+
61+
# Flask stuff:
62+
instance/
63+
.webassets-cache
64+
65+
# Scrapy stuff:
66+
.scrapy
67+
68+
# Sphinx documentation
69+
docs/_build/
70+
71+
# PyBuilder
72+
target/
73+
74+
# Jupyter Notebook
75+
.ipynb_checkpoints
76+
77+
# IPython
78+
profile_default/
79+
ipython_config.py
80+
81+
# pyenv
82+
.python-version
83+
84+
# celery beat schedule file
85+
celerybeat-schedule
86+
87+
# SageMath parsed files
88+
*.sage.py
89+
90+
# Environments
91+
.env
92+
.venv
93+
env/
94+
venv/
95+
ENV/
96+
env.bak/
97+
venv.bak/
98+
99+
# Spyder project settings
100+
.spyderproject
101+
.spyproject
102+
103+
# Rope project settings
104+
.ropeproject
105+
106+
# mkdocs documentation
107+
/site
108+
109+
# mypy
110+
.mypy_cache/
111+
.dmypy.json
112+
dmypy.json
113+
114+
# Pyre type checker
115+
.pyre/
116+
117+
.idea/

cnc_master/comm.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import serial
2+
3+
4+
def chunkify(contents, chunk_length, debug=False):
5+
if debug:
6+
chunk_length = 2
7+
8+
chunks = []
9+
for i in range(0, len(contents), chunk_length):
10+
chunks.append(contents[i:i+chunk_length])
11+
12+
return chunks
13+
14+
15+
class PicComm:
16+
INITIAL_CONNECTION_PACKET = [1]
17+
START_PROGRAM_PACKET = [2]
18+
19+
def __init__(self, port, baud):
20+
self.port = port
21+
self.baud = baud
22+
self.buffer_size = 0
23+
self.remaining = []
24+
25+
def connect(self):
26+
with serial.Serial(port=self.port, baudrate=self.baud, timeout=3) as ser:
27+
print("Requesting metadata")
28+
ser.write(self.INITIAL_CONNECTION_PACKET)
29+
30+
print("Awaiting metadata")
31+
self.buffer_size = int.from_bytes(ser.read(), byteorder='big')
32+
if self.buffer_size == 0:
33+
raise ConnectionError("No response on port " + self.port)
34+
print("Receiving data")
35+
36+
ticks_per_meter = int.from_bytes(ser.read(4), byteorder='big')
37+
38+
max_x = int.from_bytes(ser.read(2), byteorder='big')
39+
max_y = int.from_bytes(ser.read(2), byteorder='big')
40+
41+
num_accel_points = int.from_bytes(ser.read(), byteorder='big')
42+
43+
accel_points = []
44+
45+
for i in range(num_accel_points):
46+
accel_point = int.from_bytes(ser.read(3), byteorder='big')
47+
accel_points.append(accel_point)
48+
49+
accel_points = accel_points[::-1]
50+
51+
max_vals = [max_x, max_y]
52+
53+
print("Got metadata")
54+
55+
return ticks_per_meter, max_vals, accel_points
56+
57+
def get_metadata(self):
58+
with serial.Serial(port=self.port, baudrate=self.baud) as ser:
59+
ser.write('?')
60+
61+
def write_message(self, message, debug=False, update_cb=None):
62+
chunks = chunkify(message, self.buffer_size, debug=debug)
63+
first_two = chunks[0:2]
64+
chunks = chunks[2:]
65+
66+
with serial.Serial(port=self.port, baudrate=self.baud, timeout=0) as ser:
67+
print("Initiating communication")
68+
ser.write(self.START_PROGRAM_PACKET)
69+
print("Awaiting start sync packet")
70+
if not debug:
71+
while ser.read(1) != b'U':
72+
update_cb()
73+
74+
print("Start packet received")
75+
print("Writing initial buffer")
76+
for chunk in first_two:
77+
transmission = b''.join(chunk)
78+
ser.write(transmission)
79+
80+
while ser.out_waiting:
81+
pass
82+
83+
print("Initial buffer sent")
84+
85+
self.remaining = chunks
86+
87+
for chunk in chunks:
88+
print("Waiting for additional requests")
89+
transmission = b''.join(chunk)
90+
while ser.read(1) != b'U':
91+
update_cb()
92+
print("Additional data requested")
93+
ser.write(transmission)
94+
print("Additional data sent")
95+
96+
print("All data written")
97+
return
98+
99+
100+
if __name__ == "__main__":
101+
comm = PicComm('COM15', 9600)
102+
103+
#test = [[1,0,0],[2,0,0],[3,0,0],[4,0,0],[5,0,0],[6,0,0],[7,0,0],[8,0,0],[9,0,0], [10,0,0],[11,0,0],[12,0,0],[13,0,0],[14,0,0],[15,0,0],[16,0,0],[17,0,0],[18,0,0],[19,0,0], [20,0,0]]
104+
105+
#comm.write_message(test)
106+
comm.connect()

0 commit comments

Comments
 (0)