NorthStar-HMI/valveBackend.py

31 lines
954 B
Python

import canopen
import os
class ValveBackend:
def __init__(self, eds_file: str, node_id: int = 0x0F):
self.eds_file = eds_file
self.node_id = node_id
self.network = None
self.node = None
def connect(self):
try:
self.network = canopen.Network()
self.network.connect(channel='can0', bustype='socketcan')
self.node = canopen.RemoteNode(self.node_id, self.eds_file)
self.network.add_node(self.node)
return True
except Exception as e:
print(f"[VALVE CONNECT ERROR] {e}")
return False
def send_command(self, opening: int):
try:
if self.node is None:
raise RuntimeError("Valve node not initialized")
self.node.sdo[0x6000].raw = opening
print(f"[VALVE] Opening set to {opening}")
except Exception as e:
print(f"[VALVE CMD ERROR] {e}")