-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRSensor.py
executable file
·84 lines (65 loc) · 2.39 KB
/
IRSensor.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
import comedi as c
import time, sys
from multiprocessing import *
class IRSensor:
def __init__(self, channel = 0):
self.value = 0
self.voltage = 0
self.distance = 0
self.channel = channel
def read(self, comediObject):
ret, self.value = c.comedi_data_read(comediObject.device, comediObject.subdevice, self.channel, 0, 0)
self.voltage = c.comedi_to_phys(self.value, comediObject.channelRange, comediObject.maxdata)
class LongIRSensor(IRSensor):
def __init__(self, channel = 0):
self.value = 0
self.voltage = 0
self.distance = 0
self.channel = channel
class ComediObject:
def __init__(self, subdevice = c.COMEDI_SUBD_AI):
self.device = c.comedi_open('/dev/comedi0')
self.subdevice = c.comedi_find_subdevice_by_type(self.device, subdevice, 0)
self.numChannels = c.comedi_get_n_channels(self.device, self.subdevice)
self.numRanges = c.comedi_get_n_channels(self.device, self.subdevice)
self.channelRange = c.comedi_get_range(self.device, self.subdevice, 0, 0)
self.maxdata = c.comedi_get_maxdata(self.device, 0, 0)
class SensorManager:
def __init__(self, comediObj, numSensors):
self.comediObject = comediObj
self.sensorList = []
self.numSensors = numSensors
self.pool = Pool(processes=4)
def addSensors(self):
for i in range(self.numSensors):
self.sensorList.append(LongIRSensor(i))
def startAcquisition(self):
for sensor in self.sensorList:
Process(target=sensor.read, args=(comediObj,)).start()
#self.pool.apply_async(sensor.read, [self.comediObject])
#main
comediObj = ComediObject()
sm = SensorManager(comediObj, 1)
sm.addSensors()
sm.startAcquisition()
while True:
print sm.sensorList[0].value
time.sleep(1)
sys.exit()
for i in range(100):
for chan in range(nChannels):
ret, rdata = c.comedi_data_read(dev, subdev, chan, 0, 0)
#print "Read value: %d" % rdata
voltage = c.comedi_to_phys(rdata, crange, maxdata);
#print voltage
x1 = voltage
x2 = x1*voltage
x3 = x2*voltage
x4 = x3*voltage
x5 = x4*voltage
dist = -14.153*x5+110.18*x4-339.89*x3+538.13*x2-479.23*x1+243.35
print "Chan %d Distance: %f" % (chan, dist)
print
time.sleep(1)
print "Closing %s" % str(dev)
c.comedi_close(dev)