From 4519b3a8e9f2983cd64aa4a0c03ef2d4106be093 Mon Sep 17 00:00:00 2001 From: Local user Date: Mon, 25 May 2026 21:31:08 -0600 Subject: [PATCH 1/3] adjusted physical limits to max/min --- AirbrakesDriver/Inc/airbrakes.hpp | 6 +++-- AirbrakesDriver/airbrakes.cpp | 44 +++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/AirbrakesDriver/Inc/airbrakes.hpp b/AirbrakesDriver/Inc/airbrakes.hpp index 9ec3b31..a9ce2b3 100644 --- a/AirbrakesDriver/Inc/airbrakes.hpp +++ b/AirbrakesDriver/Inc/airbrakes.hpp @@ -2,8 +2,8 @@ #define _AIRBRAKES_HPP_ #include "stm32g4xx.h" -#define AIRBRAKES_NUM_DEPLOYMENT_LEVELS 10 -#define AIRBRAKES_MAX_CURRENT_AMPS 3.0 +#define AIRBRAKES_NUM_DEPLOYMENT_LEVELS 9 +#define AIRBRAKES_MAX_CURRENT_AMPS 10.0 class AirbrakesDriver { public: @@ -57,6 +57,8 @@ class AirbrakesDriver { const float hz; + bool hit = false; + }; diff --git a/AirbrakesDriver/airbrakes.cpp b/AirbrakesDriver/airbrakes.cpp index 4c51d22..ef03baf 100644 --- a/AirbrakesDriver/airbrakes.cpp +++ b/AirbrakesDriver/airbrakes.cpp @@ -18,6 +18,9 @@ AirbrakesDriver::AirbrakesDriver(TIM_HandleTypeDef* servoPWMTimerHandle, * Resets current latch and monitors for runaway current for 250ms. */ void AirbrakesDriver::Enable() { + if(enabled) { + return; + } HAL_TIM_PWM_Start(servoPWMTimer, TIM_CHANNEL_1); HAL_ADC_Start(servoADCHandle); @@ -41,16 +44,21 @@ void AirbrakesDriver::Enable() { } HAL_Delay(1); } + vel = 0; enabled = true; } /* @brief Disables the driver. Stops ADC sampling and PWM output and disables current. */ void AirbrakesDriver::Disable() { + if(!enabled) { + return; + } HAL_GPIO_WritePin(servoENPort, servoENPin, GPIO_PIN_RESET); HAL_TIM_PWM_Stop(servoPWMTimer, TIM_CHANNEL_1); HAL_ADC_Stop(servoADCHandle); HAL_GPIO_WritePin(servoLatchResetPort, servoLatchResetPin, GPIO_PIN_RESET); + vel = 0; enabled = false; } @@ -63,7 +71,9 @@ bool AirbrakesDriver::SetTargetLevel(uint8_t level) { return false; } - return SetTargetDutyCycle(static_cast(level)/(AIRBRAKES_NUM_DEPLOYMENT_LEVELS-1)*(0.0025*hz-0.0005*hz)+0.0005*hz); + hit = false; + vel = 0; + return SetTargetDutyCycle(static_cast(level)/(AIRBRAKES_NUM_DEPLOYMENT_LEVELS-1)*(0.0025*hz-0.0005*hz)/2.1+0.0005*hz); } /* @brief Set the airbrake target duty cycle directly. @@ -76,6 +86,8 @@ bool AirbrakesDriver::SetTargetDutyCycle(float dutyFrac) { return false; } targetDutyCycle = dutyFrac; + hit = false; + vel = 0; return true; } @@ -98,17 +110,27 @@ float AirbrakesDriver::ReadVoltsADC() { */ bool AirbrakesDriver::TickControlLoop() { - if(!CurrentGood() || !IsEnabled()) { + if(!IsEnabled()) { + return false; + } + if(!CurrentGood()) { Disable(); return false; } + if(hit) { + return true; + } + - const static float MAX_VELOCITY = 0.04; - const static float MAX_ACCEL = 0.0007; + const static float MAX_VELOCITY = 0.0008; + const static float MAX_ACCEL = 0.000035; + + const static float ABS_MIN = 0.000515; + const static float ABS_MAX = 0.002485 / 2.1; float distance = targetDutyCycle - currentDutyCycle; - float ideal_vel = sqrtf(2.0 * MAX_ACCEL * fabsf(distance))-MAX_ACCEL; + float ideal_vel = sqrtf(2.0 * MAX_ACCEL * fabsf(distance)); if (distance < 0) { ideal_vel = -ideal_vel; @@ -134,8 +156,20 @@ bool AirbrakesDriver::TickControlLoop() { currentDutyCycle += vel; + if(currentDutyCycle < ABS_MIN*hz) { + currentDutyCycle = ABS_MIN*hz; + } + if(currentDutyCycle > ABS_MAX*hz) { + currentDutyCycle = ABS_MAX*hz; + } + servoPWMTimer->Instance->CCR1 = round(servoPWMTimer->Instance->ARR * currentDutyCycle); + if(abs(currentDutyCycle-targetDutyCycle) < MAX_VELOCITY) { + hit = true; + vel = 0; + } + return true; } From 77b62164b45a71bc0055f034c6b33a1e7442ecfe Mon Sep 17 00:00:00 2001 From: Local user Date: Wed, 27 May 2026 18:52:26 -0600 Subject: [PATCH 2/3] airbrakes works --- AirbrakesDriver/Inc/airbrakes.hpp | 11 +++++++++++ AirbrakesDriver/airbrakes.cpp | 11 ++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/AirbrakesDriver/Inc/airbrakes.hpp b/AirbrakesDriver/Inc/airbrakes.hpp index a9ce2b3..e0ecb2a 100644 --- a/AirbrakesDriver/Inc/airbrakes.hpp +++ b/AirbrakesDriver/Inc/airbrakes.hpp @@ -59,6 +59,17 @@ class AirbrakesDriver { bool hit = false; + void SetHardwareCycle() { + servoPWMTimer->Instance->CCR1 = round(servoPWMTimer->Instance->ARR * currentDutyCycle); + } + + + constexpr static float MAX_VELOCITY = 0.0008; + constexpr static float MAX_ACCEL = 0.000035; + + constexpr static float ABS_MIN = 0.000515; + constexpr static float ABS_MAX = 0.002485 / 2.1; + }; diff --git a/AirbrakesDriver/airbrakes.cpp b/AirbrakesDriver/airbrakes.cpp index ef03baf..46f153f 100644 --- a/AirbrakesDriver/airbrakes.cpp +++ b/AirbrakesDriver/airbrakes.cpp @@ -21,6 +21,7 @@ void AirbrakesDriver::Enable() { if(enabled) { return; } + HAL_TIM_PWM_Start(servoPWMTimer, TIM_CHANNEL_1); HAL_ADC_Start(servoADCHandle); @@ -37,7 +38,7 @@ void AirbrakesDriver::Enable() { HAL_Delay(10); // Immediately stop if current starts running away - for(uint16_t i = 0; i < 250; i++) { + for(uint16_t i = 0; i < 100; i++) { if(!CurrentGood()) { Disable(); return; @@ -113,6 +114,7 @@ bool AirbrakesDriver::TickControlLoop() { if(!IsEnabled()) { return false; } + if(!CurrentGood()) { Disable(); return false; @@ -123,11 +125,6 @@ bool AirbrakesDriver::TickControlLoop() { } - const static float MAX_VELOCITY = 0.0008; - const static float MAX_ACCEL = 0.000035; - - const static float ABS_MIN = 0.000515; - const static float ABS_MAX = 0.002485 / 2.1; float distance = targetDutyCycle - currentDutyCycle; float ideal_vel = sqrtf(2.0 * MAX_ACCEL * fabsf(distance)); @@ -163,7 +160,7 @@ bool AirbrakesDriver::TickControlLoop() { currentDutyCycle = ABS_MAX*hz; } - servoPWMTimer->Instance->CCR1 = round(servoPWMTimer->Instance->ARR * currentDutyCycle); + SetHardwareCycle(); if(abs(currentDutyCycle-targetDutyCycle) < MAX_VELOCITY) { hit = true; From 9d38a6383c1bd65d093db3db96231f3c4e9685c1 Mon Sep 17 00:00:00 2001 From: Local user Date: Tue, 9 Jun 2026 19:05:36 -0600 Subject: [PATCH 3/3] adjusted for battery --- AirbrakesDriver/Inc/airbrakes.hpp | 14 ++++++++++++++ AirbrakesDriver/airbrakes.cpp | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/AirbrakesDriver/Inc/airbrakes.hpp b/AirbrakesDriver/Inc/airbrakes.hpp index 9ec3b31..3ee9444 100644 --- a/AirbrakesDriver/Inc/airbrakes.hpp +++ b/AirbrakesDriver/Inc/airbrakes.hpp @@ -24,6 +24,8 @@ class AirbrakesDriver { return enabled; } + bool OverrideCurrentLevel(uint16_t lvl); + private: TIM_HandleTypeDef* servoPWMTimer; @@ -57,6 +59,18 @@ class AirbrakesDriver { const float hz; + bool hit = false; + + void SetHardwareCycle() { + servoPWMTimer->Instance->CCR1 = round(servoPWMTimer->Instance->ARR * currentDutyCycle); + } + + + constexpr static float MAX_VELOCITY = 0.0008; + constexpr static float MAX_ACCEL = 0.00002; + + constexpr static float ABS_MIN = 0.000515; + constexpr static float ABS_MAX = 0.002485 / 2.1; }; diff --git a/AirbrakesDriver/airbrakes.cpp b/AirbrakesDriver/airbrakes.cpp index 4c51d22..7e8ad01 100644 --- a/AirbrakesDriver/airbrakes.cpp +++ b/AirbrakesDriver/airbrakes.cpp @@ -166,6 +166,14 @@ bool AirbrakesDriver::CurrentGood() { return CheckComparatorGood() && (ADCVoltsToCurrent(ReadVoltsADC()) <= AIRBRAKES_MAX_CURRENT_AMPS); } +bool AirbrakesDriver::OverrideCurrentLevel(uint16_t lvl) { + if(SetTargetLevel(lvl)) { + currentDutyCycle = targetDutyCycle; + SetHardwareCycle(); + return true; + } + return false; +} /* @brief Checks the state of the hardware failsafe latch. * @return true if the latch is good, i.e. current limit has not been triggered