Skip to content

Commit e2a49ed

Browse files
Monoaxeluntzagpre-commit-ci[bot]
authored
Settings (#29)
* add arbitrary settings to config * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * typo elise * } * , * changes from laptop * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * changelog update and addition of landis config file :wq :quit :exit() * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Blaise Thompson <blaise@untzag.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 016a5f1 commit e2a49ed

File tree

6 files changed

+193
-9
lines changed

6 files changed

+193
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
55

66
## [Unreleased]
77

8+
### changed
9+
- ability to set other modbus addresses w/ sensor
10+
### Added
11+
- config file (Landis sensor)
12+
813
## [2022.10.0]
914

1015
### Added
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[daq]
2+
address = "192.168.1.207"
3+
port = 37000
4+
loop_at_startup = true
5+
6+
[[daq.settings]]
7+
comment = "ai0 range"
8+
modbus_address = 40000
9+
modbus_type = "float32"
10+
value = 0.1
11+
12+
[[daq.settings]]
13+
comment = "ai0 differential"
14+
modbus_address = 41000
15+
modbus_type = "uint16"
16+
value = 1
17+
18+
[[daq.settings]]
19+
comment = "ai0 full resolution"
20+
modbus_address = 41500
21+
modbus_type = "uint16"
22+
value = 12
23+
24+
[[daq.settings]]
25+
comment = "temperature range"
26+
modbus_address = 40002
27+
modbus_type = "float32"
28+
value = 0.1
29+
30+
[[daq.settings]]
31+
comment = "ai2 thermocouple type k"
32+
modbus_address = 9004
33+
modbus_type = "uint32"
34+
value = 22
35+
36+
[[daq.settings]]
37+
comment = "ai2 full resolution"
38+
modbus_address = 41504
39+
modbus_type = "uint16"
40+
value = 12
41+
42+
[daq.channels.flux]
43+
modbus_address = 0
44+
modbus_type = "float32"
45+
units = "V"
46+
47+
[daq.channels.temperature]
48+
modbus_address = 7004
49+
modbus_type = "float32"
50+
units = "K"

yaqd_labjack/_bytes.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ def float32_to_data(num):
5454
return data
5555

5656

57+
def type_to_data(type, num):
58+
if type == "uint16":
59+
return uint16_to_data(int(num))
60+
elif type == "uint32":
61+
return uint32_to_data(int(num))
62+
elif type == "int32":
63+
return int32_to_data(num)
64+
elif type == "float32":
65+
return float32_to_data(num)
66+
else:
67+
raise KeyError(f"type {type} not recognized in type_to_data")
68+
69+
5770
"""
5871
Converting data arrays to numbers
5972
"""
@@ -73,3 +86,16 @@ def data_to_int32(data):
7386

7487
def data_to_float32(data):
7588
return struct.unpack(">f", struct.pack(">I", concatData(data)))[0]
89+
90+
91+
def data_to_type(data, type):
92+
if type == "unit16":
93+
return data_to_uint16(data)
94+
elif type == "uint32":
95+
return data_to_uint32(data)
96+
elif type == "int32":
97+
return data_to_int32(data)
98+
elif type == "float32":
99+
return data_to_float32(data)
100+
else:
101+
raise KeyError(f"type {type} not recognized in data_to_type")

yaqd_labjack/_labjack_sensor.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@
1515
class Channel:
1616
name: str
1717
modbus_address: int
18-
range: float
18+
modbus_type: str
19+
units: str
20+
enabled: bool
21+
22+
23+
@dataclass
24+
class Setting:
25+
modbus_address: int
26+
modbus_type: str
27+
value: Any
28+
comment: str
1929
enabled: bool
2030

2131

@@ -24,27 +34,35 @@ class LabjackSensor(HasMeasureTrigger, IsSensor, IsDaemon):
2434

2535
def __init__(self, name, config, config_filepath):
2636
super().__init__(name, config, config_filepath)
37+
print(self._config["channels"])
2738
self._channels = []
2839
for k, d in self._config["channels"].items():
40+
print(d)
2941
channel = Channel(**d, name=k)
3042
self._channels.append(channel)
43+
self._settings = []
44+
for d in self._config["settings"]:
45+
setting = Setting(**d)
46+
self._settings.append(setting)
3147
self._channel_names = [c.name for c in self._channels if c.enabled]
32-
self._channel_units = {k: "V" for k in self._channel_names}
48+
self._channel_units = {
49+
k: self._config["channels"][k]["units"] for k in self._channel_names
50+
}
3351
if self._config["read_device_temperature"]:
3452
self._channel_names.append("device_temperature")
3553
self._channel_units["device_temperature"] = "K"
3654
# hardware configuration
3755
self._client = ModbusTcpClient(self._config["address"])
3856
self._client.connect()
3957
self._client.read_holding_registers(0, 2)
40-
for c in self._channels:
41-
self._client.write_registers(40_000 + c.modbus_address, float32_to_data(c.range))
4258
# id
4359
self.make = "LabJack"
4460
response = self._client.read_holding_registers(address=60000, count=2)
4561
self.model = str(data_to_float32(response.registers))
4662
response = self._client.read_holding_registers(address=60028, count=2)
4763
self.serial = str(data_to_int32(response.registers))
64+
# launch settings setter
65+
self._loop.create_task(self._set_settings())
4866

