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:
|
2022-02-20 15:12:02 +00:00
|
|
|
static constexpr float DEFAULT_FREQUENCY = 50.0f; // The standard servo update rate
|
|
|
|
static constexpr float MIN_FREQUENCY = 10.0f; // Lowest achievable with hardware PWM with good resolution
|
|
|
|
static constexpr float MAX_FREQUENCY = 350.0f; // Highest nice value that still allows the full uS pulse range
|
|
|
|
// Some servos are rated for 333Hz for instance
|
2022-02-17 22:38:59 +00:00
|
|
|
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:
|
2022-03-30 11:49:47 +01:00
|
|
|
float servo_value;
|
|
|
|
float last_enabled_pulse;
|
|
|
|
bool enabled;
|
2022-03-21 23:51:15 +00:00
|
|
|
Calibration calib;
|
2022-02-16 22:06:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-03-21 23:51:15 +00:00
|
|
|
ServoState();
|
|
|
|
ServoState(CalibrationType default_type);
|
|
|
|
ServoState(const Calibration& calibration);
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-03-30 11:49:47 +01:00
|
|
|
float enable_with_return();
|
|
|
|
float disable_with_return();
|
2022-02-17 22:38:59 +00:00
|
|
|
bool is_enabled() const;
|
2022-02-19 19:25:15 +00:00
|
|
|
private:
|
2022-03-30 11:49:47 +01:00
|
|
|
float _enable_with_return(); // Internal version of enable without convenient initialisation to the middle
|
2022-02-19 19:25:15 +00:00
|
|
|
public:
|
2022-02-17 22:38:59 +00:00
|
|
|
float get_pulse() const;
|
2022-03-27 13:53:10 +01:00
|
|
|
float set_pulse_with_return(float pulse);
|
2022-02-16 22:06:07 +00:00
|
|
|
|
2022-03-08 14:26:57 +00:00
|
|
|
float get_value() const;
|
2022-03-27 13:53:10 +01:00
|
|
|
float set_value_with_return(float value);
|
2022-03-08 14:26:57 +00:00
|
|
|
|
2022-03-30 11:49:47 +01:00
|
|
|
//--------------------------------------------------
|
2022-02-17 22:38:59 +00:00
|
|
|
float get_min_value() const;
|
|
|
|
float get_mid_value() const;
|
|
|
|
float get_max_value() const;
|
|
|
|
|
2022-03-27 13:53:10 +01:00
|
|
|
float to_min_with_return();
|
|
|
|
float to_mid_with_return();
|
|
|
|
float to_max_with_return();
|
|
|
|
float to_percent_with_return(float in, float in_min = ZERO_PERCENT, float in_max = ONEHUNDRED_PERCENT);
|
|
|
|
float to_percent_with_return(float in, float in_min, float in_max, float value_min, float value_max);
|
2022-02-16 22:06:07 +00:00
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
}
|