kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Added frequency limits
rodzic
334ff4e9f9
commit
d1238e802e
|
@ -71,7 +71,6 @@ namespace servo {
|
||||||
pwm_set_gpio_level(pin, (uint16_t)ServoState::pulse_to_level(new_pulse, pwm_period, pwm_frequency));
|
pwm_set_gpio_level(pin, (uint16_t)ServoState::pulse_to_level(new_pulse, pwm_period, pwm_frequency));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
float Servo::get_frequency() const {
|
float Servo::get_frequency() const {
|
||||||
return pwm_frequency;
|
return pwm_frequency;
|
||||||
}
|
}
|
||||||
|
@ -79,6 +78,7 @@ namespace servo {
|
||||||
bool Servo::set_frequency(float freq) {
|
bool Servo::set_frequency(float freq) {
|
||||||
bool success = false;
|
bool success = false;
|
||||||
|
|
||||||
|
if((freq >= MIN_FREQUENCY) && (freq <= MAX_FREQUENCY)) {
|
||||||
// Calculate a suitable pwm wrap period for this frequency
|
// Calculate a suitable pwm wrap period for this frequency
|
||||||
uint16_t period; uint16_t div16;
|
uint16_t period; uint16_t div16;
|
||||||
if(Servo::calculate_pwm_factors(freq, period, div16)) {
|
if(Servo::calculate_pwm_factors(freq, period, div16)) {
|
||||||
|
@ -115,6 +115,7 @@ namespace servo {
|
||||||
|
|
||||||
success = true;
|
success = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,12 @@ namespace servo {
|
||||||
// Constants
|
// Constants
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
public:
|
public:
|
||||||
static const uint16_t DEFAULT_FREQUENCY = 50; //The standard servo update rate
|
static constexpr float DEFAULT_FREQUENCY = 50.0f; // The standard servo update rate
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static constexpr float MIN_FREQUENCY = 10.0f; // Lowest achievable with hardware PWM with good resolution
|
||||||
|
static constexpr float MAX_FREQUENCY = 350.0f; // Highest nice value that still allows the full uS pulse range
|
||||||
|
// Some servos are rated for 333Hz for instance
|
||||||
static const uint32_t MAX_PWM_WRAP = UINT16_MAX;
|
static const uint32_t MAX_PWM_WRAP = UINT16_MAX;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -664,7 +664,7 @@ extern mp_obj_t Servo_frequency(size_t n_args, const mp_obj_t *pos_args, mp_map_
|
||||||
float freq = mp_obj_get_float(args[ARG_freq].u_obj);
|
float freq = mp_obj_get_float(args[ARG_freq].u_obj);
|
||||||
|
|
||||||
if(!self->servo->set_frequency(freq))
|
if(!self->servo->set_frequency(freq))
|
||||||
mp_raise_ValueError("freq out of range");
|
mp_raise_ValueError("freq out of range. Expected 10Hz to 350Hz");
|
||||||
else
|
else
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue