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"
|
2022-02-16 22:06:07 +00:00
|
|
|
#include "calibration.hpp"
|
|
|
|
#include "servo_state.hpp"
|
2022-02-16 00:40:42 +00:00
|
|
|
|
|
|
|
namespace servo {
|
|
|
|
|
|
|
|
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 22:06:07 +00:00
|
|
|
ServoState state;
|
2022-02-16 00:40:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// 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
|
|
|
};
|
|
|
|
|
|
|
|
}
|