2022-02-16 22:06:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
|
|
|
|
namespace servo {
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
enum CalibrationType {
|
2022-02-16 22:06:07 +00:00
|
|
|
ANGULAR = 0,
|
|
|
|
LINEAR,
|
2022-03-21 23:51:15 +00:00
|
|
|
CONTINUOUS
|
2022-02-16 22:06:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Calibration {
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constants
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
|
|
|
static constexpr float DEFAULT_MIN_PULSE = 500.0f; // in microseconds
|
|
|
|
static constexpr float DEFAULT_MID_PULSE = 1500.0f; // in microseconds
|
|
|
|
static constexpr float DEFAULT_MAX_PULSE = 2500.0f; // in microseconds
|
2022-02-19 19:25:15 +00:00
|
|
|
|
2023-03-13 14:44:42 +00:00
|
|
|
static const uint MAX_CALIBRATION_PAIRS = 32;
|
|
|
|
|
2022-02-19 19:25:15 +00:00
|
|
|
private:
|
|
|
|
static constexpr float LOWER_HARD_LIMIT = 400.0f; // The minimum microsecond pulse to send
|
|
|
|
static constexpr float UPPER_HARD_LIMIT = 2600.0f; // The maximum microsecond pulse to send
|
2022-02-16 22:06:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Substructures
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-03-22 14:21:10 +00:00
|
|
|
struct Pair {
|
2022-02-16 22:06:07 +00:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
2022-03-22 14:21:10 +00:00
|
|
|
Pair();
|
|
|
|
Pair(float pulse, float value);
|
2022-02-16 22:06:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
float pulse;
|
|
|
|
float value;
|
|
|
|
};
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
2022-02-17 22:38:59 +00:00
|
|
|
public:
|
2022-02-16 22:06:07 +00:00
|
|
|
Calibration();
|
2022-02-17 22:38:59 +00:00
|
|
|
Calibration(CalibrationType default_type);
|
2022-03-08 16:54:20 +00:00
|
|
|
Calibration(const Calibration &other);
|
2022-02-16 22:06:07 +00:00
|
|
|
virtual ~Calibration();
|
|
|
|
|
|
|
|
|
2022-03-08 16:54:20 +00:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Operators
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-03-22 14:21:10 +00:00
|
|
|
Calibration &operator=(const Calibration &other);
|
|
|
|
Pair &operator[](uint8_t index);
|
|
|
|
const Pair &operator[](uint8_t index) const;
|
2022-03-08 16:54:20 +00:00
|
|
|
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-03-22 14:21:10 +00:00
|
|
|
void apply_blank_pairs(uint size);
|
|
|
|
void apply_two_pairs(float min_pulse, float max_pulse, float min_value, float max_value);
|
|
|
|
void apply_three_pairs(float min_pulse, float mid_pulse, float max_pulse, float min_value, float mid_value, float max_value);
|
|
|
|
void apply_uniform_pairs(uint size, float min_pulse, float max_pulse, float min_value, float max_value);
|
|
|
|
void apply_default_pairs(CalibrationType default_type);
|
2022-02-16 22:06:07 +00:00
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
uint size() const;
|
2022-03-22 14:21:10 +00:00
|
|
|
|
2022-03-27 11:48:58 +00:00
|
|
|
Pair &pair(uint8_t index); // Ensure the pairs are assigned in ascending value order
|
2022-03-22 14:21:10 +00:00
|
|
|
const Pair &pair(uint8_t index) const; // Ensure the pairs are assigned in ascending value order
|
2022-03-27 11:48:58 +00:00
|
|
|
float pulse(uint8_t index) const;
|
|
|
|
void pulse(uint8_t index, float pulse);
|
|
|
|
float value(uint8_t index) const;
|
|
|
|
void value(uint8_t index, float value);
|
|
|
|
|
|
|
|
Pair &first();
|
2022-03-22 14:21:10 +00:00
|
|
|
const Pair &first() const;
|
2022-03-27 11:48:58 +00:00
|
|
|
float first_pulse() const;
|
|
|
|
void first_pulse(float pulse);
|
|
|
|
float first_value() const;
|
|
|
|
void first_value(float value);
|
|
|
|
|
|
|
|
Pair &last();
|
2022-03-22 14:21:10 +00:00
|
|
|
const Pair &last() const;
|
2022-03-27 11:48:58 +00:00
|
|
|
float last_pulse() const;
|
|
|
|
void last_pulse(float pulse);
|
|
|
|
float last_value() const;
|
|
|
|
void last_value(float value);
|
2022-02-16 22:06:07 +00:00
|
|
|
|
2022-03-08 14:26:57 +00:00
|
|
|
bool has_lower_limit() const;
|
|
|
|
bool has_upper_limit() const;
|
2022-02-16 22:06:07 +00:00
|
|
|
void limit_to_calibration(bool lower, bool upper);
|
|
|
|
|
2022-02-19 19:25:15 +00:00
|
|
|
bool value_to_pulse(float value, float &pulse_out, float &value_out) const;
|
2022-02-19 20:13:18 +00:00
|
|
|
bool pulse_to_value(float pulse, float &value_out, float &pulse_out) const;
|
2022-02-17 22:38:59 +00:00
|
|
|
|
|
|
|
static float map_float(float in, float in_min, float in_max, float out_min, float out_max);
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
2022-03-08 14:26:57 +00:00
|
|
|
private:
|
2023-03-13 14:44:42 +00:00
|
|
|
Pair calibration[MAX_CALIBRATION_PAIRS];
|
2022-02-17 22:38:59 +00:00
|
|
|
uint calibration_size;
|
2022-02-16 22:06:07 +00:00
|
|
|
bool limit_lower;
|
|
|
|
bool limit_upper;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|