|
| 1 | +import RPi.GPIO as gpio |
| 2 | +import time |
| 3 | +import sys |
| 4 | +import Tkinter as tk |
| 5 | +import random |
| 6 | +import Adafruit_DHT |
| 7 | +import time |
| 8 | + |
| 9 | +DHT_SENSOR = Adafruit_DHT.DHT11 |
| 10 | +DHT_PIN = 17 |
| 11 | + |
| 12 | +gpio.setmode(gpio.BOARD) |
| 13 | +gpio.setwarnings(False) |
| 14 | + |
| 15 | + |
| 16 | +def init() : |
| 17 | + gpio.setmode(gpio.BOARD) |
| 18 | + gpio.setup(31, gpio.OUT) |
| 19 | + gpio.setup(33, gpio.OUT) |
| 20 | + gpio.setup(35, gpio.OUT) |
| 21 | + gpio.setup(37, gpio.OUT) |
| 22 | + |
| 23 | + |
| 24 | +def backward() : |
| 25 | + #gpio.output(31, False) |
| 26 | + #gpio.output(33, False) |
| 27 | + gpio.output(35, True) |
| 28 | + gpio.output(37, False) |
| 29 | + time.sleep(1) |
| 30 | + gpio.output(35, False) |
| 31 | + #gpio.cleanup() |
| 32 | + |
| 33 | + |
| 34 | +def forward() : |
| 35 | + #gpio.output(31, False) |
| 36 | + #gpio.output(33, False) |
| 37 | + gpio.output(35, False) |
| 38 | + gpio.output(37, True) |
| 39 | + time.sleep(1) |
| 40 | + gpio.output(37, False) |
| 41 | + #gpio.cleanup() |
| 42 | + |
| 43 | + |
| 44 | +def right() : |
| 45 | + gpio.output(31, True) |
| 46 | + gpio.output(33, False) |
| 47 | + #time.sleep(0.2) |
| 48 | + #gpio.output(35, False) |
| 49 | + #gpio.output(37, True) |
| 50 | + #time.sleep(tf) |
| 51 | + #gpio.cleanup() |
| 52 | + |
| 53 | +def left() : |
| 54 | + gpio.output(31, False) |
| 55 | + gpio.output(33, True) |
| 56 | + #time.sleep(0.2) |
| 57 | + #gpio.output(35, False) |
| 58 | + #gpio.output(37, True) |
| 59 | + #time.sleep(tf) |
| 60 | + #gpio.cleanup() |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +def dht(): #gets temp and humid returns [humid,temp] |
| 66 | + humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN) |
| 67 | + |
| 68 | + print([humidity,temperature]) |
| 69 | + return [humidity,temperature] |
| 70 | + |
| 71 | +def distance(): # gets obstacle distace (returns float) |
| 72 | + try: |
| 73 | + TRIG = 16 |
| 74 | + ECHO = 18 |
| 75 | + |
| 76 | + gpio.setup(TRIG,gpio.OUT) |
| 77 | + gpio.setup(ECHO,gpio.IN) |
| 78 | + |
| 79 | + gpio.output(TRIG, True) |
| 80 | + time.sleep(0.00001) |
| 81 | + gpio.output(TRIG, False) |
| 82 | + |
| 83 | + while gpio.input(ECHO) == False: |
| 84 | + start = time.time() |
| 85 | + |
| 86 | + while gpio.input(ECHO) == True: |
| 87 | + end = time.time() |
| 88 | + |
| 89 | + sig_time = end-start |
| 90 | + #CM: |
| 91 | + distance = sig_time/ 0.000058 |
| 92 | + #print('Distance: {} centimeters'.format(distance)) |
| 93 | + gpio.cleanup() |
| 94 | + |
| 95 | + return distance |
| 96 | + except: |
| 97 | + print("except") |
| 98 | + return 10 |
| 99 | + gpio.cleanup() |
| 100 | + |
| 101 | +def check(tf): |
| 102 | + init() |
| 103 | + dist=distance() |
| 104 | + |
| 105 | + if dist<40: |
| 106 | + print("Dead",) |
| 107 | + |
| 108 | + x=random.randrange(0,2) |
| 109 | + |
| 110 | + if(x==0): |
| 111 | + init() |
| 112 | + left(tf) |
| 113 | + backward(tf) |
| 114 | + else: |
| 115 | + init() |
| 116 | + right(tf) |
| 117 | + backward(tf) |
| 118 | + |
| 119 | + dist=distance() |
| 120 | + if dist<15: |
| 121 | + init() |
| 122 | + backward(1) |
| 123 | + init() |
| 124 | + forward(tf) |
| 125 | + |
| 126 | + |
| 127 | +def autonomy(): |
| 128 | + tf=0.03 |
| 129 | + |
| 130 | + x=random.randrange(0,3) |
| 131 | + |
| 132 | + if x==0: |
| 133 | + for y in range(30): |
| 134 | + check(tf) |
| 135 | + init() |
| 136 | + forward(0.2) |
| 137 | + elif(x==1): |
| 138 | + for y in range(30): |
| 139 | + check(tf) |
| 140 | + init() |
| 141 | + left(tf) |
| 142 | + forward(0.2) |
| 143 | + elif(x==2): |
| 144 | + for y in range(30): |
| 145 | + check(tf) |
| 146 | + init() |
| 147 | + right(tf) |
| 148 | + forward(tf) |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +'''def key_input(event) : |
| 155 | + init() |
| 156 | + print 'Key : ',event.char |
| 157 | + key_press = event.char |
| 158 | + sleep_time = 0.025 |
| 159 | + |
| 160 | + if key_press.lower() == 'w': |
| 161 | + forward() |
| 162 | + elif key_press.lower() == 's': |
| 163 | + backward() |
| 164 | + elif key_press.lower() == 'a': |
| 165 | + left() |
| 166 | + elif key_press.lower() == 'd': |
| 167 | + right() |
| 168 | + elif key_press.lower() == 'z': |
| 169 | + for i in range(30): |
| 170 | + autonomy() |
| 171 | + elif key_press.lower() == 'x': |
| 172 | + gpio.cleanup() |
| 173 | + sys.exit() |
| 174 | + else: |
| 175 | + gpio.cleanup() |
| 176 | + |
| 177 | + |
| 178 | +command = tk.Tk() |
| 179 | +command.bind('<KeyPress>',key_input) |
| 180 | +command.mainloop()''' |
| 181 | + |
| 182 | + |
0 commit comments