forked from thealexshi/WeWetOurPlants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (62 loc) · 1.93 KB
/
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
from motor import MotorDriver
from color import ColorSensor
from camera import Camera
from mongo_driver import MongoDriver
import RPi.GPIO as GPIO
import Adafruit_TCS34725
import smbus
import time
import os
mongo_driver = MongoDriver(os.environ['MONGOCLIENT'])
motor_driver = MotorDriver(GPIO)
sensor = ColorSensor(Adafruit_TCS34725)
camera = Camera("pics/")
def initial_scan():
for i in range(4):
move_until_black_line(motor_driver, sensor, time)
time.sleep(1)
camera.snap("plant" + str(i))
motor_driver.motor_move("3")
time.sleep(0.7)
motor_driver.motor_stop("3")
mongo_driver.upload_images(['./pics/plant0', './pics/plant1', './pics/plant2', './pics/plant3'])
def move_back():
for i in range(5):
move_until_black_line(motor_driver, sensor, time, "other way")
time.sleep(1)
def get_results_and_water():
for i in range(0, 4):
result = mongo_driver.get_plant(i)
print(result)
if result:
move_until_black_line(motor_driver, sensor, time)
if 'water' in result and result['water'] == True:
dispense_liquid(motor_driver, "water", time)
if 'fertilizer' in result and result['fertilizer'] == True:
dispense_liquid(motor_driver, "fertilizer", time)
def move_until_black_line(motor_driver, sensor, time_obj, direction="clockwise"):
motor_driver.set_direction("3", direction)
motor_driver.motor_move("3")
time_obj.sleep(0.3)
while (sensor.get_lux() >= 5):
continue
motor_driver.motor_stop("3")
def dispense_liquid(motor_driver, liq_type, time_obj):
motor_num = "1" if liq_type == "fertilizer" else "2"
print("Dispensing " + liq_type + "\n")
motor_driver.set_direction(motor_num, "clockwise")
motor_driver.motor_move(motor_num)
time_obj.sleep(3)
motor_driver.motor_stop(motor_num)
time_obj.sleep(3)
while True:
initial_scan()
move_back()
while not mongo_driver.contains_results():
time.sleep(5)
continue
get_results_and_water()
move_back()
mongo_driver.delete_all()
camera.clear_pics()
time.sleep(120)