Skip to content

Commit 883aa28

Browse files
committed
Worked on acceleration calculation, Moved all unit conversion constants to Core - Units, and split encoder into inc/src instead of lib
Tried to put units in commonconfig but that messes up the import order. Core: Moved Units to it's own header to avoid import order issues
1 parent e6a1d07 commit 883aa28

5 files changed

Lines changed: 59 additions & 42 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include <etl/to_string.h>
1313

1414
// Custom Includes
15-
#include "../../include/NacelleConfig.hpp"
1615
#include "AS5600.h"
16+
#include "NacelleConfig.hpp"
1717

1818
/**
1919
* @namespace Encoder

include/NacelleConfig.hpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
static_assert(__cplusplus >= 202302L, "C++23 standard or later required.");
77

88
// Imports
9+
#include "2026Core/Units.hpp"
910
#include <cstdint>
11+
1012
#define ESP32C5 1
1113
#define BOARD ESP32C5
1214
#if not(BOARD == ESP32C5)
@@ -68,13 +70,6 @@ namespace FR_FIREBEETLE2_ESP32C6 {
6870
constexpr uint_fast8_t LPUART_RX_PIN = LP_RX;
6971
} // namespace FR_FIREBEETLE2_ESP32C6
7072

71-
// MARK: Constants
72-
namespace CONSTS {
73-
constexpr uint_fast32_t MILLIS_PER_SEC = 1000;
74-
constexpr uint_fast32_t SECS_PER_MIN = 60;
75-
constexpr uint_fast16_t uVOLTS_PER_VOLT = 1'000'000;
76-
} // namespace CONSTS
77-
7873
// Mark: Application
7974
namespace LED {
8075
/**
@@ -85,14 +80,14 @@ namespace LED {
8580
// constexpr uint_fast32_t BLINK_OFF_SECS = 3;
8681
constexpr uint_fast32_t BLINK_OFF_MILLIS = 3500;
8782
// constexpr uint_fast32_t LED_BLINK_ON_SEC = 1;
88-
constexpr uint_fast32_t BLINK_ON_MILLIS = CONSTS::MILLIS_PER_SEC / 3;
83+
constexpr uint_fast32_t BLINK_ON_MILLIS = UNITS::MILLIS_PER_SEC / 3;
8984
} // namespace LED
9085

9186
namespace ENCODER {
9287
constexpr uint_fast16_t START_RUN_2_RPM = 200; // CONFIG
9388
constexpr uint_fast16_t TARGET_RPM = 2200; // CONFIG
9489
constexpr uint_fast16_t MAX_RPM = 3000; // CONFIG
95-
constexpr uint_fast8_t MAX_RPS = MAX_RPM / CONSTS::SECS_PER_MIN;
90+
constexpr uint_fast8_t MAX_RPS = MAX_RPM / UNITS::SECS_PER_MIN;
9691
constexpr uint_fast16_t MIN_T_mS_PER_REV = 1000 / MAX_RPS;
9792
constexpr uint_fast32_t OPTIMAL_SAMPLE_TIME_mS = MIN_T_mS_PER_REV / 4;
9893
// static_assert(SAMPLE_DELAY_MS == OPTIMAL_SAMPLE_TIME_mS,
@@ -125,7 +120,7 @@ namespace ENCODER {
125120
constexpr uint_fast16_t MAX_RATED_RPM = 3500; // CONFIG
126121
constexpr uint_fast16_t MOTOR_kV_RPMPV = 107;
127122
constexpr uint_fast16_t MOTOR_IkV_RPSPuV =
128-
CONSTS::uVOLTS_PER_VOLT * CONSTS::SECS_PER_MIN / MOTOR_kV_RPMPV;
123+
UNITS::uVOLTS_PER_VOLT * UNITS::SECS_PER_MIN / MOTOR_kV_RPMPV;
129124
} // namespace ENCODER
130125

131126
namespace PITCHING {
@@ -188,8 +183,8 @@ namespace RUN {
188183
// todo: What was this for?
189184
// constexpr uint_fast32_t SLEEP_TIME_MINS = 10;
190185
// constexpr uint_fast32_t SLEEP_TIME_SECS = SLEEP_TIME_MINS *
191-
// CONSTS::SECS_PER_MIN; constexpr uint_fast32_t SLEEP_TIME_MILLIS =
192-
// SLEEP_TIME_SECS * CONSTS::MILLIS_PER_SEC;
186+
// UNITS::SECS_PER_MIN; constexpr uint_fast32_t SLEEP_TIME_MILLIS =
187+
// SLEEP_TIME_SECS * UNITS::MILLIS_PER_SEC;
193188
} // namespace RUN
194189

195190
namespace TELNET {

lib/2026-Controls_Core

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ namespace Encoder {
122122
(void)logString.append(", AGC: ");
123123

124124
etl::format_spec decFormatB;
125-
(void)decFormatB.width(3).fill('0'); // [3 chars]
126-
etl::to_string(as5600.readAGC(), logString, decFormatB, true); // 3 chars
127-
(void)logString.append(", MD: "); // 6 chars
125+
(void)decFormatB.width(3).fill('0'); // [3 chars]
126+
etl::to_string(as5600.readAGC(), logString, decFormatB,
127+
true); // 3 chars
128+
(void)logString.append(", MD: "); // 6 chars
128129

129130
boolFormatA.binary().width(1).fill('0'); // [1 char]
130131
etl::to_string(as5600.detectMagnet(), logString, decFormatB,

src/main.cpp

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
// Project Includes
1414
#include "2026Core/CommonConfig.hpp" // Include after NacelleConfig due to macro precednece
15+
#include "2026Core/Units.hpp"
1516
#include "ActuonixL12.hpp"
1617
#include "Encoder.hpp"
1718
#include "NacelleComms.hpp"
@@ -310,37 +311,57 @@ vTaskUpdateFSM([[maybe_unused]] void *pvParameters) { // NOSONAR
310311
[[noreturn]] void
311312
vTaskPollSensors([[maybe_unused]] void *pvParameters) { // NOSONAR
312313
while (true) {
314+
// Save last wake time to ensure consistent timing
313315
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();
318316

319317
// 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;
332353

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;
339359

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+
}
343363

364+
// Run at a fixed frequency
344365
BaseType_t xWasDelayed = xTaskDelayUntil(
345366
&xLastWakeTime,
346367
pdMS_TO_TICKS(RUN::TASK_INTERVALS::TI_POLL_SENSORS_mS));

0 commit comments

Comments
 (0)