Stepper speed changes based on ESP32 processor load

I have an issue where I set the speed in steps/sec but speed is not very smooth, kinda glitches a few times a sec (auditable). Also, when I go into ESPHome log, the stepper speed drops by about 20%. I believe this is happening because of increased processor load and it is not able to keep up with processing the stepper library.

Lastly, I have not been able to just perform a simple run command without setting the position. You will see in the code I have to set the position to a very high positive or negative number to get it to run.

api:
  encryption:
    key: "********************"
  services:
  - service: control_stepper
    variables:
      speed: float
    then:
      - globals.set:
          id: stepper1_speed
          value: !lambda 'return speed;'
      - logger.log:
          format: "New speed= %.1f"
          args: ['id(stepper1_speed)']
      - stepper.set_speed:
          id: my_stepper1
          speed: !lambda 'return speed;'
      - stepper.set_target:
          id: my_stepper1
          target: !lambda |-
            if (id(stepper1_speed) > 0) {
              return 10000000.0;
            }
            else if (id(stepper1_speed) < 0) {
              return -10000000.0;
            }
            else {return 0.0;}

ota:
  password: "*******************"

wifi:
  networks:
  - ssid: !secret wifi_ssid
    password: !secret wifi_password
  - ssid: !secret wifi_ssid_2
    password: !secret wifi_password_2
  - ssid: !secret wifi_ssid_3
    password: !secret wifi_password_3
  - ssid: !secret wifi_ssid_4
    password: !secret wifi_password_4

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Aqrecycle Fallback Hotspot"
    password: "*************"

captive_portal:

web_server:
  port: 80
  include_internal: true

globals:
  - id: stepper1_speed
    type: float
    restore_value: no
    initial_value: '0'

stepper:
  - platform: a4988
    id: my_stepper1
    step_pin: GPIO27
    dir_pin: GPIO33
    max_speed: 5000 steps/s

For now I’ve given up on the stepper platform and will just use the ledc platform to output the frequency to the stepper driver. What I’ve found is that I cannot use a variable for frequency. Is there a way around this?

number:
  - platform: template
    name: "Stepper 1 freq"
    id: stepper1_freq
    optimistic: true
    min_value: 500
    max_value: 4000
    initial_value: 500
    step: 1
    #mode: slider

output:
  - platform: ledc
    pin: GPIO26
    id: stepper1
    frequency: stepper1_freq

Ok I think I’m getting somewhere. Here is the code which almost works. The issue is that when I enter a number to change the frequency only 50% of the time it changes the speed. If I use the up/down arrow in HA it seems to always change

number:
  - platform: template
    name: "Stepper 1 freq"
    id: stepper1_freq
    optimistic: true
    min_value: 500
    max_value: 4000
    initial_value: 500
    step: 1
   # mode: slider
    set_action:
      then:
        - output.ledc.set_frequency:
            id: stepper1
            frequency: !lambda |-
              return id(stepper1_freq).state;

output:
  - platform: ledc
    pin: GPIO26
    id: stepper1
   # frequency: 1000Hz

switch:
  - platform: output 
    name: 'stepper 1'
    id: stepper1_switch
    output: stepper1
    on_turn_on: 
      then:
        - output.turn_on: stepper1
        - output.ledc.set_frequency:
            id: stepper1
            frequency: !lambda |-
              return id(stepper1_freq).state;
        - output.set_level:
            id: stepper1
            level: "50%"

Here is a log of me entering 800 (from 600) but it doesn’t change the frequency. but I press the up arrow to incriment by 1 and it jumps up to 801

[01:15:49][D][number:054]: ‘Stepper 1 freq’ - Setting number value
[01:15:49][D][number:113]: New number value: 800.000000
[01:15:49][D][ledc.output:051]: Calculating resolution bit-depth for frequency 600.000000
[01:15:49][D][ledc.output:056]: Resolution calculated as 17
[01:15:49][D][number:012]: ‘Stepper 1 freq’: Sending state 800.000000
[01:16:14][D][number:054]: ‘Stepper 1 freq’ - Setting number value
[01:16:14][D][number:113]: New number value: 801.000000
[01:16:14][D][ledc.output:051]: Calculating resolution bit-depth for frequency 800.000000
[01:16:14][D][ledc.output:056]: Resolution calculated as 16
[01:16:14][D][number:012]: ‘Stepper 1 freq’: Sending state 801.000000