-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
363 lines (219 loc) · 7.58 KB
/
app.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
#!/usr/bin/python
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor
# from Adafruit_GPIO import MCP230xx
import wiringpi
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket
import RPi.GPIO as GPIO
import smbus
import time
import atexit
import pickle
import os
import git
#------------------------------------------------------------------------------------
# STEP MOTOR
class StepMotor:
#init gpio and rotate motor to initial position (0 degrees)
def __init__( self ):
# create a default object, no changes to I2C address or frequency
self.mh = Adafruit_MotorHAT( addr=0x60 )
# 200 steps/rev, motor port #1
self.stepper = self.mh.getStepper( 200, 1 )
# 30 RPM
self.stepper.setSpeed(30)
# Start with the motor off
self.turnOff();
#current motor position in degrees [0,360]
self.motorPosDeg = 0
# Initalise the starting position
self.initPos()
# Register the turn off function
atexit.register( self.turnOff )
#rotate motor in clockwise direction
def rotateR( self, noSteps, speed ):
# Set the speed
self.stepper.setSpeed( speed )
# Start stepping: SINGLE, DOUBLE, INTERLEAVE, MICROSTEP
self.stepper.step( noSteps, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.MICROSTEP )
# Turn off the motor
self.turnOff()
# Get the motor position
self.motorPosDeg -= noSteps * 1.8
# If the position is less than 0 then add the position to 360
# (pos is going to be a negative which is why we add the position to 360)
if ( self.motorPosDeg < 0 ):
self.motorPosDeg = 360 + self.motorPosDeg
# Store the position just in case we loose power
self.storePos()
#rotate motor in counterclockwise direction
def rotateL( self, noSteps, speed ):
# Set the speed
self.stepper.setSpeed( speed )
# Start stepping: SINGLE, DOUBLE, INTERLEAVE, MICROSTEP
self.stepper.step( noSteps, Adafruit_MotorHAT.BACKWARD, Adafruit_MotorHAT.MICROSTEP )
# Turn off the motor
self.turnOff()
# Get the motor position
self.motorPosDeg += noSteps * 1.8
# If the position is greater than 3060 then reset the rotation
if ( self.motorPosDeg >= 360 ):
self.motorPosDeg = self.motorPosDeg - 360
# Store the position just in case we loose power
self.storePos()
#rotate motor to specific position
def rotateToAngle( self, desiredAngle, speed ):
s = 1
# print 'DESIRED ANGLE: %s\n' %str( desiredAngle )
# print 'CURRENT POS: %s\n' %str( self.motorPosDeg )
if desiredAngle == self.motorPosDeg:
return
deltaAngle = abs( self.motorPosDeg - desiredAngle )
if ( desiredAngle > self.motorPosDeg ):
if ( deltaAngle >= 180 ):
self.rotateR( int(( 360 - deltaAngle ) / 1.8 ), s )
else:
self.rotateL( int( deltaAngle / 1.8 ), s )
else:
if ( deltaAngle >= 180 ):
self.rotateL( int(( 360 - deltaAngle ) / 1.8 ), s )
else:
self.rotateR( int( deltaAngle / 1.8 ), s )
# Go to initial position defined by optical sensor
def initPos( self ):
data = None
if ( os.path.isfile( 'objs.pickle' )):
with open('objs.pickle') as f:
data = pickle.load( f )
if data :
self.motorPosDeg = data[0]
else :
self.motorPosDeg = 0
print 'START POS: %s\n' %str( self.motorPosDeg )
# Rotate the turntable to 0 so it has "reset"
self.rotateToAngle( 0, 0 )
def storePos( self ):
# print 'CURRENT POS: %s\n' %str( self.motorPosDeg )
# Saving the objects:
with open('objs.pickle', 'w') as f:
pickle.dump([ self.motorPosDeg ], f )
#turn coils off
# recommended for auto-disabling motors on shutdown!
def turnOff( self ):
# Turn off the motor
self.mh.getMotor(1).run( Adafruit_MotorHAT.RELEASE )
self.mh.getMotor(2).run( Adafruit_MotorHAT.RELEASE )
self.mh.getMotor(3).run( Adafruit_MotorHAT.RELEASE )
self.mh.getMotor(4).run( Adafruit_MotorHAT.RELEASE )
def cleanup( self ):
# Turn off the motors
self.turnOff()
#------------------------------------------------------------------------------------
# STEP MOTOR
class RelaySwitch:
def __init__( self ):
# Set the sleep time
self.sleepTime = 0.1
# Set the pins to be outputs
self.create()
# INitalise the switches
self.initSwitches()
def initSwitches( self ):
i=0;
for x in range( 0, 45 ):
# if i is 0 then trigger the switch
if i == 1 :
self.switch( x )
# Wait for the sleep time
time.sleep( self.sleepTime );
# Increase I so that we miss the open relay
i=0
# if i is not 0 then reset it to 0
else :
i+=1
def switch( self, id ):
# Get the pin number
pin = id + 65
# TURN ON THE PIN
wiringpi.digitalWrite( pin, 0 )
# Wait for the sleep time
time.sleep( self.sleepTime );
# TURN OFF THE PIN
wiringpi.digitalWrite( pin, 1 )
def cleanup( self ):
# Turn off the output pins
for x in range( 65, 114 ):
wiringpi.digitalWrite( x, 1 ) # Set pin to off
def create( self ):
# Addresses: 0x20, 0x21, 0x22
# Setup WIRING PI in Pin Mode
wiringpi.wiringPiSetup()
# Setup the chips
wiringpi.mcp23017Setup( 65, 0x20 )
wiringpi.mcp23017Setup( 81, 0x21 )
wiringpi.mcp23017Setup( 97, 0x22 )
# Setup pins on the I2C Controller chip
for x in range( 65, 114 ):
wiringpi.pinMode( x, 1 ) # Set pin to output
wiringpi.digitalWrite( x, 1 ) # Set pin to off
#------------------------------------------------------------------------------------
from tornado.options import define, options
define("port", default=8080, help="run on the given port", type=int)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render('app.html')
#------------------------------------------------------------------------------------
class WebSocketHandler( tornado.websocket.WebSocketHandler ):
def open(self):
print 'CONNECTED.\n'
if ( self.stepper !== None and self.switcher !== None ) :
print "Stepper and Switcher Already Created"
else :
# Create the stepper motor controller
self.stepper = StepMotor()
# Create the relay switch controller
self.switcher = RelaySwitch()
def on_message( self, message ):
print 'RECIEVED MESSAGE: %s\n' %message
# Split the message so that we can access the data elements
data = message.split(";")
# If the first code is "turntable" then we want to control the turntable stepper motor
if ( str(data[0]) == "turntable" ):
self.stepper.rotateToAngle( float( data[1]), float( data[2]))
# If the first code is "switch" then we want to switch a point
elif ( str(data[0]) == "switch" ):
self.switcher.switch( int( data[1]))
# If the first code is "shutdown" to shutdown the Raspberry PI
elif ( str(data[0]) == "shutdown" ):
self.switcher.cleanup();
self.stepper.cleanup();
os.system('shutdown -h now')
def on_close( self ):
print 'CONNECTION CLOSED\n'
def check_origin( self, origin ):
return True
def write_message( self, message ):
print ( "IN WRITE MESSAGE " + message )
self.write_message( message )
#------------------------------------------------------------------------------------
if __name__ == "__main__":
tornado.options.parse_command_line()
settings = {
"debug": True,
}
static_path = os.path.join(os.path.dirname(__file__), "assets" )
app = tornado.web.Application(
handlers=[
(r"/", IndexHandler),
(r"/ws", WebSocketHandler),
(r"/assets/(.*)", tornado.web.StaticFileHandler, {"path": static_path})
],
**settings
)
httpServer = tornado.httpserver.HTTPServer( app )
httpServer.listen( options.port )
print "Listening on port:", options.port
tornado.ioloop.IOLoop.instance().start()