Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement handling of e-stops #34

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### 🚀 New

* [#33](https://github.com/sdss/lvmecp/pull/33) Add a mode to close the dome using only drive overcurrent to determine when the movement has completed.
* [#34](https://github.com/sdss/lvmecp/pull/34) Implement emergency stop command, resetting e-stops, and prevent dome from opening if safety flags are active.

### ✨ Improved

Expand Down
29 changes: 29 additions & 0 deletions python/lvmecp/actor/commands/e_stop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author: José Sánchez-Gallego ([email protected])
# @Date: 2025-01-07
# @Filename: e-stop.py
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)

from __future__ import annotations

import asyncio

from typing import TYPE_CHECKING

from . import parser


if TYPE_CHECKING:
from lvmecp.actor import ECPCommand


@parser.command()
async def emergency_stop(command: ECPCommand):
"""Trigger and emergency stop."""

await command.actor.plc.safety.emergency_stop()
await asyncio.sleep(0.1)

return command.finish(text="Emergency stop triggered.")
9 changes: 9 additions & 0 deletions python/lvmecp/actor/commands/engineering.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,12 @@ async def status(command: ECPCommand):
"""Returns the status of the engineering mode."""

return command.finish(engineering_mode=await get_eng_mode_status(command.actor))


@engineering_mode.command()
async def reset_e_stops(command: ECPCommand):
"""Resets the e-stop relays."""

await command.actor.plc.safety.reset_e_stops()

return command.finish()
5 changes: 4 additions & 1 deletion python/lvmecp/dome.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from lvmecp import config, log
from lvmecp.exceptions import DomeError
from lvmecp.maskbits import DomeStatus
from lvmecp.maskbits import DomeStatus, SafetyStatus
from lvmecp.module import PLCModule


Expand Down Expand Up @@ -102,6 +102,9 @@ async def _move(
if not (await self.plc.safety.is_remote()):
raise DomeError("Cannot move dome while in local mode.")

if not self.plc.safety.status or self.plc.safety.status & SafetyStatus.E_STOP:
raise DomeError("E-stops are pressed.")

if mode == "overcurrent" and open:
raise DomeError("Cannot open dome in overcurrent mode.")

Expand Down
2 changes: 2 additions & 0 deletions python/lvmecp/etc/lvmecp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ simulator:
door_locked: true
door_closed: true
dome_closed: true
oxygen_read_utilities_room: 200
oxygen_read_spectrograph_room: 200
events:
ur_new:
on_value: 1
Expand Down
10 changes: 10 additions & 0 deletions python/lvmecp/safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,13 @@ async def is_remote(self):
assert self.status is not None and self.flag is not None

return not (self.status & self.flag.LOCAL)

async def emergency_stop(self):
"""Triggers an emergency stop."""

await self.plc.modbus["e_stop"].write(True)

async def reset_e_stops(self):
"""Resets the E-stop relays."""

await self.plc.modbus["e_relay_reset"].write(True)
15 changes: 15 additions & 0 deletions tests/test_command_dome.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,18 @@ async def test_dome_close_while_opening(

stop_mock.assert_called()
assert "Stopping the dome before moving to the commanded position" in caplog.text


async def test_dome_open_with_safety_alerts(
actor: ECPActor,
context: ModbusSlaveContext,
mocker: MockerFixture,
):
mocker.patch.object(actor.plc.dome, "is_daytime", return_value=False)
context.setValues(1, actor.plc.modbus["e_status"].address, [1])

cmd = await actor.invoke_mock_command("dome open")
await cmd

assert cmd.status.did_fail
assert "E-stops are pressed" in cmd.replies.get("error")
15 changes: 15 additions & 0 deletions tests/test_command_engineering_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


if TYPE_CHECKING:
from pymodbus.datastore import ModbusSlaveContext
from pytest_mock import MockerFixture

from lvmecp.actor import ECPActor
Expand Down Expand Up @@ -60,3 +61,17 @@ async def test_command_engineering_mode_timeouts(actor: ECPActor):
await asyncio.sleep(0.3)

assert actor.is_engineering_mode_enabled() is False


async def test_command_engineering_mode_reset_e_stops(
actor: ECPActor,
context: ModbusSlaveContext,
):
context.setValues(1, actor.plc.modbus["e_status"].address, [1])

cmd = await actor.invoke_mock_command("engineering-mode reset-e-stops")
await cmd

await asyncio.sleep(0.1)

assert context.getValues(1, actor.plc.modbus["e_status"].address, 1)[0] == 0
Loading