Skip to content
Snippets Groups Projects
Commit 93d6522a authored by Bram Veenboer's avatar Bram Veenboer
Browse files

Remove psconfig binary

parent f3dd552c
Branches
Tags
No related merge requests found
#!/usr/bin/python
# Copyright (C) 2016
# ASTRON (Netherlands Institute for Radio Astronomy) / John W. Romein
# P.O. Box 2, 7990 AA Dwingeloo, the Netherlands
# This file is part of PowerSensor.
# PowerSensor 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 3 of the License, or
# (at your option) any later version.
# PowerSensor 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 PowerSensor. If not, see <http://www.gnu.org/licenses/>.
import getopt, serial, sys, time, struct
maxSensors = 5
def approximates(a, b):
return a / b > .999999 and a / b < 1.000001
class Sensor:
def readEEPROM(self, ser):
self.volt, self.voltPerAmpere, self.nullLevel = struct.unpack("<ffh", ser.read(10))
def writeEEPROM(self, ser):
ser.write(struct.pack("<ffh", self.volt, self.voltPerAmpere, self.nullLevel))
def readLevel(self, ser):
self.level = struct.unpack("<h", ser.read(2))[0]
class EEPROM:
def __init__(self, device = "/dev/ttyUSB0"):
self.ser = None
try:
self.ser = serial.Serial(device, 2000000);
except serial.SerialException:
print "Could not open", device
sys.exit(1)
time.sleep(2) # wait until device reset complete
# read EEPROM sensor configuration
self.sensors = []
self.ser.write('r')
for i in range(maxSensors):
self.sensors.append(Sensor())
self.sensors[i].readEEPROM(self.ser)
# read current sensor levels
self.ser.write('l')
for sensor in self.sensors:
sensor.readLevel(self.ser)
self.currentSensor = self.sensors[0]
def __del__(self):
if self.ser != None: # may be None if constructor threw exception
self.ser.write('w')
self.ser.flush() # serial comm from host to Arduino seems less reliable --> reduce baud rate
time.sleep(.2)
self.ser.baudrate = 115200
for sensor in self.sensors:
sensor.writeEEPROM(self.ser)
self.ser.close()
def setSensor(self, arg):
sensorNumber = int(arg)
if sensorNumber < 0 or sensorNumber >= maxSensors:
print >> sys.stderr, 'illegal sensor number'
sys.exit(1)
self.currentSensor = self.sensors[sensorNumber]
def setNullLevel(self, arg):
if arg == "auto":
arg = self.currentSensor.level - 512
if int(arg) < -512 or int(arg) >= 512:
print >> sys.stderr, 'illegal nullLevel value'
sys.exit(1)
self.currentSensor.nullLevel = int(arg) + 512
def setVolt(self, arg):
self.currentSensor.volt = float(arg)
def setType(self, arg):
if arg in ("acs712-5", "ACS712-5"):
voltPerAmpere = 0.185
elif arg in ("acs712-20", "ACS712-20"):
voltPerAmpere = 0.1
elif arg in ("acs712-30", "ACS712-30"):
voltPerAmpere = 0.66
else:
voltPerAmpere = float(arg)
self.currentSensor.voltPerAmpere = voltPerAmpere
def doPrint(self):
totalWatt = 0.0
for i in range(len(self.sensors)):
sensor = self.sensors[i]
if approximates(sensor.voltPerAmpere, 0.185):
sensorType = "type = ACS712-5"
elif approximates(sensor.voltPerAmpere, 0.1):
sensorType = "type = ACS712-20"
elif approximates(sensor.voltPerAmpere, 0.66):
sensorType = "type = ACS712-30"
else:
sensorType = "voltPerAmpere = " + str(sensor.voltPerAmpere)
if sensor.volt == 0:
print "sensor %u: off" % i
else:
watt = sensor.volt * 2.5 / sensor.voltPerAmpere * (sensor.level - sensor.nullLevel) / 512
totalWatt += watt
print "sensor %u: volt = %f, %s, nullLevel = %d, level = %d, Watt = %f" % (i, sensor.volt, sensorType, sensor.nullLevel - 512, sensor.level - 512, watt)
print "total Watt = %f" % totalWatt
def printHelp():
print "Usage: [-d device] [-s sensor_nr] [-v volatage] [-t sensor_type] [-n null_level] [-s next_sensor_nr] [-o] ... [-p]"
print
print "the order in which the arguments appear is important"
print "-h (--help): print this help message"
print "-d dev (--device=dev): use this device (default: /dev/ttyUSB0)"
print "-s sensor_nr (--sensor=sensor_nr): from now on, configuration parameters apply to this sennsor number"
print "-v volt (--voltage=volt: voltage of the rail being measured"
print "-t sensor_type (--type=sensor_type): use this sensor type (ACS712-5, ACS712-20, ACS712-30, or a number indicating the volt/ampere ratio of the sensor"
print "-n null_level (--nulllevel=null_level): null level to be used for calibration. If no current is flowing, use \"-n auto\""
print "-o (--off): do not use this sensor number"
print "-p (--print): print configuration parameterss and the currently measured sensor values"
def getopts(argv):
eeprom = None
try:
opts, argv = getopt.getopt(argv, "d:hn:ops:t:v:", ["device=", "help", "nulllevel=", "off", "print", "sensor=", "type=", "voltage="])
except getopt.GetoptError:
printHelp()
sys.exit(1)
for opt, arg in opts:
if opt in ("-h", "--help"):
printHelp()
sys.exit()
if opt in ("-d", "--device"):
del eeprom
eeprom = EEPROM(arg)
elif eeprom == None:
eeprom = EEPROM()
if opt in ("-n", "--nullLevel"):
eeprom.setNullLevel(arg)
elif opt in ("-o", "--off"):
eeprom.setVolt(0.0)
elif opt in ("-p", "--print"):
eeprom.doPrint()
elif opt in ("-s", "--sensor"):
eeprom.setSensor(arg)
elif opt in ("-t", "--type"):
eeprom.setType(arg)
elif opt in ("-v", "--volt"):
eeprom.setVolt(arg)
if __name__ == "__main__":
getopts(sys.argv[1:])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment