From 1c2b802f47d47e1c6c12dd47ad8380f247c207c9 Mon Sep 17 00:00:00 2001 From: PWND0U <42665365+PWND0U@users.noreply.github.com> Date: Wed, 23 Oct 2024 21:56:41 +0800 Subject: [PATCH] servo: Add function `SetAngleWithMicroseconds` (#695) servo: Add function `SetAngleWithMicroseconds`,Adjust the angle by customizing the control pulse width --- servo/servo.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/servo/servo.go b/servo/servo.go index 73ef8e007..e073aa130 100644 --- a/servo/servo.go +++ b/servo/servo.go @@ -102,3 +102,16 @@ func (s Servo) SetAngle(angle int) error { return nil } + +// SetAngleWithMicroseconds sets the angle of the servo in degrees. The angle should be between +// 0 and 180, where 0 is the minimum angle and 180 is the maximum angle. +// The high duration can be customized +// 0° is lowMicroseconds(us), 180° is highMicroseconds(us) +func (s Servo) SetAngleWithMicroseconds(angle int, lowMicroseconds, highMicroseconds int) error { + if angle < 0 || angle > 180 { + return ErrInvalidAngle + } + microseconds := lowMicroseconds + (highMicroseconds-lowMicroseconds)*angle/180 + s.SetMicroseconds(int16(microseconds)) + return nil +}