Skip to content

Commit 879812f

Browse files
committed
nacelle sCurtail state update
1 parent a71eabb commit 879812f

5 files changed

Lines changed: 31 additions & 18 deletions

File tree

include/NacelleConfig.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ namespace LED {
8585

8686
namespace ENCODER {
8787
constexpr uint_fast16_t START_RUN_2_RPM = 200; // CONFIG
88-
inline uint_fast16_t TARGET_RPM = 2200; // CONFIG
88+
inline uint_fast16_t TARGET_RPM = 3200; // CONFIG
8989
constexpr uint_fast16_t MAX_RPM = 3000; // CONFIG
9090
constexpr uint_fast8_t MAX_RPS = MAX_RPM / UNITS::SECS_PER_MIN;
9191
constexpr uint_fast16_t MIN_T_mS_PER_REV = 1000 / MAX_RPS;
@@ -160,8 +160,8 @@ namespace PITCHING {
160160
// todo: Ftarget rpm var
161161

162162
/* PID Config */
163-
constexpr float PITCH_Kp = 0.08f; // CONFIG - 0.005f last year // TODO
164-
constexpr float PITCH_Ki = 0.000f; // CONFIG - 0.001f last year // TODO
163+
constexpr float PITCH_Kp = 0.35f; // CONFIG - 0.005f last year // TODO
164+
constexpr float PITCH_Ki = 0.03f; // CONFIG - 0.001f last year // TODO
165165
constexpr float PITCH_Kd = 0.0f;
166166

167167
inline bool enableRpmOutput = false; // CONFIG - whether to output RPM to log

include/NacelleContainer.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class NacelleContainer {
127127
std::atomic<int16_t> d_mVPS = 0;
128128
std::atomic<int16_t> current_mA = 0;
129129
std::atomic<int16_t> dIPS = 0;
130+
std::atomic<int16_t> power_mW = 0;
130131

131132
NacelleContainer(ActuonixL12 &pitchActuator, PID &pitchPIDController,
132133
NacelleComms &nacelleComms)
@@ -146,18 +147,23 @@ class NacelleContainer {
146147
return true;
147148
// return angularAccel_RPMPS < 20;
148149
} // todo
150+
151+
inline bool isTargetPowerExceeded(float targetPower_mW) {
152+
return (power_mW > targetPower_mW);
153+
}
154+
149155
inline bool isTargetRPMExceeded() const {
150156
return (currentRPM > ENCODER::TARGET_RPM);
151157
}
152158

153-
154159
// checks if the rpm has been in a steady state (within tolerance) for a certain duration
155160
bool isTargetRPMExceededHysteresis() {
156161
const TickType_t now = xTaskGetTickCount();
157162
if (std::abs(static_cast<int32_t>(currentRPM) -
158163
static_cast<int32_t>(ENCODER::TARGET_RPM)) < steadyRpmTolerance) {
159164
if (initialTime != 0) {
160165
if ((now - initialTime) >= pdMS_TO_TICKS(MIN_STEADY_STATE_DURATION_MS)) {
166+
ESP_LOGI(TAG, "Target RPM exceeded hysteresis condition met. Current RPM: %d, Target RPM: %d", currentRPM.load(), ENCODER::TARGET_RPM);
161167
return true;
162168
}
163169
}
@@ -291,6 +297,6 @@ class NacelleContainer {
291297
bool powerPositive = false;
292298
bool enableSafetyFlag = true;
293299
TickType_t initialTime = 0;
294-
constexpr static uint32_t MIN_STEADY_STATE_DURATION_MS = 5000; // 5 seconds
295-
constexpr static uint8_t steadyRpmTolerance = 20; // RPM tolerance for steady state check
300+
constexpr static uint32_t MIN_STEADY_STATE_DURATION_MS = 1500; // minimum time for the rpm to be in relative steady state before setpoint is adjusted
301+
constexpr static uint8_t steadyRpmTolerance = 80; // RPM tolerance for steady state check
296302
};

include/NacelleFSM.hpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
class NacelleFSM {
1616
public: // MARK: Public
1717
static constexpr const char *TAG = "NFSM";
18-
float power = 0;
19-
float targetPower = 38; // W
18+
uint32_t targetPower_mW = 27000; // mW
19+
constexpr static int_fast16_t CURTAIL_EXIT_RPM_TOLERANCE = 400; // RPM tolerance for exiting curtailment, prevents oscillation
2020

2121
/**
2222
* @brief Construct a new Nacelle FSM object
@@ -145,7 +145,7 @@ class NacelleFSM {
145145

146146
return UPDATE_RESULT::STATE_CHANGED;
147147
} else if ((currentState == FSMCommon::States::sRunLoad) &&
148-
nacelle.isTargetRPMExceeded()) {
148+
nacelle.isTargetPowerExceeded(targetPower_mW)) { // todo
149149
// sRunLoad -> sCurtail
150150
currentState = FSMCommon::States::sCurtail;
151151

@@ -164,17 +164,17 @@ class NacelleFSM {
164164
} else if ((currentState == FSMCommon::States::sCurtail) &&
165165
nacelle.isTargetRPMExceededHysteresis()) {
166166

167-
constexpr float POWER_TOLERANCE = 0.06f;
167+
constexpr float POWER_TOLERANCE = 0.05f;
168168
constexpr int_fast16_t RPM_STEP = 40;
169169

170-
float dPower = power - targetPower;
170+
float powerError = nacelle.power_mW - targetPower_mW; // only checks once, noise could mess this up
171171

172-
if ((dPower) > POWER_TOLERANCE * targetPower) {
172+
if ((powerError) >= POWER_TOLERANCE * targetPower_mW) {
173173
nacelle.pitchPIDController.setTarget(ENCODER::TARGET_RPM - RPM_STEP); // todo adjust decrement amount
174174
ENCODER::TARGET_RPM = ENCODER::TARGET_RPM - RPM_STEP; // todo adjust decrement amount
175175
ESP_LOGI(TAG, "Decreasing target RPM to %u due to overpower condition", ENCODER::TARGET_RPM);
176176
}
177-
else if (dPower < -POWER_TOLERANCE * targetPower) {
177+
else if (powerError <= (-1) * POWER_TOLERANCE * targetPower_mW) {
178178
nacelle.pitchPIDController.setTarget(ENCODER::TARGET_RPM + RPM_STEP); // todo adjust increment amount
179179
ENCODER::TARGET_RPM = ENCODER::TARGET_RPM + RPM_STEP; // todo adjust increment amount
180180
ESP_LOGI(TAG, "Increasing target RPM to %u due to underpower condition", ENCODER::TARGET_RPM);
@@ -183,6 +183,12 @@ class NacelleFSM {
183183
nacelle.pitchPIDController.disable();
184184
ESP_LOGI(TAG, "Power within acceptable range, PID disabled");
185185
}
186+
187+
if (nacelle.currentRPM < ENCODER::TARGET_RPM - CURTAIL_EXIT_RPM_TOLERANCE) {
188+
currentState = FSMCommon::States::sRunLoad;
189+
ESP_LOGI(TAG, "Current RPM below target RPM, transitioning back to sRunLoad");
190+
}
191+
186192
// sCurtail -> sRunLoad
187193
//currentState = FSMCommon::States::sRunLoad;
188194

@@ -203,8 +209,8 @@ class NacelleFSM {
203209
return UPDATE_RESULT::ERROR;
204210
}
205211

206-
void setPower(float power) {
207-
this->power = power;
212+
void setTargetPower(float targetPower_mW) {
213+
this->targetPower_mW = targetPower_mW;
208214
}
209215

210216
private: // MARK: Private

lib/SerialInterface/SerialInterface.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ class SerialInterface {
103103
ESP_LOGI(TAG, "Disabled RPM output to log");
104104

105105
}
106-
else if (command.equalsIgnoreCase("setPower")) {
107-
nacelleFSM.setPower(parts.tokens[1].toFloat());
108-
ESP_LOGI(TAG, "Updated power: %.2f", nacelleFSM.power);
106+
else if (command.equalsIgnoreCase("setTargetPower")) {
107+
nacelleFSM.setTargetPower(parts.tokens[1].toFloat());
108+
ESP_LOGI(TAG, "Updated target power: %.2f", nacelleFSM.targetPower_mW);
109109
}
110110
else if (command.equalsIgnoreCase("help")) {
111111
Serial.println("Available commands:");

src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ vTaskRecvData([[maybe_unused]] void *pvParameters) { // NOSONAR
440440
nacelle.d_mVPS = packet.d_mVPS;
441441
nacelle.current_mA = packet.current_mA;
442442
nacelle.dIPS = packet.dIPS;
443+
nacelle.power_mW = packet.powerIfWholeNum_mW;
443444
nacelle.setSafetyFlag(
444445
static_cast<ESTOP_TYPE_FAST>(packet.safety));
445446
}

0 commit comments

Comments
 (0)