forked from rtitmuss/splitflap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
155 lines (123 loc) · 3.88 KB
/
Copy pathmain.py
File metadata and controls
155 lines (123 loc) · 3.88 KB
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
import gc
import math
import machine
import micropython
import time
from array import array
from random import randint
from machine import Pin, Timer, UART
from micropython import const
from ElementGpio import ElementGpio
from ElementUart import ElementUart
from InvertedNeoPixel import InvertedNeoPixel
from Panel import Panel
from Source import Source
from SourceUart import SourceUart
from UartFrame import UartFrame
from UartMessage import UartMessage
# primary or downstream panel
is_picow: bool = True
try:
import network
except ImportError:
is_picow = False
# leds
rp2040_led = Pin("LED", Pin.OUT)
timer = Timer(period=2000, mode=Timer.PERIODIC, callback=lambda t: rp2040_led.toggle())
panel_led = Pin(3, Pin.OUT, value=0)
neopixel = InvertedNeoPixel(Pin(0), 2)
# uart
UART_BAUD_RATE = const(19200)
UART_CHAR_TIMEOUT_MS = const(10)
UART_FRAME_TIMEOUT_MS = const(200)
uart_upstream = UartMessage(
UartFrame(
UART(
1,
baudrate=UART_BAUD_RATE,
tx=Pin(4, Pin.IN, Pin.PULL_UP),
rx=Pin(5, Pin.OUT, Pin.PULL_UP),
timeout=UART_CHAR_TIMEOUT_MS,
)
)
)
uart_downstream = UartMessage(
UartFrame(
UART(
0,
baudrate=UART_BAUD_RATE,
tx=Pin(16, Pin.IN, Pin.PULL_UP),
rx=Pin(17, Pin.OUT, Pin.PULL_UP),
timeout=UART_CHAR_TIMEOUT_MS,
)
)
)
# panel and element
panel = Panel(
[
ElementGpio(15, 9, 8, 7, 6, reverse_direction=True), # Motor D with sensor D
ElementGpio(2, 10, 11, 12, 13, reverse_direction=True), # Motor C with sensor B
ElementUart(uart_downstream),
]
)
def downstream_machine_reset():
seq = uart_downstream.next_seq()
uart_downstream.send_machine_reset(seq)
# TODO wait for ack
@micropython.native
def main_loop(source: Source):
# loop metrics
loop_buffer_size = 1000
loop_buffer = array("i", [0] * loop_buffer_size)
loop_buffer_index = 0
interval_us = 0
gc.collect()
while True:
t0_us = time.ticks_us()
is_stopped = panel.is_stopped()
panel_led.value(not is_stopped)
message = source.load_message(is_stopped, panel.get_motor_position())
if message:
if message == UartMessage.MACHINE_RESET_MESSAGE:
downstream_machine_reset()
print("machine reset")
time.sleep_ms(500)
machine.reset()
panel.set_message(message)
# loop metrics
loop_average_time = sum(loop_buffer) / loop_buffer_size
loop_variance = sum((x - loop_average_time) ** 2 for x in loop_buffer) / (
loop_buffer_size - 1
)
loop_std_deviation = math.sqrt(loop_variance)
print(
"loop: {:.2f}us +/-{:.2f}; interval {}us".format(
loop_average_time, loop_std_deviation, interval_us
)
)
neopixel.fill((randint(0, 255), randint(0, 255), randint(0, 255)))
neopixel.write()
gc.collect()
print("mem_free:", gc.mem_free())
interval_us = panel.step()
t1_us = time.ticks_us()
elapsed_us = time.ticks_diff(t1_us, t0_us)
delay_us = interval_us - elapsed_us
if delay_us > 0:
time.sleep_us(delay_us)
loop_buffer[loop_buffer_index] = elapsed_us
loop_buffer_index = (loop_buffer_index + 1) % loop_buffer_size
if is_picow:
import Config
from primary.Display import Display
from primary.SourceMQTT import SourceMQTT
from primary.Wifi import Wifi
wifi = Wifi()
wifi.connect()
downstream_machine_reset()
time.sleep_ms(1000)
display = Display(Config.display_order, Config.display_offsets)
board_source = SourceMQTT(display, Config.providers)
else:
board_source = SourceUart(uart_upstream)
main_loop(board_source)