-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConveyorBeltX.py
executable file
·220 lines (176 loc) · 8.79 KB
/
ConveyorBeltX.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
# _____ __ __ ____ __
# / ___/ ____ _ / /____ / /_ __ __ _____ ____ _ / __ \ ___ _____ ___ ____ _ _____ _____ / /_
# \__ \ / __ `// //_ / / __ \ / / / // ___// __ `/ / /_/ // _ \ / ___// _ \ / __ `// ___// ___// __ \
# ___/ // /_/ // / / /_ / /_/ // /_/ // / / /_/ / / _, _// __/(__ )/ __// /_/ // / / /__ / / / /
# /____/ \__,_//_/ /___//_.___/ \__,_//_/ \__, / /_/ |_| \___//____/ \___/ \__,_//_/ \___//_/ /_/
# /____/
# Salzburg Research ForschungsgesmbH
# Armin Niedermueller & Christoph Schranz
#
# class to control a conveyor belt with an stepper motor via the PiXtend Hardware
from pixtendv2s import PiXtendV2S
import threading
import logging
import time
class ConveyorBeltX:
def __init__(self):
self.logger = logging.getLogger(__name__)
self.shotstate = 0
self.state = "init" # possible states: "left", "right", "halt", "stop", "fail"
self.distance = 0.0
self.total_distance = 0.0
# PiXtend Control Object
self.pixtend = PiXtendV2S()
# Enable Pull Up Resistors at GPIOs
self.pixtend.gpio_pullups_enable = True
# Configure the GPIOs as Input
self.pixtend.gpio0_ctrl = 0
self.pixtend.gpio1_ctrl = 0
self.pixtend.gpio2_ctrl = 0
self.pixtend.gpio3_ctrl = 0
# Switch the GPIOs 1 & 2 to HIGH (Pull Up)
self.pixtend.gpio1 = True
self.pixtend.gpio0 = True
# Red light OFF & Green light ON
self.pixtend.relay0 = False
self.service_interval = 2 # maintenance is required, if the total distance exceeds this number
self.sim_breakdown = 3 # we simulate a breakdown, if the total distance exceeds this number
self.maintenance_required = False
self.velocity = 0.05428 # Velocity of the belt im m/s (5.5cm/s)
self.pixtend.pwm0_ctrl0 = 0b01100011 # Channel A & B deactivated, Frequency Mode activated, Prescaler at 64
# Bit 0 - Mode0 1 <-
# Bit 1 - Mode1 1 <-
# Bit 2 - Dummy 0
# Bit 3 - EnableA 0
# Bit 4 - EnableB 0
# Bit 5 - Prescaler0 1 <-
# Bit 6 - Prescaler1 1 <-
# Bit 7 - Prescaler2 0
# set PWM registers
self.pixtend.pwm0a = 125 # Oscillator Frequency / 2 / Prescaler / PWM0A Register = Frequency
self.pixtend.pwm0b = 125 # 16 Mhz / 2 / 64 / 125 = 1000Hz
try:
with open("state.log") as f:
self.state = f.read()
with open("distance.log") as f:
self.distance = float(f.read())
with open("total_distance.log") as f:
self.total_distance = float(f.read())
if self.total_distance > self.service_interval:
self.maintenance_required = True
self.logger.info("restored conveyor belt (state, distance, total_distance) ({}, {}, {})".format(self.state, self.distance, self.total_distance))
except:
self.logger.info("could not restore state, creating new")
with open("state.log", "w") as f:
f.write(self.state)
with open("distance.log", "w") as f:
f.write(str(self.distance))
def write_state(self, state):
with open("state.log", "w") as f:
f.write(state)
def write_distance(self, distance):
with open("distance.log", "w") as f:
f.write(str(distance))
def write_total_distance(self, total_distance):
with open("total_distance.log", "w") as f:
f.write(str(total_distance))
def busy_light(self, busy):
if busy is True:
self.pixtend.relay0 = True # Red light ON & Green light OFF
else:
self.pixtend.relay0 = False # Red light OF & Green light ON
return True
def move_left(self):
self.state = "left"
self.write_state(self.state)
self.pixtend.digital_out3 = True # RELAY ON
self.pixtend.pwm0_ctrl0 = 0b01111011 # PWM Channel A & B - ON
self.pixtend.digital_out0 = True # Direction = Left
#self.pixtend.relay0 = True # Red light ON & Green light OFF
def move_right(self):
self.state = "right"
self.write_state(self.state)
self.pixtend.digital_out3 = True # RELAY ON
self.pixtend.pwm0_ctrl0 = 0b01111011 # PWM Channel B - ON
self.pixtend.digital_out0 = False # Direction = Right
#self.pixtend.relay0 = True # Red light ON & Green light OFF
def halt(self):
self.state = "halt"
self.write_state(self.state)
self.pixtend.digital_out3 = False # RELAY OFF
self.pixtend.pwm0_ctrl0 = 0b01100011 # PWM Channels A & B - OFF
#self.pixtend.relay0 = False # Red light OFF & Green light ON
def init(self):
self.state = "init"
self.write_state(self.state)
def fail(self):
self.state = "fail"
self.write_state(self.state)
def stop(self):
self.state = "stop"
self.write_state(self.state)
self.pixtend.digital_out3 = False # RELAY OFF
self.pixtend.pwm0_ctrl0 = 0b01100011 # PWM Channels A & B - OFF
#self.pixtend.relay0 = False # Red light OFF & Green light ON
def wait_for_it(self, distance):
with open("total_distance.log") as f:
self.total_distance = float(f.read())
init_state = self.state
traveltime = distance/self.velocity
was_interrrupted = False
self.logger.info("start moving %sm to %s for %ss", distance, init_state, traveltime)
starttime = prevtime = time.time()
while prevtime < (starttime + traveltime):
time.sleep(0.1)
currenttime = time.time()
looptime = currenttime-prevtime
currentdistance = looptime*self.velocity
self.logger.debug("moved %sm to %s in %ss", currentdistance, init_state, looptime)
if self.state != init_state:
was_interrrupted = True
break
if init_state == "fail":
self.logger.error("can't move in fail state, reset total dist!")
return False
if init_state == "left":
self.distance += currentdistance
self.write_distance(self.distance)
self.total_distance += currentdistance
self.write_total_distance(self.total_distance)
elif init_state == "right":
self.distance -= currentdistance
self.write_distance(self.distance)
self.total_distance += currentdistance
self.write_total_distance(self.total_distance)
if self.total_distance > self.service_interval:
self.maintenance_required = True
else:
self.maintenance_required = False
if self.total_distance > self.sim_breakdown:
self.fail()
self.logger.error("stopped move due to failure at distance %sm", self.total_distance)
return False
prevtime = currenttime
self.logger.info("finished move of %sm to %s in %ss", distance, init_state, traveltime)
self.halt()
return True
def move_left_for(self, distance=0):
if self.state == "halt" or self.state == "init":
self.logger.debug("spawning thread for move_left")
self.move_left()
waiting = threading.Thread(name='waiter', target=self.wait_for_it, args=(distance,))
waiting.start()
waiting.join()
def move_right_for(self, distance=0):
if self.state == "halt" or self.state == "init":
self.logger.debug("spawning thread for move_right")
self.move_right()
waiting = threading.Thread(name='waiter', target=self.wait_for_it, args=(distance,))
waiting.start()
waiting.join()
def reset_totaldistance(self, totaldist=0):
if totaldist < self.service_interval:
self.maintenance_required = False
self.init()
self.write_total_distance(totaldist)
self.write_distance(totaldist)