|
12 | 12 |
|
13 | 13 | // Project Includes |
14 | 14 | #include "2026Core/CommonConfig.hpp" // Include after NacelleConfig due to macro precednece |
| 15 | +#include "2026Core/Units.hpp" |
15 | 16 | #include "ActuonixL12.hpp" |
16 | 17 | #include "Encoder.hpp" |
17 | 18 | #include "NacelleComms.hpp" |
@@ -310,37 +311,57 @@ vTaskUpdateFSM([[maybe_unused]] void *pvParameters) { // NOSONAR |
310 | 311 | [[noreturn]] void |
311 | 312 | vTaskPollSensors([[maybe_unused]] void *pvParameters) { // NOSONAR |
312 | 313 | while (true) { |
| 314 | + // Save last wake time to ensure consistent timing |
313 | 315 | static TickType_t xLastWakeTime = xTaskGetTickCount(); |
314 | | - static unsigned long previousTime = 0; |
315 | | - static etl::circular_buffer<int_fast32_t, 20> accelSamples; |
316 | | - static int_fast32_t accelSum = 0; |
317 | | - unsigned long currentTime = micros(); |
318 | 316 |
|
319 | 317 | // Poll encoder & update stored value |
320 | | - // todo: back off and retry if not working |
321 | | - int_fast32_t lastRPM = nacelle.currentRPM; // Save |
322 | | - Encoder::getRpmMovingAverage(nacelle.currentRPM); // Update |
323 | | - int_fast32_t deltaRPM = nacelle.currentRPM - lastRPM; |
324 | | - // ESP_LOGD(TAG, "deltaRPM: %ld", deltaRPM); |
325 | | - int_fast32_t deltaTime_us = currentTime - previousTime; |
326 | | - constexpr unsigned long m_TO_BASE = 1000; |
327 | | - constexpr unsigned long u_TO_m = 1000; |
328 | | - constexpr unsigned long u_TO_BASE = m_TO_BASE * u_TO_m; |
329 | | - int_fast32_t accelSample_RPMPS = static_cast<int_fast32_t>(deltaRPM) * |
330 | | - static_cast<int_fast32_t>(u_TO_BASE) / |
331 | | - deltaTime_us; |
| 318 | + // TODO: back off and retry if not working |
| 319 | + Encoder::getRpmMovingAverage(nacelle.currentRPM); // Update RPM |
| 320 | + |
| 321 | + // Reduce the update speed of acceleration to reduce noise |
| 322 | + static uint_fast16_t RPMSamplesTaken = 0; |
| 323 | + |
| 324 | + // Wait until the filter history has been refreshed |
| 325 | + if (RPMSamplesTaken >= ENCODER::FILTER_HISTORY_SIZE) { |
| 326 | + static unsigned long previousTime = 0; |
| 327 | + // The first deltaRPM will be 0 instead of (current - 0) |
| 328 | + static int_fast32_t lastRPM = nacelle.currentRPM; |
| 329 | + |
| 330 | + // Calculate deltas |
| 331 | + unsigned long currentTime = micros(); |
| 332 | + int_fast32_t deltaRPM = nacelle.currentRPM - lastRPM; // dY |
| 333 | + // ESP_LOGD(TAG, "deltaRPM: %ld", deltaRPM); |
| 334 | + int_fast32_t deltaTime_us = currentTime - previousTime; // dT |
| 335 | + |
| 336 | + // Calculate acceleration and perform unit conversion |
| 337 | + // (RPM / t us) * (10^6 us / 1 s) = (RPM / 1 s) |
| 338 | + int_fast32_t accelSample_RPMPS = |
| 339 | + deltaRPM * static_cast<int_fast32_t>(UNITS::MICROS_PER_SEC) / |
| 340 | + deltaTime_us; |
| 341 | + |
| 342 | + // Filter - moving average |
| 343 | + static etl::circular_buffer<int_fast32_t, |
| 344 | + ENCODER::FILTER_HISTORY_SIZE> |
| 345 | + accelSamples; |
| 346 | + static int_fast32_t accelSum = 0; |
| 347 | + if (accelSamples.full()) { |
| 348 | + accelSum -= accelSamples.front(); |
| 349 | + accelSamples.pop(); |
| 350 | + } |
| 351 | + accelSamples.push(accelSample_RPMPS); |
| 352 | + accelSum += accelSample_RPMPS; |
332 | 353 |
|
333 | | - if (accelSamples.full()) { |
334 | | - accelSum -= accelSamples.front(); |
335 | | - accelSamples.pop(); |
336 | | - } |
337 | | - accelSamples.push(accelSample_RPMPS); |
338 | | - accelSum += accelSample_RPMPS; |
| 354 | + nacelle.angularAccel_RPMPS = |
| 355 | + accelSum / static_cast<int_fast32_t>(accelSamples.size()); |
| 356 | + |
| 357 | + // Save current time |
| 358 | + previousTime = currentTime; |
339 | 359 |
|
340 | | - nacelle.angularAccel_RPMPS = static_cast<int_fast32_t>( |
341 | | - accelSum / static_cast<int_fast32_t>(accelSamples.size())); |
342 | | - previousTime = currentTime; |
| 360 | + // Reset counter |
| 361 | + RPMSamplesTaken = 0; |
| 362 | + } |
343 | 363 |
|
| 364 | + // Run at a fixed frequency |
344 | 365 | BaseType_t xWasDelayed = xTaskDelayUntil( |
345 | 366 | &xLastWakeTime, |
346 | 367 | pdMS_TO_TICKS(RUN::TASK_INTERVALS::TI_POLL_SENSORS_mS)); |
|
0 commit comments