-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation.py
More file actions
216 lines (184 loc) · 7.12 KB
/
animation.py
File metadata and controls
216 lines (184 loc) · 7.12 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
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
import numpy as np
import time
import copy
from disp_def import DispDef as DD
class animation:
def __init__(self, displayDimensions):
# Final Image Dimensions and Colors
self.dispWidth = displayDimensions[0]
self.dispHeight = displayDimensions[1]
self.initialState = 0
self.desiredState = 0
self.difference = 0
#print(' dimension: ' + str(self.displayWidth) + 'x' + str(self.displayHeight))
def setInitialState(self, initialState):
self.initialState = initialState
def setDesiredState(self, desiredState):
self.desiredState = desiredState
def _findDifMat(self):
self.difference = self.desiredState - self.initialState
self.difference = (self.difference + 1) % 4 - 1
return self.difference
def _leastDifRow(self, diffArr):
difs = self._getDifOfRows(diffArr)
#print("difs " + str(difs))
leastIndex = 0
leastVal = len(difs) * 1000
for i in range(0,len(difs)):
if difs[i] < leastVal and not difs[i] == 0:
leastIndex = i
leastVal = difs[i]
#print("least val " + str(difs[leastIndex]))
return leastIndex
def _getDifOfRows(self, diffArr):
rowDifs = np.zeros(len(diffArr))
for row in range(0,len(diffArr)):
for col in range(0,len(diffArr[0])):
rowDifs[row] += abs(diffArr[row][col])
return rowDifs
# Returns what rows the change decreases delta from desired row state.
def _changeHelpRow(self, diffArr, change):
changed = copy.deepcopy(diffArr)
for row in range(0,len(diffArr)):
for col in range(0,len(diffArr[0])):
changed[row][col] -= change[col]
offsetWithChange = self._getDifOfRows(changed)
#print("offsetWithChange " + str(offsetWithChange))
offsetWithoutChange = self._getDifOfRows(diffArr)
#print("offsetWithoutChange " + str(offsetWithoutChange))
changeHelp = []
for row in range(0,len(diffArr)):
if offsetWithChange[row] < offsetWithoutChange[row]:
changeHelp.append(row)
#print("Change Help " + str(changeHelp))
return changeHelp
def makeMoveQue(self):
diffArr = copy.deepcopy(self._findDifMat())
moveQue = []
while(True):
change = copy.deepcopy(diffArr[self._leastDifRow(diffArr)])
unlock = self._changeHelpRow(diffArr, change)
#print("QUE ADDED")
#print("change: " + str(change))
#print("unlock: " + str(unlock))
for i in range(0,len(unlock)):
diffArr[unlock[i]] -= change
diffArr = (diffArr + 1) % 4 -1
moveQue.append([change, unlock])
#time.sleep(.5)
if np.all(diffArr == 0):
break
#print("diffArr")
#print(diffArr)
return moveQue
def getGcode(self):
#print("initial")
#print(self.initialState)
#print("desired")
#print(self.desiredState)
mq = self.makeMoveQue()
moves = []
# Row Lock
rowReMove = DD.ROWLOCK
rowReActuateTo = [DD.LOCK] * self.dispHeight
moves.append([rowReMove, rowReActuateTo])
for i in range(0, len(mq)):
# Column Return
colRetActuateTo = []
for x in range(0, self.dispWidth):
if not mq[i][0][x] == 2:
colRetActuateTo.append(DD.MIDDLE)
else:
colRetActuateTo.append(DD.SUBTRACT)
moves.append([DD.COLRETURN, colRetActuateTo])
# Row Unlocks
rowUnActuateTo = [DD.LOCK] * self.dispHeight
for x in range(0, len(mq[i][1])):
rowUnActuateTo[mq[i][1][x]] = DD.UNLOCK
moves.append([DD.ROWUNLOCK, rowUnActuateTo])
# Column Actuate
# Produces 2 different moves. If adjacent columns move simultaneously,
# they would interfere with one another and they would fail.
colActActuateTo = []
for x in range(0, len(mq[i][0])):
if mq[i][0][x] == -1:
colActActuateTo.append(DD.SUBTRACT)
if mq[i][0][x] == 0:
colActActuateTo.append(DD.MIDDLE)
if mq[i][0][x] == 1 or mq[i][0][x] == 2:
colActActuateTo.append(DD.ADD)
toSend = []
for x in range(len(mq[i][0])):
if x % 2 == 0:
toSend.append(colActActuateTo[x])
else:
toSend.append(colRetActuateTo[x])
# Moves Half the Columns
moves.append([DD.COLACTUATE, toSend])
# Row Lock
rowReMove = DD.ROWLOCK
rowReActuateTo = [DD.LOCK] * self.dispHeight
moves.append([rowReMove, rowReActuateTo])
# Row Unlocks
rowUnActuateTo = [DD.LOCK] * self.dispHeight
for x in range(0, len(mq[i][1])):
rowUnActuateTo[mq[i][1][x]] = DD.UNLOCK
moves.append([DD.ROWUNLOCK, rowUnActuateTo])
# Moves the other half
moves.append([DD.COLACTUATE, colActActuateTo])
# Row Lock
rowReMove = DD.ROWLOCK
rowReActuateTo = [DD.LOCK] * self.dispHeight
moves.append([rowReMove, rowReActuateTo])
return moves
def printGcode(self, gcode):
for i in range(0 , len(gcode)):
strang = ""
if gcode[i][0] is DD.ROWLOCK:
strang += "ROW LOCK"
if gcode[i][0] is DD.COLRETURN:
strang += "COL RETURN"
if gcode[i][0] is DD.ROWUNLOCK:
strang += "ROW UNLOCK"
if gcode[i][0] is DD.COLACTUATE:
strang += "COL ACTUATE"
print("")
print(strang)
strang = ""
for j in range(0, len(gcode[i][1])):
if not j == 0:
strang += ", "
if gcode[i][1][j] == DD.LOCK:
strang += "LOCK"
if gcode[i][1][j] == DD.UNLOCK:
strang += "UNLK"
if gcode[i][1][j] == DD.SUBTRACT:
strang += "SUB"
if gcode[i][1][j] == DD.MIDDLE:
strang += "MID"
if gcode[i][1][j] == DD.ADD:
strang += "ADD"
print(strang)
def printMq(self, mq):
for m in range(0, len(mq)):
print("Blocks: " + str(mq[m][0]) + " Locks: " + str(mq[m][1]))
if __name__ == '__main__':
w = 16
h = 32
a = animation((w, h))
initState = np.random.randint(0,4, size=(h,w))
a.setInitialState(initState)
desState = np.random.randint(0,4, size=(h,w))
a.setDesiredState(desState)
diff = a._findDifMat()
ldRow = a._leastDifRow(diff)
print("diff")
print(diff)
mq = a.makeMoveQue()
a.printMq(mq)
gg = a.getGcode()
a.printGcode(gg)
i = 1
while True:
time.sleep(1)
i += 1