-
Notifications
You must be signed in to change notification settings - Fork 1
/
Argimax_Main.py
263 lines (237 loc) · 8.26 KB
/
Argimax_Main.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
#!/usr/bin/python
# Author: HARRY CHAND <[email protected]>
# Copyright (c) 2014 Intel Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import time, sys, signal, atexit, mraa, thread, threading, os, gc
import pyupm_grove as grove
import pyupm_guvas12d as upmUV
import pyupm_grovemoisture as upmMoisture
import pyupm_stepmotor as mylib
import pyupm_servo as servo
from multiprocessing import Process
# IO Def
myIRProximity = mraa.Aio(5) #GP2Y0A on Analog pin 5
temp = grove.GroveTemp(0) #grove temperature on A0
myMoisture = upmMoisture.GroveMoisture(1) #Moisture sensor on A1
light = grove.GroveLight(2) #Light sensor on A2
myUVSensor = upmUV.GUVAS12D(3) #UV sensor on A3
stepperX = mylib.StepMotor(2, 3) #StepMotorY object on pins 2 (dir) and 3 (step)
stepperY = mylib.StepMotor(4, 5) #StepMotorX object on pins 4 (dir) and 5 (step)
waterpump = mraa.Gpio(10) #Water pump's Relay on GPIO 10
waterpump.dir(mraa.DIR_OUT)
waterpump.write(0)
gServo = servo.ES08A(6) #Servo object using D6
gServo.setAngle(50)
switchY = mraa.Gpio(7) #SwitchY for GPIO 7
switchY.dir(mraa.DIR_IN)
switchX = mraa.Gpio(8) #SwitchX for GPIO 8
switchX.dir(mraa.DIR_IN)
EnableStepperX = mraa.Gpio(9) #StepperMotorX Enable on GPIO 9
EnableStepperX.dir(mraa.DIR_OUT)
EnableStepperX.write(1)
EnableStepperY = mraa.Gpio(11) #StepperMotorY Enable on GPIO 11
EnableStepperY.dir(mraa.DIR_OUT)
EnableStepperY.write(1)
button = grove.GroveButton(0) #Digital Button on D0
# Variable Def
AREF = 5.0
SAMPLES_PER_QUERY = 1024
flag = 1
# Defined all 5 Sensors
UVvalue = myUVSensor.value(AREF, SAMPLES_PER_QUERY) #Voltage value (higher means more UV)
Lightvalue = light.value() # in lux
#Distancevalue = float(myIRProximity.read())*AREF/SAMPLES_PER_QUERY #Distance in Voltage (higher mean closer)
#Soilvalue = myMoisture.value() # 0-300 Dry, 300-600 Most, <600 Wet
Tempvalue = temp.value() # Celsius
# Defined Motors
stepperX.setSpeed(150)
stepperY.setSpeed(150)
#Mx = Process(target = init_MotorX)
#My = Process(target = init_MotorY)
## Exit handlers ##
# This function stops python from printing a stacktrace when you hit control-C
def SIGINTHandler(signum, frame):
raise SystemExit
# This function lets you run code on exit, including functions from myUVSensor
def exitHandler():
cleanup_stop_thread()
print "Exiting"
sys.exit(0)
# Register exit handlers
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)
def init_MotorX():
if (switchX.read()):
stepperX.stepForward(3000)
time.sleep(0.3)
return
def init_MotorY():
if (switchY.read()):
stepperY.stepBackward(3000)
time.sleep(0.3)
return
def Restart_Program():
while (button.value() == 1):
cleanup_stop_thread()
os.execv(sys.executable, sys.executable + sys.argv)
#os.execv(sys.executable, ['python'] + sys.argv)
return
def initial():
print "Reset to initial stages ..... "
'''
# Test input value for switch(s) and restart button
# Test Stepper Motor (going to initial stages)
#Mx = Process(target = init_MotorX)
#My = Process(target = init_MotorY)
EnableStepperX.write(0)
EnableStepperY.write(0)
Mx.start()
My.start()
while (switchX.read() | switchY.read()):
if (switchX.read()==0): Mx.terminate()
if (switchY.read()==0): My.terminate()
EnableStepperX.write(1)
EnableStepperY.write(1)
Mx.start()
while (switchX.read()):
if (switchX.read()==0): Mx.terminate(); EnableStepperX.write(1)
print "Finish Motor X"
#EnableStepperX.write(1)
My.start()
while (switchY.read()):
if (switchY.read()==0): My.terminate(); EnableStepperY.write(1)
print "Finish Motor Y"
EnableStepperY.write(1)
'''
EnableStepperX.write(0)
EnableStepperY.write(0)
while(switchX.read()):
stepperX.stepForward(80)
time.sleep(0.3)
while(switchY.read()):
stepperY.stepBackward(80)
time.sleep(0.3)
EnableStepperX.write(1)
EnableStepperY.write(1)
# Turn OFF water pump relay
waterpump.write(0)
# Servo z-axis should be up
gServo.setAngle(50)
return
def MoveToPot(pot):
print "Moving to Pot %d " %(pot)
posX = 200; posY = 200
EnableStepperX.write(0)
EnableStepperY.write(0)
if (pot == 1): stepperY.stepForward(350); stepperX.stepBackward(267)
elif (pot == 2): stepperX.stepBackward(534)
elif (pot == 3): stepperX.stepBackward(534)
elif (pot == 4): stepperY.stepForward(700)
elif (pot == 5): stepperX.stepForward(534)
elif (pot == 6): stepperX.stepForward(534)
elif (pot == 7): stepperY.stepForward(700)
elif (pot == 8): stepperX.stepBackward(534)
elif (pot == 9): stepperX.stepBackward(534)
else: print "Invalid operation for Pot Position";
EnableStepperX.write(1)
EnableStepperY.write(1)
time.sleep(1)
return
def PlantMoist(pot):
Distancevalue = float(myIRProximity.read())*AREF/SAMPLES_PER_QUERY
print "Check Soil Moisture Pot %d "%(pot)
print "Distance Now is at %f "%(Distancevalue)
if (Distancevalue < 0.9):
# print "Pot detected !"
gServo.setAngle(100)
time.sleep(1.5)
Soilvalue = myMoisture.value()
if (Soilvalue == 0):
print("Pot"+str(pot)+" not detected ! \n skip")
elif (Soilvalue < 300):
print "Pot detected !"
print "Soil value is %d , Need Watering"%(Soilvalue)
waterpump.write(1)
time.sleep(1)
waterpump.write(0)
time.sleep(1.2)
print "Done watering on pot %d"%(pot)
else: print "Soil in pot %d is already Moisture"%(pot)
else: print "Pot %d detected"%(pot)
gServo.setAngle(50)
time.sleep(1)
return
def GetSensorsValue():
while (1):
print "Test all the 5 SENSORS :"
print "1. UV Sensor : %d V" % UVvalue
print "2. Light Sensor : %d Lux" % Lightvalue
print "3. Distance Sensor : %f V" % Distancevalue
print "4. Moisture Sensor : %d " % Soilvalue
print "5. Temperature Sensor : %d Celsius" % Tempvalue
return
def Plotting_Cam():
Local = os.getcwd()
print "node "+os.getcwd()+"web_plot_cam/web/server/server.js"
os.system("node "+os.getcwd()+"web_plot_cam/web/server/server.js")
def cleanup_stop_thread():
gc.collect()
graph.terminate()
restart.terminate()
# Mx.terminate()
# My.terminate()
waterpump.write(0)
EnableStepperX.write(1)
EnableStepperY.write(1)
os.system("killall node")
# del [light, temp, button, gServo]
# Global Definition
restart = Process(target = Restart_Program) #Go into Initial Stages
sensor = Process(target = GetSensorsValue) #Go into Sensors Display
Mx = Process(target = init_MotorX)
My = Process(target = init_MotorY)
graph = Process(target = Plotting_Cam)
if __name__ == '__main__':
restart.start()
while (flag):
graph.start()
initial()
for x in range(1,10):
MoveToPot(x)
PlantMoist(x)
EnableStepperX.write(0)
EnableStepperY.write(0)
stepperX.stepForward(1335)
stepperY.stepBackward(1750)
EnableStepperX.write(1)
EnableStepperY.write(1)
flag = 0
cleanup_stop_thread()
# Information:
## Soil moisture Values (approximate):
# 0-300, sensor in air or dry soil
# 300-600, sensor in humid soil
# 600+, sensor in wet soil or submerged in water
## Infrared Proximity Sensor
# The higher the voltage (closer to AREF) the closer the object is.
# NOTE: The measured voltage will probably not exceed 3.3 volts.
# Every second, print the averaged voltage value
# (averaged over 20 samples).