-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.py
164 lines (133 loc) · 3.5 KB
/
decoder.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
from PIL import Image
import numpy as np
from pyfirmata import Arduino, util
import sys
import time
from scipy.interpolate import interp1d
'''
Inilization look line 136 for different color pixel
'''
resolution = 255
stepPin_white = 6
dirPin_white = 7
stepPin2_black = 3
dirPin2_black = 4
min_speed = 0.016
'''
Created an grayscale image of intensity 128 (0-255) for calabration of motor speed
'''
def create_image():
k = Image.open('result.png')
size = (40,40)
width, height = size
k = k.resize((round(width/4),round(height/4)))
out = Image.new('L', k.size)
for x in range(round(width/4)):
for y in range(round(height/4)):
out.putpixel((x,y),128)
out.save('final_result.png')
'''
Gets image color (here gray scale intensity )
'''
def get_intensity():
k = Image.open('final_result.png').getcolors()
print(k)
print(k[0][1])
return (k[0][1])
white_length = 0
black_length = 0
def motor_white(step_local):
global white_length
board.digital[dirPin_white].write(1)
for i in range(0,step_local):
board.digital[stepPin_white].write(1)
time.sleep(min_speed)
board.digital[stepPin_white].write(0)
time.sleep(min_speed)
white_length = white_length + 1
def motor2_black(step_local):
global black_length
board.digital[dirPin2_black].write(0)
for i in range(0, step_local):
board.digital[stepPin2_black].write(1)
time.sleep(min_speed)
board.digital[stepPin2_black].write(0)
time.sleep(min_speed)
black_length = black_length +1
f = 1.8/180
'''
Resolved Step function
'''
def step_generator(w,b):
x = w / b
y = 1
if x < 1:
x = 1 / x
if x <= 1.2 or x >= 1.8:
x = round(x)
y = 1
elif x > 1.4 and x <= 1.6:
y = 2
x = round(y * x)
else:
y = 3
x = round(y * x)
step = []
if w < b:
d = int(min(w // y, b // x))
for i in range(0, d):
step.append([x, y])
w = w - y
b = b - x
step.append([w, b])
elif w == b:
step = [[1, 1]] * w
else:
d = int(min(w // x, b // y))
for i in range(0, d):
step.append([x, y])
w = w - x
b = b - y
step.append([w, b])
return step
def pixel(color_grad):
w = int(mwhite(color_grad))
b = int(mblack(color_grad))
print([w, b])
if w != 0 and b != 0:
step = step_generator(w, b)
else: # Handling edge cases
if w == 0:
step = [[0, resolution]]
else:
step = [[resolution, 0]]
return step
def printer(step):
try:
for kk in range(0,1):
for i in step:
motor2_black(i[1])
motor_white(i[0])
time.sleep(.05)
except KeyboardInterrupt:
print("KeyInterrupt")
pass
color_grad = [0,128,255] #gray scale intensity replace constant from get_intensity funciton
'''
Speed of Stepper according to color required
'''
mwhite = interp1d([0,255],[0,resolution]) #Linear plotting
mblack = interp1d([0,255],[resolution,0])
board = Arduino("COM5")
it = util.Iterator(board)
it.start()
for color in color_grad:
step = pixel(color)
print("step [white,black] : " + str(step))
print(len(step))
printer(step)
print("White length excruded :" + str(f * white_length) + "mm\n" + "Black Length excruded :" + str(
f * black_length) + 'mm')
white_length = 0
black_length = 0
print("----------------------")