2022-02-16 00:40:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
#include "hardware/pwm.h"
|
2022-02-16 22:06:07 +00:00
|
|
|
#include "servo_state.hpp"
|
2022-02-16 00:40:42 +00:00
|
|
|
|
|
|
|
namespace servo {
|
|
|
|
|
|
|
|
class Servo {
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
2022-03-27 12:53:10 +00:00
|
|
|
uint servo_pin;
|
2022-03-02 18:59:17 +00:00
|
|
|
ServoState state;
|
2022-02-16 00:40:42 +00:00
|
|
|
pwm_config pwm_cfg;
|
|
|
|
uint16_t pwm_period;
|
2022-03-02 18:59:17 +00:00
|
|
|
float pwm_frequency;
|
2022-02-16 00:40:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-03-02 18:59:17 +00:00
|
|
|
Servo(uint pin, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY);
|
2022-03-21 23:51:15 +00:00
|
|
|
Servo(uint pin, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY);
|
2022-02-16 00:40:42 +00:00
|
|
|
~Servo();
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
|
|
|
bool init();
|
|
|
|
|
2022-02-17 17:59:09 +00:00
|
|
|
// For print access in micropython
|
2022-03-27 12:53:10 +00:00
|
|
|
uint pin() const;
|
2022-02-17 17:59:09 +00:00
|
|
|
|
2022-02-16 10:28:47 +00:00
|
|
|
void enable();
|
|
|
|
void disable();
|
2022-02-17 22:38:59 +00:00
|
|
|
bool is_enabled() const;
|
2022-02-16 00:40:42 +00:00
|
|
|
|
2022-03-27 12:53:10 +00:00
|
|
|
float pulse() const;
|
|
|
|
void pulse(float pulse);
|
2022-02-16 00:40:42 +00:00
|
|
|
|
2022-03-27 12:53:10 +00:00
|
|
|
float value() const;
|
|
|
|
void value(float value);
|
2022-03-08 14:26:57 +00:00
|
|
|
|
2022-03-27 12:53:10 +00:00
|
|
|
float frequency() const;
|
|
|
|
bool frequency(float freq);
|
2022-02-19 01:07:19 +00:00
|
|
|
|
2022-02-20 15:12:02 +00:00
|
|
|
//--------------------------------------------------
|
2022-03-27 12:53:10 +00:00
|
|
|
float min_value() const;
|
|
|
|
float mid_value() const;
|
|
|
|
float max_value() const;
|
2022-02-17 22:38:59 +00:00
|
|
|
|
2022-02-16 10:28:47 +00:00
|
|
|
void to_min();
|
|
|
|
void to_mid();
|
|
|
|
void to_max();
|
2022-02-17 22:38:59 +00:00
|
|
|
void to_percent(float in, float in_min = ServoState::ZERO_PERCENT, float in_max = ServoState::ONEHUNDRED_PERCENT);
|
2022-02-16 10:28:47 +00:00
|
|
|
void 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-20 15:12:02 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
|
|
|
void apply_pulse(float pulse);
|
2022-02-16 00:40:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|