Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 4, 2023
1 parent 80db0f5 commit c7b326a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
6 changes: 3 additions & 3 deletions docs/agents/hi6200.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Hi6200 Agent

This agent uses Modbus TCP to communicate with the Hi6200 Weight Sensor.
This agent uses ModbusClient from pyModbusTCP to facilitate the communication.
The agent is able to communicate over ethernet to read and monitor the net and
The agent is able to communicate over ethernet to read and monitor the net and
gross weights of the scale.

.. argparse::
Expand Down Expand Up @@ -75,6 +75,6 @@ Below is an example client demonstrating full agent functionality.::

# Begin Monitoring Weight
scale.monitor_weight.start()

#Stop Monitoring Weight
scale.stop_monitoring.start()
scale.stop_monitoring.start()
17 changes: 8 additions & 9 deletions socs/agents/hi6200/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import time

from ocs import ocs_agent, site_config
from ocs.ocs_twisted import TimeoutLock
from ocs.ocs_twisted import Pacemaker
from ocs.ocs_twisted import Pacemaker, TimeoutLock

from socs.agents.hi6200.drivers import Hi6200Interface

Expand All @@ -12,15 +11,15 @@ class Hi6200Agent:
"""
Agent to connect to the Hi6200 weight controller that measures the weight
of the LN2 dewar on the SAT platform.
Parameters:
ip_address (string): IP address set on the Hi6200
tcp_port (int): Modbus TCP port of the Hi6200. Default
Default set on the device is 502.
scale (Hi6200Interface): A driver object that allows
scale (Hi6200Interface): A driver object that allows
for communication with the scale.
"""

def __init__(self, agent, ip_address, tcp_port):
self.agent = agent
self.log = agent.log
Expand All @@ -29,7 +28,7 @@ def __init__(self, agent, ip_address, tcp_port):
self.ip_address = ip_address
self.tcp_port = tcp_port
self.scale = None

self.monitor = False

# Registers Scale Output
Expand Down Expand Up @@ -71,10 +70,10 @@ def monitor_weight(self, session, params=None):
"""
session.set_status('running')
self.monitor = True

pm = Pacemaker(1, quantize=True)
while self.monitor:

pm.sleep()
with self.lock.acquire_timeout(1) as acquired:
if acquired:
Expand All @@ -92,7 +91,7 @@ def monitor_weight(self, session, params=None):

except ValueError as e:
self.log.error(f"Scale responded with an anomolous number, ignorning: {e}")

except TypeError as e:
self.log.error(f"Scale responded with 'None' and broke the hex decoding, trying again: {e}")

Expand Down
20 changes: 10 additions & 10 deletions socs/agents/hi6200/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def __init__(self, ip_address, tcp_port, verbose=False, **kwargs):
"""
Connects to the Hi6200 weight sensor using a TCP ModbusClient with pyModbusTCP.
The Modbus Client uses a socket connection to facillitate Modbus communications.
ModbusClient requires an IP address and port to connect.
ModbusClient requires an IP address and port to connect.
ModbusClient will not throw errors upon incorrect ip_address!
ModbusClient auto-reconnects upon socket failure if auto_open is True.
"""

Expand All @@ -26,22 +26,22 @@ def __init__(self, ip_address, tcp_port, verbose=False, **kwargs):
def decode_scale_weight_registers(register_a, register_b):
"""
Decodes the scales weight registers and returns a single weight value (float).
The scale holds both the net and gross weights in permanent holding registers.
Each weight is held across 2 registers in 4 hex bits (2 hex bits/4 bits per register, 8 bits total).
The hex bits must be concatenated and converted to a float.
"""
#Strip the '0x' hex bit
#We must have 8 total bits to convert, so we zfill until each register value is 4 bits
# Strip the '0x' hex bit
# We must have 8 total bits to convert, so we zfill until each register value is 4 bits
hex_a = hex(register_b)[2:].zfill(4)
hex_b = hex(register_b)[2:].zfill(4)
#Concatenate the hex bits in cdab order.

# Concatenate the hex bits in cdab order.
hex_weight = hex_b + hex_a

# This struct function converts the concatenated hex bits to a float.
return struct.unpack('!f', bytes.fromhex(hex_weight))[0]

def read_scale_gross_weight(self):
"""
Returns the current gross weight reading of the scale in the sensors chosen unit (kg)
Expand All @@ -62,4 +62,4 @@ def read_scale_net_weight(self):
# Reading these registers will return an int.
a, b = self.scale.read_holding_registers(6, 2)

return decode_scale_weight_registers(a, b)
return decode_scale_weight_registers(a, b)

0 comments on commit c7b326a

Please sign in to comment.