-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal_adc_value.bk
executable file
·171 lines (146 loc) · 6.44 KB
/
hal_adc_value.bk
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
#!/usr/bin/python
# encoding: utf-8
########################################################################
# Description: hal_adc_value #
# This code reads an ADC input on the BeagleBone and converts the #
# resulting value into a offset/scaled value #
# #
# Adapted from temp.py originally by Author(s): Charles Steinkuehler #
# License: GNU GPL Version 2.0 or (at your option) any later version. #
# #
########################################################################
# Copyright (C) 2013 Charles Steinkuehler #
# <charles AT steinkuehler DOT net> #
# #
# This program is free software; you can redistribute it and/or #
# modify it under the terms of the GNU General Public License #
# as published by the Free Software Foundation; either version 2 #
# of the License, or (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA #
# 02110-1301, USA. #
# #
# THE AUTHORS OF THIS PROGRAM ACCEPT ABSOLUTELY NO LIABILITY FOR #
# ANY HARM OR LOSS RESULTING FROM ITS USE. IT IS _EXTREMELY_ UNWISE #
# TO RELY ON SOFTWARE ALONE FOR SAFETY. Any machinery capable of #
# harming persons must have provisions for completely removing power #
# from all motors, etc, before persons enter any danger area. All #
# machinery must be designed to comply with local and national safety #
# codes, and the authors of this software can not, and do not, take #
# any responsibility for such compliance. #
########################################################################
import argparse
import glob
import sys
import time
import hal
class Pin:
def __init__(self):
self.pin = 0
self.scale = None
self.offset = None
self.halValuePin = 0
self.halRawPin = 0
self.filterSamples = []
self.filterSize = 10
self.rawValue = 0.0
self.filename = ""
self.filterSamples = []
self.rawValue = 0.0
def addSample(self, value):
self.filterSamples.append(value)
if (len(self.filterSamples) > self.filterSize):
self.filterSamples.pop(0)
sampleSum = 0.0
for sample in self.filterSamples:
sampleSum += sample
self.rawValue = sampleSum / len(self.filterSamples)
return round((pin.rawValue - pin.offset) * pin.scale * 10.0) / 10.0
def getHalName(pin):
return "ch-" + '{0:02d}'.format(pin.pin)
def checkAdcInput(pin):
syspath = '/sys/bus/iio/devices/iio:device0/'
tempName = glob.glob(syspath + 'in_voltage' + str(pin.pin) + '_raw')
pin.filename = tempName[0]
try:
if len(pin.filename) > 0:
f = open(pin.filename, 'r')
f.close()
time.sleep(0.001)
else:
raise UserWarning('Bad Filename')
except (UserWarning, IOError):
print(("Cannot read ADC input: %s" % pin.filename))
sys.exit(1)
parser = argparse.ArgumentParser(description='HAL component to read ADC value and convert to scaled value')
parser.add_argument('-n','--name', help='HAL component name',required=True)
parser.add_argument('-i', '--interval', help='Adc update interval', default=0.05)
parser.add_argument('-c', '--channels', help='Comma separated list of channels, offset, and scales to use e.g. 01:0.072:57.47', required=True)
parser.add_argument('-f', '--filter_size', help='Size of the low pass filter to use', default=10)
args = parser.parse_args()
updateInterval = float(args.interval)
filterSize = int(args.filter_size)
error = False
watchdog = True
# Create pins
pins = []
if (args.channels != ""):
channelsRaw = args.channels.split(',')
for channel in channelsRaw:
pinRaw = channel.split(':')
if (len(pinRaw) != 2):
print(("wrong input"))
sys.exit(1)
pin = Pin()
pin.pin = int(pinRaw[0])
if ((pin.pin > 5) or (pin.pin < 0)):
print(("Pin not available"))
sys.exit(1)
checkAdcInput(pin)
if (pinRaw[1] != "none"):
pin.offset = pinRaw[1]
if (pinRaw[2] != "none"):
pin.scale = pinRaw[2]
pin.filterSize = filterSize
pins.append(pin)
# Initialize HAL
h = hal.component(args.name)
for pin in pins:
pin.halRawPin = h.newpin(getHalName(pin) + ".raw", hal.HAL_FLOAT, hal.HAL_OUT)
if (pin.scale is not None):
pin.halValuePin = h.newpin(getHalName(pin) + ".value", hal.HAL_FLOAT, hal.HAL_OUT)
halErrorPin = h.newpin("error", hal.HAL_BIT, hal.HAL_OUT)
halNoErrorPin = h.newpin("no-error", hal.HAL_BIT, hal.HAL_OUT)
halWatchdogPin = h.newpin("watchdog", hal.HAL_BIT, hal.HAL_OUT)
h.ready()
halErrorPin.value = error
halNoErrorPin.value = not error
halWatchdogPin.value = watchdog
try:
while (True):
try:
for pin in pins:
f = open(pin.filename, 'r')
value = float(f.readline())
pin.addSample(value)
pin.halRawPin.value = pin.rawValue
if (pin.scale is not None):
pin.halValuePin.value = round((pin.rawValue - pin.offset) * pin.scale * 10.0) / 10.0
error = False
except IOError:
error = True
halErrorPin.value = error
halNoErrorPin.value = not error
watchdog = not watchdog
halWatchdogPin.value = watchdog
time.sleep(updateInterval)
except:
print(("exiting HAL component " + args.name))
h.exit()