hilsim 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hilsim-0.1.0/LICENSE +9 -0
- hilsim-0.1.0/PKG-INFO +67 -0
- hilsim-0.1.0/README.md +40 -0
- hilsim-0.1.0/pyproject.toml +32 -0
- hilsim-0.1.0/setup.cfg +4 -0
- hilsim-0.1.0/src/hilsim/__init__.py +0 -0
- hilsim-0.1.0/src/hilsim/cli/commands.py +20 -0
- hilsim-0.1.0/src/hilsim/core/__init__.py +0 -0
- hilsim-0.1.0/src/hilsim/core/actuators.py +150 -0
- hilsim-0.1.0/src/hilsim/core/builders.py +115 -0
- hilsim-0.1.0/src/hilsim/core/controls.py +351 -0
- hilsim-0.1.0/src/hilsim/core/device_exceptions.py +8 -0
- hilsim-0.1.0/src/hilsim/core/entry.py +145 -0
- hilsim-0.1.0/src/hilsim/core/file_manager.py +113 -0
- hilsim-0.1.0/src/hilsim/core/i2c_bus.py +61 -0
- hilsim-0.1.0/src/hilsim/core/i2c_exceptions.py +10 -0
- hilsim-0.1.0/src/hilsim/core/load_toml.py +14 -0
- hilsim-0.1.0/src/hilsim/core/sensor_exceptions.py +17 -0
- hilsim-0.1.0/src/hilsim/core/sensor_manager.py +128 -0
- hilsim-0.1.0/src/hilsim/core/sensor_tracker.py +124 -0
- hilsim-0.1.0/src/hilsim/core/sensors.py +170 -0
- hilsim-0.1.0/src/hilsim/core/world_state.py +44 -0
- hilsim-0.1.0/src/hilsim/scaffold/__init__.py +0 -0
- hilsim-0.1.0/src/hilsim/scaffold/engine.py +26 -0
- hilsim-0.1.0/src/hilsim/scaffold/templates/actuator_config.toml.jinja +126 -0
- hilsim-0.1.0/src/hilsim/scaffold/templates/actuators/actuator_template.py.jinja +60 -0
- hilsim-0.1.0/src/hilsim/scaffold/templates/device_config.toml.jinja +109 -0
- hilsim-0.1.0/src/hilsim/scaffold/templates/main.py.jinja +61 -0
- hilsim-0.1.0/src/hilsim/scaffold/templates/sensor_config.toml.jinja +122 -0
- hilsim-0.1.0/src/hilsim/scaffold/templates/sensors/sensor_template.py.jinja +157 -0
- hilsim-0.1.0/src/hilsim/scaffold/templates/world_state.toml.jinja +15 -0
- hilsim-0.1.0/src/hilsim.egg-info/PKG-INFO +67 -0
- hilsim-0.1.0/src/hilsim.egg-info/SOURCES.txt +35 -0
- hilsim-0.1.0/src/hilsim.egg-info/dependency_links.txt +1 -0
- hilsim-0.1.0/src/hilsim.egg-info/entry_points.txt +2 -0
- hilsim-0.1.0/src/hilsim.egg-info/requires.txt +11 -0
- hilsim-0.1.0/src/hilsim.egg-info/top_level.txt +1 -0
hilsim-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Clint Carafelli
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
hilsim-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hilsim
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Hardware-in-the-Loop / Software-in-the-Loop simulation framework
|
|
5
|
+
Author: Clint Carafelli
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Clint Carafelli
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: click
|
|
19
|
+
Requires-Dist: jinja2
|
|
20
|
+
Requires-Dist: tomllib; python_version < "3.11"
|
|
21
|
+
Requires-Dist: pyserial
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: pytest; extra == "dev"
|
|
24
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
25
|
+
Requires-Dist: rich; extra == "dev"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# hilsim
|
|
29
|
+
A Python package for rapid hardware-in-the-loop (HIL) and software-in-the-loop (SIL) simulation,
|
|
30
|
+
designed for software/hardware testing, deployment, and development.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
pip install hilsim
|
|
34
|
+
|
|
35
|
+
## Quick Start
|
|
36
|
+
See the [examples](examples/) folder for a host of configuration files, sensors, and actuators.
|
|
37
|
+
|
|
38
|
+
## How It Works
|
|
39
|
+
hilsim generates a project structure where the developer has four main tasks:
|
|
40
|
+
1) fill out configuration files
|
|
41
|
+
2) Writes basic drivers for sensors based on a template.
|
|
42
|
+
3) Describe the physics/behavior of actuators (anything that changes the state of the world), also based on a template.
|
|
43
|
+
4) Write the main logic.
|
|
44
|
+
|
|
45
|
+
A simple run command then simulates the entire system. Switching the "sim" keyword for either a sensor, device, or actuator allows the user to rapidly switch between simulating a component and deploying the actual hardware. No need to worry about any middleware or fatal exceptions in the background.
|
|
46
|
+
|
|
47
|
+
## Features
|
|
48
|
+
- Data recording.
|
|
49
|
+
- User defined event driven log file management with a built-in detailed message logging system.
|
|
50
|
+
- Standardized serial protocol with built-in recovery.
|
|
51
|
+
- High-level i2c bus interactions with built-in recovery.
|
|
52
|
+
- Simulate latency between the host and peripheral device communication and actual hardware deployment.
|
|
53
|
+
- Simulate sensor errors/failures.
|
|
54
|
+
- Simulate noise and drift in sensors.
|
|
55
|
+
- Simulate host-peripheral device communication failures.
|
|
56
|
+
- Simulate hardware failures.
|
|
57
|
+
- Background dynamics simulation. Background dynamics run on a high-speed background thread (on the ms level) which sensors may routinely pull from, mirroring the continuous nature of system dynamics and separating them from the discrete nature of sensor reads.
|
|
58
|
+
|
|
59
|
+
## Current Limitations
|
|
60
|
+
Currently, the "supported" i2c_bus wraps the board and busio libraries, allowing hilsim to accommodate a wide range of readily-available/cheap sensors from manufacturers like Adafruit. A future release will include support for the smbus library, allowing the the use of custom sensors / i2c devices at a lower level.
|
|
61
|
+
|
|
62
|
+
## Roadmap
|
|
63
|
+
- Add detailed examples and walkthroughs.
|
|
64
|
+
- Support for the smbus library.
|
|
65
|
+
- Rapid power cycling / software cycling integration. The current release flags errors that the developer can accommodate in main, but does not have the capability to rapidly integrate software cycling or power cycling.
|
|
66
|
+
- SPI support
|
|
67
|
+
- Virtual serial ports with socat, ideal for testing microcontroller code alongside the host.
|
hilsim-0.1.0/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# hilsim
|
|
2
|
+
A Python package for rapid hardware-in-the-loop (HIL) and software-in-the-loop (SIL) simulation,
|
|
3
|
+
designed for software/hardware testing, deployment, and development.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
pip install hilsim
|
|
7
|
+
|
|
8
|
+
## Quick Start
|
|
9
|
+
See the [examples](examples/) folder for a host of configuration files, sensors, and actuators.
|
|
10
|
+
|
|
11
|
+
## How It Works
|
|
12
|
+
hilsim generates a project structure where the developer has four main tasks:
|
|
13
|
+
1) fill out configuration files
|
|
14
|
+
2) Writes basic drivers for sensors based on a template.
|
|
15
|
+
3) Describe the physics/behavior of actuators (anything that changes the state of the world), also based on a template.
|
|
16
|
+
4) Write the main logic.
|
|
17
|
+
|
|
18
|
+
A simple run command then simulates the entire system. Switching the "sim" keyword for either a sensor, device, or actuator allows the user to rapidly switch between simulating a component and deploying the actual hardware. No need to worry about any middleware or fatal exceptions in the background.
|
|
19
|
+
|
|
20
|
+
## Features
|
|
21
|
+
- Data recording.
|
|
22
|
+
- User defined event driven log file management with a built-in detailed message logging system.
|
|
23
|
+
- Standardized serial protocol with built-in recovery.
|
|
24
|
+
- High-level i2c bus interactions with built-in recovery.
|
|
25
|
+
- Simulate latency between the host and peripheral device communication and actual hardware deployment.
|
|
26
|
+
- Simulate sensor errors/failures.
|
|
27
|
+
- Simulate noise and drift in sensors.
|
|
28
|
+
- Simulate host-peripheral device communication failures.
|
|
29
|
+
- Simulate hardware failures.
|
|
30
|
+
- Background dynamics simulation. Background dynamics run on a high-speed background thread (on the ms level) which sensors may routinely pull from, mirroring the continuous nature of system dynamics and separating them from the discrete nature of sensor reads.
|
|
31
|
+
|
|
32
|
+
## Current Limitations
|
|
33
|
+
Currently, the "supported" i2c_bus wraps the board and busio libraries, allowing hilsim to accommodate a wide range of readily-available/cheap sensors from manufacturers like Adafruit. A future release will include support for the smbus library, allowing the the use of custom sensors / i2c devices at a lower level.
|
|
34
|
+
|
|
35
|
+
## Roadmap
|
|
36
|
+
- Add detailed examples and walkthroughs.
|
|
37
|
+
- Support for the smbus library.
|
|
38
|
+
- Rapid power cycling / software cycling integration. The current release flags errors that the developer can accommodate in main, but does not have the capability to rapidly integrate software cycling or power cycling.
|
|
39
|
+
- SPI support
|
|
40
|
+
- Virtual serial ports with socat, ideal for testing microcontroller code alongside the host.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "hilsim"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A Hardware-in-the-Loop / Software-in-the-Loop simulation framework"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = {file = "LICENSE"}
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Clint Carafelli"}
|
|
14
|
+
]
|
|
15
|
+
dependencies = [
|
|
16
|
+
"click",
|
|
17
|
+
"jinja2",
|
|
18
|
+
"tomllib; python_version < '3.11'",
|
|
19
|
+
"pyserial",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
hilsim = "hilsim.cli.commands:cli"
|
|
24
|
+
|
|
25
|
+
[tool.setuptools.packages.find]
|
|
26
|
+
where = ["src"]
|
|
27
|
+
|
|
28
|
+
[tool.setuptools.package-data]
|
|
29
|
+
"hilsim" = ["scaffold/templates/**/*"]
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
dev = ["pytest", "pytest-cov", "rich"]
|
hilsim-0.1.0/setup.cfg
ADDED
|
File without changes
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import click
|
|
2
|
+
from hilsim.scaffold.engine import scaffold_project
|
|
3
|
+
|
|
4
|
+
@click.group()
|
|
5
|
+
def cli():
|
|
6
|
+
"""hilsim: hardware-in-the-loop simulation framework"""
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
@cli.command()
|
|
10
|
+
@click.argument("project_name")
|
|
11
|
+
def create(project_name):
|
|
12
|
+
"""Scaffold a new hilsim project in the current directory"""
|
|
13
|
+
click.echo(f"Creating project '{project_name}'...")
|
|
14
|
+
try:
|
|
15
|
+
scaffold_project(project_name)
|
|
16
|
+
click.echo(f"Project '{project_name}' created successfully")
|
|
17
|
+
except FileExistsError:
|
|
18
|
+
"""Note: engine won't create a project that already exists
|
|
19
|
+
(i.e. has the same name)"""
|
|
20
|
+
click.echo(f"Error: directory '{project_name}' already exists", err=True)
|
|
File without changes
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""Define a world state class that serves as truth for all system variables"""
|
|
2
|
+
|
|
3
|
+
import inspect
|
|
4
|
+
import logging
|
|
5
|
+
from random import gauss, random, uniform
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from hilsim.core.controls import Controls
|
|
9
|
+
from hilsim.core.world_state import WorldState
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
#---------------------------------------------------------------------------------------
|
|
13
|
+
class ActuatorBase:
|
|
14
|
+
"""Actuator Base Class to enforce actuator class standards"""
|
|
15
|
+
|
|
16
|
+
registry: dict[str, Any] = {}
|
|
17
|
+
|
|
18
|
+
def __init_subclass__(cls, **kwargs):
|
|
19
|
+
super().__init_subclass__(**kwargs)
|
|
20
|
+
ActuatorBase.registry[cls.__name__] = cls
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self, actuator_config: dict, world_state: WorldState, controller: Controls
|
|
24
|
+
) -> None:
|
|
25
|
+
self.dt: float = 0.0
|
|
26
|
+
self.cst: float = 0.0
|
|
27
|
+
self.time = world_state.state["time"]
|
|
28
|
+
self.world_state = world_state
|
|
29
|
+
self.failure_sim = actuator_config["failure_sim"]
|
|
30
|
+
self.settings = actuator_config["settings"]
|
|
31
|
+
self.effects = actuator_config["effects"]
|
|
32
|
+
self.sim = actuator_config["settings"]["sim"]
|
|
33
|
+
self.commanded_state = actuator_config["settings"]["initial_state"]
|
|
34
|
+
self.hardware_state = actuator_config["settings"]["initial_state"]
|
|
35
|
+
self.name = actuator_config["settings"]["name"]
|
|
36
|
+
self._stuck_counter: int = 0
|
|
37
|
+
self._transition_state: str = "settled"
|
|
38
|
+
self._stuck: bool = False
|
|
39
|
+
self._latency_remaining: float = 0.0
|
|
40
|
+
self._discover_effects()
|
|
41
|
+
self._controller = controller
|
|
42
|
+
|
|
43
|
+
def send_command(self, target_state: str, advanced_connection: bool = True) -> dict[str,bool]:
|
|
44
|
+
"""Send command to microcontroller for target_state"""
|
|
45
|
+
device_name = self.settings["device"]
|
|
46
|
+
logger.info("Command triggered for %s, sending command to %s", self.name, device_name)
|
|
47
|
+
logger.info("Command has target_state: %s", target_state)
|
|
48
|
+
package = self.build_command(target_state)
|
|
49
|
+
if advanced_connection:
|
|
50
|
+
confirmation = self._controller.advanced_send(device_name, package)
|
|
51
|
+
else:
|
|
52
|
+
confirmation = self._controller.send(device_name, package)
|
|
53
|
+
|
|
54
|
+
self._update_commanded_state(confirmation, target_state)
|
|
55
|
+
return confirmation
|
|
56
|
+
|
|
57
|
+
def _update_commanded_state(self, confirmation: dict[str, bool], commanded_state: str) -> None:
|
|
58
|
+
"""Update the commanded state attribute"""
|
|
59
|
+
if confirmation["confirmed"]:
|
|
60
|
+
self.commanded_state = commanded_state
|
|
61
|
+
self._transition_state = "commanded"
|
|
62
|
+
|
|
63
|
+
def _resolve_state_transition(self) -> None:
|
|
64
|
+
"""Determine latency and encode failure mode of hardware (i.e. actuator is stuck)"""
|
|
65
|
+
if self._stuck and self._stuck_counter == 0:
|
|
66
|
+
self._stuck_counter += 1
|
|
67
|
+
logger.critical("%s is stuck in state %s.", self.name, self.hardware_state)
|
|
68
|
+
|
|
69
|
+
if not self._stuck:
|
|
70
|
+
self._stuck_counter = 0
|
|
71
|
+
|
|
72
|
+
if self._transition_state == "settled":
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
if self._transition_state == "commanded":
|
|
76
|
+
self._latency_remaining = self._sample_latency()
|
|
77
|
+
self._stuck = self._sample_stuck()
|
|
78
|
+
self._transition_state = "pending_latency"
|
|
79
|
+
|
|
80
|
+
if self._transition_state == "pending_latency":
|
|
81
|
+
self._latency_remaining -= self.dt
|
|
82
|
+
if self._latency_remaining <= 0:
|
|
83
|
+
if self._stuck:
|
|
84
|
+
self._transition_state = "stuck"
|
|
85
|
+
logger.critical("%s is stuck", self.name)
|
|
86
|
+
else:
|
|
87
|
+
self._transition_state = "settled"
|
|
88
|
+
self.hardware_state = self.commanded_state
|
|
89
|
+
self.cst = 0
|
|
90
|
+
|
|
91
|
+
def _sample_latency(self) -> float:
|
|
92
|
+
"""Sample latency in milliseconds
|
|
93
|
+
Note, all values divided by 1000 to convert millesconds to seconds to
|
|
94
|
+
reflect the standard of self._dt"""
|
|
95
|
+
|
|
96
|
+
dist_type = self.failure_sim["cta_latency"]["distribution"]
|
|
97
|
+
if dist_type == "normal":
|
|
98
|
+
mean_val = self.failure_sim["cta_latency"]["mean"] / 1000
|
|
99
|
+
std_val = self.failure_sim["cta_latency"]["std"] / 1000
|
|
100
|
+
return max(0, gauss(mean_val, std_val))
|
|
101
|
+
|
|
102
|
+
if dist_type == "uniform":
|
|
103
|
+
max_lat = self.failure_sim["cta_latency"]["max"] / 1000
|
|
104
|
+
min_lat = self.failure_sim["cta_latency"]["min"] / 1000
|
|
105
|
+
return uniform(min_lat, max_lat)
|
|
106
|
+
|
|
107
|
+
# Could raise an error here sense dist_type would be unknown
|
|
108
|
+
# sorta a silent failure right now
|
|
109
|
+
logger.warning("Unknown distribution type: %s. Setting latency to zero", dist_type)
|
|
110
|
+
return 0
|
|
111
|
+
|
|
112
|
+
def _sample_stuck(self) -> bool:
|
|
113
|
+
"Determine if in this sim case the actuator gets stuck in its current state"
|
|
114
|
+
if random() <= self.failure_sim["stuck_probability"]:
|
|
115
|
+
return True
|
|
116
|
+
return False
|
|
117
|
+
|
|
118
|
+
def update(self, dt: float) -> None:
|
|
119
|
+
"""Update world state based on commands, actuator latency, and effects"""
|
|
120
|
+
self.cst += dt
|
|
121
|
+
self.dt = dt
|
|
122
|
+
if self.sim:
|
|
123
|
+
self._resolve_state_transition()
|
|
124
|
+
self._apply_effects()
|
|
125
|
+
|
|
126
|
+
def _discover_effects(self):
|
|
127
|
+
"""Find developer effects"""
|
|
128
|
+
self._effect_methods = {
|
|
129
|
+
name.replace("effect_", ""): method
|
|
130
|
+
for name, method in inspect.getmembers(self, predicate=inspect.ismethod)
|
|
131
|
+
if name.startswith("effect_")
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
def _apply_effects(self) -> None:
|
|
135
|
+
"""Apply developer defined effects"""
|
|
136
|
+
for variable_name, method in self._effect_methods.items():
|
|
137
|
+
magnitude = method()
|
|
138
|
+
memory_type = self._get_memory_type(variable_name)
|
|
139
|
+
|
|
140
|
+
if "p" in memory_type:
|
|
141
|
+
self.world_state.apply_delta(variable_name, magnitude)
|
|
142
|
+
else:
|
|
143
|
+
self.world_state.add_contributions(variable_name, self.name, magnitude)
|
|
144
|
+
|
|
145
|
+
def _get_memory_type(self, variable_name: str) -> str:
|
|
146
|
+
"""Get the memory type of an actuator-effected variable"""
|
|
147
|
+
return self.effects[variable_name]["effect_types"]
|
|
148
|
+
|
|
149
|
+
#---------------------------------------------------------------------------------------
|
|
150
|
+
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""Build instances of actuators, sensors, and set up device connections"""
|
|
2
|
+
|
|
3
|
+
import importlib
|
|
4
|
+
import logging
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from hilsim.core.actuators import ActuatorBase
|
|
8
|
+
from hilsim.core.controls import (AdvancedCommunication, Controls,
|
|
9
|
+
SerialInterface)
|
|
10
|
+
from hilsim.core.device_exceptions import DeviceConnectionError
|
|
11
|
+
from hilsim.core.i2c_bus import I2CBus
|
|
12
|
+
from hilsim.core.load_toml import LoadTOML
|
|
13
|
+
from hilsim.core.sensors import DriverBase, SensorBase
|
|
14
|
+
from hilsim.core.world_state import WorldState
|
|
15
|
+
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
# ---------------------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
class BuildComponents:
|
|
21
|
+
"""Build instances of actuators, sensors, and set up device connections"""
|
|
22
|
+
|
|
23
|
+
def __init__(self, build_ctx: dict) -> None:
|
|
24
|
+
self.sensor_config = build_ctx["sensor_config"]
|
|
25
|
+
self.device_config = build_ctx["device_config"]
|
|
26
|
+
self.actuator_config = build_ctx["actuator_config"]
|
|
27
|
+
self.world_state = build_ctx["world_state"]
|
|
28
|
+
self.i2c_bus = build_ctx["i2c_bus"]
|
|
29
|
+
self.project_root = build_ctx["project_root"]
|
|
30
|
+
self._auto_discover(
|
|
31
|
+
self.project_root / "sensors"
|
|
32
|
+
) # Populates SensorBase.registry and DriverBase.Registry
|
|
33
|
+
self._auto_discover(
|
|
34
|
+
self.project_root / "actuators"
|
|
35
|
+
) # Populates ActuatorBase.registry
|
|
36
|
+
|
|
37
|
+
def _auto_discover(self, directory: str) -> None:
|
|
38
|
+
"""Import all modules in a directory to trigger the __init_subclass__ registration"""
|
|
39
|
+
path = Path(directory)
|
|
40
|
+
for module_file in path.glob("*.py"):
|
|
41
|
+
if module_file.name.startswith("_"):
|
|
42
|
+
continue
|
|
43
|
+
module_name = module_file.stem
|
|
44
|
+
try:
|
|
45
|
+
spec = importlib.util.spec_from_file_location(module_name, module_file)
|
|
46
|
+
module = importlib.util.module_from_spec(spec)
|
|
47
|
+
spec.loader.exec_module(module)
|
|
48
|
+
except Exception as e:
|
|
49
|
+
raise ImportError(f"failed to load module '{module_file}': {e}") from e
|
|
50
|
+
|
|
51
|
+
def _build_actuators(self) -> dict:
|
|
52
|
+
"""Create dictonary of instances of each actuator"""
|
|
53
|
+
actuators = {}
|
|
54
|
+
for name, config in self.actuator_config.items():
|
|
55
|
+
driver_name = config["settings"]["driver"]
|
|
56
|
+
driver_class = ActuatorBase.registry[driver_name]
|
|
57
|
+
actuators[name] = driver_class(config, self.world_state, self.controller)
|
|
58
|
+
|
|
59
|
+
return actuators
|
|
60
|
+
|
|
61
|
+
def _build_fake_sensors_with_drivers(self):
|
|
62
|
+
"""Construct instances of drivers"""
|
|
63
|
+
drivers = {}
|
|
64
|
+
for sensor in self.sensor_config["sensor_params"]["enabled_sensors"]:
|
|
65
|
+
|
|
66
|
+
# Query important parameters
|
|
67
|
+
sensor_config = self.sensor_config["sensors"][sensor]
|
|
68
|
+
fake_sensor_name = sensor_config["fake_sensor_name"]
|
|
69
|
+
driver_name = sensor_config["driver_name"]
|
|
70
|
+
|
|
71
|
+
# Create instance of fake sensor
|
|
72
|
+
fake_sensor_class = SensorBase.registry[fake_sensor_name]
|
|
73
|
+
fake_sensor = fake_sensor_class(sensor_config, self.world_state)
|
|
74
|
+
|
|
75
|
+
# Create instance of driver
|
|
76
|
+
driver_class = DriverBase.registry[driver_name]
|
|
77
|
+
drivers[sensor] = driver_class(
|
|
78
|
+
sensor, sensor_config, self.i2c_bus, fake_sensor
|
|
79
|
+
)
|
|
80
|
+
logger.info("Registered %s → %s", sensor, driver_class.__name__)
|
|
81
|
+
|
|
82
|
+
return drivers
|
|
83
|
+
|
|
84
|
+
def _build_devices(self) -> dict:
|
|
85
|
+
"""Build peripherial devices and open serial connections to them"""
|
|
86
|
+
device_configs = self.device_config["devices"]
|
|
87
|
+
advanced_comms_config = self.device_config["advanced_communication"]
|
|
88
|
+
logger.info("building peripherial microcontrollers")
|
|
89
|
+
|
|
90
|
+
device_dict = {}
|
|
91
|
+
for device in self.device_config["enabled_devices"]:
|
|
92
|
+
sub_config = device_configs[device]
|
|
93
|
+
device_comms = SerialInterface(sub_config, device)
|
|
94
|
+
device_w_advanced_comms = AdvancedCommunication(
|
|
95
|
+
sub_config, advanced_comms_config, device_comms
|
|
96
|
+
)
|
|
97
|
+
connected = device_w_advanced_comms.device.connect()
|
|
98
|
+
if not connected:
|
|
99
|
+
logger.error("Error connecting to %s", device)
|
|
100
|
+
raise DeviceConnectionError(
|
|
101
|
+
device, "Failed to connect to device. See message logs."
|
|
102
|
+
)
|
|
103
|
+
logger.info("successfully connected to %s", device)
|
|
104
|
+
device_dict[device] = device_w_advanced_comms
|
|
105
|
+
return device_dict
|
|
106
|
+
|
|
107
|
+
def build_all(self) -> dict:
|
|
108
|
+
"""Build all sensors, devices, and actuators"""
|
|
109
|
+
self.controller = Controls(self._build_devices()) # actuators depend on the Controller!
|
|
110
|
+
actuators = self._build_actuators()
|
|
111
|
+
sensors = self._build_fake_sensors_with_drivers()
|
|
112
|
+
return {"actuators": actuators, "sensors": sensors}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
# ---------------------------------------------------------------------------------------
|