Add spectrum measurement stuff
This commit is contained in:
parent
c641bbc090
commit
c95e8296bf
8 changed files with 2780 additions and 0 deletions
2531
firmware/Spectrum Measurement.ipynb
Normal file
2531
firmware/Spectrum Measurement.ipynb
Normal file
File diff suppressed because one or more lines are too long
38
firmware/calc_framerate.py
Normal file
38
firmware/calc_framerate.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
TIMER_FREQ = 30e6 # MHz
|
||||
|
||||
|
||||
with open('main.c') as f:
|
||||
lines = f.readlines()
|
||||
|
||||
defs = {}
|
||||
for line in lines:
|
||||
if line.startswith('#define'):
|
||||
_pragma, name, val, *_comment = line.split()
|
||||
val = defs.get(val, val)
|
||||
defs[name] = val
|
||||
|
||||
print('Bit cycle timings:')
|
||||
timings_total = 0
|
||||
in_array = False
|
||||
for line in lines:
|
||||
if not in_array:
|
||||
if line.startswith('static uint16_t timer_period_lookup'):
|
||||
in_array = True
|
||||
else:
|
||||
if '}' in line:
|
||||
break
|
||||
if ',' not in line:
|
||||
continue
|
||||
val, *_comment = line.split(',')
|
||||
for name, defval in defs.items():
|
||||
val = val.replace(name, defval)
|
||||
duration = eval(val)
|
||||
print(duration)
|
||||
timings_total += duration + int(defs['RESET_PERIOD_LENGTH'])
|
||||
|
||||
total_len = timings_total/TIMER_FREQ
|
||||
print('Total cycles:', timings_total)
|
||||
print('Total cycle length: {:.3f}ms'.format(total_len*1e3))
|
||||
print('Frame rate: {:.3f}Hz'.format(1/total_len))
|
||||
122
firmware/measure_spectrum.py
Normal file
122
firmware/measure_spectrum.py
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
import statistics
|
||||
import sqlite3
|
||||
from olsndot import Olsndot, Driver
|
||||
from datetime import datetime
|
||||
|
||||
from pyBusPirateLite import BitBang
|
||||
|
||||
if __name__ == '__main__':
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('run_name', nargs='?', default='auto')
|
||||
parser.add_argument('buspirate_port', nargs='?', default='/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AD01W1RF-if00-port0')
|
||||
parser.add_argument('-s', '--steps', type=int, nargs='?', default=100, help='Steps to run through')
|
||||
parser.add_argument('-d', '--database', default='spectra.sqlite3', help='sqlite3 database file to store results in')
|
||||
parser.add_argument('-w', '--wait', type=float, default=0.1, help='time to wait between samples in seconds')
|
||||
parser.add_argument('-o', '--oversample', type=int, default=16, help='oversampling ratio')
|
||||
args = parser.parse_args()
|
||||
|
||||
db = sqlite3.connect(args.database)
|
||||
db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS runs (
|
||||
run_id INTEGER PRIMARY KEY,
|
||||
name TEXT,
|
||||
comment TEXT,
|
||||
timestamp REAL -- unix timestamp in fractional seconds
|
||||
)""")
|
||||
db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS measurements (
|
||||
measurement_id INTEGER PRIMARY KEY,
|
||||
run_id INTEGER,
|
||||
led_on INTEGER,
|
||||
step INTEGER,
|
||||
voltage REAL, -- volts
|
||||
voltage_stdev REAL, -- volts
|
||||
timestamp REAL, -- unix timestamp in fractional seconds
|
||||
FOREIGN KEY (run_id) REFERENCES runs)""")
|
||||
|
||||
class BPState:
|
||||
def __init__(self, port):
|
||||
self.bp = BitBang(port)
|
||||
self._led = 0
|
||||
self._stepper_dir = 'down'
|
||||
self.reinit()
|
||||
|
||||
def reinit(self):
|
||||
self.bp.enter_bb()
|
||||
self.led(self._led)
|
||||
self.stepper_direction(self._stepper_dir)
|
||||
self.bp.cs = 0
|
||||
|
||||
def led(self, st):
|
||||
self._led = st
|
||||
self.bp.mosi = st
|
||||
|
||||
def stepper_direction(self, direction):
|
||||
self._stepper_dir = direction
|
||||
self.bp.aux = 0 if direction == 'down' else 1
|
||||
|
||||
def step(self):
|
||||
self.bp.cs = 1
|
||||
time.sleep(0.005)
|
||||
self.bp.cs = 0
|
||||
time.sleep(0.005)
|
||||
|
||||
def adc(self, oversampling):
|
||||
self.reinit()
|
||||
return [ self.bp.adc_value for _ in range(oversampling) ]
|
||||
|
||||
bp = BPState(args.buspirate_port)
|
||||
|
||||
run_name = args.run_name
|
||||
if not str.isnumeric(args.run_name[-1]):
|
||||
names = [ n[len(run_name):] for n, in db.execute(
|
||||
'SELECT name FROM runs WHERE name LIKE ?||"%"', (run_name,)).fetchall() ]
|
||||
names.append('0') # in case we get no results
|
||||
run_name += str(1+max(int(n) if str.isnumeric(n) else 0 for n in names))
|
||||
with db:
|
||||
cur = db.cursor()
|
||||
cur.execute('INSERT INTO runs(name, timestamp) VALUES (?, ?)',
|
||||
(run_name, time.time()))
|
||||
run_id = cur.lastrowid
|
||||
|
||||
print('Starting run {} "{}" at {:%y-%m-%d %H:%M:%S:%f}'.format(run_id, run_name, datetime.now()))
|
||||
print('[measurement id] " " [step number] " " [reading (V)]')
|
||||
|
||||
bp.stepper_direction('down')
|
||||
for _ in range(10):
|
||||
bp.step()
|
||||
|
||||
bp.stepper_direction('up')
|
||||
for step in range(args.steps):
|
||||
bp.step()
|
||||
for led_val in [0, 1]:
|
||||
try:
|
||||
bp.led(led_val)
|
||||
time.sleep(args.wait)
|
||||
|
||||
readings = bp.adc(args.oversample)
|
||||
mean, stdev = statistics.mean(readings), statistics.stdev(readings)
|
||||
|
||||
with db:
|
||||
cur = db.cursor()
|
||||
cur.execute('''
|
||||
INSERT INTO measurements (
|
||||
run_id, led_on, step, voltage, voltage_stdev, timestamp
|
||||
) VALUES (?, ?, ?, ?, ?, ?)''',
|
||||
(run_id, led_val, step, mean, stdev, time.time()))
|
||||
print('{:08d} {:03} {}: {:5.4f} stdev {:5.4f}'.format(
|
||||
cur.lastrowid, step, led_val, mean, stdev))
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except TypeError as e:
|
||||
print('Buspirate hiccup, ignoring:', e)
|
||||
|
||||
bp.stepper_direction('down')
|
||||
for _ in range(args.steps):
|
||||
bp.step()
|
||||
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
import statistics
|
||||
|
|
|
|||
Binary file not shown.
BIN
firmware/spectra.sqlite3
Normal file
BIN
firmware/spectra.sqlite3
Normal file
Binary file not shown.
32
firmware/spectrum_progress.py
Normal file
32
firmware/spectrum_progress.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/env python3
|
||||
import sqlite3
|
||||
import argparse
|
||||
import time
|
||||
|
||||
import tqdm
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-d', '--database', nargs='?', default='spectra.sqlite3')
|
||||
parser.add_argument('-u', '--update-delay', nargs='?', type=float, default=1.0)
|
||||
parser.add_argument('max_step', nargs='?', type=int, default=250)
|
||||
args = parser.parse_args()
|
||||
|
||||
db = sqlite3.connect(args.database)
|
||||
def current_step():
|
||||
step, = db.execute(
|
||||
'SELECT MAX(step) FROM measurements WHERE run_id = (SELECT MAX(run_id) FROM runs)'
|
||||
).fetchone()
|
||||
return int(step)+1
|
||||
|
||||
def step_gen():
|
||||
while True:
|
||||
step = current_step()
|
||||
yield step
|
||||
if step >= args.max_step:
|
||||
break
|
||||
time.sleep(args.update_delay)
|
||||
|
||||
bar = tqdm.tqdm(total=args.max_step)
|
||||
for step in step_gen():
|
||||
bar.update(step - bar.n)
|
||||
56
firmware/stepper_test.py
Normal file
56
firmware/stepper_test.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
import statistics
|
||||
import sqlite3
|
||||
from olsndot import Olsndot, Driver
|
||||
from datetime import datetime
|
||||
|
||||
from pyBusPirateLite import BitBang
|
||||
|
||||
if __name__ == '__main__':
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('buspirate_port', nargs='?', default='/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AD01W1RF-if00-port0')
|
||||
args = parser.parse_args()
|
||||
|
||||
bp = BitBang(args.buspirate_port)
|
||||
bp.enter_bb()
|
||||
bp.mosi = 1
|
||||
|
||||
def stepper_direction_down():
|
||||
bp.aux = 0
|
||||
|
||||
def stepper_direction_up():
|
||||
bp.aux = 1
|
||||
|
||||
def stepper_step():
|
||||
bp.cs = 1
|
||||
time.sleep(0.005)
|
||||
bp.cs = 0
|
||||
time.sleep(0.005)
|
||||
|
||||
import curses
|
||||
screen = curses.initscr()
|
||||
curses.noecho()
|
||||
curses.cbreak()
|
||||
screen.keypad(True)
|
||||
try:
|
||||
while True:
|
||||
key = screen.getch()
|
||||
if key == ord('q'):
|
||||
break
|
||||
|
||||
if key == curses.KEY_DOWN:
|
||||
stepper_direction_down()
|
||||
stepper_step()
|
||||
elif key == curses.KEY_UP:
|
||||
stepper_direction_up()
|
||||
stepper_step()
|
||||
|
||||
finally:
|
||||
curses.nocbreak()
|
||||
screen.keypad(0)
|
||||
curses.echo()
|
||||
curses.endwin()
|
||||
Loading…
Add table
Add a link
Reference in a new issue