Skip to content

Instantly share code, notes, and snippets.

@daniel-frenkel
Last active November 14, 2022 17:49
Show Gist options
  • Save daniel-frenkel/8e4cdc51d2ce889c06ae5fc3ca4e8293 to your computer and use it in GitHub Desktop.
Save daniel-frenkel/8e4cdc51d2ce889c06ae5fc3ca4e8293 to your computer and use it in GitHub Desktop.
ESPHome YAML File
substitutions:
device_name: edit-name
pulley_diameter_mm: "15" # 1 turn about 47 mm
gear_ratio: "1" # not used yet for simplicity
distance_mm: "500" # about 10 turns
acceleration: 12800 steps/s^2
velocity: 12800 steps/s # 1 turn per second
open_current: 500ma
open_stall_threshold: "20"
close_current: 600ma
close_stall_threshold: "17"
tcool_threshold: "910"
microsteps: "64"
external_components:
source: github://glmnet/esphome@stepper-tmc2209
components: [tmc2209]
esphome:
name: ${device_name}
platform: ESP32
board: nodemcu-32s
platformio_options:
upload_speed: 921600
on_boot:
- tmc2209.setup:
microsteps: 64
tcool_threshold: ${tcool_threshold}
current: 600mA
stall_threshold: 20
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_pass
# Enable logging
logger:
level: INFO
# verbose logger over uart causes motor artifacts as pulses are generated in main loop
# logger:
# level: VERY_VERBOSE
# logs:
# api: DEBUG
# api.service: DEBUG
# scheduler: DEBUG
ota:
uart:
id: uart_stepper
tx_pin: GPIO17
rx_pin: GPIO16
baud_rate: 9600
status_led:
pin: GPIO26
api:
services:
- service: control_stepper
variables:
target: int
speed: int
microsteps: int
tcool_threshold: int
stall_threshold: int
rms_current_amps: float
then:
- tmc2209.setup:
id: my_stepper
microsteps: !lambda "return microsteps;"
tcool_threshold: !lambda "return tcool_threshold;"
stall_threshold: !lambda "return stall_threshold;"
current: !lambda "return rms_current_amps;"
- logger.log:
format: moving to %d
args: [target]
- stepper.set_speed:
id: my_stepper
speed: !lambda "return speed;"
- stepper.set_target:
id: my_stepper
target: !lambda "return target;"
- service: set_stepper_zero
then:
- stepper.report_position:
id: my_stepper
position: 0
- stepper.set_target:
id: my_stepper
target: 0
binary_sensor:
- platform: gpio
name: Button1
pin:
number: GPIO23
inverted: true
on_press:
then:
if:
condition:
lambda: "return id(my_stepper).target_position == 0;"
then:
stepper.set_target:
id: my_stepper
target: 128000
else:
stepper.set_target:
id: my_stepper
target: 0
- platform: gpio
name: Button2
pin:
number: GPIO34
inverted: true
mode: INPUT_PULLUP
on_press:
script.execute: stop_at_current_position
- platform: gpio
name: Sensor1
pin:
number: GPIO22
inverted: true
- platform: gpio
name: SensorGPIO0
pin:
number: GPIO0
inverted: true
- platform: gpio
name: Sensor2
pin:
number: GPIO32
inverted: true
- platform: gpio
id: stall_guard_sensor
name: StallGuard
pin: GPIO2
on_press:
script.execute: stop_at_current_position
stepper:
- platform: tmc2209
id: my_stepper
step_pin: GPIO13
dir_pin: GPIO14
sleep_pin:
number: GPIO27
inverted: true
acceleration: ${acceleration}
max_speed: ${velocity}
script:
- id: stop_at_current_position
then:
stepper.set_target:
id: my_stepper
target: !lambda "return id(my_stepper).current_position;"
globals:
- id: open_position
type: float
initial_value: ${distance_mm} / (${pulley_diameter_mm} * PI) * 200 * ${microsteps}
cover:
- platform: template
id: template_cov
name: "${device_name} cover"
open_action:
- tmc2209.setup:
current: ${open_current}
stall_threshold: ${open_stall_threshold}
- stepper.set_target:
id: my_stepper
target: !lambda "return id(open_position);"
close_action:
- tmc2209.setup:
current: ${close_current}
stall_threshold: ${close_stall_threshold}
- stepper.set_target:
id: my_stepper
# 0 Means closed
target: 0
position_action:
- stepper.set_target:
id: my_stepper
target: !lambda "return id(open_position) * pos;"
stop_action:
- script.execute: stop_at_current_position
interval:
interval: 1s
then:
lambda: |-
static auto operation = COVER_OPERATION_IDLE;
static auto position = id(my_stepper).current_position;
if (operation != id(template_cov).current_operation ||
position != id(my_stepper).current_position)
{
ESP_LOGD("main", "Stepper Position is: %d/%d", id(my_stepper).current_position, (int)id(open_position));
if (id(my_stepper).current_position > id(my_stepper).target_position)
operation = COVER_OPERATION_CLOSING;
else if (id(my_stepper).current_position < id(my_stepper).target_position)
operation = COVER_OPERATION_OPENING;
else
operation = COVER_OPERATION_IDLE;
id(template_cov).current_operation = operation;
position = id(my_stepper).current_position;
id(template_cov).position = position / id(open_position);
id(template_cov).publish_state();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment