forked from peter-dye/ButtBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathultrasonic_driver.py
More file actions
45 lines (37 loc) · 1.3 KB
/
ultrasonic_driver.py
File metadata and controls
45 lines (37 loc) · 1.3 KB
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
import time
from smbus2 import SMBus
from constants import *
from multiprocessing import Array
from threading import Thread, Lock
#import motor_driver
#from path_planning import motor_controller
class UltrasonicDriver():
def __init__(self):
self.bus = SMBus(0)
self.buffer = Array('I', range(4))
self.lock = Lock()
self.t = Thread(target=self.write_to_mem)
self.t.start()
def readI2C(self):
#Reads the distances from the slave
bval = 0 #Temp variable to store the byte read from the bus
bval = self.bus.read_byte_data(SLAVE_ADDR, 0) #Read the byte from the bus
return bval #Return read byte
def write_to_mem(self):
while True:
self.lock.acquire()
while (self.readI2C() < 255):
pass
for i in range(4):
self.buffer[i] = self.readI2C()
self.lock.release()
time.sleep(0.200)
def read_from_mem(self):
temp = [0,0,0,0]
self.lock.acquire()
for i in range(4):
temp[i] = self.buffer[i]
self.lock.release()
return temp
def get_distances(self):
return self.read_from_mem()