Changes to integrate ploop

This commit is contained in:
AzureAD\AniketSaha 2025-07-09 13:36:04 +02:00
parent e39002b8e7
commit 743dcb81c3
2 changed files with 21 additions and 92 deletions

View File

@ -67,11 +67,11 @@ class CANBackend:
raise RuntimeError("CAN not connected") raise RuntimeError("CAN not connected")
state_map = { state_map = {
"IDLE": 0, "IDLE": 1,
"PRODUCTION": 1, "PRODUCTION": 2,
"MAINTENANCE": 3, "MAINTENANCE": 8,
"EMERGENCY_STOP": 4, "EMERGENCY_STOP": 9,
"FIRST_START": 5 "FIRST_START": 10
} }
if state not in state_map: if state not in state_map:
@ -79,14 +79,14 @@ class CANBackend:
try: try:
print(f"[DEBUG] Writing state {state_map[state]} to index 0x2024, subindex {pu_number}") print(f"[DEBUG] Writing state {state_map[state]} to index 0x2024, subindex {pu_number}")
self.node.sdo[0x2024][0x01].raw = state_map[state] self.node.sdo[0x2024][pu_number].raw = state_map[state]
# Write the ploop setpoint to its own index/subindex # Write the ploop setpoint to its own index/subindex
print(f"[DEBUG] Writing ploop_setpoint {ploop_setpoint} to index 0x2007, subindex {0x00}") print(f"[DEBUG] Writing ploop_setpoint {ploop_setpoint} to index 0x2007")
self.node.sdo[2007][0x00].raw = int(ploop_setpoint * 100) self.node.sdo[0x2007].raw = int(ploop_setpoint * 100)
except Exception as e: except Exception as e:
print(f"[SDO WRITE ERROR] Failed to write to 0x2024:{pu_number} - {e}") print(f"[SDO WRITE ERROR] Failed to write to 0x2007:{e}")
raise raise

93
main.py
View File

