2022-02-16 22:06:07 +00:00
|
|
|
#include "servo_cluster.hpp"
|
2022-02-20 15:12:02 +00:00
|
|
|
#include "pwm.hpp"
|
|
|
|
#include <cstdio>
|
2022-02-16 22:06:07 +00:00
|
|
|
|
|
|
|
namespace servo {
|
2022-03-07 16:26:20 +00:00
|
|
|
ServoCluster::ServoCluster(PIO pio, uint sm, uint pin_mask, CalibrationType default_type, float freq, bool auto_phase)
|
|
|
|
: pwms(pio, sm, pin_mask), pwm_frequency(freq) {
|
2022-03-07 22:27:43 +00:00
|
|
|
create_servo_states(default_type, auto_phase);
|
2022-02-21 00:04:36 +00:00
|
|
|
}
|
2022-02-20 15:12:02 +00:00
|
|
|
|
2022-03-07 16:26:20 +00:00
|
|
|
ServoCluster::ServoCluster(PIO pio, uint sm, uint pin_base, uint pin_count, CalibrationType default_type, float freq, bool auto_phase)
|
|
|
|
: pwms(pio, sm, pin_base, pin_count), pwm_frequency(freq) {
|
2022-03-07 22:27:43 +00:00
|
|
|
create_servo_states(default_type, auto_phase);
|
2022-02-21 00:04:36 +00:00
|
|
|
}
|
2022-02-20 15:12:02 +00:00
|
|
|
|
2022-03-07 16:26:20 +00:00
|
|
|
ServoCluster::ServoCluster(PIO pio, uint sm, std::initializer_list<uint8_t> pins, CalibrationType default_type, float freq, bool auto_phase)
|
|
|
|
: pwms(pio, sm, pins), pwm_frequency(freq) {
|
2022-03-07 22:27:43 +00:00
|
|
|
create_servo_states(default_type, auto_phase);
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ServoCluster::~ServoCluster() {
|
2022-03-07 22:27:43 +00:00
|
|
|
delete[] servos;
|
|
|
|
delete[] servo_phases;
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ServoCluster::init() {
|
2022-02-21 00:04:36 +00:00
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
if(pwms.init()) {
|
|
|
|
// Calculate a suitable pwm wrap period for this frequency
|
|
|
|
uint32_t period; uint16_t div16;
|
|
|
|
if(pimoroni::PWMCluster::calculate_pwm_factors(pwm_frequency, period, div16)) {
|
|
|
|
pwm_period = period;
|
|
|
|
|
|
|
|
// Update the pwm before setting the new wrap
|
2022-03-07 22:27:43 +00:00
|
|
|
uint8_t servo_count = pwms.get_chan_count();
|
|
|
|
for(uint servo = 0; servo < servo_count; servo++) {
|
2022-02-21 00:04:36 +00:00
|
|
|
pwms.set_chan_level(servo, 0, false);
|
2022-03-07 16:26:20 +00:00
|
|
|
pwms.set_chan_offset(servo, (uint32_t)(servo_phases[servo] * (float)pwm_period), false);
|
2022-02-21 00:04:36 +00:00
|
|
|
}
|
2022-02-16 22:06:07 +00:00
|
|
|
|
2022-02-21 00:04:36 +00:00
|
|
|
// Set the new wrap (should be 1 less than the period to get full 0 to 100%)
|
2022-03-07 16:26:20 +00:00
|
|
|
pwms.set_wrap(pwm_period, true); // NOTE Minus 1 not needed here. Maybe should change Wrap behaviour so it is needed, for consistency with hardware pwm?
|
2022-02-16 22:06:07 +00:00
|
|
|
|
2022-02-21 00:04:36 +00:00
|
|
|
// Apply the new divider
|
|
|
|
// This is done after loading new PWM values to avoid a lockup condition
|
|
|
|
uint8_t div = div16 >> 4;
|
|
|
|
uint8_t mod = div16 % 16;
|
|
|
|
pwms.set_clkdiv_int_frac(div, mod);
|
2022-02-16 22:06:07 +00:00
|
|
|
|
2022-02-21 00:04:36 +00:00
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 22:06:07 +00:00
|
|
|
|
2022-02-21 00:04:36 +00:00
|
|
|
return success;
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 22:27:43 +00:00
|
|
|
uint8_t ServoCluster::get_count() const {
|
|
|
|
return pwms.get_chan_count();
|
2022-02-17 22:38:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 22:27:43 +00:00
|
|
|
uint8_t ServoCluster::get_pin(uint8_t servo) const {
|
|
|
|
return pwms.get_chan_pin(servo);
|
2022-03-02 17:36:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
void ServoCluster::enable(uint servo, bool load) {
|
2022-03-07 22:27:43 +00:00
|
|
|
assert(servo < pwms.get_chan_count());
|
|
|
|
float new_pulse = servos[servo].enable();
|
2022-03-02 17:36:00 +00:00
|
|
|
apply_pulse(servo, new_pulse, load);
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServoCluster::disable(uint servo, bool load) {
|
2022-03-07 22:27:43 +00:00
|
|
|
assert(servo < pwms.get_chan_count());
|
|
|
|
float new_pulse = servos[servo].disable();
|
2022-03-02 17:36:00 +00:00
|
|
|
apply_pulse(servo, new_pulse, load);
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
bool ServoCluster::is_enabled(uint servo) const {
|
2022-03-07 22:27:43 +00:00
|
|
|
assert(servo < pwms.get_chan_count());
|
|
|
|
return servos[servo].is_enabled();
|
2022-02-17 17:59:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
float ServoCluster::get_value(uint servo) const {
|
2022-03-07 22:27:43 +00:00
|
|
|
assert(servo < pwms.get_chan_count());
|
|
|
|
return servos[servo].get_value();
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServoCluster::set_value(uint servo, float value, bool load) {
|
2022-03-07 22:27:43 +00:00
|
|
|
assert(servo < pwms.get_chan_count());
|
|
|
|
float new_pulse = servos[servo].set_value(value);
|
2022-03-02 17:36:00 +00:00
|
|
|
apply_pulse(servo, new_pulse, load);
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
float ServoCluster::get_pulse(uint servo) const {
|
2022-03-07 22:27:43 +00:00
|
|
|
assert(servo < pwms.get_chan_count());
|
|
|
|
return servos[servo].get_pulse();
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServoCluster::set_pulse(uint servo, float pulse, bool load) {
|
2022-03-07 22:27:43 +00:00
|
|
|
assert(servo < pwms.get_chan_count());
|
|
|
|
float new_pulse = servos[servo].set_pulse(pulse);
|
2022-03-02 17:36:00 +00:00
|
|
|
apply_pulse(servo, new_pulse, load);
|
2022-02-20 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 22:27:43 +00:00
|
|
|
float ServoCluster::get_phase(uint servo) const {
|
|
|
|
assert(servo < pwms.get_chan_count());
|
2022-03-07 16:26:20 +00:00
|
|
|
return servo_phases[servo];
|
|
|
|
}
|
|
|
|
|
2022-03-07 22:27:43 +00:00
|
|
|
void ServoCluster::set_phase(uint servo, float phase, bool load) {
|
|
|
|
assert(servo < pwms.get_chan_count());
|
2022-03-07 16:26:20 +00:00
|
|
|
servo_phases[servo] = MIN(MAX(phase, 0.0f), 1.0f);
|
|
|
|
pwms.set_chan_offset(servo, (uint32_t)(servo_phases[servo] * (float)pwms.get_wrap()), load);
|
|
|
|
}
|
|
|
|
|
2022-03-07 22:27:43 +00:00
|
|
|
float ServoCluster::get_frequency() const {
|
2022-02-20 15:12:02 +00:00
|
|
|
return pwm_frequency;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ServoCluster::set_frequency(float freq) {
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
if((freq >= ServoState::MIN_FREQUENCY) && (freq <= ServoState::MAX_FREQUENCY)) {
|
|
|
|
// Calculate a suitable pwm wrap period for this frequency
|
|
|
|
uint32_t period; uint16_t div16;
|
|
|
|
if(pimoroni::PWMCluster::calculate_pwm_factors(freq, period, div16)) {
|
|
|
|
|
|
|
|
pwm_period = period;
|
|
|
|
pwm_frequency = freq;
|
|
|
|
|
|
|
|
// Update the pwm before setting the new wrap
|
2022-03-07 22:27:43 +00:00
|
|
|
uint8_t servo_count = pwms.get_chan_count();
|
|
|
|
for(uint servo = 0; servo < servo_count; servo++) {
|
|
|
|
float current_pulse = servos[servo].get_pulse();
|
|
|
|
apply_pulse(servo, current_pulse, false);
|
|
|
|
pwms.set_chan_offset(servo, (uint32_t)(servo_phases[servo] * (float)pwm_period), false);
|
2022-02-20 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the new wrap (should be 1 less than the period to get full 0 to 100%)
|
|
|
|
pwms.set_wrap(pwm_period, true);
|
|
|
|
|
|
|
|
// Apply the new divider
|
|
|
|
// This is done after loading new PWM values to avoid a lockup condition
|
|
|
|
uint8_t div = div16 >> 4;
|
|
|
|
uint8_t mod = div16 % 16;
|
|
|
|
pwms.set_clkdiv_int_frac(div, mod);
|
|
|
|
|
|
|
|
success = true;
|
|
|
|
}
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
2022-02-20 15:12:02 +00:00
|
|
|
return success;
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
float ServoCluster::get_min_value(uint servo) const {
|
2022-03-02 17:36:00 +00:00
|
|
|
assert(is_assigned(servo));
|
2022-03-07 22:27:43 +00:00
|
|
|
return servos[servo].get_min_value();
|
2022-02-17 22:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float ServoCluster::get_mid_value(uint servo) const {
|
2022-03-02 17:36:00 +00:00
|
|
|
assert(is_assigned(servo));
|
2022-03-07 22:27:43 +00:00
|
|
|
return servos[servo].get_mid_value();
|
2022-02-17 22:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float ServoCluster::get_max_value(uint servo) const {
|
2022-03-02 17:36:00 +00:00
|
|
|
assert(is_assigned(servo));
|
2022-03-07 22:27:43 +00:00
|
|
|
return servos[servo].get_max_value();
|
2022-02-17 22:38:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
void ServoCluster::to_min(uint servo, bool load) {
|
2022-03-02 17:36:00 +00:00
|
|
|
assert(is_assigned(servo));
|
2022-03-07 22:27:43 +00:00
|
|
|
float new_pulse = servos[servo].to_min();
|
2022-03-02 17:36:00 +00:00
|
|
|
apply_pulse(servo, new_pulse, load);
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServoCluster::to_mid(uint servo, bool load) {
|
2022-03-02 17:36:00 +00:00
|
|
|
assert(is_assigned(servo));
|
2022-03-07 22:27:43 +00:00
|
|
|
float new_pulse = servos[servo].to_mid();
|
2022-03-02 17:36:00 +00:00
|
|
|
apply_pulse(servo, new_pulse, load);
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServoCluster::to_max(uint servo, bool load) {
|
2022-03-02 17:36:00 +00:00
|
|
|
assert(is_assigned(servo));
|
2022-03-07 22:27:43 +00:00
|
|
|
float new_pulse = servos[servo].to_max();
|
2022-03-02 17:36:00 +00:00
|
|
|
apply_pulse(servo, new_pulse, load);
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServoCluster::to_percent(uint servo, float in, float in_min, float in_max, bool load) {
|
2022-03-02 17:36:00 +00:00
|
|
|
assert(is_assigned(servo));
|
2022-03-07 22:27:43 +00:00
|
|
|
float new_pulse = servos[servo].to_percent(in, in_min, in_max);
|
2022-03-02 17:36:00 +00:00
|
|
|
apply_pulse(servo, new_pulse, load);
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServoCluster::to_percent(uint servo, float in, float in_min, float in_max, float value_min, float value_max, bool load) {
|
2022-03-02 17:36:00 +00:00
|
|
|
assert(is_assigned(servo));
|
2022-03-07 22:27:43 +00:00
|
|
|
float new_pulse = servos[servo].to_percent(in, in_min, in_max, value_min, value_max);
|
2022-03-02 17:36:00 +00:00
|
|
|
apply_pulse(servo, new_pulse, load);
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Calibration* ServoCluster::calibration(uint servo) {
|
2022-03-02 17:36:00 +00:00
|
|
|
assert(is_assigned(servo));
|
2022-03-07 22:27:43 +00:00
|
|
|
return &servos[servo].calibration();
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
2022-02-17 22:38:59 +00:00
|
|
|
|
|
|
|
const Calibration* ServoCluster::calibration(uint servo) const {
|
2022-03-02 17:36:00 +00:00
|
|
|
assert(is_assigned(servo));
|
2022-03-07 22:27:43 +00:00
|
|
|
return &servos[servo].calibration();
|
2022-02-17 22:38:59 +00:00
|
|
|
}
|
2022-02-20 15:12:02 +00:00
|
|
|
|
|
|
|
void ServoCluster::apply_pulse(uint servo, float pulse, bool load) {
|
|
|
|
pwms.set_chan_level(servo, ServoState::pulse_to_level(pulse, pwm_period, pwm_frequency), load);
|
|
|
|
}
|
2022-03-07 16:26:20 +00:00
|
|
|
|
2022-03-07 22:27:43 +00:00
|
|
|
void ServoCluster::create_servo_states(CalibrationType default_type, bool auto_phase) {
|
2022-03-07 16:26:20 +00:00
|
|
|
uint8_t servo_count = pwms.get_chan_count();
|
2022-03-07 22:27:43 +00:00
|
|
|
if(servo_count > 0) {
|
|
|
|
servos = new ServoState[servo_count];
|
|
|
|
servo_phases = new float[servo_count];
|
|
|
|
|
|
|
|
for(uint servo = 0; servo < servo_count; servo++) {
|
|
|
|
servos[servo] = ServoState(default_type);
|
|
|
|
servo_phases[servo] = (auto_phase) ? (float)servo / (float)servo_count : 0.0f;
|
2022-03-07 16:26:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 22:06:07 +00:00
|
|
|
};
|