Retiming all the PWM's
This commit is contained in:
@@ -135,48 +135,36 @@ uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) {
|
||||
return res;
|
||||
}
|
||||
|
||||
void setTipPWM(uint8_t pulse) {
|
||||
PWMSafetyTimer = 10; // This is decremented in the handler for PWM so that the tip pwm is
|
||||
// disabled if the PID task is not scheduled often enough.
|
||||
|
||||
pendingPWM = pulse;
|
||||
}
|
||||
|
||||
static void switchToFastPWM(void) {
|
||||
fastPWM = true;
|
||||
totalPWM = powerPWM + tempMeasureTicks * 2 + holdoffTicks;
|
||||
htim2.Instance->ARR = totalPWM;
|
||||
// ~3.5 Hz rate
|
||||
htim2.Instance->CCR1 = powerPWM + holdoffTicks * 2;
|
||||
// 2 MHz timer clock/2000 = 1 kHz tick rate
|
||||
htim2.Instance->PSC = 2000;
|
||||
// 10Hz
|
||||
fastPWM = true;
|
||||
totalPWM = powerPWM + tempMeasureTicks + holdoffTicks;
|
||||
htim2.Instance->ARR = totalPWM;
|
||||
htim2.Instance->CCR1 = powerPWM + holdoffTicks;
|
||||
htim2.Instance->PSC = 2690;
|
||||
}
|
||||
|
||||
static void switchToSlowPWM(void) {
|
||||
fastPWM = false;
|
||||
totalPWM = powerPWM + tempMeasureTicks + holdoffTicks;
|
||||
htim2.Instance->ARR = totalPWM;
|
||||
// ~1.84 Hz rate
|
||||
htim2.Instance->CCR1 = powerPWM + holdoffTicks;
|
||||
// 2 MHz timer clock/4000 = 500 Hz tick rate
|
||||
htim2.Instance->PSC = 4000;
|
||||
// 5Hz
|
||||
fastPWM = false;
|
||||
totalPWM = powerPWM + tempMeasureTicks / 2 + holdoffTicks / 2;
|
||||
htim2.Instance->ARR = totalPWM;
|
||||
htim2.Instance->CCR1 = powerPWM + holdoffTicks / 2;
|
||||
htim2.Instance->PSC = 2690 * 2;
|
||||
}
|
||||
|
||||
bool tryBetterPWM(uint8_t pwm) {
|
||||
if (fastPWM && pwm == powerPWM) {
|
||||
// maximum power for fast PWM reached, need to go slower to get more
|
||||
switchToSlowPWM();
|
||||
return true;
|
||||
} else if (!fastPWM && pwm < 230) {
|
||||
// 254 in fast PWM mode gives the same power as 239 in slow
|
||||
// allow for some reasonable hysteresis by switching only when it goes
|
||||
// below 230 (equivalent to 245 in fast mode)
|
||||
switchToFastPWM();
|
||||
return true;
|
||||
void setTipPWM(const uint8_t pulse, const bool shouldUseFastModePWM) {
|
||||
PWMSafetyTimer = 10; // This is decremented in the handler for PWM so that the tip pwm is
|
||||
// disabled if the PID task is not scheduled often enough.
|
||||
pendingPWM = pulse;
|
||||
if (fastPWM != shouldUseFastModePWM) {
|
||||
if (shouldUseFastModePWM) {
|
||||
switchToFastPWM();
|
||||
} else {
|
||||
switchToSlowPWM();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// These are called by the HAL after the corresponding events from the system
|
||||
// timers.
|
||||
|
||||
|
||||
@@ -326,7 +326,8 @@ static void MX_TIM2_Init(void) {
|
||||
// These values give a rate of around 3.5 Hz for "fast" mode and 1.84 Hz for "slow"
|
||||
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
// dummy value, will be reconfigured by BSPInit()
|
||||
htim2.Init.Period = 255 + 17 * 2;
|
||||
htim2.Init.Period = powerPWM + 14 * 2;
|
||||
|
||||
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4; // 8 MHz (x2 APB1) before divide
|
||||
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
htim2.Init.RepetitionCounter = 0;
|
||||
@@ -344,7 +345,7 @@ static void MX_TIM2_Init(void) {
|
||||
|
||||
sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
||||
// dummy value, will be reconfigured by BSPInit() in the BSP.cpp
|
||||
sConfigOC.Pulse = 255 + 13 * 2; // 13 -> Delay of 7 ms
|
||||
sConfigOC.Pulse = powerPWM + 14; // 13 -> Delay of 7 ms
|
||||
// 255 is the largest time period of the drive signal, and then offset ADC sample to be a bit delayed after this
|
||||
/*
|
||||
* It takes 4 milliseconds for output to be stable after PWM turns off.
|
||||
|
||||
Reference in New Issue
Block a user