@ -6,10 +6,8 @@ import os
from fastapi import Request, APIRouter from fastapi import Request, APIRouter
import subprocess import subprocess
import platform import platform
if platform.system() in ['Darwin']: # macOS or Windows
from MockCAN import CANBackend from classCAN import CANBackend # Your real backend
else :
from classCAN import CANBackend # Your real backend
app = FastAPI() app = FastAPI()
router = APIRouter() router = APIRouter()
@ -52,6 +50,7 @@ def connect_toggle():
node_id=0x02, node_id=0x02,
eds_path = os.path.join(os.path.dirname(__file__), "eds_file", "processBoard_0.eds") eds_path = os.path.join(os.path.dirname(__file__), "eds_file", "processBoard_0.eds")
) )
if not success: if not success:
raise HTTPException(status_code=500, detail="Connection failed.") raise HTTPException(status_code=500, detail="Connection failed.")
return {"connected": True} return {"connected": True}
@ -59,87 +58,25 @@ def connect_toggle():
@app.post("/command/{state}/pu/{pu_number}") @app.post("/command/{state}/pu/{pu_number}")
def send_command(state: str, pu_number: int, ploop_setpoint: float = Query(...)): def send_command(state: str, pu_number: int, ploop_setpoint: float = Query(...)):
"""Send a state command to a specific PU.""" """Send a state command to a specific PU."""
logging.info(f"Sending state '{state}' to PU {pu_number} with ploop_setpoint {ploop_setpoint}") logging.info(f"Sending state '{state}' to PU {pu_number}")
try: try:
can_backend.send_state_command(state.upper(), pu_number, ploop_setpoint) can_backend.send_state_command(state.upper(), pu_number, ploop_setpoint)
return {"status": "success", "command": state.upper(), "pu": pu_number, "ploop_setpoint": ploop_setpoint} return {"status": "success", "command": state.upper(), "pu": pu_number, "ploop_setpoint": ploop_setpoint}
except Exception as e: except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) raise HTTPException(status_code=500, detail=str(e))
@app.get("/monitor") @app.get("/monitor")
def get_monitor_data(): def get_monitor_data():
# TODO : agree on the structure
data = can_backend.get_latest_data() data = can_backend.get_latest_data()
print(f"[MONITOR] Latest SDO: {data}") print(f"[MONITOR] Latest SDO: {data}")
return { return {
"PU_1": { "Qperm": [data.get("FM2", 0.0), data.get("FM2", 0.0), 0.0],
"Qperm": data.get("FM2", 0.0), "Pdilute": [data.get("PS1", 0.0), 0.0, 0.0],
"Qdilute": data.get("FM3", 0.0), "Conductivity": [0.0, 0.0, 0.0],
"Qdrain": data.get("FM4", 0.0), "Pro": [0.0, 0.0, 0.0],
"Qrecirc": data.get("FM5", 0.0),
"Pro": data.get("PS1", 0.0),
"Pdilute": data.get("PS2", 0.0),
"Prentate": data.get("PS3", 0.0),
"Conductivity": data.get("Cond", 0.0),
"MV02": data.get("MV02", 0.0),
"MV02_sp": data.get("MV02_sp", 0.0),
"MV03": data.get("MV03", 0.0),
"MV03_sp": data.get("MV03_sp", 0.0),
"MV05": data.get("MV05", 0.0),
"MV05_sp": data.get("MV05_sp", 0.0),
"MV06": data.get("MV06", 0.0),
"MV06_sp": data.get("MV06_sp", 0.0),
"MV07": data.get("MV07", 0.0),
"MV07_sp": data.get("MV07_sp", 0.0),
"MV08": data.get("MV08", 0.0),
"MV08_sp": data.get("MV08_sp", 0.0)
},
"PU_2": {
"Qperm": data.get("FM2", 0.0),
"Qdilute": data.get("FM3", 0.0),
"Qdrain": data.get("FM4", 0.0),
"Qrecirc": data.get("FM5", 0.0),
"Pro": data.get("PS1", 0.0),
"Pdilute": data.get("PS2", 0.0),
"Prentate": data.get("PS3", 0.0),
"Conductivity": data.get("Cond", 0.0),
"MV02": data.get("MV02", 0.0),
"MV02_sp": data.get("MV02_sp", 0.0),
"MV03": data.get("MV03", 0.0),
"MV03_sp": data.get("MV03_sp", 0.0),
"MV05": data.get("MV05", 0.0),
"MV05_sp": data.get("MV05_sp", 0.0),
"MV06": data.get("MV06", 0.0),
"MV06_sp": data.get("MV06_sp", 0.0),
"MV07": data.get("MV07", 0.0),
"MV07_sp": data.get("MV07_sp", 0.0),
"MV08": data.get("MV08", 0.0),
"MV08_sp": data.get("MV08_sp", 0.0)
},
"PU_3": {
"Qperm": data.get("FM2", 0.0),
"Qdilute": data.get("FM3", 0.0),
"Qdrain": data.get("FM4", 0.0),
"Qrecirc": data.get("FM5", 0.0),
"Pro": data.get("PS1", 0.0),
"Pdilute": data.get("PS2", 0.0),
"Prentate": data.get("PS3", 0.0),
"Conductivity": data.get("Cond", 0.0),
"MV02": data.get("MV02", 0.0),
"MV02_sp": data.get("MV02_sp", 0.0),
"MV03": data.get("MV03", 0.0),
"MV03_sp": data.get("MV03_sp", 0.0),
"MV05": data.get("MV05", 0.0),
"MV05_sp": data.get("MV05_sp", 0.0),
"MV06": data.get("MV06", 0.0),
"MV06_sp": data.get("MV06_sp", 0.0),
"MV07": data.get("MV07", 0.0),
"MV07_sp": data.get("MV07_sp", 0.0),
"MV08": data.get("MV08", 0.0),
"MV08_sp": data.get("MV08_sp", 0.0)
}
} }
@app.get("/can_status") @app.get("/can_status")
def can_status(): def can_status():
"""Return current CAN connection status.""" """Return current CAN connection status."""
@ -151,14 +88,6 @@ async def read_root():
with open("static/index.html", "r") as file: with open("static/index.html", "r") as file:
return HTMLResponse(content=file.read(), status_code=200) return HTMLResponse(content=file.read(), status_code=200)
@app.get("/monitor-page", response_class=HTMLResponse)
async def read_monitor_page():
"""Serve monitor HTML page."""
with open("static/monitor.html", "r") as file:
return HTMLResponse(content=file.read(), status_code=200)
app.include_router(router) app.include_router(router)
if __name__ == "__main__": if __name__ == "__main__":
@ -166,7 +95,7 @@ if __name__ == "__main__":
uvicorn.run( uvicorn.run(
"main:app", "main:app",
host="127.0.0.1", host="127.0.0.1",
port=8000, port=8080,
reload=True, reload=True,
reload_dirs=["."], reload_dirs=["."],
) )