Commit b1f971be authored by cermak's avatar cermak
Browse files

First implementation of unipi

Change-Id: Ifc07bda2e11b67bfc3ec7a9b958be9a346d6f4f7
parent 190ac683
# -*- coding: utf-8 -*-
# *****************************************************************************
# MLZ library of Tango servers
# Copyright (c) 2015-2021 by the authors, see LICENSE
#
# 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.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Module authors:
# Petr Cermak <cermak@mag.mff.cuni.cz>
#
# *****************************************************************************
from entangle import base
from entangle.core import ON, Prop, subdev, intrange, oneof
from entangle.core.errors import UnrecognizedHardware
from entangle.lib.util import format_statusbits
# Values are valid at least for Neuron S103
REG_AOCONF = 1019 # Analog Output Configuration(U/I/R-measure) 1.1
REG_AICONF = 1024 # Analog Input Configuration(U/I) 1.1
REG_AO = 3000 # Analog Output value 1.1
REG_AI = 3002 # Analog Input value 1.1
REG_FWVER = 1000 # Firmware version
# Analog Input Configuration(U/I)
AI_MODES = {
"U": 0, # Voltage 0 – 10V
"I": 1, # Current 0 – 20mA
}
# Analog Input Configuration(U/I/R)
AI_MODES2 = {
"off": 0, # Off
"U10": 1, # Voltage 0 – 10 V
"U2": 2, # Voltage 0 – 2.5 V
"I": 3, # Current 0 – 20 mA
"R3": 4, # Resistance (three conductors) 0 – 1 960 Ω
"R2": 5, # Resistance (two conductors) 0 – 100 kΩ
}
# Analog Output Configuration(U/I/R-measure)
AO_MODES = {
"U": 0, # Voltage 0 – 10V
"I": 1, # Current 0 – 20mA
"R": 3, # Resistance 0 – 2kΩ
}
BITS_DO = [0,1,2,3] # Digital Outputs 1.1 - 1.4
BITS_DI = [4,5,6,7] # Digital Inputs 1.1 - 1.4
BITS_LED = [8,9,10,11] # User Programmable LEDs 1.1 - 1.4
class UnipiBase(object):
properties = {
'iodev': Prop(subdev, 'Modbus I/O device name.'),
}
iodev_defaults = {
'class': 'Modbus',
}
def init(self):
self._iodev = self.connect_client('iodev', self.iodev, 'Modbus')
self.hw_version = self._iodev.ReadOutputWord(REG_FWVER)
class DiscreteOutput(UnipiBase, base.DiscreteOutput):
"""Device for turning the Digital output ON and OFF."""
properties = {
'output': Prop(intrange(1,4), 'DO port to control.'),
}
def state(self):
return ON, 'idle'
def read_value(self):
return self._iodev.ReadInputBit(BITS_DO[self.output])
def write_value(self, value):
self._iodev.WriteOutputBit([BITS_DO[self.output], value])
def On(self):
self.write_value(1)
def Off(self):
self.write_value(0)
class DiscreteInput(UnipiBase, base.DiscreteInput):
"""Device for reading the Digital input."""
properties = {
'input': Prop(intrange(1,4), 'DI port to read.'),
}
def read_value(self):
return self._iodev.ReadInputBit(BITS_DI[self.input])
class AnalogOutput(UnipiBase, base.AnalogOutput):
"""Device for setting the Analog output."""
properties = {
'mode': Prop(oneof(*AO_MODES), 'Analog output mode (U,I,R)'),
}
def init(self):
UnipiBase.init(self)
# set correct mode
self._iodev.WriteOutputWord(REG_AOCONF, AO_MODES[self.mode])
def read_value(self):
return self._iodev.ReadInputFloat(REG_AO)
def write_value(self, value):
self._iodev.WriteOutputFloat([REG_AO, value])
class Sensor(UnipiBase, base.Sensor):
"""Device for reading the Analog input."""
properties = {
'mode': Prop(oneof(*AI_MODES), 'Analog input mode (U,I)'),
}
def init(self):
UnipiBase.init(self)
# set correct mode
self._iodev.WriteOutputWord(REG_AICONF, AI_MODES[self.mode])
def read_value(self):
return self._iodev.ReadInputFloat(REG_AI)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment