Skip to content

Commit 1c2b802

Browse files
authored
servo: Add function SetAngleWithMicroseconds (#695)
servo: Add function `SetAngleWithMicroseconds`,Adjust the angle by customizing the control pulse width
1 parent ce80e75 commit 1c2b802

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

servo/servo.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,16 @@ func (s Servo) SetAngle(angle int) error {
102102

103103
return nil
104104
}
105+
106+
// SetAngleWithMicroseconds sets the angle of the servo in degrees. The angle should be between
107+
// 0 and 180, where 0 is the minimum angle and 180 is the maximum angle.
108+
// The high duration can be customized
109+
// 0° is lowMicroseconds(us), 180° is highMicroseconds(us)
110+
func (s Servo) SetAngleWithMicroseconds(angle int, lowMicroseconds, highMicroseconds int) error {
111+
if angle < 0 || angle > 180 {
112+
return ErrInvalidAngle
113+
}
114+
microseconds := lowMicroseconds + (highMicroseconds-lowMicroseconds)*angle/180
115+
s.SetMicroseconds(int16(microseconds))
116+
return nil
117+
}

0 commit comments

Comments
 (0)