hil2.component

  1from typing import Callable, Optional
  2
  3from . import can_helper
  4
  5
  6# Shutdownable component interface ----------------------------------------------------#
  7class ShutdownableComponent:
  8    """Interface for components that need to be 'shutdown' when HIL is stopped"""
  9
 10    def shutdown(self) -> None:
 11        raise NotImplementedError()
 12
 13
 14# DO ----------------------------------------------------------------------------------#
 15class DO(ShutdownableComponent):
 16    """Digital Output"""
 17
 18    def __init__(self, set_fn: Callable[[bool], None], hiZ_fn: Callable[[], None]):
 19        """
 20        :param set_fn: Function to set the digital output value
 21        :param hiZ_fn: Function to set the digital output to high impedance (HiZ)
 22        """
 23        self._set_fn: Callable[[bool], None] = set_fn
 24        self._hiZ_fn: Callable[[], None] = hiZ_fn
 25
 26    def set(self, value: bool) -> None:
 27        """
 28        Sets the digital output value.
 29
 30        :param value: The value to set the digital output to (low = false, high = true)
 31        """
 32        self._set_fn(value)
 33
 34    def hiZ(self) -> None:
 35        """
 36        Sets the digital output to high impedance (HiZ) mode.
 37        """
 38        self._hiZ_fn()
 39
 40    def shutdown(self) -> None:
 41        """
 42        Shuts down the digital output by setting it to high impedance (HiZ) mode.
 43        """
 44        self._hiZ_fn()
 45
 46
 47# DI ----------------------------------------------------------------------------------#
 48class DI:
 49    """Digital Input"""
 50
 51    def __init__(self, get_fn: Callable[[], bool]):
 52        """
 53        :param get_fn: Function to get the digital input value
 54        """
 55        self._get_fn: Callable[[], bool] = get_fn
 56
 57    def get(self) -> bool:
 58        """
 59        Gets the digital input value.
 60
 61        :return: The digital input value
 62        """
 63        return self._get_fn()
 64
 65
 66# AO ----------------------------------------------------------------------------------#
 67class AO(ShutdownableComponent):
 68    """Analog Output"""
 69
 70    def __init__(self, set_fn: Callable[[float], None], hiZ_fn: Callable[[], None]):
 71        """
 72        :param set_fn: Function to set the analog output value
 73        :param hiZ_fn: Function to set the analog output to high impedance (HiZ)
 74        """
 75        self._set_fn: Callable[[float], None] = set_fn
 76        self._hiZ_fn: Callable[[], None] = hiZ_fn
 77
 78    def set(self, value: float) -> None:
 79        """
 80        Sets the analog output value.
 81
 82        :param value: The value to set the analog output to in volts
 83        """
 84        self._set_fn(value)
 85
 86    def hiZ(self) -> None:
 87        """
 88        Sets the analog output to high impedance (HiZ) mode.
 89        """
 90        self._hiZ_fn()
 91
 92    def shutdown(self) -> None:
 93        """
 94        Shuts down the analog output by setting it to high impedance (HiZ) mode.
 95        """
 96        self._hiZ_fn()
 97
 98
 99# AI ----------------------------------------------------------------------------------#
100class AI:
101    """Analog Input"""
102
103    def __init__(self, get_fn: Callable[[], float]):
104        """
105        :param get_fn: Function to get the analog input value
106        """
107        self._get_fn: Callable[[], float] = get_fn
108
109    def get(self) -> float:
110        """
111        Gets the analog input value.
112
113        :return: The analog input value in volts.
114        """
115        return self._get_fn()
116
117
118# POT ---------------------------------------------------------------------------------#
119class POT:
120    """Potentiometer"""
121
122    def __init__(self, set_fn: Callable[[float], None]):
123        """
124        :param set_fn: Function to set the potentiometer value
125        """
126        self._set_fn: Callable[[float], None] = set_fn
127
128    def set(self, value: float) -> None:
129        """
130        Sets the potentiometer value.
131
132        :param value: The value to set the potentiometer to in ohms
133        """
134        self._set_fn(value)
135
136
137# CAN ---------------------------------------------------------------------------------#
138class CAN:
139    """CAN Bus Interface"""
140
141    def __init__(
142        self,
143        send_fn: Callable[[str | int, dict], None],
144        get_last_fn: Callable[[Optional[str | int]], Optional[can_helper.CanMessage]],
145        get_all_fn: Callable[[Optional[str | int]], list[can_helper.CanMessage]],
146        clear_fn: Callable[[Optional[str | int]], None],
147    ):
148        """
149        :param send_fn: Function to send CAN messages
150        :param get_last_fn: Function to get the last received CAN message
151        :param get_all_fn: Function to get all received CAN messages
152        :param clear_fn: Function to clear CAN messages
153        """
154        self._send_fn: Callable[[str | int, dict], None] = send_fn
155        self._get_last_fn: Callable[[Optional[str | int]], Optional[dict]] = get_last_fn
156        self._get_all_fn: Callable[[Optional[str | int]], list[dict]] = get_all_fn
157        self._clear_fn: Callable[[Optional[str | int]], None] = clear_fn
158
159    def send(self, signal: str | int, data: dict) -> None:
160        """
161        Sends a CAN message.
162
163        :param signal: The signal identifier or message id
164        :param data: The data to send. Will later be encoded to raw bytes
165        """
166        self._send_fn(signal, data)
167
168    def get_last(
169        self, signal: Optional[str | int] = None
170    ) -> Optional[can_helper.CanMessage]:
171        """
172        Gets the last received CAN message.
173
174        :param signal: The signal identifier or message id. If not specified, the last
175                       message for any signal will be returned.
176        :return: The last received CAN message or None if not found
177        """
178        return self._get_last_fn(signal)
179
180    def get_all(
181        self, signal: Optional[str | int] = None
182    ) -> list[can_helper.CanMessage]:
183        """
184        Gets all received CAN messages.
185
186        :param signal: The signal identifier or message id. If not specified, all
187                       messages for any signal will be returned.
188        :return: A list of all received CAN messages
189        """
190        return self._get_all_fn(signal)
191
192    def clear(self, signal: Optional[str | int] = None) -> None:
193        """
194        Clears the received CAN messages.
195
196        :param signal: The signal identifier or message id. If not specified, all
197                       messages for any signal will be cleared.
198        """
199        self._clear_fn(signal)
class ShutdownableComponent:
 8class ShutdownableComponent:
 9    """Interface for components that need to be 'shutdown' when HIL is stopped"""
10
11    def shutdown(self) -> None:
12        raise NotImplementedError()

Interface for components that need to be 'shutdown' when HIL is stopped

def shutdown(self) -> None:
11    def shutdown(self) -> None:
12        raise NotImplementedError()
class DO(ShutdownableComponent):
16class DO(ShutdownableComponent):
17    """Digital Output"""
18
19    def __init__(self, set_fn: Callable[[bool], None], hiZ_fn: Callable[[], None]):
20        """
21        :param set_fn: Function to set the digital output value
22        :param hiZ_fn: Function to set the digital output to high impedance (HiZ)
23        """
24        self._set_fn: Callable[[bool], None] = set_fn
25        self._hiZ_fn: Callable[[], None] = hiZ_fn
26
27    def set(self, value: bool) -> None:
28        """
29        Sets the digital output value.
30
31        :param value: The value to set the digital output to (low = false, high = true)
32        """
33        self._set_fn(value)
34
35    def hiZ(self) -> None:
36        """
37        Sets the digital output to high impedance (HiZ) mode.
38        """
39        self._hiZ_fn()
40
41    def shutdown(self) -> None:
42        """
43        Shuts down the digital output by setting it to high impedance (HiZ) mode.
44        """
45        self._hiZ_fn()

Digital Output

DO(set_fn: Callable[[bool], NoneType], hiZ_fn: Callable[[], NoneType])
19    def __init__(self, set_fn: Callable[[bool], None], hiZ_fn: Callable[[], None]):
20        """
21        :param set_fn: Function to set the digital output value
22        :param hiZ_fn: Function to set the digital output to high impedance (HiZ)
23        """
24        self._set_fn: Callable[[bool], None] = set_fn
25        self._hiZ_fn: Callable[[], None] = hiZ_fn
Parameters
  • set_fn: Function to set the digital output value
  • hiZ_fn: Function to set the digital output to high impedance (HiZ)
def set(self, value: bool) -> None:
27    def set(self, value: bool) -> None:
28        """
29        Sets the digital output value.
30
31        :param value: The value to set the digital output to (low = false, high = true)
32        """
33        self._set_fn(value)

Sets the digital output value.

Parameters
  • value: The value to set the digital output to (low = false, high = true)
def hiZ(self) -> None:
35    def hiZ(self) -> None:
36        """
37        Sets the digital output to high impedance (HiZ) mode.
38        """
39        self._hiZ_fn()

Sets the digital output to high impedance (HiZ) mode.

def shutdown(self) -> None:
41    def shutdown(self) -> None:
42        """
43        Shuts down the digital output by setting it to high impedance (HiZ) mode.
44        """
45        self._hiZ_fn()

Shuts down the digital output by setting it to high impedance (HiZ) mode.

class DI:
49class DI:
50    """Digital Input"""
51
52    def __init__(self, get_fn: Callable[[], bool]):
53        """
54        :param get_fn: Function to get the digital input value
55        """
56        self._get_fn: Callable[[], bool] = get_fn
57
58    def get(self) -> bool:
59        """
60        Gets the digital input value.
61
62        :return: The digital input value
63        """
64        return self._get_fn()

Digital Input

DI(get_fn: Callable[[], bool])
52    def __init__(self, get_fn: Callable[[], bool]):
53        """
54        :param get_fn: Function to get the digital input value
55        """
56        self._get_fn: Callable[[], bool] = get_fn
Parameters
  • get_fn: Function to get the digital input value
def get(self) -> bool:
58    def get(self) -> bool:
59        """
60        Gets the digital input value.
61
62        :return: The digital input value
63        """
64        return self._get_fn()

Gets the digital input value.

Returns

The digital input value

class AO(ShutdownableComponent):
68class AO(ShutdownableComponent):
69    """Analog Output"""
70
71    def __init__(self, set_fn: Callable[[float], None], hiZ_fn: Callable[[], None]):
72        """
73        :param set_fn: Function to set the analog output value
74        :param hiZ_fn: Function to set the analog output to high impedance (HiZ)
75        """
76        self._set_fn: Callable[[float], None] = set_fn
77        self._hiZ_fn: Callable[[], None] = hiZ_fn
78
79    def set(self, value: float) -> None:
80        """
81        Sets the analog output value.
82
83        :param value: The value to set the analog output to in volts
84        """
85        self._set_fn(value)
86
87    def hiZ(self) -> None:
88        """
89        Sets the analog output to high impedance (HiZ) mode.
90        """
91        self._hiZ_fn()
92
93    def shutdown(self) -> None:
94        """
95        Shuts down the analog output by setting it to high impedance (HiZ) mode.
96        """
97        self._hiZ_fn()

Analog Output

AO(set_fn: Callable[[float], NoneType], hiZ_fn: Callable[[], NoneType])
71    def __init__(self, set_fn: Callable[[float], None], hiZ_fn: Callable[[], None]):
72        """
73        :param set_fn: Function to set the analog output value
74        :param hiZ_fn: Function to set the analog output to high impedance (HiZ)
75        """
76        self._set_fn: Callable[[float], None] = set_fn
77        self._hiZ_fn: Callable[[], None] = hiZ_fn
Parameters
  • set_fn: Function to set the analog output value
  • hiZ_fn: Function to set the analog output to high impedance (HiZ)
def set(self, value: float) -> None:
79    def set(self, value: float) -> None:
80        """
81        Sets the analog output value.
82
83        :param value: The value to set the analog output to in volts
84        """
85        self._set_fn(value)

Sets the analog output value.

Parameters
  • value: The value to set the analog output to in volts
def hiZ(self) -> None:
87    def hiZ(self) -> None:
88        """
89        Sets the analog output to high impedance (HiZ) mode.
90        """
91        self._hiZ_fn()

Sets the analog output to high impedance (HiZ) mode.

