diff --git a/classCAN.py b/classCAN.py index 81fc2ed..bec8cd8 100644 --- a/classCAN.py +++ b/classCAN.py @@ -32,7 +32,6 @@ class CANBackend: 1: 0x02, 2: 0x04, 3: 0x127, - 4: 0xF } for pu_number, node_id in node_map.items(): @@ -166,18 +165,6 @@ class CANBackend: except Exception as e: print(f"[SDO WRITE ERROR] PU{pu_number}: {e}") raise - - def send_command(self, opening :int): - try: - node = self.nodes.get(4) - if node is None: - raise ValueError(f"Feed valve is not connected") - - node.sdo[0x6000].raw = opening - - except Exception as e: - print(f"[SDO WRITE ERROR] Valve Board: {e}") - raise def send_thermal_loop_cleaning(self, mode: str, pu_number: int): if not self.connected: diff --git a/main.py b/main.py index 3197523..865d460 100644 --- a/main.py +++ b/main.py @@ -17,6 +17,7 @@ from typing import Optional, Dict, Any from fastapi import Query import asyncio import datetime +from valveBackend import ValveBackend if platform.system() in ["Darwin"]: # macOS or Windows from MockCAN import CANBackend @@ -29,7 +30,7 @@ router = APIRouter() templates = Jinja2Templates(directory="templates") logging.basicConfig(level=logging.INFO) can_backend = CANBackend() -valve_backend = CANBackend(eds_file="/home/hmi/Desktop/HMI/eds_file/inletvalveboard.eds") +valve_backend = ValveBackend(eds_file="/home/hmi/Desktop/HMI/eds_file/inletvalveboard.eds") # Serve static files (HTML, JS, CSS) app.mount("/static", StaticFiles(directory="static"), name="static") @@ -224,7 +225,7 @@ def can_status(): @app.post("/command/feed_valve") def feedvalve_control(MV01_opening: int = Query(...)): """Control MV01 feed valve""" - valve_backend.send_command(MV01_opening) # TODO: TODO + valve_backend.send_command(MV01_opening) logging.info(f"Feed valve opening to {MV01_opening}") return {"status": "ok"} diff --git a/valveBackend.py b/valveBackend.py new file mode 100644 index 0000000..82c7ef2 --- /dev/null +++ b/valveBackend.py @@ -0,0 +1,30 @@ +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}")