ESPHome  2024.11.2
stepper.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace stepper {
9 
10 #define LOG_STEPPER(this) \
11  ESP_LOGCONFIG(TAG, " Acceleration: %.0f steps/s^2", this->acceleration_); \
12  ESP_LOGCONFIG(TAG, " Deceleration: %.0f steps/s^2", this->deceleration_); \
13  ESP_LOGCONFIG(TAG, " Max Speed: %.0f steps/s", this->max_speed_);
14 
15 class Stepper {
16  public:
17  void set_target(int32_t steps) { this->target_position = steps; }
18  void report_position(int32_t steps) { this->current_position = steps; }
19  void set_acceleration(float acceleration) { this->acceleration_ = acceleration; }
20  void set_deceleration(float deceleration) { this->deceleration_ = deceleration; }
21  void set_max_speed(float max_speed) { this->max_speed_ = max_speed; }
22  virtual void on_update_speed() {}
23  bool has_reached_target() { return this->current_position == this->target_position; }
24 
25  int32_t current_position{0};
26  int32_t target_position{0};
27 
28  protected:
29  void calculate_speed_(uint32_t now);
30  int32_t should_step_();
31 
32  float acceleration_{1e6f};
33  float deceleration_{1e6f};
34  float current_speed_{0.0f};
35  float max_speed_{1e6f};
36  uint32_t last_calculation_{0};
37  uint32_t last_step_{0};
38 };
39 
40 template<typename... Ts> class SetTargetAction : public Action<Ts...> {
41  public:
42  explicit SetTargetAction(Stepper *parent) : parent_(parent) {}
43 
44  TEMPLATABLE_VALUE(int32_t, target)
45 
46  void play(Ts... x) override { this->parent_->set_target(this->target_.value(x...)); }
47 
48  protected:
50 };
51 
52 template<typename... Ts> class ReportPositionAction : public Action<Ts...> {
53  public:
54  explicit ReportPositionAction(Stepper *parent) : parent_(parent) {}
55 
57 
58  void play(Ts... x) override { this->parent_->report_position(this->position_.value(x...)); }
59 
60  protected:
62 };
63 
64 template<typename... Ts> class SetSpeedAction : public Action<Ts...> {
65  public:
66  explicit SetSpeedAction(Stepper *parent) : parent_(parent) {}
67 
68  TEMPLATABLE_VALUE(float, speed);
69 
70  void play(Ts... x) override {
71  float speed = this->speed_.value(x...);
72  this->parent_->set_max_speed(speed);
73  this->parent_->on_update_speed();
74  }
75 
76  protected:
78 };
79 
80 template<typename... Ts> class SetAccelerationAction : public Action<Ts...> {
81  public:
82  explicit SetAccelerationAction(Stepper *parent) : parent_(parent) {}
83 
84  TEMPLATABLE_VALUE(float, acceleration);
85 
86  void play(Ts... x) override {
87  float acceleration = this->acceleration_.value(x...);
88  this->parent_->set_acceleration(acceleration);
89  }
90 
91  protected:
93 };
94 
95 template<typename... Ts> class SetDecelerationAction : public Action<Ts...> {
96  public:
97  explicit SetDecelerationAction(Stepper *parent) : parent_(parent) {}
98 
99  TEMPLATABLE_VALUE(float, deceleration);
100 
101  void play(Ts... x) override {
102  float deceleration = this->deceleration_.value(x...);
103  this->parent_->set_deceleration(deceleration);
104  }
105 
106  protected:
108 };
109 
110 } // namespace stepper
111 } // namespace esphome
void set_acceleration(float acceleration)
Definition: stepper.h:19
void play(Ts... x) override
Definition: stepper.h:86
SetTargetAction(Stepper *parent)
Definition: stepper.h:42
void set_max_speed(float max_speed)
Definition: stepper.h:21
TEMPLATABLE_VALUE(int32_t, target) void play(Ts... x) override
Definition: stepper.h:44
virtual void on_update_speed()
Definition: stepper.h:22
uint32_t last_calculation_
Definition: stepper.h:36
void calculate_speed_(uint32_t now)
Definition: stepper.cpp:10
uint16_t x
Definition: tt21100.cpp:17
ReportPositionAction(Stepper *parent)
Definition: stepper.h:54
int speed
Definition: fan.h:35
void set_deceleration(float deceleration)
Definition: stepper.h:20
void set_target(int32_t steps)
Definition: stepper.h:17
void play(Ts... x) override
Definition: stepper.h:101
SetDecelerationAction(Stepper *parent)
Definition: stepper.h:97
SetSpeedAction(Stepper *parent)
Definition: stepper.h:66
SetAccelerationAction(Stepper *parent)
Definition: stepper.h:82
TEMPLATABLE_VALUE(int32_t, position) void play(Ts... x) override
Definition: stepper.h:56
void play(Ts... x) override
Definition: stepper.h:70
void report_position(int32_t steps)
Definition: stepper.h:18
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
float position
Definition: cover.h:14