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