/
controller.py
675 lines (593 loc) · 24.5 KB
/
controller.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
try:
# TODO: this was a trial to move to micropython
# not used
from micropython import const
a = const
upython = True
except ImportError:
upython = False
from struct import unpack
from time import sleep
if upython:
from ulab import numpy as np
from .constants import platform as platformmicro
else:
from gpiozero import LED
import subprocess
from .platforms import Firestarter
import numpy as np
from . import ulabext
from .constants import (
COMMAND_BYTES,
COMMANDS,
INSTRUCTIONS,
MOTORFREQ,
MOVE_TICKS,
STATE,
WORD_BYTES,
bit_shift,
params,
)
def executor(func):
"""executes generator until stop iteration
nMigen uses generator syntax and this is used
as hack to execute functions.
"""
def inner(self):
for _ in func(self):
pass
return inner
class Memfull(Exception):
"""SRAM memory of FPGA, i.e. FIFO, is full
Exception is raised when the memory is full.
"""
pass
class Host:
"""Class to interact with FPGA"""
def __init__(self, platform=None):
"""platform -- object which has gateware settings
only passed to controller if virtual
test is executed. Needed in lasers.py
as each test here has a slightly
different TestPlatform
"""
# case raspberry
if platform is None:
# case micropython
if upython:
self.test = False
import machine
self.platform = platformmicro(micropython=True)
# TODO: would this also work with device number
self.bus = machine.I2C(
scl=self.platform.scl, sda=self.platform.sda
)
# SPI
# spi port is hispi
self.spi = machine.SPI(
self.platform.spi_dev, baudrate=round(1e6)
)
self.chip_select = machine.Pin(self.platform.chip_select)
# program TMC2130
# disabled, motor is not in SPI mode
# self.init_steppers()
# stepper motor enable pin
self.enable = machine.Pin(self.platform.enable_pin)
# case raspberry
else:
self.test = False
import spidev
from gpiozero import LED
from smbus2 import SMBus
self.platform = Firestarter(micropython=False)
# IC bus used to set power laser
self.bus = SMBus(self.platform.ic_dev_nr)
# SPI to sent data to scanner
self.spi = spidev.SpiDev()
self.spi.open(*self.platform.spi_dev)
self.spi.mode = 1
self.spi.max_speed_hz = round(1e6)
self.chip_select = LED(self.platform.chip_select)
# drivers are now in standalone mode
# self.init_steppers()
# stepper motor enable pin
self.enable = LED(self.platform.enable_pin)
self.enable.on()
else:
self.platform = platform
self.test = True
# maximum number of times tried to write to FIFO
# if memory is full
self.maxtrials = 10 if self.test else 1e5
self.laser_params = params(self.platform)
self._position = np.array([0] * self.platform.motors, dtype=np.float)
def init_steppers(self):
"""configure TMC2130 steppers via SPI
Uses teemuatflut CPP library with custom python wrapper
https://github.com/hstarmans/TMCStepper
"""
import steppers
self.motors = [
steppers.TMC2130(link_index=i)
for i in range(1, 1 + self.platform.motors)
]
steppers.bcm2835_init()
for motor in self.motors:
motor.begin()
motor.toff(5)
# ideally should be 0
# on working equipment it is always 2
assert motor.test_connection() == 2
motor.rms_current(600)
motor.microsteps(16)
motor.en_pwm_mode(True)
steppers.bcm2835_close()
def build(self, do_program=True, verbose=True, mod="all"):
"""builds the FPGA code using amaranth HDL, Yosys, Nextpnr and icepack
do_program -- flashes the FPGA chip using fomu-flash,
resets aftwards
verbose -- prints output of Yosys, Nextpnr and icepack
"""
if upython:
print("Micropython cannot update binary, using stored one")
else:
from .core import Dispatcher
from .motor import Driver
if mod == "all":
module = Dispatcher(self.platform)
elif mod == "motor":
module = Driver(self.platform, top=True)
else:
raise Exception(f"Print building {mod} is not supported.")
self.platform = Firestarter()
self.platform.laser_var = self.laser_params
self.platform.build(
module,
do_program=do_program,
verbose=verbose,
)
if do_program:
self.reset()
def reset(self):
"restart the FPGA by flipping the reset pin"
if upython:
import machine
reset_pin = machine.Pin(self.platform.reset_pin)
reset_pin.value(0)
sleep(1)
reset_pin.value(1)
sleep(1)
else:
from gpiozero import LED
reset_pin = LED(self.platform.reset_pin)
reset_pin.off()
sleep(1)
reset_pin.on()
sleep(1)
# a blank needs to be send, Statictest succeeds but
# testlaser fails in test_electrical.py
# on HX4K this was not needed
# is required for the UP5K
self.spi_exchange_data([0] * (WORD_BYTES + COMMAND_BYTES))
def get_motordebug(self, blocking=False):
"""retrieves the motor debug word
This is used to debug the PI controller and
set the correct setting for the Hall interpolation
blocking -- checks if memory is full, only needed for
a build with all modules
"""
command = [COMMANDS.DEBUG] + WORD_BYTES * [0]
response = (yield from self.send_command(command, blocking=blocking))[
1:
]
clock = int(self.platform.clks[self.platform.hfosc_div] * 1e6)
mode = self.platform.laser_var["MOTORDEBUG"]
def cntcnv(cnt):
if cnt != 0:
speed = (
clock
/ (cnt * 4 * self.platform.laser_var["MOTORDIVIDER"])
* 60
)
else:
speed = 0
return speed
if (mode == "cycletime") & (response != 0):
response = int.from_bytes(response, "big")
# you measure 180 degrees
if response != 0:
response = round((clock / (response * 2) * 60))
elif mode == "PIcontrol":
cnt = int.from_bytes(
response[(WORD_BYTES - 2) :], "big", signed=False
)
speed = cntcnv(cnt)
duty = int.from_bytes(
response[: (WORD_BYTES - 2)], "big", signed=True
)
response = [speed, duty]
elif mode == "ticksinfacet":
cnt = int.from_bytes(
response[(WORD_BYTES - 2) :], "big", signed=False
)
speed = cntcnv(cnt)
cntdiode = int.from_bytes(
response[: (WORD_BYTES - 2)], "big", signed=False
)
speedd = cntcnv(cntdiode)
response = [speed, speedd]
else:
response = int.from_bytes(response, "big")
if not isinstance(response, list):
return [response]
else:
return response
def get_state(self, data=None):
"""retrieves the state of the FPGA as dictionary
data: string to decode to state, if None data is retrieved from FPGA
dictionary with the following keys
parsing: True if commands are executed
mem_full: True if memory is full
error: True if an error state is reached by any of
the submodules
x, y, z: state of motor endswitches
photodiode_trigger: True if photodiode is triggered during last
rotation of prism
synchronized: True if laserhead is synchronized by photodiode
"""
if data is None:
command = [COMMANDS.READ] + WORD_BYTES * [0]
data = yield from self.send_command(command)
dct = {}
# 9 bytes are returned
# the state is decoded from byte 7 and 8, i.e. -2 and -1
bits = "{:08b}".format(data[-1])
dct["parsing"] = int(bits[STATE.PARSING])
dct["error"] = int(bits[STATE.ERROR])
dct["mem_full"] = int(bits[STATE.FULL])
bits = "{:08b}".format(data[-2])
mapping = list(self.platform.stepspermm.keys())
for i in range(self.platform.motors):
dct[mapping[i]] = int(bits[i])
dct["photodiode_trigger"] = int(bits[self.platform.motors])
dct["synchronized"] = int(bits[self.platform.motors + 1])
return dct
@property
def position(self):
"""retrieves position from FPGA and updates internal position
position is stored on the FPGA in steps
position is stored on object in mm
return positions as np.array in mm
order is [x, y, z]
"""
command = [COMMANDS.POSITION] + WORD_BYTES * [0]
for i in range(self.platform.motors):
read_data = yield from self.send_command(command)
self._position[i] = unpack("!q", read_data[1:])[0]
# step --> mm
self._position = self._position / np.array(
list(self.platform.stepspermm.values())
)
return self._position
@property
def enable_steppers(self):
"""returns 1 if steppers are enabled, 0 otherwise
The enable pin for the stepper drivers is not routed via FPGA.
The enable pin is low if enabled.
Enabled stepper motor do not move if the FPGA is
not parsing instructions from FIFO.
"""
return not self.enable.value
@enable_steppers.setter
def enable_steppers(self, val):
"""set enable pin stepper motor drivers and parsing FIFO buffer by FPGA
val -- boolean, True enables steppers
"""
if val:
self.enable.off() if not upython else self.enable.value(0)
self.spi_exchange_data([COMMANDS.START] + WORD_BYTES * [0])
else:
self.enable.on() if not upython else self.enable.value(1)
self.spi_exchange_data([COMMANDS.STOP] + WORD_BYTES * [0])
@property
def laser_current(self):
"""return laser current per channel as integer
both channels have the same current
integer ranges from 0 to 255 where
0 no current and 255 full driver current
"""
if upython:
data = self.bus.readfrom(self.platform.ic_address, 1)
else:
data = self.bus.read_byte_data(self.platform.ic_address, 0)
return data
@laser_current.setter
def laser_current(self, val):
"""sets maximum laser current of laser driver per channel
This does not turn on or off the laser. Laser is set
to this current if pulsed. Laser current is set by enabling
one or two channels. Second by setting a value between
0-255 at the laser driver chip for the laser current. The laser
needs a minimum current.
"""
if val < 0 or val > 150:
# 255 kills laser at single channel
raise Exception("Invalid or too high laser current")
if upython:
self.bus.writeto(self.platform.ic_address, bytes(val))
else:
self.bus.write_byte_data(self.platform.ic_address, 0, val)
def set_parsing(self, value):
"""enables or disables parsing of FIFO by FPGA
val -- True FPGA parses FIFO
False FPGA does not parse FIFO
"""
assert isinstance(value, bool)
if value:
command = [COMMANDS.START]
else:
command = [COMMANDS.STOP]
command += WORD_BYTES * [0]
return (yield from self.send_command(command))
def home_axes(self, axes, speed=None, displacement=-200):
"""home given axes, i.e. [1,0,1] homes x, z and not y
axes -- list with axes to home
speed -- speed in mm/s used to home
displacement -- displacement used to touch home switch
"""
assert len(axes) == self.platform.motors
dist = np.array(axes) * np.array([displacement] * self.platform.motors)
yield from self.gotopoint(
position=dist.tolist(), speed=speed, absolute=False
)
# TODO: this is strange, should it be here
# on the board steps and count is stored
# you could move this to spline_coefficients
# the flow over method of a certain bit comes from beagleg
def steps_to_count(self, steps):
"""compute count for a given number of steps
steps -- motor moves in small steps
Shift is needed as two ticks per step are required
You need to count slightly over the threshold. That is why
+1 is added.
"""
bitshift = bit_shift(self.platform)
count = (steps << (1 + bitshift)) + (1 << (bitshift - 1))
return count
def gotopoint(self, position, speed=None, absolute=True):
"""move machine to position or with displacement at constant speed
Axes are moved independently to simplify the calculation.
The move is carried out as a first order spline, i.e. only velocity.
position -- list with position or displacement in mm for each motor
speed -- list with speed in mm/s, if None default speeds used
absolute -- True if position, False if displacement
"""
assert len(position) == self.platform.motors
if speed is not None:
assert len(speed) == self.platform.motors
else:
speed = [10] * self.platform.motors
# conversions to steps / count give rounding errors
# minimized by setting speed to integer
speed = abs(np.array(speed))
displacement = np.array(position)
if absolute:
# TODO: position machine should be in line with self._position
# which to pick?
displacement -= self._position
homeswitches_hit = [0] * len(position)
for idx, disp in enumerate(displacement):
if disp == 0:
# no displacement, go to next axis
continue
# Time needed for move
# unit oscillator ticks (times motor position is updated)
time = abs(disp / speed[idx])
ticks_total = (time * MOTORFREQ).round().astype(int)
# mm -> steps
steps_per_mm = list(self.platform.stepspermm.values())[idx]
speed_steps = int(
round(speed[idx] * steps_per_mm * ulabext.sign(disp))
)
velocity = [0] * len(speed)
velocity[idx] = self.steps_to_count(speed_steps) // MOTORFREQ
if self.test:
(yield from self.set_parsing(True))
else:
self.set_parsing(True)
while ticks_total > 0:
ticks_move = (
MOVE_TICKS if ticks_total >= MOVE_TICKS else ticks_total
)
# execute move and retrieve if switch is hit
switches_hit = yield from self.spline_move(
int(ticks_move), velocity
)
ticks_total -= ticks_move
# move is aborted if home switch is hit and
# velocity is negative
cond = (switches_hit[idx] == 1) & (ulabext.sign(disp) < 0)
if cond:
break
# update internally stored position
self._position += displacement
# set position to zero if home switch hit
self._position[homeswitches_hit == 1] = 0
def send_command(self, command, blocking=False):
"""writes command to spi port
blocking -- try again if memory is full
returns bytearray with length equal to data sent
"""
def send_command(command):
assert len(command) == WORD_BYTES + COMMAND_BYTES
if self.test:
data = yield from self.spi_exchange_data(command)
else:
data = self.spi_exchange_data(command)
return data
if blocking:
trials = 0
while True:
trials += 1
data = yield from send_command(command)
state = yield from self.get_state(data)
if state["error"]:
raise Exception("Error detected on FPGA")
if not state["mem_full"]:
break
if trials > self.maxtrials:
raise Memfull(f"Too many trials {trials} needed")
else:
data = yield from send_command(command)
return data
def enable_comp(
self, laser0=False, laser1=False, polygon=False, synchronize=False
):
"""enable components
FPGA does need to be parsing FIFO
These instructions are executed directly.
laser0 -- True enables laser channel 0
laser1 -- True enables laser channel 1
polygon -- False enables polygon motor
"""
laser0, laser1, polygon = (
int(bool(laser0)),
int(bool(laser1)),
int(bool(polygon)),
)
synchronize = int(bool(synchronize))
data = (
[COMMANDS.WRITE]
+ [0] * (WORD_BYTES - 2)
+ [int(f"{synchronize}{polygon}{laser1}{laser0}", 2)]
+ [INSTRUCTIONS.WRITEPIN]
)
yield from self.send_command(data, blocking=True)
def spline_move(self, ticks, coefficients):
"""write spline move instruction with ticks and coefficients to FIFO
If you have 2 motors and execute a second order spline
You send 4 coefficients.
If the controller supports a third order spline,
remaining coefficients are padded as zero.
User needs to submit all coefficients up to highest order used.
ticks -- number of ticks in move, integer
coefficients -- coefficients for spline move per axis, list
returns array with zero if home switch is hit
"""
platform = self.platform
# maximum allowable ticks is move ticks,
# otherwise counters overflow in FPGA
assert ticks <= MOVE_TICKS
assert len(coefficients) % platform.motors == 0
write_byte = COMMANDS.WRITE.to_bytes(1, "big")
move_byte = INSTRUCTIONS.MOVE.to_bytes(1, "big")
commands = [write_byte + ticks.to_bytes(7, "big") + move_byte]
# check max order given by caller of function
max_coeff_order = len(coefficients) // platform.motors
# prepare commands
for motor in range(platform.motors):
for degree in range(platform.poldegree):
# set to zero if coeff not provided by caller
if degree > max_coeff_order - 1:
coeff = 0
else:
idx = degree + motor * max_coeff_order
coeff = coefficients[idx]
data = (
coeff.to_bytes(8, "big", signed=True)
if not upython
else coeff.to_bytes(8, "big")
)
commands += [write_byte + data]
# send commands to FPGA
for command in commands:
data_out = yield from self.send_command(command, blocking=True)
state = yield from self.get_state(data_out)
axes_names = list(platform.stepspermm.keys())
return np.array([state[key] for key in axes_names])
def spi_exchange_data(self, data):
"""writes data to peripheral
data -- command followed with word
list of multiple bytes
returns bytearray with length equal to data sent
"""
assert len(data) == (COMMAND_BYTES + WORD_BYTES)
# spidev changes values passed to it
if not upython:
from copy import deepcopy
self.chip_select.off()
datachanged = deepcopy(data)
response = bytearray(self.spi.xfer2(datachanged))
self.chip_select.on()
else:
self.chip_select.value(0)
response = bytearray(data)
self.spi.write_readinto(data, response)
self.chip_select.value(1)
return response
def writeline(self, bitlst, stepsperline=1, direction=0):
"""write bits to FIFO
bit list bits which are written to substrate
at the moment laser can only be on or off
if bitlst is empty stop command is sent
stepsperline stepsperline, should be greater than 0
if you don't want to move simply disable motor
direction motor direction of scanning axis
"""
bytelst = self.bittobytelist(bitlst, stepsperline, direction)
write_byte = COMMANDS.WRITE.to_bytes(1, "big")
for i in range(0, len(bytelst), 8):
lst = bytelst[i : i + 8]
lst.reverse()
data = write_byte + bytes(lst)
(yield from self.send_command(data, blocking=True))
def bittobytelist(
self, bitlst, stepsperline=1, direction=0, bitorder="little"
):
"""converts bitlst to bytelst
bit list bits which are written to substrate
at the moment laser can only be on or off
if bitlst is empty stop command is sent
stepsperline stepsperline, should be greater than 0
if you don't want to move simply disable motor
direction motor direction of scanning axis
"""
# the halfperiod is sent over
# this is the amount of ticks in half a cycle of
# the motor
# watch out for python "banker's rounding"
# sometimes target might not be equal to steps
bits = self.laser_params["BITSINSCANLINE"]
halfperiod = int((bits - 1) // (stepsperline * 2))
if halfperiod < 1:
raise Exception("Steps per line cannot be achieved")
# TODO: is this still an issue?
# you could check as follows
# steps = self.laser_params['TICKSINFACET']/(halfperiod*2)
# print(f"{steps} is actual steps per line")
direction = [int(bool(direction))]
def remainder(bytelst):
rem = len(bytelst) % WORD_BYTES
if rem > 0:
res = WORD_BYTES - rem
else:
res = 0
return res
if len(bitlst) == 0:
bytelst = [INSTRUCTIONS.LASTSCANLINE]
bytelst += remainder(bytelst) * [0]
else:
assert len(bitlst) == self.laser_params["BITSINSCANLINE"]
assert max(bitlst) <= 1
assert min(bitlst) >= 0
bytelst = [INSTRUCTIONS.SCANLINE]
halfperiodbits = [int(i) for i in bin(halfperiod)[2:]]
halfperiodbits.reverse()
assert len(halfperiodbits) < 56
bytelst += ulabext.packbits(
direction + halfperiodbits, bitorder=bitorder
).tolist()
bytelst += remainder(bytelst) * [0]
bytelst += ulabext.packbits(bitlst, bitorder=bitorder).tolist()
bytelst += remainder(bytelst) * [0]
return bytelst