Skip to content

Commit 8a4b875

Browse files
committed
Fixes to get web interface working
1 parent fb30b79 commit 8a4b875

1 file changed

Lines changed: 35 additions & 15 deletions

File tree

mirte_robot/robot.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import os
44
import platform
55
import time
6+
import signal
7+
import sys
68
import weakref
79
from typing import TYPE_CHECKING, Literal, Optional, overload
810

@@ -38,7 +40,17 @@
3840

3941
# No QoS Profiles are set, but this might not be required, since they might already behave like ROS 1 persistant.
4042

43+
def singleton(cls):
44+
instances = {}
4145

46+
def get_instance(*args, **kwargs):
47+
if cls not in instances:
48+
instances[cls] = cls(*args, **kwargs)
49+
return instances[cls]
50+
51+
return get_instance
52+
53+
@singleton
4254
class Robot:
4355
"""Robot API
4456
@@ -60,20 +72,20 @@ def __init__(
6072
machine_namespace (Optional[str], optional): The Namespace from '/' to the ROS namespace for the specific Mirte. Defaults to "/{HOSTNAME}". (This only has to be changed when running the Robot API from a different machine directly. It is configured correctly for the Web interface)
6173
hardware_namespace (str, optional): The namespace for the hardware peripherals. Defaults to "io".
6274
"""
63-
self._machine_namespace = (
64-
machine_namespace
65-
if machine_namespace and validate_namespace(machine_namespace)
66-
else "/" + platform.node().replace("-", "_").lower()
67-
)
68-
self._hardware_namespace = (
69-
hardware_namespace
70-
if validate_namespace(
71-
hardware_namespace
72-
if hardware_namespace.startswith("/")
73-
else (self._machine_namespace + "/" + hardware_namespace)
74-
)
75-
else "io"
76-
)
75+
self._machine_namespace = "" #(
76+
# machine_namespace
77+
# if machine_namespace and validate_namespace(machine_namespace)
78+
# else "/" + platform.node().replace("-", "_").lower()
79+
# )
80+
self._hardware_namespace = "/io" #(
81+
# hardware_namespace
82+
# if validate_namespace(
83+
# hardware_namespace
84+
# if hardware_namespace.startswith("/")
85+
# else (self._machine_namespace + "/" + hardware_namespace)
86+
# )
87+
# else "io"
88+
# )
7789

7890
ROS_DISTRO = os.getenv("ROS_DISTRO")
7991

@@ -129,7 +141,7 @@ def __init__(
129141
)
130142

131143
# Wait for the get_board_characteristics to prevent weird errors.
132-
if not get_board_characteristics.wait_for_service(1):
144+
if not get_board_characteristics.wait_for_service(2):
133145
self._node.get_logger().fatal(
134146
f"Telemetrix node at '{self._node.get_namespace()+ '/'+ self._hardware_namespace + '/telemetrix'}' does not provide a '{self._node.get_namespace() + '/' + self._hardware_namespace + '/get_board_characteristics'}' service! Aborting"
135147
)
@@ -391,6 +403,9 @@ def finalize(node: rclpy.node.Node, motors: dict[str, rclpy.client.Client]):
391403
SetPWMPinValue, self._hardware_namespace + "/set_pwm_pin_value"
392404
)
393405

406+
signal.signal(signal.SIGINT, self._signal_handler)
407+
signal.signal(signal.SIGTERM, self._signal_handler)
408+
394409
def _call_service(
395410
self, client: rclpy.client.Client, request: rclpy.client.SrvTypeRequest
396411
) -> rclpy.client.SrvTypeResponse:
@@ -809,12 +824,17 @@ def stop(self) -> None:
809824
for motor in self.motors:
810825
self.setMotorSpeed(motor, 0)
811826

827+
def _signal_handler(self, sig, frame):
828+
self._at_exit()
829+
812830
def _at_exit(self) -> None:
813831
self.stop()
832+
sys.exit()
814833

815834

816835
# We need a special function to initiate the Robot() because the main.py need to call the
817836
# init_node() (see: https://answers.ros.org/question/266612/rospy-init_node-inside-imported-file/)
837+
# TODO: We probably do not need this anymore in ROS2. But it affects the python imports.
818838
def createRobot(
819839
machine_namespace: Optional[str] = None, hardware_namespace: str = "io"
820840
) -> Robot:

0 commit comments

Comments
 (0)