mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-23 18:56:38 +00:00
fix on servo and ESp32 (#19878)
servo did not react same on ESP32 than ESP8266. Changed to TASMOTA pwmapply capabilities
This commit is contained in:
parent
be94a6cac8
commit
8540ee37fc
@ -559,7 +559,6 @@ void ShutterInit(void)
|
|||||||
//Initialize to get relay that changed
|
//Initialize to get relay that changed
|
||||||
ShutterGlobal.RelayOldMask = TasmotaGlobal.power;
|
ShutterGlobal.RelayOldMask = TasmotaGlobal.power;
|
||||||
|
|
||||||
ShutterGlobal.open_velocity_max = ShutterSettings.open_velocity_max;
|
|
||||||
for (uint32_t i = 0; i < MAX_SHUTTERS_ESP32; i++) {
|
for (uint32_t i = 0; i < MAX_SHUTTERS_ESP32; i++) {
|
||||||
// set startrelay to 1 on first init, but only to shutter 1. 90% usecase
|
// set startrelay to 1 on first init, but only to shutter 1. 90% usecase
|
||||||
if (ShutterSettings.shutter_startrelay[i] && (ShutterSettings.shutter_startrelay[i] <= 32 )) {
|
if (ShutterSettings.shutter_startrelay[i] && (ShutterSettings.shutter_startrelay[i] <= 32 )) {
|
||||||
@ -645,20 +644,19 @@ void ShutterInit(void)
|
|||||||
if (Shutter[i].tilt_config[2] == 0) {
|
if (Shutter[i].tilt_config[2] == 0) {
|
||||||
Shutter[i].tilt_config[3] = Shutter[i].tilt_config[4] = 0;
|
Shutter[i].tilt_config[3] = Shutter[i].tilt_config[4] = 0;
|
||||||
}
|
}
|
||||||
Shutter[i].tilt_target_pos = Shutter[i].tilt_real_pos = ShutterSettings.shutter_tilt_pos[i];
|
|
||||||
Shutter[i].tilt_velocity = Shutter[i].tilt_config[2] > 0 ? ((Shutter[i].tilt_config[1] - Shutter[i].tilt_config[0]) / Shutter[i].tilt_config[2]) + 1 : 1;
|
|
||||||
Shutter[i].close_velocity_max = ShutterGlobal.open_velocity_max*Shutter[i].open_time / Shutter[i].close_time;
|
|
||||||
Shutter[i].min_realPositionChange = 2 * tmax(ShutterGlobal.open_velocity_max, Shutter[i].close_velocity_max);
|
|
||||||
Shutter[i].min_TiltChange = 2 * Shutter[i].tilt_velocity;
|
|
||||||
|
|
||||||
switch (ShutterGlobal.position_mode) {
|
switch (ShutterGlobal.position_mode) {
|
||||||
case SHT_PWM_VALUE:
|
case SHT_PWM_VALUE:
|
||||||
ShutterGlobal.open_velocity_max = RESOLUTION;
|
|
||||||
// Initiate pwm range with defaults if not already set.
|
// Initiate pwm range with defaults if not already set.
|
||||||
ShutterSettings.shutter_pwmrange[0][i] = ShutterSettings.shutter_pwmrange[0][i] > 0 ? ShutterSettings.shutter_pwmrange[0][i] : pwm_servo_min;
|
ShutterSettings.shutter_pwmrange[0][i] = ShutterSettings.shutter_pwmrange[0][i] > 0 ? ShutterSettings.shutter_pwmrange[0][i] : pwm_servo_min;
|
||||||
ShutterSettings.shutter_pwmrange[1][i] = ShutterSettings.shutter_pwmrange[1][i] > 0 ? ShutterSettings.shutter_pwmrange[1][i] : pwm_servo_max;
|
ShutterSettings.shutter_pwmrange[1][i] = ShutterSettings.shutter_pwmrange[1][i] > 0 ? ShutterSettings.shutter_pwmrange[1][i] : pwm_servo_max;
|
||||||
Shutter[i].min_realPositionChange = 0;
|
Shutter[i].min_realPositionChange = 0;
|
||||||
Shutter[i].min_TiltChange = 0;
|
Shutter[i].min_TiltChange = 0;
|
||||||
|
|
||||||
|
// set pwm to actual position. This may cause fast movements if not in sync
|
||||||
|
Shutter[i].pwm_value = SHT_DIV_ROUND((ShutterSettings.shutter_pwmrange[1][i] - ShutterSettings.shutter_pwmrange[0][i]) * Shutter[i].target_position , Shutter[i].open_max)+ShutterSettings.shutter_pwmrange[0][i];
|
||||||
|
TasmotaGlobal.pwm_value[i] = Shutter[i].pwm_value;
|
||||||
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SHT: Shtr%d pwmset: %d -> %d"), i+1, TasmotaGlobal.pwm_cur_value[i], TasmotaGlobal.pwm_value[i]);
|
||||||
|
PwmApplyGPIO(false);
|
||||||
break;
|
break;
|
||||||
case SHT_TIME:
|
case SHT_TIME:
|
||||||
// Test is the relays are in interlock mode. Disable shuttermode if error
|
// Test is the relays are in interlock mode. Disable shuttermode if error
|
||||||
@ -668,13 +666,22 @@ void ShutterInit(void)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case SHT_COUNTER:
|
||||||
|
ShutterGlobal.open_velocity_max = ShutterSettings.open_velocity_max;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Shutter[i].tilt_target_pos = Shutter[i].tilt_real_pos = ShutterSettings.shutter_tilt_pos[i];
|
||||||
|
Shutter[i].tilt_velocity = Shutter[i].tilt_config[2] > 0 ? ((Shutter[i].tilt_config[1] - Shutter[i].tilt_config[0]) / Shutter[i].tilt_config[2]) + 1 : 1;
|
||||||
|
Shutter[i].close_velocity_max = ShutterGlobal.open_velocity_max*Shutter[i].open_time / Shutter[i].close_time;
|
||||||
|
Shutter[i].min_realPositionChange = 2 * tmax(ShutterGlobal.open_velocity_max, Shutter[i].close_velocity_max);
|
||||||
|
Shutter[i].min_TiltChange = 2 * Shutter[i].tilt_velocity;
|
||||||
|
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("SHT: Shtr%d min realpos_chg: %d, min tilt_chg %d"), i+1, Shutter[i].min_realPositionChange, Shutter[i].min_TiltChange);
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SHT: Shtr%d min realpos_chg: %d, min tilt_chg %d"), i+1, Shutter[i].min_realPositionChange, Shutter[i].min_TiltChange);
|
||||||
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shtr%d Openvel %d, Closevel: %d"), i+1, ShutterGlobal.open_velocity_max, Shutter[i].close_velocity_max);
|
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shtr%d Openvel %d, Closevel: %d"), i+1, ShutterGlobal.open_velocity_max, Shutter[i].close_velocity_max);
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("SHT: Shtr%d Init. Pos %d, Inv %d, Locked %d, Endstop enab %d, webButt inv %d, Motordel: %d"),
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SHT: Shtr%d Init. Pos %d, Inv %d, Locked %d, Endstop enab %d, webButt inv %d, Motordel: %d"),
|
||||||
i+1, Shutter[i].real_position,
|
i+1, Shutter[i].real_position,
|
||||||
(ShutterSettings.shutter_options[i] & 1) ? 1 : 0, (ShutterSettings.shutter_options[i] & 2) ? 1 : 0, (ShutterSettings.shutter_options[i] & 4) ? 1 : 0, (ShutterSettings.shutter_options[i] & 8) ? 1 : 0, Shutter[i].motordelay);
|
(ShutterSettings.shutter_options[i] & 1) ? 1 : 0, (ShutterSettings.shutter_options[i] & 2) ? 1 : 0, (ShutterSettings.shutter_options[i] & 4) ? 1 : 0, (ShutterSettings.shutter_options[i] & 8) ? 1 : 0, Shutter[i].motordelay);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// terminate loop at first INVALID Shutter[i].
|
// terminate loop at first INVALID Shutter[i].
|
||||||
break;
|
break;
|
||||||
@ -757,6 +764,7 @@ int32_t ShutterPercentToRealPosition(int16_t percent, uint32_t index)
|
|||||||
|
|
||||||
void ShutterPowerOff(uint8_t i)
|
void ShutterPowerOff(uint8_t i)
|
||||||
{
|
{
|
||||||
|
bool pwm_apply = false;
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("SHT: Stop %d Mode %d time %d, last source %d"), i+1,Shutter[i].switch_mode, Shutter[i].time, TasmotaGlobal.last_source); // fix log to indicate correct shutter number
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SHT: Stop %d Mode %d time %d, last source %d"), i+1,Shutter[i].switch_mode, Shutter[i].time, TasmotaGlobal.last_source); // fix log to indicate correct shutter number
|
||||||
ShutterDecellerateForStop(i);
|
ShutterDecellerateForStop(i);
|
||||||
uint8_t cur_relay = ShutterSettings.shutter_startrelay[i] + (Shutter[i].direction == 1 ? 0 : (uint8_t)(ShutterGlobal.position_mode == SHT_TIME)) ;
|
uint8_t cur_relay = ShutterSettings.shutter_startrelay[i] + (Shutter[i].direction == 1 ? 0 : (uint8_t)(ShutterGlobal.position_mode == SHT_TIME)) ;
|
||||||
@ -797,16 +805,18 @@ void ShutterPowerOff(uint8_t i)
|
|||||||
switch (ShutterGlobal.position_mode) {
|
switch (ShutterGlobal.position_mode) {
|
||||||
case SHT_PWM_VALUE:
|
case SHT_PWM_VALUE:
|
||||||
Shutter[i].pwm_value = SHT_DIV_ROUND((ShutterSettings.shutter_pwmrange[1][i]-ShutterSettings.shutter_pwmrange[0][i]) * Shutter[i].target_position , Shutter[i].open_max)+ShutterSettings.shutter_pwmrange[0][i];
|
Shutter[i].pwm_value = SHT_DIV_ROUND((ShutterSettings.shutter_pwmrange[1][i]-ShutterSettings.shutter_pwmrange[0][i]) * Shutter[i].target_position , Shutter[i].open_max)+ShutterSettings.shutter_pwmrange[0][i];
|
||||||
analogWrite(Pin(GPIO_PWM1, i), Shutter[i].pwm_value);
|
//analogWrite(Pin(GPIO_PWM1, i), Shutter[i].pwm_value);
|
||||||
|
TasmotaGlobal.pwm_value[i] = Shutter[i].pwm_value;
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("SHT: PWM final %d"),Shutter[i].pwm_value);
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SHT: PWM final %d"),Shutter[i].pwm_value);
|
||||||
|
pwm_apply = true;
|
||||||
char scmnd[20];
|
char scmnd[20];
|
||||||
#ifdef SHUTTER_CLEAR_PWM_ONSTOP
|
#ifdef SHUTTER_CLEAR_PWM_ONSTOP
|
||||||
// free the PWM servo lock on stop.
|
// free the PWM servo lock on stop.
|
||||||
analogWrite(Pin(GPIO_PWM1, i), 0);
|
TasmotaGlobal.pwm_value[i] = 0;
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (pwm_apply) { PwmApplyGPIO(false); }
|
||||||
// restore save_data behavior if all shutters are in stopped state
|
// restore save_data behavior if all shutters are in stopped state
|
||||||
bool shutter_all_stopped = true;
|
bool shutter_all_stopped = true;
|
||||||
for (uint8_t j = 0 ; j < TasmotaGlobal.shutters_present ; j++) {
|
for (uint8_t j = 0 ; j < TasmotaGlobal.shutters_present ; j++) {
|
||||||
@ -962,7 +972,7 @@ void ShutterReportPosition(bool always, uint32_t index)
|
|||||||
|
|
||||||
void ShutterRtc50mS(void)
|
void ShutterRtc50mS(void)
|
||||||
{
|
{
|
||||||
bool pwm_apply = false; // ESP32 only, do we need to apply PWM changes
|
bool pwm_apply = false;
|
||||||
// No Logging allowed. RTC Timer
|
// No Logging allowed. RTC Timer
|
||||||
for (uint8_t i = 0; i < TasmotaGlobal.shutters_present; i++) {
|
for (uint8_t i = 0; i < TasmotaGlobal.shutters_present; i++) {
|
||||||
if (Shutter[i].direction) {
|
if (Shutter[i].direction) {
|
||||||
@ -975,12 +985,15 @@ void ShutterRtc50mS(void)
|
|||||||
ShutterUpdateVelocity(i);
|
ShutterUpdateVelocity(i);
|
||||||
Shutter[i].real_position += Shutter[i].direction > 0 ? Shutter[i].pwm_velocity : (Shutter[i].direction < 0 ? -Shutter[i].pwm_velocity : 0);
|
Shutter[i].real_position += Shutter[i].direction > 0 ? Shutter[i].pwm_velocity : (Shutter[i].direction < 0 ? -Shutter[i].pwm_velocity : 0);
|
||||||
Shutter[i].pwm_value = SHT_DIV_ROUND((ShutterSettings.shutter_pwmrange[1][i]-ShutterSettings.shutter_pwmrange[0][i]) * Shutter[i].real_position , Shutter[i].open_max)+ShutterSettings.shutter_pwmrange[0][i];
|
Shutter[i].pwm_value = SHT_DIV_ROUND((ShutterSettings.shutter_pwmrange[1][i]-ShutterSettings.shutter_pwmrange[0][i]) * Shutter[i].real_position , Shutter[i].open_max)+ShutterSettings.shutter_pwmrange[0][i];
|
||||||
analogWrite(Pin(GPIO_PWM1, i), Shutter[i].pwm_value);
|
TasmotaGlobal.pwm_value[i] = Shutter[i].pwm_value;
|
||||||
|
//analogWritePhase(Pin(GPIO_PWM1, i), Shutter[i].pwm_value, 0);
|
||||||
|
//analogWriteFreq(200,Pin(GPIO_PWM1, i));
|
||||||
|
pwm_apply = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SHT_COUNTER:
|
case SHT_COUNTER:
|
||||||
if (Shutter[i].accelerator) {
|
if (Shutter[i].accelerator) {
|
||||||
//AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Accelerator i=%d -> %d"),i, Shutter[i].accelerator);
|
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Accelerator i=%d -> %d"),i, Shutter[i].accelerator);
|
||||||
ShutterUpdateVelocity(i);
|
ShutterUpdateVelocity(i);
|
||||||
digitalWrite(Pin(GPIO_PWM1, i), LOW);
|
digitalWrite(Pin(GPIO_PWM1, i), LOW);
|
||||||
analogWriteFreq(Shutter[i].pwm_velocity,Pin(GPIO_PWM1, i));
|
analogWriteFreq(Shutter[i].pwm_velocity,Pin(GPIO_PWM1, i));
|
||||||
@ -1158,7 +1171,7 @@ void ShutterStartInit(uint32_t i, int32_t direction, int32_t target_pos)
|
|||||||
switch (ShutterGlobal.position_mode) {
|
switch (ShutterGlobal.position_mode) {
|
||||||
#ifdef SHUTTER_STEPPER
|
#ifdef SHUTTER_STEPPER
|
||||||
case SHT_COUNTER:
|
case SHT_COUNTER:
|
||||||
analogWriteFreq(PWM_MIN,Pin(GPIO_PWM1, i));
|
//analogWriteFreq(PWM_MIN,Pin(GPIO_PWM1, i));
|
||||||
TasmotaGlobal.pwm_value[i] = 0;
|
TasmotaGlobal.pwm_value[i] = 0;
|
||||||
PwmApplyGPIO(false);
|
PwmApplyGPIO(false);
|
||||||
RtcSettings.pulse_counter[i] = 0;
|
RtcSettings.pulse_counter[i] = 0;
|
||||||
@ -1264,10 +1277,10 @@ void ShutterUpdatePosition(void)
|
|||||||
// Update time information
|
// Update time information
|
||||||
int32_t deltatime = Shutter[i].time - Shutter[i].last_reported_time;
|
int32_t deltatime = Shutter[i].time - Shutter[i].last_reported_time;
|
||||||
Shutter[i].last_reported_time = Shutter[i].time + 1;
|
Shutter[i].last_reported_time = Shutter[i].time + 1;
|
||||||
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shtr%d Time %d(%d), cStop %d, cVelo %d, mVelo %d, aVelo %d, mRun %d, aPos %d, aPos2 %d, nStop %d, Trgt %d, mVelo %d, Dir %d, Tilt %d, TrgtTilt: %d, Tiltmove: %d"),
|
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shtr%d Time %d(%d), cStop %d, cVelo %d, mVelo %d, aVelo %d, mRun %d, aPos %d, aPos2 %d, nStop %d, Trgt %d, mVelo %d, Dir %d, Tilt %d, TrgtTilt: %d, Tiltmove: %d, PWM: %d"),
|
||||||
i+1, Shutter[i].time, deltatime, current_stop_way, current_pwm_velocity, velocity_max, Shutter[i].accelerator, min_runtime_ms, current_real_position,Shutter[i].real_position,
|
i+1, Shutter[i].time, deltatime, current_stop_way, current_pwm_velocity, velocity_max, Shutter[i].accelerator, min_runtime_ms, current_real_position,Shutter[i].real_position,
|
||||||
next_possible_stop_position, Shutter[i].target_position, velocity_change_per_step_max, Shutter[i].direction,Shutter[i].tilt_real_pos, Shutter[i].tilt_target_pos,
|
next_possible_stop_position, Shutter[i].target_position, velocity_change_per_step_max, Shutter[i].direction,Shutter[i].tilt_real_pos, Shutter[i].tilt_target_pos,
|
||||||
Shutter[i].tiltmoving);
|
Shutter[i].tiltmoving, Shutter[i].pwm_value);
|
||||||
|
|
||||||
// Check calibration mode and energy information
|
// Check calibration mode and energy information
|
||||||
// only execute every second step to remove some stress from the current sensor.
|
// only execute every second step to remove some stress from the current sensor.
|
||||||
@ -1687,7 +1700,7 @@ void CmndShutterFrequency(void)
|
|||||||
ShutterSettings.open_velocity_max = ShutterGlobal.open_velocity_max;
|
ShutterSettings.open_velocity_max = ShutterGlobal.open_velocity_max;
|
||||||
ShutterInit();
|
ShutterInit();
|
||||||
}
|
}
|
||||||
ResponseCmndNumber(ShutterGlobal.open_velocity_max);
|
ResponseCmndNumber(ShutterSettings.open_velocity_max);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CmndShutterIncDec(void)
|
void CmndShutterIncDec(void)
|
||||||
@ -1957,14 +1970,11 @@ void CmndShutterPwmRange(void)
|
|||||||
}
|
}
|
||||||
ShutterSettings.shutter_pwmrange[i][XdrvMailbox.index - 1] = field;
|
ShutterSettings.shutter_pwmrange[i][XdrvMailbox.index - 1] = field;
|
||||||
}
|
}
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("SHT: Shtr%d Init1. pwmmin %d, pwmmax %d"), XdrvMailbox.index , ShutterSettings.shutter_pwmrange[0][XdrvMailbox.index -1], ShutterSettings.shutter_pwmrange[1][XdrvMailbox.index -1]);
|
|
||||||
ShutterInit();
|
ShutterInit();
|
||||||
ResponseCmndIdxChar(XdrvMailbox.data);
|
}
|
||||||
} else {
|
char setting_chr[30] = "0";
|
||||||
char setting_chr[30] = "0";
|
snprintf_P(setting_chr, sizeof(setting_chr), PSTR("SHT %d: pwmmin:%d pwmmax:%d"), XdrvMailbox.index, ShutterSettings.shutter_pwmrange[0][XdrvMailbox.index -1], ShutterSettings.shutter_pwmrange[1][XdrvMailbox.index -1]);
|
||||||
snprintf_P(setting_chr, sizeof(setting_chr), PSTR("Shutter %d: min:%d max:%d"), XdrvMailbox.index, ShutterSettings.shutter_pwmrange[0][XdrvMailbox.index -1], ShutterSettings.shutter_pwmrange[1][XdrvMailbox.index -1]);
|
ResponseCmndIdxChar(setting_chr);
|
||||||
ResponseCmndIdxChar(setting_chr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user