-
Notifications
You must be signed in to change notification settings - Fork 0
/
manual.py
209 lines (170 loc) · 4.68 KB
/
manual.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
import RPi.GPIO as GPIO
import curses
import time
from time import ctime
from time import sleep
import httplib, urllib
import math
import datetime
from pushover import push
debugmode = True
import sys
WORKING = 0
SUCCESS = 1
TIMEOUT = 2
SWITCHABORT = 3
FORWARD = 1
REVERSE = 2
STOPMOTOR = 3
MAX_TIME = 164
CLOSE_TIME = 164 #time to take to close
OPEN_TIME = 164 #time to open
OPEN_DISTANCE = 70
CLOSED_DISTANCE = 24
LEEWAY = 1
#GPIO PINS
GPIO_MOTOR_FORWARD = 15
GPIO_MOTOR_REVERSE = 21
GPIO_TRIG = 23 #Distance sensor
GPIO_ECHO = 24 #Distance sensor
GPIO_UP_SWITCH = 29
def debugprint(message):
if (debugmode == True):
print (message)
return
def howfar():
#GPIO.setmode(GPIO.BOARD)
GPIO.setup(GPIO_TRIG,GPIO.OUT)
GPIO.setup(GPIO_ECHO,GPIO.IN)
GPIO.output(GPIO_TRIG, False)
debugprint ("Waiting For Sensor To Settle")
time.sleep(0.1)
GPIO.output(GPIO_TRIG, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIG, False)
while GPIO.input(GPIO_ECHO)==0:
pulse_start = time.time()
while GPIO.input(GPIO_ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
print "Distance:",distance,"cm"
return distance
#end of function howfar
def timer():
ticks = time.time()
print ticks
return ticks
def motor(direction):
if direction == FORWARD:
GPIO.setup(GPIO_MOTOR_FORWARD, GPIO.OUT)
GPIO.setup(GPIO_MOTOR_REVERSE, GPIO.OUT)
GPIO.output(GPIO_MOTOR_FORWARD, GPIO.HIGH)
GPIO.output(GPIO_MOTOR_REVERSE, GPIO.LOW)
debugprint("Motor:Forward")
elif direction == REVERSE:
GPIO.setup(GPIO_MOTOR_FORWARD, GPIO.OUT)
GPIO.setup(GPIO_MOTOR_REVERSE, GPIO.OUT)
GPIO.output(GPIO_MOTOR_FORWARD, GPIO.LOW)
GPIO.output(GPIO_MOTOR_REVERSE, GPIO.HIGH)
debugprint("Motor: Reverse")
elif direction == STOPMOTOR:
GPIO.output(GPIO_MOTOR_FORWARD, GPIO.LOW)
GPIO.output(GPIO_MOTOR_REVERSE, GPIO.LOW)
debugprint("Motor: Stop")
else:
debugprint ("Invalid input to motor function")
return #enf of function mortor
def manualswitch():
return 0
def closeDoor():
status = WORKING
finishtime = timer() + CLOSE_TIME
print finishtime
push("Starting Motor")
motor(REVERSE) #start the motor closing
while status == WORKING:
if timer() >= finishtime: #timer expired
debugprint ("TIMER EXPIRED")
status = TIMEOUT
push("Time Out - Check Ramp")
motor(STOPMOTOR)
push("Stopping Motor")
return(status)
def openDoor():
status = WORKING
finishtime = timer() + OPEN_TIME
print finishtime
push("Starting Open Door")
motor(FORWARD) #start the motor closing
while status == WORKING:
if timer() >= finishtime: #timer expired
debugprint ("TIMER EXPIRED")
status = TIMEOUT
push("Time Out - Check Ramp")
motor(STOPMOTOR)
push("Stopping Motor")
return(status)
def closeDoorTest():
status = WORKING
finishtime = timer() + OPEN_TIME
starttime = timer()
print ("Start time")
ticks = time.time()
print ticks
motor(REVERSE) #start the motor closing
while status == WORKING:
fred=1
#print timer()
#status = SUCCESS #All good
motor(STOPMOTOR)
return(status)
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print "No arguments passes, needs up or down and an optional number of seconds"
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("open", "down"):
state = open
seconds = arg
elif opt in ("down", "close"):
state = close
seconds = arg
starttime = time.time()
try:
GPIO.setmode(GPIO.BOARD)
#start the motor closing
motor(REVERSE)
sleep (5)
motor(STOPMOTOR)
except KeyboardInterrupt:
# here you put any code you want to run before the program
# exits when you press CTRL+C
print "Key hit"
#except:
# this catches ALL other exceptions including errors.
# You won't get any error messages for debugging
# so only use it once your code is working
# print "Other error or exception occurred!"
finally:
GPIO.cleanup() # this ensures a clean exit
print "In Finally function"
print ("Running time")
ticks = time.time()
print ticks-starttime
# manual motor control
# usage:
# up - closes ramp
# up 10 - 10 seconds of up
# down - opens ramp
# up 5 - 5 seconds of down
if __name__ == "__main__":
main(sys.argv[1:])