This repository has been archived by the owner on Jun 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app3.py
222 lines (175 loc) · 7.18 KB
/
app3.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
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 10 14:28:20 2013
@author: Godwin
"""
from flask import (Flask,
g,
request,
render_template,
redirect,
url_for,
jsonify,
abort)
#Basic imports
from ctypes import *
import sys
from time import sleep
#Phidget specific imports
from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException
from Phidgets.Events.Events import AttachEventArgs, DetachEventArgs, ErrorEventArgs, CurrentChangeEventArgs, PositionChangeEventArgs, VelocityChangeEventArgs
from Phidgets.Devices.AdvancedServo import AdvancedServo
from Phidgets.Devices.Servo import ServoTypes
#open phidget control board
advancedServo = AdvancedServo()
def Attached(e):
attached = e.device
print("Servo %i Attached!" % (attached.getSerialNum()))
advancedServo.openRemoteIP('10.251.77.216',5001, 303604)
#attach phidget, engage motors, and set them to default position
advancedServo.waitForAttach(100000)
for k in range(0,8):
advancedServo.setServoType(k, ServoTypes.PHIDGET_SERVO_HITEC_HS422)
advancedServo.setEngaged(k, True)
sleep(.2)
advancedServo.setVelocityLimit(k, 100.00)
if k % 2 == 0:
advancedServo.setPosition(k,0)
elif k % 2 == 1:
advancedServo.setPosition(k,180)
#_______________________________________________________________________________________________________
# -------------------------------------------------------
# Flask App Setup (flask + flask-restful)
# -------------------------------------------------------
app = Flask(__name__)
@app.route('/')
def index():
return render_template('gui2.html')
#@app.route('/start')
@app.route('/door1')
def open1():
if advancedServo.getPosition(0) == 0 and advancedServo.getPosition(1) == 180:
advancedServo.setPosition(0, 90.00)
advancedServo.setPosition(1, 90.00)
elif advancedServo.getPosition(0) == 90 and advancedServo.getPosition(1) == 90:
advancedServo.setPosition(0, 0.00)
advancedServo.setPosition(1, 180.00)
return render_template('gui2.html')
@app.route('/door2')
def door2():
if advancedServo.getPosition(2) == 0 and advancedServo.getPosition(3) == 180:
advancedServo.setPosition(2, 90.00)
advancedServo.setPosition(3, 90.00)
elif advancedServo.getPosition(2) == 90 and advancedServo.getPosition(3) == 90:
advancedServo.setPosition(2, 0.00)
advancedServo.setPosition(3, 180.00)
return render_template('gui2.html')
@app.route('/door3')
def door3():
if advancedServo.getPosition(4) == 0 and advancedServo.getPosition(5) == 180:
advancedServo.setPosition(4, 90.00)
advancedServo.setPosition(5, 90.00)
elif advancedServo.getPosition(4) == 90 and advancedServo.getPosition(5) == 90:
advancedServo.setPosition(4, 0.00)
advancedServo.setPosition(5, 180.00)
return render_template('gui2.html')
#all door controls
@app.route('/alldoors')
def omnidoor():
for k in range (0,8):
if k % 2 == 0:
if advancedServo.getPosition(k) == 0:
advancedServo.setPosition(k, 90.00)
advancedServo.setPosition(k+1, 90.00)
elif advancedServo.getPosition(k) == 90:
advancedServo.setPosition(k, 0.00)
advancedServo.setPosition(k+1, 180.00)
return render_template('gui2.html')
@app.route('/alldoors_open')
def omnidoor_o():
for k in range(0,8):
if k % 2 == 0:
if advancedServo.getPosition(k) == 0:
advancedServo.setPosition(k, 90.00)
advancedServo.setPosition(k+1, 90.00)
return render_template('gui2.html')
@app.route('/alldoors_close')
def omnidoor_c():
for k in range(0,8):
if k % 2 == 0:
if advancedServo.getPosition(k) == 90:
advancedServo.setPosition(k, 0.00)
advancedServo.setPosition(k+1, 180.00)
return render_template('gui2.html')
@app.route('/shutdown')
def shutdown():
for k in range(0,8):
advancedServo.setEngaged(k, False)
sys.exit()
return render_template('gui2.html')
#Door States
@app.route('/door_state1')
def DoorState1():
if advancedServo.getEngaged(0) == True and advancedServo.getEngaged(1) == True:
return "Door1 is ON"
elif advancedServo.getEngaged(0) == False or advancedServo.getEngaged(1) == False:
return "Door1 is OFF"
@app.route('/door_state2')
def DoorState2():
if advancedServo.getEngaged(2) == True and advancedServo.getEngaged(3) == True:
return "Door2 is ON"
elif advancedServo.getEngaged(1) == False or advancedServo.getEngaged(3) == False:
return "Door2 is OFF"
@app.route('/door_state3')
def DoorState3():
if advancedServo.getEngaged(4) == True and advancedServo.getEngaged(5) == True:
return "Door3 is ON"
elif advancedServo.getEngaged(4) == False or advancedServo.getEngaged(5) == False:
return "Door3 is OFF"
# Door open or closed
@app.route('/door_open_close1')
def Dooropenclose():
if advancedServo.getEngaged(0) == False or advancedServo.getEngaged(1) == False:
return "Door1 is not on"
elif advancedServo.getEngaged(0) == True and advancedServo.getEngaged(1) == True:
if advancedServo.getPosition(0) == 90 and advancedServo.getPosition(1) == 90:
return "Door1 is OPEN"
elif advancedServo.getPosition(0) == 0 and advancedServo.getPosition(1) == 180.00:
return "Door1 is CLOSED"
@app.route('/door_open_close2')
def Dooropenclose2():
if advancedServo.getEngaged(2) == False or advancedServo.getEngaged(3) == False:
return "Door2 is not on"
elif advancedServo.getEngaged(2) == True and advancedServo.getEngaged(3) == True:
if advancedServo.getPosition(2) == 90 and advancedServo.getPosition(3) == 90:
return "Door2 is OPEN"
elif advancedServo.getPosition(2) == 0 and advancedServo.getPosition(3) == 180.00:
return "Door2 is CLOSED"
@app.route('/door_open_close3')
def Dooropenclose3():
if advancedServo.getEngaged(4) == False or advancedServo.getEngaged(5) == False:
return "Door3 is not on"
elif advancedServo.getEngaged(4) == True and advancedServo.getEngaged(5) == True:
if advancedServo.getPosition(4) == 90 and advancedServo.getPosition(5) == 90:
return "Door3 is OPEN"
elif advancedServo.getPosition(4) == 0 and advancedServo.getPosition(5) == 180.00:
return "Door3 is CLOSED"
#Door position
@app.route('/door_posit1')
def doorposition1():
if advancedServo.getEngaged(0) == True and advancedServo.getEngaged(1) == True:
return str(advancedServo.getPosition(0))
@app.route('/door_posit2')
def doorposition2():
if advancedServo.getEngaged(2) == True and advancedServo.getEngaged(3) == True:
return str(advancedServo.getPosition(2))
@app.route('/door_posit3')
def doorposition3():
if advancedServo.getEngaged(4) == True and advancedServo.getEngaged(5) == True:
return str(advancedServo.getPosition(4))
# -------------------------------------------------------
# Debug
# -------------------------------------------------------
if __name__ == "__main__":
app.run(port=2667, debug=True)
#______________________________________________________________________________________________________