added slow mode and detach time to servo (#1413)

* added slow mode and detach time to servo

* tidy

* and again tidy

* add change requests

* tidy

* tidy

* tidy

Co-authored-by: Andreas Hergert <andreas.hergert@otrs.com>
This commit is contained in:
Andreas Hergert
2021-01-03 21:57:30 +01:00
committed by GitHub
parent e49b568fd4
commit 16d11be213
3 changed files with 81 additions and 13 deletions
+17 -12
View File
@@ -14,18 +14,9 @@ extern uint32_t global_servo_id;
class Servo : public Component {
public:
void set_output(output::FloatOutput *output) { output_ = output; }
void write(float value) {
value = clamp(value, -1.0f, 1.0f);
float level;
if (value < 0.0)
level = lerp(-value, this->idle_level_, this->min_level_);
else
level = lerp(value, this->idle_level_, this->max_level_);
this->output_->set_level(level);
this->save_level_(level);
}
void loop() override;
void write(float value);
void internal_write(float value);
void detach() {
this->output_->set_level(0.0f);
this->save_level_(0.0f);
@@ -48,6 +39,8 @@ class Servo : public Component {
void set_idle_level(float idle_level) { idle_level_ = idle_level; }
void set_max_level(float max_level) { max_level_ = max_level; }
void set_restore(bool restore) { restore_ = restore; }
void set_auto_detach_time(uint32_t auto_detach_time) { auto_detach_time_ = auto_detach_time; }
void set_transition_length(uint32_t transition_length) { transition_length_ = transition_length; }
protected:
void save_level_(float v) { this->rtc_.save(&v); }
@@ -57,7 +50,19 @@ class Servo : public Component {
float idle_level_ = 0.0750f;
float max_level_ = 0.1200f;
bool restore_{false};
uint32_t auto_detach_time_ = 0;
uint32_t transition_length_ = 0;
ESPPreferenceObject rtc_;
uint8_t state_;
float target_value_ = 0;
float source_value_ = 0;
float current_value_ = 0;
uint32_t start_millis_ = 0;
enum State {
STATE_ATTACHED = 0,
STATE_DETACHED = 1,
STATE_TARGET_REACHED = 2,
};
};
template<typename... Ts> class ServoWriteAction : public Action<Ts...> {