2022-02-16 22:06:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
#include "calibration.hpp"
|
|
|
|
|
|
|
|
namespace servo {
|
|
|
|
|
|
|
|
class ServoState {
|
2022-02-17 22:38:59 +00:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Constants
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
|
|
|
static constexpr float ZERO_PERCENT = 0.0f;
|
|
|
|
static constexpr float ONEHUNDRED_PERCENT = 1.0f;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr float MIN_VALID_PULSE = 1.0f;
|
|
|
|
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
|
|
|
float servo_value = 0.0f;
|
|
|
|
float last_enabled_pulse = 0.0f;
|
|
|
|
bool enabled = false;
|
2022-02-17 22:38:59 +00:00
|
|
|
Calibration table;
|
2022-02-16 22:06:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-02-17 22:38:59 +00:00
|
|
|
ServoState(CalibrationType default_type = ANGULAR);
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
|
|
|
bool init();
|
|
|
|
|
|
|
|
float enable();
|
|
|
|
float disable();
|
2022-02-17 22:38:59 +00:00
|
|
|
bool is_enabled() const;
|
2022-02-19 19:25:15 +00:00
|
|
|
private:
|
|
|
|
float _enable(); // Internal version of enable without convenient initialisation to mid point
|
|
|
|
public:
|
2022-02-17 22:38:59 +00:00
|
|
|
float get_value() const;
|
2022-02-16 22:06:07 +00:00
|
|
|
float set_value(float value);
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
float get_pulse() const;
|
2022-02-16 22:06:07 +00:00
|
|
|
float set_pulse(float pulse);
|
|
|
|
|
2022-02-19 19:25:15 +00:00
|
|
|
public:
|
2022-02-17 22:38:59 +00:00
|
|
|
float get_min_value() const;
|
|
|
|
float get_mid_value() const;
|
|
|
|
float get_max_value() const;
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
float to_min();
|
|
|
|
float to_mid();
|
|
|
|
float to_max();
|
2022-02-17 22:38:59 +00:00
|
|
|
float to_percent(float in, float in_min = ZERO_PERCENT, float in_max = ONEHUNDRED_PERCENT);
|
2022-02-16 22:06:07 +00:00
|
|
|
float to_percent(float in, float in_min, float in_max, float value_min, float value_max);
|
|
|
|
|
|
|
|
Calibration& calibration();
|
2022-02-17 22:38:59 +00:00
|
|
|
const Calibration& calibration() const;
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
2022-02-19 01:07:19 +00:00
|
|
|
static uint32_t pulse_to_level(float pulse, uint32_t resolution, float freq);
|
2022-02-16 22:06:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|