2022-02-16 00:40:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
#include "hardware/pwm.h"
|
|
|
|
#include "hardware/clocks.h"
|
|
|
|
#include "common/pimoroni_common.hpp"
|
|
|
|
|
|
|
|
namespace servo {
|
|
|
|
|
2022-02-16 10:28:47 +00:00
|
|
|
enum Type {
|
|
|
|
ANGULAR = 0,
|
|
|
|
LINEAR,
|
|
|
|
CONTINUOUS
|
|
|
|
};
|
|
|
|
|
2022-02-16 00:40:42 +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
|
|
|
|
static constexpr float DEFAULT_VALUE_EXTENT = 90.0f; // a range of -90 to +90
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Substructures
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
|
|
|
struct CalibrationPoint {
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
CalibrationPoint();
|
|
|
|
CalibrationPoint(uint16_t pulse, float value);
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
float pulse;
|
|
|
|
float value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
protected:
|
2022-02-16 10:28:47 +00:00
|
|
|
Calibration(Type type);
|
2022-02-16 00:40:42 +00:00
|
|
|
virtual ~Calibration();
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-02-16 10:28:47 +00:00
|
|
|
void create_default_calibration(Type type);
|
2022-02-16 00:40:42 +00:00
|
|
|
bool create_blank_calibration(uint num_points); // Must have at least two points
|
2022-02-16 10:28:47 +00:00
|
|
|
void create_two_point_calibration(float min_pulse, float max_pulse, float min_value, float max_value);
|
|
|
|
void create_three_point_calibration(float min_pulse, float mid_pulse, float max_pulse, float min_value, float mid_value, float max_value);
|
2022-02-16 00:40:42 +00:00
|
|
|
bool create_uniform_calibration(uint num_points, float min_pulse, float min_value, float max_pulse, float max_value); // Must have at least two points
|
|
|
|
|
|
|
|
uint points();
|
|
|
|
bool get_point(uint8_t index, CalibrationPoint& point_out);
|
|
|
|
void set_point(uint8_t index, const CalibrationPoint& point); // Ensure the points are entered in ascending value order
|
|
|
|
|
|
|
|
void limit_to_calibration(bool lower, bool upper);
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
protected:
|
|
|
|
CalibrationPoint* calibration;
|
|
|
|
uint calibration_points;
|
|
|
|
bool limit_lower;
|
|
|
|
bool limit_upper;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Converter : public Calibration {
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constants
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
|
|
|
static constexpr float LOWER_HARD_LIMIT = 500.0f; // The minimum microsecond pulse to send
|
|
|
|
static constexpr float UPPER_HARD_LIMIT = 2500.0f; // The maximum microsecond pulse to send
|
|
|
|
static constexpr float SERVO_PERIOD = 1000000 / 50; // This is hardcoded as all servos *should* run at this frequency
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-02-16 10:28:47 +00:00
|
|
|
Converter(Type type) : Calibration(type) {}
|
2022-02-16 00:40:42 +00:00
|
|
|
virtual ~Converter() {}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-02-16 10:28:47 +00:00
|
|
|
float min_value();
|
|
|
|
float mid_value();
|
|
|
|
float max_value();
|
2022-02-16 00:40:42 +00:00
|
|
|
float value_to_pulse(float value);
|
2022-02-16 10:28:47 +00:00
|
|
|
float value_from_pulse(float pulse);
|
|
|
|
|
|
|
|
static uint32_t pulse_to_level(float pulse, uint32_t resolution);
|
|
|
|
static float map_float(float in, float in_min, float in_max, float out_min, float out_max);
|
2022-02-16 00:40:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Servo {
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constants
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
|
|
|
static const uint16_t DEFAULT_PWM_FREQUENCY = 50; //The standard servo update rate
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const uint32_t MAX_PWM_WRAP = UINT16_MAX;
|
|
|
|
static constexpr uint16_t MAX_PWM_DIVIDER = (1 << 7);
|
|
|
|
|
2022-02-16 10:28:47 +00:00
|
|
|
static constexpr float MIN_VALID_PULSE = 1.0f;
|
|
|
|
|
2022-02-16 00:40:42 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
|
|
|
uint pin;
|
|
|
|
pwm_config pwm_cfg;
|
|
|
|
uint16_t pwm_period;
|
|
|
|
float pwm_frequency = DEFAULT_PWM_FREQUENCY;
|
|
|
|
|
2022-02-16 10:28:47 +00:00
|
|
|
float servo_value = 0.0f;
|
|
|
|
float last_enabled_pulse = 0.0f;
|
|
|
|
bool enabled = false;
|
2022-02-16 00:40:42 +00:00
|
|
|
|
|
|
|
Converter converter;
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-02-16 10:28:47 +00:00
|
|
|
Servo(uint pin, Type type = ANGULAR);
|
2022-02-16 00:40:42 +00:00
|
|
|
~Servo();
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
|
|
|
bool init();
|
|
|
|
|
2022-02-16 10:28:47 +00:00
|
|
|
bool is_enabled();
|
|
|
|
void enable();
|
|
|
|
void disable();
|
2022-02-16 00:40:42 +00:00
|
|
|
|
2022-02-16 10:28:47 +00:00
|
|
|
float get_value();
|
2022-02-16 00:40:42 +00:00
|
|
|
void set_value(float value);
|
|
|
|
|
2022-02-16 10:28:47 +00:00
|
|
|
float get_pulse();
|
|
|
|
void set_pulse(float pulse);
|
2022-02-16 00:40:42 +00:00
|
|
|
|
2022-02-16 10:28:47 +00:00
|
|
|
void to_min();
|
|
|
|
void to_mid();
|
|
|
|
void to_max();
|
|
|
|
void to_percent(float in, float in_min = 0.0f, float in_max = 1.0f);
|
|
|
|
void to_percent(float in, float in_min, float in_max, float value_min, float value_max);
|
|
|
|
|
|
|
|
Calibration& calibration();
|
2022-02-16 00:40:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|