tinygo_servo

package module
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2025 License: GPL-3.0 Imports: 4 Imported by: 0

README

tinygo-servo

Servo wrapper for TinyGo projects

Documentation

Index

Constants

View Source
const (
	ErrorCodeServoFailedToConfigurePWM tinygoerrors.ErrorCode = tinygoerrors.ErrorCode(iota + ErrorCodeServoStartNumber)
	ErrorCodeServoZeroFrequency
	ErrorCodeServoAngleOutOfRange
	ErrorCodeServoInvalidMinPulseWidth
	ErrorCodeServoInvalidMaxPulseWidth
	ErrorCodeServoNilHandler
	ErrorCodeServoUnknownDirection
	ErrorCodeServoFailedToGetPWMChannel
	ErrorCodeServoInvalidActuationRange
	ErrorCodeServoInvalidCenterAngle
)
View Source
const (
	// ErrorCodeServoStartNumber is the starting number for servo-related error codes.
	ErrorCodeServoStartNumber uint16 = 5230
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultHandler

type DefaultHandler struct {
	// contains filtered or unexported fields
}

DefaultHandler is the default implementation of the Servo interface

func NewDefaultHandler

func NewDefaultHandler(
	pwm tinygopwm.PWM,
	pin machine.Pin,
	afterSetAngleFunc func(angle uint16),
	isMovementEnabled func() bool,
	frequency uint16,
	minPulseWidth uint32,
	maxPulseWidth uint32,
	actuationRange uint16,
	centerAngle uint16,
	maxLeftAngle uint16,
	maxRightAngle uint16,
	isDirectionInverted bool,
	logger tinygologger.Logger,
) (*DefaultHandler, tinygoerrors.ErrorCode)

NewDefaultHandler creates a new instance of DefaultHandler

Parameters:

pwm: The PWM interface to control the servo pin: The pin connected to the servo afterSetAngleFunc: A callback function to be called after setting the angle isMovementEnabled: A function to check if movement is enabled frequency: The frequency of the PWM signal minPulseWidth: The minimum pulse width for the servo motor maxPulseWidth: The maximum pulse width for the servo motor centerAngle: The center angle of the servo motor maxLeftAngle: The maximum left angle from the center maxRightAngle: The maximum right angle from the center isDirectionInverted: Whether the direction of the servo motor is inverted logger: The logger instance for logging messages

Returns:

An instance of DefaultHandler and an error if any occurred during initialization

func (*DefaultHandler) GetAngle

func (h *DefaultHandler) GetAngle() uint16

GetAngle returns the current angle of the servo motor

Returns:

The current angle of the servo motor

func (*DefaultHandler) IsAngleCentered

func (h *DefaultHandler) IsAngleCentered() bool

IsAngleCentered checks if the servo motor angle is centered

Returns:

True if the servo motor is centered, false otherwise

func (*DefaultHandler) SetAngle

func (h *DefaultHandler) SetAngle(angle uint16) tinygoerrors.ErrorCode

SetAngle sets the angle of the servo motor

Parameters:

angle: The angle to set the servo motor to, must be between 0 and the actuation range

func (*DefaultHandler) SetAngleRelativeToCenter

func (h *DefaultHandler) SetAngleRelativeToCenter(relativeAngle int16) tinygoerrors.ErrorCode

SetAngleRelativeToCenter sets the angle of the servo motor relative to the center position

Parameters:

relativeAngle: The relative angle value between -90 and 90 degrees

Returns:

An error if the relative angle is not within the left and right limits

func (*DefaultHandler) SetAngleToCenter

func (h *DefaultHandler) SetAngleToCenter() tinygoerrors.ErrorCode

SetAngleToCenter centers the servo motor to the middle position

Returns:

An error if the servo motor could not be centered

func (*DefaultHandler) SetAngleToLeft

func (h *DefaultHandler) SetAngleToLeft(angle uint16) tinygoerrors.ErrorCode

SetAngleToLeft sets the servo motor to the left by a specified angle

Parameters:

angle: The angle value to move the servo to the left, must be between 0 and the left limit

Returns:

An error if the angle is not within the left limit

func (*DefaultHandler) SetAngleToRight

func (h *DefaultHandler) SetAngleToRight(angle uint16) tinygoerrors.ErrorCode

SetAngleToRight sets the servo motor to the right by a specified angle

Parameters:

angle: The angle value to move the servo to the right, must be between 0 and the right limit

Returns:

An error if the angle is not within the right limit

type Direction added in v0.1.1

type Direction uint8

Direction is an enum to represent the different servo directions for the vehicle.

const (
	DirectionNil Direction = iota
	DirectionLeft
	DirectionRight
	DirectionStraight
)

func (Direction) InvertedDirection added in v0.1.1

func (d Direction) InvertedDirection() Direction

InvertedDirection returns the inverted direction.

type Handler

type Handler interface {
	SetAngle(angle uint16) tinygoerrors.ErrorCode
	GetAngle() uint16
	SetAngleRelativeToCenter(relativeAngle int16) tinygoerrors.ErrorCode
	IsAngleCentered() bool
	SetAngleToCenter() tinygoerrors.ErrorCode
	SetAngleToRight(angle uint16) tinygoerrors.ErrorCode
	SetAngleToLeft(angle uint16) tinygoerrors.ErrorCode
}

Handler is the interface to handle servo operations

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL