Skip to content

Commit 246db2d

Browse files
committed
refactor: modified direction inverted angle calculation and order
1 parent f5ffa59 commit 246db2d

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

types.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ func (h *DefaultHandler) GetAngle() uint16 {
179179
//
180180
// angle: The angle to set the servo motor to, must be between 0 and the actuation range
181181
func (h *DefaultHandler) SetAngle(angle uint16) tinygoerrors.ErrorCode {
182+
// Check if the direction is inverted
183+
if h.isDirectionInverted {
184+
angle = h.actuationRange - angle
185+
}
186+
182187
// Check if the angle is within the valid range
183188
if angle < h.centerAngle-h.leftLimitAngle || angle > h.centerAngle+h.rightLimitAngle {
184189
return ErrorCodeServoAngleOutOfRange
@@ -189,11 +194,6 @@ func (h *DefaultHandler) SetAngle(angle uint16) tinygoerrors.ErrorCode {
189194
return tinygoerrors.ErrorCodeNil
190195
}
191196

192-
// Check if the direction is inverted
193-
if h.isDirectionInverted {
194-
angle = h.rightLimitAngle - (angle - h.leftLimitAngle)
195-
}
196-
197197
// Update the current angle
198198
h.angle = angle
199199

0 commit comments

Comments
 (0)