2022-02-16 22:06:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "pico/stdlib.h"
|
2022-02-20 11:00:36 +00:00
|
|
|
#include "pwm_cluster.hpp"
|
2022-02-16 22:06:07 +00:00
|
|
|
#include "servo_state.hpp"
|
|
|
|
|
2022-03-18 12:06:35 +00:00
|
|
|
using namespace pimoroni;
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
namespace servo {
|
|
|
|
|
|
|
|
class ServoCluster {
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
2022-03-18 12:06:35 +00:00
|
|
|
PWMCluster pwms;
|
2022-02-20 15:12:02 +00:00
|
|
|
uint32_t pwm_period;
|
2022-03-02 18:59:17 +00:00
|
|
|
float pwm_frequency;
|
2022-03-10 17:09:06 +00:00
|
|
|
ServoState* states;
|
2022-03-07 22:27:43 +00:00
|
|
|
float* servo_phases;
|
2022-02-16 22:06:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-03-18 12:06:35 +00:00
|
|
|
ServoCluster(PIO pio, uint sm, uint pin_mask, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr);
|
|
|
|
ServoCluster(PIO pio, uint sm, uint pin_base, uint pin_count, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr);
|
|
|
|
ServoCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr);
|
|
|
|
ServoCluster(PIO pio, uint sm, std::initializer_list<uint8_t> pins, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr);
|
2022-03-21 23:51:15 +00:00
|
|
|
|
|
|
|
ServoCluster(PIO pio, uint sm, uint pin_mask, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr);
|
|
|
|
ServoCluster(PIO pio, uint sm, uint pin_base, uint pin_count, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr);
|
|
|
|
ServoCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr);
|
|
|
|
ServoCluster(PIO pio, uint sm, std::initializer_list<uint8_t> pins, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr);
|
2022-02-16 22:06:07 +00:00
|
|
|
~ServoCluster();
|
|
|
|
|
2022-03-08 14:26:57 +00:00
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
|
|
|
bool init();
|
|
|
|
|
2022-03-07 22:27:43 +00:00
|
|
|
uint8_t get_count() const;
|
|
|
|
uint8_t get_pin(uint8_t servo) const;
|
2022-03-02 17:36:00 +00:00
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
void enable(uint servo, bool load = true);
|
2022-03-10 17:09:06 +00:00
|
|
|
void enable(const uint8_t *servos, uint8_t length, bool load = true);
|
|
|
|
void enable(std::initializer_list<uint8_t> servos, bool load = true);
|
|
|
|
void enable_all(bool load = true);
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
void disable(uint servo, bool load = true);
|
2022-03-10 17:09:06 +00:00
|
|
|
void disable(const uint8_t *servos, uint8_t length, bool load = true);
|
|
|
|
void disable(std::initializer_list<uint8_t> servos, bool load = true);
|
|
|
|
void disable_all(bool load = true);
|
2022-03-10 20:34:59 +00:00
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
bool is_enabled(uint servo) const;
|
2022-02-16 22:06:07 +00:00
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
float get_pulse(uint servo) const;
|
2022-02-16 22:06:07 +00:00
|
|
|
void set_pulse(uint servo, float pulse, bool load = true);
|
2022-03-10 20:34:59 +00:00
|
|
|
void set_pulse(const uint8_t *servos, uint8_t length, float pulse, bool load = true);
|
|
|
|
void set_pulse(std::initializer_list<uint8_t> servos, float pulse, bool load = true);
|
|
|
|
void set_all_pulses(float pulse, bool load = true);
|
2022-02-16 22:06:07 +00:00
|
|
|
|
2022-03-08 14:26:57 +00:00
|
|
|
float get_value(uint servo) const;
|
|
|
|
void set_value(uint servo, float value, bool load = true);
|
2022-03-10 20:34:59 +00:00
|
|
|
void set_value(const uint8_t *servos, uint8_t length, float value, bool load = true);
|
|
|
|
void set_value(std::initializer_list<uint8_t> servos, float value, bool load = true);
|
|
|
|
void set_all_values(float value, bool load = true);
|
2022-03-08 14:26:57 +00:00
|
|
|
|
2022-03-07 22:27:43 +00:00
|
|
|
float get_phase(uint servo) const;
|
|
|
|
void set_phase(uint servo, float phase, bool load = true);
|
2022-03-10 20:34:59 +00:00
|
|
|
void set_phase(const uint8_t *servos, uint8_t length, float phase, bool load = true);
|
|
|
|
void set_phase(std::initializer_list<uint8_t> servos, float phase, bool load = true);
|
|
|
|
void set_all_phases(float phase, bool load = true);
|
2022-03-07 16:26:20 +00:00
|
|
|
|
2022-02-20 15:12:02 +00:00
|
|
|
float get_frequency() const;
|
|
|
|
bool set_frequency(float freq);
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
2022-02-17 22:38:59 +00:00
|
|
|
float get_min_value(uint servo) const;
|
|
|
|
float get_mid_value(uint servo) const;
|
|
|
|
float get_max_value(uint servo) const;
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
void to_min(uint servo, bool load = true);
|
2022-03-10 20:34:59 +00:00
|
|
|
void to_min(const uint8_t *servos, uint8_t length, bool load = true);
|
|
|
|
void to_min(std::initializer_list<uint8_t> servos, bool load = true);
|
|
|
|
void all_to_min(bool load = true);
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
void to_mid(uint servo, bool load = true);
|
2022-03-10 20:34:59 +00:00
|
|
|
void to_mid(const uint8_t *servos, uint8_t length, bool load = true);
|
|
|
|
void to_mid(std::initializer_list<uint8_t> servos, bool load = true);
|
|
|
|
void all_to_mid(bool load = true);
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
void to_max(uint servo, bool load = true);
|
2022-03-10 20:34:59 +00:00
|
|
|
void to_max(const uint8_t *servos, uint8_t length, bool load = true);
|
|
|
|
void to_max(std::initializer_list<uint8_t> servos, bool load = true);
|
|
|
|
void all_to_max(bool load = true);
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
void to_percent(uint servo, float in, float in_min = ServoState::ZERO_PERCENT, float in_max = ServoState::ONEHUNDRED_PERCENT, bool load = true);
|
2022-03-10 20:34:59 +00:00
|
|
|
void to_percent(const uint8_t *servos, uint8_t length, float in, float in_min = ServoState::ZERO_PERCENT, float in_max = ServoState::ONEHUNDRED_PERCENT, bool load = true);
|
|
|
|
void to_percent(std::initializer_list<uint8_t> servos, float in, float in_min = ServoState::ZERO_PERCENT, float in_max = ServoState::ONEHUNDRED_PERCENT, bool load = true);
|
|
|
|
void all_to_percent(float in, float in_min = ServoState::ZERO_PERCENT, float in_max = ServoState::ONEHUNDRED_PERCENT, bool load = true);
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
void to_percent(uint servo, float in, float in_min, float in_max, float value_min, float value_max, bool load = true);
|
2022-03-10 20:34:59 +00:00
|
|
|
void to_percent(const uint8_t *servos, uint8_t length, float in, float in_min, float in_max, float value_min, float value_max, bool load = true);
|
|
|
|
void to_percent(std::initializer_list<uint8_t> servos, float in, float in_min, float in_max, float value_min, float value_max, bool load = true);
|
|
|
|
void all_to_percent(float in, float in_min, float in_max, float value_min, float value_max, bool load = true);
|
2022-02-16 22:06:07 +00:00
|
|
|
|
2022-03-08 16:54:20 +00:00
|
|
|
Calibration& calibration(uint servo);
|
|
|
|
const Calibration& calibration(uint servo) const;
|
2022-02-20 15:12:02 +00:00
|
|
|
|
2022-03-08 18:15:59 +00:00
|
|
|
void load();
|
|
|
|
|
2022-02-20 15:12:02 +00:00
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
|
|
|
void apply_pulse(uint servo, float pulse, bool load);
|
2022-03-07 22:27:43 +00:00
|
|
|
void create_servo_states(CalibrationType default_type, bool auto_phase);
|
2022-03-21 23:51:15 +00:00
|
|
|
void create_servo_states(const Calibration& calibration, bool auto_phase);
|
2022-02-16 22:06:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|