HardFOC Vortex V1 HAL
Unified HAL API for the HardFOC Vortex motor-control platform (ESP32-C6, TMC9660)
Loading...
Searching...
No Matches
TemperatureManager.h
Go to the documentation of this file.
1
16#ifndef VORTEX_TEMPERATURE_MANAGER_H_
17#define VORTEX_TEMPERATURE_MANAGER_H_
18
19#include <array>
20#include <atomic>
21#include <cstdint>
22#include <optional>
23#include <string_view>
24
25#include "base/BaseTemperature.h"
26#include "handlers/ntc/NtcTemperatureHandler.h"
27#include "RtosMutex.h"
28
29//==============================================================================
30// FORWARD DECLARATIONS
31//==============================================================================
32
33class Tmc9660Handler;
34class MotorController;
35class AdcManager;
36
37//==============================================================================
38// TMC9660 TEMPERATURE WRAPPER
39//==============================================================================
40
44class Tmc9660TemperatureWrapper : public BaseTemperature {
45public:
50 explicit Tmc9660TemperatureWrapper(Tmc9660Handler& handler) noexcept;
51 ~Tmc9660TemperatureWrapper() override = default;
52
54 bool Initialize() noexcept override;
55
57 bool Deinitialize() noexcept override;
58
64 hf_temp_err_t ReadTemperatureCelsiusImpl(float* temperature_celsius) noexcept override;
65
71 hf_temp_err_t GetSensorInfo(hf_temp_sensor_info_t* info) const noexcept override;
72
77 hf_u32_t GetCapabilities() const noexcept override;
78
79private:
80 Tmc9660Handler& handler_;
81};
82
83//==============================================================================
84// SENSOR TYPE ENUM
85//==============================================================================
86
90enum class VortexTempSensorType : uint8_t {
91 ESP32_INTERNAL = 0,
92 NTC_THERMISTOR = 1,
93 TMC9660_CHIP = 2,
95};
96
102constexpr const char* VortexTempSensorTypeToString(VortexTempSensorType t) noexcept {
103 switch (t) {
104 case VortexTempSensorType::ESP32_INTERNAL: return "ESP32 Internal";
105 case VortexTempSensorType::NTC_THERMISTOR: return "NTC Thermistor";
106 case VortexTempSensorType::TMC9660_CHIP: return "TMC9660 Chip";
107 default: return "Unknown";
108 }
109}
110
111//==============================================================================
112// TEMPERATURE ENTRY
113//==============================================================================
114
118struct TempEntry {
119 std::string_view name;
121 bool registered{false};
122 float last_reading_celsius{0.0f};
123 uint32_t read_count{0};
124 uint32_t error_count{0};
125};
126
127//==============================================================================
128// DIAGNOSTICS STRUCTURE
129//==============================================================================
130
152
153//==============================================================================
154// TEMPERATURE MANAGER
155//==============================================================================
156
170public:
171 //==========================================================================
172 // SINGLETON AND LIFECYCLE
173 //==========================================================================
174
176 static TemperatureManager& GetInstance() noexcept;
177
182
187 [[nodiscard]] bool EnsureInitialized() noexcept;
188
193 [[nodiscard]] bool Deinitialize() noexcept;
194
199 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_.load(std::memory_order_acquire); }
200
201 //==========================================================================
202 // TEMPERATURE READING
203 //==========================================================================
204
211 [[nodiscard]] hf_temp_err_t ReadTemperatureCelsius(std::string_view name, float& temperature_celsius) noexcept;
212
219 [[nodiscard]] hf_temp_err_t ReadTemperatureFahrenheit(std::string_view name, float& temperature_fahrenheit) noexcept;
220
221 //==========================================================================
222 // CONVENIENCE READING — cross-HAL compatible
223 //==========================================================================
224
230 [[nodiscard]] hf_temp_err_t ReadInternalTemperature(float& temperature_c) noexcept;
231
237 [[nodiscard]] hf_temp_err_t ReadNtcTemperature(float& temperature_c) noexcept;
238
244 [[nodiscard]] hf_temp_err_t ReadMotorTemperature(float& temperature_c) noexcept;
245
253 [[nodiscard]] hf_temp_err_t GetSystemTemperatureStats(float& min_c, float& max_c, float& avg_c) noexcept;
254
255 //==========================================================================
256 // SENSOR INFO
257 //==========================================================================
258
260 [[nodiscard]] size_t GetSensorCount() const noexcept { return registered_count_; }
261
267 [[nodiscard]] const TempEntry* GetSensorEntry(std::string_view name) const noexcept;
268
269 //==========================================================================
270 // DIAGNOSTICS
271 //==========================================================================
272
278 [[nodiscard]] hf_temp_err_t GetSystemDiagnostics(TempSystemDiagnostics& diagnostics) const noexcept;
279
284 [[nodiscard]] hf_temp_err_t GetLastError() const noexcept { return last_error_.load(std::memory_order_acquire); }
285
287 void DumpStatistics() const noexcept;
288
289private:
290 TemperatureManager() noexcept = default;
292
293 [[nodiscard]] bool Initialize() noexcept;
295 void ReleaseLocked() noexcept;
296 bool InitializeEsp32Internal() noexcept;
297 bool InitializeNtcThermistor() noexcept;
298 bool InitializeTmc9660Chip() noexcept;
299
300 [[nodiscard]] size_t FindByName(std::string_view name) const noexcept;
301
302 //==========================================================================
303 // STORAGE
304 //==========================================================================
305
306 static constexpr size_t kMaxSensors = static_cast<size_t>(VortexTempSensorType::SENSOR_COUNT);
307 static constexpr size_t kInvalidIndex = SIZE_MAX;
308
309 std::array<TempEntry, kMaxSensors> entries_{};
310 size_t registered_count_{0};
311 uint64_t init_time_ms_{0};
312
313 // Named sensor driver instances
314 BaseTemperature* esp32_temp_{nullptr};
315 std::optional<NtcTemperatureHandler> ntc_handler_;
316 std::optional<Tmc9660TemperatureWrapper> tmc9660_temp_;
317
318 std::atomic<bool> initialized_{false};
319 std::atomic<hf_temp_err_t> last_error_{hf_temp_err_t::TEMP_SUCCESS};
320 mutable RtosMutex mutex_;
321};
322
323//==============================================================================
324// CONVENIENCE
325//==============================================================================
326
331[[nodiscard]] inline TemperatureManager& GetTemperatureManager() noexcept {
333}
334
335#endif // VORTEX_TEMPERATURE_MANAGER_H_
TemperatureManager & GetTemperatureManager() noexcept
Convenience accessor — equivalent to TemperatureManager::GetInstance().
Definition TemperatureManager.h:331
VortexTempSensorType
Identifies the physical temperature sensor type on the Vortex board.
Definition TemperatureManager.h:90
@ SENSOR_COUNT
Number of sensor types (not a valid sensor)
@ ESP32_INTERNAL
ESP32 on-chip temperature sensor.
@ TMC9660_CHIP
TMC9660 internal chip temperature.
@ NTC_THERMISTOR
External NTC thermistor via TMC9660 AIN3.
constexpr const char * VortexTempSensorTypeToString(VortexTempSensorType t) noexcept
Convert VortexTempSensorType to a human-readable string.
Definition TemperatureManager.h:102
Singleton manager for all analogue-to-digital channels on Vortex V1.
Definition AdcManager.h:92
Singleton managing multiple TMC9660 motor controllers on Vortex V1.
Definition MotorController.h:207
Singleton manager for all temperature sensors on Vortex V1.
Definition TemperatureManager.h:169
RtosMutex mutex_
Definition TemperatureManager.h:320
static TemperatureManager & GetInstance() noexcept
Return the singleton TemperatureManager instance.
Definition TemperatureManager.cpp:90
TemperatureManager & operator=(const TemperatureManager &)=delete
TemperatureManager(const TemperatureManager &)=delete
std::optional< NtcTemperatureHandler > ntc_handler_
In place; emplaced at init.
Definition TemperatureManager.h:315
std::optional< Tmc9660TemperatureWrapper > tmc9660_temp_
In place; emplaced at init.
Definition TemperatureManager.h:316
size_t GetSensorCount() const noexcept
Get the number of registered temperature sensors.
Definition TemperatureManager.h:260
TemperatureManager(TemperatureManager &&)=delete
TemperatureManager & operator=(TemperatureManager &&)=delete
hf_temp_err_t GetLastError() const noexcept
Get the most recent error code from any temperature operation.
Definition TemperatureManager.h:284
Adapter wrapping Tmc9660Handler's internal temperature into BaseTemperature.
Definition TemperatureManager.h:44
hf_temp_err_t ReadTemperatureCelsiusImpl(float *temperature_celsius) noexcept override
Read the TMC9660 chip temperature in Celsius.
Definition TemperatureManager.cpp:61
hf_u32_t GetCapabilities() const noexcept override
Get capability flags for this sensor.
Definition TemperatureManager.cpp:79
bool Initialize() noexcept override
Initialise the underlying TMC9660 temperature path.
Definition TemperatureManager.cpp:35
hf_temp_err_t GetSensorInfo(hf_temp_sensor_info_t *info) const noexcept override
Retrieve sensor information for the TMC9660.
Definition TemperatureManager.cpp:71
bool Deinitialize() noexcept override
Deinitialise the underlying TMC9660 temperature path.
Definition TemperatureManager.cpp:54
Tmc9660Handler & handler_
Definition TemperatureManager.h:80
~Tmc9660TemperatureWrapper() override=default
A single temperature sensor entry in the fixed-size registry.
Definition TemperatureManager.h:118
std::string_view name
Human-readable name.
Definition TemperatureManager.h:119
System-level temperature diagnostics (compatible with old API).
Definition TemperatureManager.h:137
uint32_t hardware_errors
Errors attributed to hardware faults.
Definition TemperatureManager.h:145
uint32_t total_operations
Cumulative read attempts across all sensors.
Definition TemperatureManager.h:141
float system_avg_temp_celsius
Average temperature across all sensors (°C)
Definition TemperatureManager.h:150
uint64_t system_uptime_ms
Milliseconds since temperature subsystem init.
Definition TemperatureManager.h:146
uint32_t failed_operations
Cumulative failed reads.
Definition TemperatureManager.h:143
uint32_t successful_operations
Cumulative successful reads.
Definition TemperatureManager.h:142
bool system_healthy
True when all registered sensors are responsive.
Definition TemperatureManager.h:138
float system_max_temp_celsius
Maximum temperature across all sensors (°C)
Definition TemperatureManager.h:149
uint32_t total_sensors_registered
Number of sensors currently registered.
Definition TemperatureManager.h:139
hf_temp_err_t last_error
Most recent error code from any operation.
Definition TemperatureManager.h:147
uint32_t communication_errors
Errors attributed to bus communication failures.
Definition TemperatureManager.h:144
float system_min_temp_celsius
Minimum temperature across all sensors (°C)
Definition TemperatureManager.h:148