Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adaptors(pwm): introduce scale option for servo #1046

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
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
16 changes: 9 additions & 7 deletions examples/tinkerboard_servo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/adaptors"
"gobot.io/x/gobot/v2/platforms/tinkerboard"
)

Expand All @@ -25,15 +26,16 @@ func main() {

fiftyHzNanos = 20 * 1000 * 1000 // 50Hz = 0.02 sec = 20 ms
)
adaptor := tinkerboard.NewAdaptor()
// usually a frequency of 50Hz is used for servos, most servos have 0.5 ms..2.5 ms for 0-180°,
// however the mapping can be changed with options:
adaptor := tinkerboard.NewAdaptor(
adaptors.WithPWMDefaultPeriodForPin(pwmPin, fiftyHzNanos),
adaptors.WithPWMServoDutyCycleRangeForPin(pwmPin, time.Millisecond, 2*time.Millisecond),
adaptors.WithPWMServoAngleRangeForPin(pwmPin, 0, 270),
)
servo := gpio.NewServoDriver(adaptor, pwmPin)

work := func() {
// usually a frequency of 50Hz is used for servos
if err := adaptor.SetPeriod(pwmPin, fiftyHzNanos); err != nil {
log.Println(err)
}

fmt.Printf("first move to minimal position for %s...\n", wait)
if err := servo.ToMin(); err != nil {
log.Println(err)
Expand All @@ -55,7 +57,7 @@ func main() {

time.Sleep(wait)

fmt.Println("finally move 0-180° and back forever...")
fmt.Println("finally move 0-180° (or what your servo do for the new mapping) and back forever...")
angle := 0
fadeAmount := 45

Expand Down
Loading