Skip to content

Commit

Permalink
fix on servo and ESp32 (#19878)
Browse files Browse the repository at this point in the history
servo did not react same on ESP32 than ESP8266. Changed to TASMOTA pwmapply  capabilities
  • Loading branch information
stefanbode authored Oct 30, 2023
1 parent be94a6c commit 8540ee3
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions tasmota/tasmota_xdrv_driver/xdrv_27_esp32_shutter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,6 @@ void ShutterInit(void)
//Initialize to get relay that changed
ShutterGlobal.RelayOldMask = TasmotaGlobal.power;

ShutterGlobal.open_velocity_max = ShutterSettings.open_velocity_max;
for (uint32_t i = 0; i < MAX_SHUTTERS_ESP32; i++) {
// set startrelay to 1 on first init, but only to shutter 1. 90% usecase
if (ShutterSettings.shutter_startrelay[i] && (ShutterSettings.shutter_startrelay[i] <= 32 )) {
Expand Down Expand Up @@ -645,20 +644,19 @@ void ShutterInit(void)
if (Shutter[i].tilt_config[2] == 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) {
case SHT_PWM_VALUE:
ShutterGlobal.open_velocity_max = RESOLUTION;
// 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[1][i] = ShutterSettings.shutter_pwmrange[1][i] > 0 ? ShutterSettings.shutter_pwmrange[1][i] : pwm_servo_max;
Shutter[i].min_realPositionChange = 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;
case SHT_TIME:
// Test is the relays are in interlock mode. Disable shuttermode if error
Expand All @@ -668,13 +666,22 @@ void ShutterInit(void)
return;
}
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_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"),
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);

} else {
// terminate loop at first INVALID Shutter[i].
break;
Expand Down Expand Up @@ -757,6 +764,7 @@ int32_t ShutterPercentToRealPosition(int16_t percent, uint32_t index)

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
ShutterDecellerateForStop(i);
uint8_t cur_relay = ShutterSettings.shutter_startrelay[i] + (Shutter[i].direction == 1 ? 0 : (uint8_t)(ShutterGlobal.position_mode == SHT_TIME)) ;
Expand Down Expand Up @@ -797,16 +805,18 @@ void ShutterPowerOff(uint8_t i)
switch (ShutterGlobal.position_mode) {
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];
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);
pwm_apply = true;
char scmnd[20];
#ifdef SHUTTER_CLEAR_PWM_ONSTOP
// free the PWM servo lock on stop.
analogWrite(Pin(GPIO_PWM1, i), 0);
TasmotaGlobal.pwm_value[i] = 0;
#endif
break;
}

if (pwm_apply) { PwmApplyGPIO(false); }
// restore save_data behavior if all shutters are in stopped state
bool shutter_all_stopped = true;
for (uint8_t j = 0 ; j < TasmotaGlobal.shutters_present ; j++) {
Expand Down Expand Up @@ -962,7 +972,7 @@ void ShutterReportPosition(bool always, uint32_t index)

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
for (uint8_t i = 0; i < TasmotaGlobal.shutters_present; i++) {
if (Shutter[i].direction) {
Expand All @@ -975,12 +985,15 @@ void ShutterRtc50mS(void)
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].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;

case SHT_COUNTER:
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);
digitalWrite(Pin(GPIO_PWM1, i), LOW);
analogWriteFreq(Shutter[i].pwm_velocity,Pin(GPIO_PWM1, i));
Expand Down Expand Up @@ -1158,7 +1171,7 @@ void ShutterStartInit(uint32_t i, int32_t direction, int32_t target_pos)
switch (ShutterGlobal.position_mode) {
#ifdef SHUTTER_STEPPER
case SHT_COUNTER:
analogWriteFreq(PWM_MIN,Pin(GPIO_PWM1, i));
//analogWriteFreq(PWM_MIN,Pin(GPIO_PWM1, i));
TasmotaGlobal.pwm_value[i] = 0;
PwmApplyGPIO(false);
RtcSettings.pulse_counter[i] = 0;
Expand Down Expand Up @@ -1264,10 +1277,10 @@ void ShutterUpdatePosition(void)
// Update time information
int32_t deltatime = Shutter[i].time - Shutter[i].last_reported_time;
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,
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
// only execute every second step to remove some stress from the current sensor.
Expand Down Expand Up @@ -1687,7 +1700,7 @@ void CmndShutterFrequency(void)
ShutterSettings.open_velocity_max = ShutterGlobal.open_velocity_max;
ShutterInit();
}
ResponseCmndNumber(ShutterGlobal.open_velocity_max);
ResponseCmndNumber(ShutterSettings.open_velocity_max);
}

void CmndShutterIncDec(void)
Expand Down Expand Up @@ -1957,14 +1970,11 @@ void CmndShutterPwmRange(void)
}
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();
ResponseCmndIdxChar(XdrvMailbox.data);
} else {
char setting_chr[30] = "0";
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);
}
}
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]);
ResponseCmndIdxChar(setting_chr);
}
}

Expand Down

0 comments on commit 8540ee3

Please sign in to comment.