4967
async def _measure(self):
5068
out = dict()
@@ -59,3 +77,12 @@ async def _measure(self):
5977
if self._looping:
6078
await asyncio.sleep(0.01)
6179
return out
80+
81+
async def _set_settings(self):
82+
while True:
83+
for setting in self._settings:
84+
data = type_to_data(setting.modbus_type, setting.value)
85+
print(setting.modbus_address, data)
86+
self._client.write_registers(setting.modbus_address, data)
87+
await asyncio.sleep(1)
88+
await asyncio.sleep(60)

yaqd_labjack/labjack-sensor.avpr

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@
8080
"null",
8181
"string"
8282
]
83+
},
84+
"settings": {
85+
"default": [],
86+
"doc": "All settings will be written roughly once per minute.",
87+
"items": "setting",
88+
"type": "array"
8389
}
8490
},
8591
"doc": "",
@@ -225,23 +231,72 @@
225231
"is-sensor"
226232
],
227233
"types": [
234+
{
235+
"name": "modbus_type",
236+
"symbols": [
237+
"float32",
238+
"uint16",
239+
"uint32",
240+
"int32"
241+
],
242+
"type": "enum"
243+
},
228244
{
229245
"fields": [
230246
{
231247
"name": "modbus_address",
232248
"type": "int"
233249
},
250+
{
251+
"name": "modbus_type",
252+
"type": "modbus_type"
253+
},
254+
{
255+
"default": null,
256+
"name": "units",
257+
"type": [
258+
"string",
259+
"null"
260+
]
261+
},
234262
{
235263
"default": true,
236264
"name": "enabled",
237265
"type": "boolean"
266+
}
267+
],
268+
"name": "channel",
269+
"type": "record"
270+
},
271+
{
272+
"fields": [
273+
{
274+
"name": "modbus_address",
275+
"type": "int"
276+
},
277+
{
278+
"name": "modbus_type",
279+
"type": "modbus_type"
280+
},
281+
{
282+
"name": "value",
283+
"type": [
284+
"double",
285+
"long"
286+
]
238287
},
239288
{
240-
"name": "range",
241-
"type": "float"
289+
"doc": "comment for human consumption",
290+
"name": "comment",
291+
"type": "string"
292+
},
293+
{
294+
"default": true,
295+
"name": "enabled",
296+
"type": "boolean"
242297
}
243298
],
244-
"name": "channel",
299+
"name": "setting",
245300
"type": "record"
246301
},
247302
{

yaqd_labjack/labjack-sensor.toml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,27 @@ example-configs = "https://github.com/yaq-project/yaqd-labjack/tree/main/example
1111
[installation]
1212
PyPI = "https://pypi.org/project/yaqd-labjack"
1313

14+
[[types]]
15+
type = "enum"
16+
name = "modbus_type"
17+
symbols = ["float32", "uint16", "uint32", "int32"]
18+
1419
[[types]]
1520
type = "record"
1621
name = "channel"
1722
fields = [{"name"="modbus_address", "type"="int"},
18-
{"name"="enabled", "type"="boolean", "default"=true},
19-
{"name"="range", "type"="float"}]
23+
{"name"="modbus_type", "type"="modbus_type"},
24+
{"name"="units", "type"=["string", "null"], "default"="__null__"},
25+
{"name"="enabled", "type"="boolean", "default"=true}]
26+
27+
[[types]]
28+
type = "record"
29+
name = "setting"
30+
fields = [{"name"="modbus_address", "type"="int"},
31+
{"name"="modbus_type", "type"="modbus_type"},
32+
{"name"="value", "type"=["double", "long"]},
33+
{"name"="comment", "type"="string", "doc"="comment for human consumption"},
34+
{"name"="enabled", "type"="boolean", "default"=true}]
2035

2136
[config]
2237

@@ -25,6 +40,12 @@ type = "map"
2540
values = "channel"
2641
default = {}
2742

43+
[config.settings]
44+
type = "array"
45+
items = "setting"
46+
default = []
47+
doc = "All settings will be written roughly once per minute."
48+
2849
[config.address]
2950
type = "string"
3051
default = "192.168.1.207"

0 commit comments

Comments
 (0)