Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions components/servo/gpio/servo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
minWidthUs uint = 500 // absolute minimum PWM width
maxWidthUs uint = 2500 // absolute maximum PWM width
defaultFreq uint = 300
minFreqHz uint = 50
maxFreqHz uint = 450
)

// We want to distinguish values that are 0 because the user set them to 0 from ones that are 0
Expand Down Expand Up @@ -187,9 +189,9 @@ func (s *servoGPIO) Reconfigure(ctx context.Context, deps resource.Dependencies,
}

if newConf.Frequency != nil {
if *newConf.Frequency > 450 || *newConf.Frequency < 50 {
if *newConf.Frequency > maxFreqHz || *newConf.Frequency < minFreqHz {
return errors.Errorf(
"PWM frequencies should not be above 450Hz or below 50, have %d", newConf.Frequency)
"PWM frequencies should not be above %dHz or below %dHz, have %dHz", maxFreqHz, minFreqHz, newConf.Frequency)
}

s.frequency = *newConf.Frequency
Expand All @@ -200,8 +202,12 @@ func (s *servoGPIO) Reconfigure(ctx context.Context, deps resource.Dependencies,
// microsecond, but rarely over 10. Call it 50 microseconds just to be safe.
const maxDeadbandWidthUs = 50
if maxFrequency := 1e6 / (s.maxUs + maxDeadbandWidthUs); s.frequency > maxFrequency {
s.logger.CWarnf(ctx, "servo frequency (%f.1) is above maximum (%f.1), setting to max instead",
s.frequency, maxFrequency)
// if the frequency wasn't set in the config we will default to whatever the driver sets as default frequency
// we silence the warning below if clamping the frequency was needed
if newConf.Frequency != nil {
s.logger.CWarnf(ctx, "servo frequency (%dHz) is above maximum (%dHz), setting to max instead",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dunno: if the default is above the maximum, I think we should log warnings about that.

Reconfigure() shouldn't run often; this shouldn't spam the logs. Why do you want to hide it?

s.frequency, maxFrequency)
}
s.frequency = maxFrequency
}

Expand Down