27 if (this->parent_->is_failed())
32 bool reachable = !this->parent_->is_unreachable();
33 auto read = [
this, &reachable](uint8_t reg) -> optional<uint32_t> {
36 auto data = this->parent_->read_register(reg);
37 reachable = data.has_value();
41 if (this->actual_current_sensor_ !=
nullptr) {
42 auto drv_status = read(tmc22xx::REG_DRV_STATUS);
44 if (drv_status.has_value()) {
46 current = this->parent_->scale_to_current(scale);
48 this->actual_current_sensor_->publish_state(current);
52 bool moving = !this->parent_->has_reached_target();
53 if (this->stallguard_result_sensor_ !=
nullptr || (this->motor_load_sensor_ !=
nullptr && moving)) {
54 auto sg_result = read(REG_SG_RESULT);
55 publish_field(this->stallguard_result_sensor_, sg_result, SG_RESULT);
56 if (this->motor_load_sensor_ !=
nullptr) {
58 float stall_result = 2.0f * this->parent_->get_stallguard_threshold();
60 if (moving && sg_result.has_value() && stall_result < STALLGUARD_RESULT_MAX) {
62 load = (STALLGUARD_RESULT_MAX - result) / (STALLGUARD_RESULT_MAX - stall_result) * 100.0f;
64 this->motor_load_sensor_->publish_state(load);
66 }
else if (this->motor_load_sensor_ !=
nullptr) {
67 this->motor_load_sensor_->publish_state(NAN);
70 if (this->pwm_scale_sum_sensor_ !=
nullptr || this->pwm_scale_auto_sensor_ !=
nullptr) {
71 auto pwm_scale = read(tmc22xx::REG_PWM_SCALE);
72 publish_field(this->pwm_scale_sum_sensor_, pwm_scale, tmc22xx::PWM_SCALE_SUM);
73 publish_field(this->pwm_scale_auto_sensor_, pwm_scale, tmc22xx::PWM_SCALE_AUTO);
76 if (this->pwm_ofs_auto_sensor_ !=
nullptr || this->pwm_grad_auto_sensor_ !=
nullptr) {
77 auto pwm_auto = read(tmc22xx::REG_PWM_AUTO);
78 publish_field(this->pwm_ofs_auto_sensor_, pwm_auto, tmc22xx::PWM_OFS_AUTO);
79 publish_field(this->pwm_grad_auto_sensor_, pwm_auto, tmc22xx::PWM_GRAD_AUTO);
84 ESP_LOGCONFIG(TAG,
"TMC2209 Sensor:");
85 LOG_UPDATE_INTERVAL(
this);
86 LOG_SENSOR(
" ",
"Actual Current", this->actual_current_sensor_);
87 LOG_SENSOR(
" ",
"StallGuard Result", this->stallguard_result_sensor_);
88 LOG_SENSOR(
" ",
"Motor Load", this->motor_load_sensor_);
89 LOG_SENSOR(
" ",
"PWM Scale Sum", this->pwm_scale_sum_sensor_);
90 LOG_SENSOR(
" ",
"PWM Scale Auto", this->pwm_scale_auto_sensor_);
91 LOG_SENSOR(
" ",
"PWM Offset Auto", this->pwm_ofs_auto_sensor_);
92 LOG_SENSOR(
" ",
"PWM Gradient Auto", this->pwm_grad_auto_sensor_);