def shutdown(self) -> None:
93    def shutdown(self) -> None:
94        """
95        Shuts down the analog output by setting it to high impedance (HiZ) mode.
96        """
97        self._hiZ_fn()

Shuts down the analog output by setting it to high impedance (HiZ) mode.

class AI:
101class AI:
102    """Analog Input"""
103
104    def __init__(self, get_fn: Callable[[], float]):
105        """
106        :param get_fn: Function to get the analog input value
107        """
108        self._get_fn: Callable[[], float] = get_fn
109
110    def get(self) -> float:
111        """
112        Gets the analog input value.
113
114        :return: The analog input value in volts.
115        """
116        return self._get_fn()

Analog Input

AI(get_fn: Callable[[], float])
104    def __init__(self, get_fn: Callable[[], float]):
105        """
106        :param get_fn: Function to get the analog input value
107        """
108        self._get_fn: Callable[[], float] = get_fn
Parameters
  • get_fn: Function to get the analog input value
def get(self) -> float:
110    def get(self) -> float:
111        """
112        Gets the analog input value.
113
114        :return: The analog input value in volts.
115        """
116        return self._get_fn()

Gets the analog input value.

Returns

The analog input value in volts.

class POT:
120class POT:
121    """Potentiometer"""
122
123    def __init__(self, set_fn: Callable[[float], None]):
124        """
125        :param set_fn: Function to set the potentiometer value
126        """
127        self._set_fn: Callable[[float], None] = set_fn
128
129    def set(self, value: float) -> None:
130        """
131        Sets the potentiometer value.
132
133        :param value: The value to set the potentiometer to in ohms
134        """
135        self._set_fn(value)

Potentiometer

POT(set_fn: Callable[[float], NoneType])
123    def __init__(self, set_fn: Callable[[float], None]):
124        """
125        :param set_fn: Function to set the potentiometer value
126        """
127        self._set_fn: Callable[[float], None] = set_fn
Parameters
  • set_fn: Function to set the potentiometer value
def set(self, value: float) -> None:
129    def set(self, value: float) -> None:
130        """
131        Sets the potentiometer value.
132
133        :param value: The value to set the potentiometer to in ohms
134        """
135        self._set_fn(value)

Sets the potentiometer value.

Parameters
  • value: The value to set the potentiometer to in ohms
class CAN:
139class CAN:
140    """CAN Bus Interface"""
141
142    def __init__(
143        self,
144        send_fn: Callable[[str | int, dict], None],
145        get_last_fn: Callable[[Optional[str | int]], Optional[can_helper.CanMessage]],
146        get_all_fn: Callable[[Optional[str | int]], list[can_helper.CanMessage]],
147        clear_fn: Callable[[Optional[str | int]], None],
148    ):
149        """
150        :param send_fn: Function to send CAN messages
151        :param get_last_fn: Function to get the last received CAN message
152        :param get_all_fn: Function to get all received CAN messages
153        :param clear_fn: Function to clear CAN messages
154        """
155        self._send_fn: Callable[[str | int, dict], None] = send_fn
156        self._get_last_fn: Callable[[Optional[str | int]], Optional[dict]] = get_last_fn
157        self._get_all_fn: Callable[[Optional[str | int]], list[dict]] = get_all_fn
158        self._clear_fn: Callable[[Optional[str | int]], None] = clear_fn
159
160    def send(self, signal: str | int, data: dict) -> None:
161        """
162        Sends a CAN message.
163
164        :param signal: The signal identifier or message id
165        :param data: The data to send. Will later be encoded to raw bytes
166        """
167        self._send_fn(signal, data)
168
169    def get_last(
170        self, signal: Optional[str | int] = None
171    ) -> Optional[can_helper.CanMessage]:
172        """
173        Gets the last received CAN message.
174
175        :param signal: The signal identifier or message id. If not specified, the last
176                       message for any signal will be returned.
177        :return: The last received CAN message or None if not found
178        """
179        return self._get_last_fn(signal)
180
181    def get_all(
182        self, signal: Optional[str | int] = None
183    ) -> list[can_helper.CanMessage]:
184        """
185        Gets all received CAN messages.
186
187        :param signal: The signal identifier or message id. If not specified, all
188                       messages for any signal will be returned.
189        :return: A list of all received CAN messages
190        """
191        return self._get_all_fn(signal)
192
193    def clear(self, signal: Optional[str | int] = None) -> None:
194        """
195        Clears the received CAN messages.
196
197        :param signal: The signal identifier or message id. If not specified, all
198                       messages for any signal will be cleared.
199        """
200        self._clear_fn(signal)

CAN Bus Interface

CAN( send_fn: Callable[[str | int, dict], NoneType], get_last_fn: Callable[[Union[str, int, NoneType]], Optional[hil2.can_helper.CanMessage]], get_all_fn: Callable[[Union[str, int, NoneType]], list[hil2.can_helper.CanMessage]], clear_fn: Callable[[Union[str, int, NoneType]], NoneType])
142    def __init__(
143        self,
144        send_fn: Callable[[str | int, dict], None],
145        get_last_fn: Callable[[Optional[str | int]], Optional[can_helper.CanMessage]],
146        get_all_fn: Callable[[Optional[str | int]], list[can_helper.CanMessage]],
147        clear_fn: Callable[[Optional[str | int]], None],
148    ):
149        """
150        :param send_fn: Function to send CAN messages
151        :param get_last_fn: Function to get the last received CAN message
152        :param get_all_fn: Function to get all received CAN messages
153        :param clear_fn: Function to clear CAN messages
154        """
155        self._send_fn: Callable[[str | int, dict], None] = send_fn
156        self._get_last_fn: Callable[[Optional[str | int]], Optional[dict]] = get_last_fn
157        self._get_all_fn: Callable[[Optional[str | int]], list[dict]] = get_all_fn
158        self._clear_fn: Callable[[Optional[str | int]], None] = clear_fn
Parameters
  • send_fn: Function to send CAN messages
  • get_last_fn: Function to get the last received CAN message
  • get_all_fn: Function to get all received CAN messages
  • clear_fn: Function to clear CAN messages
def send(self, signal: str | int, data: dict) -> None:
160    def send(self, signal: str | int, data: dict) -> None:
161        """
162        Sends a CAN message.
163
164        :param signal: The signal identifier or message id
165        :param data: The data to send. Will later be encoded to raw bytes
166        """
167        self._send_fn(signal, data)

Sends a CAN message.

Parameters
  • signal: The signal identifier or message id
  • data: The data to send. Will later be encoded to raw bytes
def get_last( self, signal: Union[str, int, NoneType] = None) -> Optional[hil2.can_helper.CanMessage]:
169    def get_last(
170        self, signal: Optional[str | int] = None
171    ) -> Optional[can_helper.CanMessage]:
172        """
173        Gets the last received CAN message.
174
175        :param signal: The signal identifier or message id. If not specified, the last
176                       message for any signal will be returned.
177        :return: The last received CAN message or None if not found
178        """
179        return self._get_last_fn(signal)

Gets the last received CAN message.

Parameters
  • signal: The signal identifier or message id. If not specified, the last message for any signal will be returned.
Returns

The last received CAN message or None if not found

def get_all( self, signal: Union[str, int, NoneType] = None) -> list[hil2.can_helper.CanMessage]:
181    def get_all(
182        self, signal: Optional[str | int] = None
183    ) -> list[can_helper.CanMessage]:
184        """
185        Gets all received CAN messages.
186
187        :param signal: The signal identifier or message id. If not specified, all
188                       messages for any signal will be returned.
189        :return: A list of all received CAN messages
190        """
191        return self._get_all_fn(signal)

Gets all received CAN messages.

Parameters
  • signal: The signal identifier or message id. If not specified, all messages for any signal will be returned.
Returns

A list of all received CAN messages

def clear(self, signal: Union[str, int, NoneType] = None) -> None:
193    def clear(self, signal: Optional[str | int] = None) -> None:
194        """
195        Clears the received CAN messages.
196
197        :param signal: The signal identifier or message id. If not specified, all
198                       messages for any signal will be cleared.
199        """
200        self._clear_fn(signal)

Clears the received CAN messages.

Parameters
  • signal: The signal identifier or message id. If not specified, all messages for any signal will be cleared.