feat: Working v2

This commit is contained in:
2025-11-06 19:48:57 +01:00
parent 3fc98d51ed
commit aba170f937
8 changed files with 109 additions and 112 deletions

View File

@@ -338,25 +338,6 @@ hw->mqtt_reader();
hw->close_mqtt_conn();
```
### Via LifecycleManager (Recommended)
```bash
# Launch and manage via lifecycle
ros2 run g2_2025_imu_reader_pkg g2_2025_lifecycle_node \
--ros-args -p device_path:=/dev/ttyUSB0 -p baudrate:=115200 -p comm_t:=serial
# Configure and activate
ros2 lifecycle set /lifecycle_manager configure
ros2 lifecycle set /lifecycle_manager activate
# Subscribe to IMU data
ros2 topic echo /imu_data
# Deactivate and cleanup
ros2 lifecycle set /lifecycle_manager deactivate
ros2 lifecycle set /lifecycle_manager shutdown
```
## Design Patterns
1. **Abstraction Pattern**: Encapsulates serial and MQTT complexity behind a unified interface

View File

@@ -1,7 +1,7 @@
# LifecycleManager (`assignments::two::g2_2025_lifecycle_node`)
## Overview
The `LifecycleManager` is the lifecycle node responsible for receiving the data of the ESP32-IMU combination via serial/MQTT and publishing that data to the database management node. For the functionality outside of the lifecycle see [HardwareInterface.md](HardwareInterface.md])
The `LifecycleManager` is the core lifecycle-aware node responsible for managing the IMU reader system's operational states and hardware communication. It orchestrates transitions between configuration, activation, and deactivation phases, abstracting the complexity of dual communication backends (serial and MQTT) into a unified interface.
#### Implementation Details
@@ -9,7 +9,7 @@ The `LifecycleManager` is the lifecycle node responsible for receiving the data
- **`device_path`** (string, default: "/dev/ttyUSB0"): Serial device path for hardware connection (e.g., USB serial adapter).
- **`baudrate`** (int, default: 115200): Serial communication baud rate in bits per second.
- **`comm_t`** (string, default: "serial"): Communication type selector—either "serial" or "mqtt".
- **`comm_t`** (string, default: "serial"): Communication type selector—either "serial" or "mqtt" to determine which backend to use.
**Constructor**
```cpp
@@ -63,15 +63,18 @@ LifecycleManager()
## Communication Architecture
The `LifecycleManager` switches communication type depending on the `comm_t` parameter:
### Dual Backend Support
#### Serial Communication
The `LifecycleManager` provides a flexible, pluggable communication architecture via the `comm_t` parameter:
#### Serial Communication Path
1. **Configuration Phase** (`on_configure`):
- Opens the serial device at the path specified by `device_path` and baudrate
- Validates device readiness
2. **Activation Phase** (`on_activate`):
- Spawns a background reader thread via `hw_interface->start_read()`
- Continuously polls the serial device with a timeout
- Thread continuously polls the serial device with a timeout
- Reads are accumulated in a partial buffer, split on newline, and parsed as JSON
- Each valid JSON IMU payload is parsed into a `sensor_msgs::msg::Imu` and published to the ROS topic `imu/data`
@@ -80,7 +83,7 @@ The `LifecycleManager` switches communication type depending on the `comm_t` par
- Joins the thread to ensure clean termination
- Closes the serial device
#### MQTT Communication
#### MQTT Communication Path
1. **Configuration Phase** (`on_configure`):
- Creates a persistent MQTT async client pointing to the broker at `SERVER_ADDRESS` (default: `tcp://localhost:1883`)
- Initializes MQTT callback infrastructure
@@ -100,20 +103,24 @@ To interact with the `LifecycleManager` from the command line, use the following
```bash
# List current lifecycle state
ros2 lifecycle list /lifecycle_manager
ros2 lifecycle list /LifecycleManager
# Transition: UNCONFIGURED -> INACTIVE
ros2 lifecycle set /lifecycle_manager configure
ros2 lifecycle set /LifecycleManager configure
# Transition: INACTIVE -> UNCONFIGURED
ros2 lifecycle set /LifecycleManager cleanup
# Transition: INACTIVE -> ACTIVE
ros2 lifecycle set /lifecycle_manager activate
ros2 lifecycle set /LifecycleManager activate
# Transition: ACTIVE -> INACTIVE
ros2 lifecycle set /lifecycle_manager deactivate
ros2 lifecycle set /LifecycleManager deactivate
# Transition: INACTIVE -> FINALIZED
ros2 lifecycle set /lifecycle_manager shutdown
ros2 lifecycle set /LifecycleManager shutdown
```
![img](https://design.ros2.org/img/node_lifecycle/life_cycle_sm.png)
## Data Flow
@@ -194,15 +201,15 @@ ros2 run g2_2025_imu_reader_pkg g2_2025_lifecycle_node \
-p comm_t:=mqtt
# In another terminal, configure and activate the lifecycle
ros2 lifecycle set /lifecycle_manager configure
ros2 lifecycle set /lifecycle_manager activate
ros2 lifecycle set /LifecycleManager configure
ros2 lifecycle set /LifecycleManager activate
# Subscribe to published IMU data
ros2 topic echo /imu_data
# Deactivate and shutdown
ros2 lifecycle set /lifecycle_manager deactivate
ros2 lifecycle set /lifecycle_manager shutdown
ros2 lifecycle set /LifecycleManager deactivate
ros2 lifecycle set /LifecycleManager shutdown
```
---