motor

package
v0.52.0-rc0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: AGPL-3.0 Imports: 16 Imported by: 3

Documentation

Overview

Package motor contains a gRPC bases motor client

Package motor defines machines that convert electricity into rotary motion. For more information, see the motor component docs.

Package motor contains a struct representing optional motor properties

Package motor contains a gRPC based motor service server

Index

Constants

View Source
const SubtypeName = "motor"

SubtypeName is a constant that identifies the component resource API string "motor".

Variables

View Source
var API = resource.APINamespaceRDK.WithComponentType(SubtypeName)

API is a variable that identifies the component resource API.

Functions

func CheckRevolutions added in v0.42.0

func CheckRevolutions(revs float64) error

CheckRevolutions checks if the input revolutions is non-zero.

func CheckSpeed added in v0.30.0

func CheckSpeed(rpm, max float64) (string, error)

CheckSpeed checks if the input rpm is too slow or fast and returns a warning and/or error.

func ClampPower added in v0.46.0

func ClampPower(pwr float64) float64

ClampPower clamps a percentage power to 1.0 or -1.0.

func CreateStatus

func CreateStatus(ctx context.Context, m Motor) (*pb.Status, error)

CreateStatus creates a status from the motor.

func GetRequestedDirection added in v0.42.0

func GetRequestedDirection(rpm, revolutions float64) float64

GetRequestedDirection returns the direction based on the rpm and revolutions.

func GetSign added in v0.46.0

func GetSign(x float64) float64

GetSign returns the sign of the float as a helper for getting the intended direction of travel of a motor.

func Named

func Named(name string) resource.Name

Named is a helper for getting the named Motor's typed resource name.

func NamesFromRobot

func NamesFromRobot(r robot.Robot) []string

NamesFromRobot is a helper for getting all motor names from the given Robot.

func NewControlParametersUnimplementedError added in v0.26.0

func NewControlParametersUnimplementedError() error

NewControlParametersUnimplementedError returns an error when a control parameters are unimplemented in the config being used of a controlledMotor.

func NewGoToUnsupportedError added in v0.2.8

func NewGoToUnsupportedError(motorName string) error

NewGoToUnsupportedError returns error when a motor is required to support GoTo feature.

func NewPropertyUnsupportedError added in v0.6.0

func NewPropertyUnsupportedError(prop Properties, motorName string) error

NewPropertyUnsupportedError returns an error representing the need for a motor to support a particular property.

func NewRPCServiceServer added in v0.2.36

func NewRPCServiceServer(coll resource.APIResourceCollection[Motor]) interface{}

NewRPCServiceServer constructs a motor gRPC service server. It is intentionally untyped to prevent use outside of tests.

func NewResetZeroPositionUnsupportedError

func NewResetZeroPositionUnsupportedError(motorName string) error

NewResetZeroPositionUnsupportedError returns a standard error for when a motor is required to support reseting the zero position.

func NewSetRPMUnsupportedError added in v0.29.0

func NewSetRPMUnsupportedError(motorName string) error

NewSetRPMUnsupportedError returns an error when a motor does not support SetRPM.

func NewZeroRPMError added in v0.1.4

func NewZeroRPMError() error

NewZeroRPMError returns an error representing a request to move a motor at zero speed (i.e., moving the motor without moving the motor).

func NewZeroRevsError added in v0.42.0

func NewZeroRevsError() error

NewZeroRevsError returns an error representing a request to move a motor for 0 revolutions.

func PropertiesToProtoResponse added in v0.6.0

func PropertiesToProtoResponse(
	props Properties,
) (*pb.GetPropertiesResponse, error)

PropertiesToProtoResponse takes a Properties struct (indicating whether the property is supported) and converts it to a GetPropertiesResponse.

Types

type Motor

type Motor interface {
	resource.Resource
	resource.Actuator

	// SetPower sets the percentage of power the motor should employ between -1 and 1.
	// Negative power corresponds to a backward direction of rotation.
	SetPower(ctx context.Context, powerPct float64, extra map[string]interface{}) error

	// GoFor instructs the motor to go in a specific direction for a specific amount of
	// revolutions at a given speed in revolutions per minute. Both the RPM and the revolutions
	// can be assigned negative values to move in a backwards direction. Note: if both are
	// negative the motor will spin in the forward direction.
	// If revolutions != 0, this will block until the number of revolutions has been completed or another operation comes in.
	GoFor(ctx context.Context, rpm, revolutions float64, extra map[string]interface{}) error

	// GoTo instructs the motor to go to a specific position (provided in revolutions from home/zero),
	// at a specific speed. Regardless of the directionality of the RPM this function will move the motor
	// towards the specified target/position.
	// This will block until the position has been reached.
	GoTo(ctx context.Context, rpm, positionRevolutions float64, extra map[string]interface{}) error

	// SetRPM instructs the motor to move at the specified RPM indefinitely.
	SetRPM(ctx context.Context, rpm float64, extra map[string]interface{}) error

	// Set an encoded motor's current position (+/- offset) to be the new zero (home) position.
	ResetZeroPosition(ctx context.Context, offset float64, extra map[string]interface{}) error

	// Position reports the position of an encoded motor based on its encoder. If it's not supported,
	// the returned data is undefined. The unit returned is the number of revolutions which is
	// intended to be fed back into calls of GoFor.
	Position(ctx context.Context, extra map[string]interface{}) (float64, error)

	// Properties returns whether or not the motor supports certain optional properties.
	Properties(ctx context.Context, extra map[string]interface{}) (Properties, error)

	// IsPowered returns whether or not the motor is currently on, and the percent power (between 0
	// and 1, if the motor is off then the percent power will be 0).
	IsPowered(ctx context.Context, extra map[string]interface{}) (bool, float64, error)
}

A Motor represents a physical motor connected to a board. For more information, see the motor component docs.

SetPower example:

myMotorComponent, err := motor.FromRobot(machine, "my_motor")
// Set the motor power to 40% forwards.
myMotorComponent.SetPower(context.Background(), 0.4, nil)

GoFor example:

myMotorComponent, err := motor.FromRobot(machine, "my_motor")
// Turn the motor 7.2 revolutions at 60 RPM.
myMotorComponent.GoFor(context.Background(), 60, 7.2, nil)

GoTo example:

// Turn the motor to 8.3 revolutions from home at 75 RPM.
myMotorComponent.GoTo(context.Background(), 75, 8.3, nil)

SetRPM example:

// Set the motor's RPM to 50
myMotorComponent.SetRPM(context.Background(), 50, nil)

ResetZeroPosition example:

// Set the current position as the new home position with no offset.
myMotorComponent.ResetZeroPosition(context.Background(), 0.0, nil)

Position example:

// Get the current position of an encoded motor.
position, err := myMotorComponent.Position(context.Background(), nil)

// Log the position
logger.Info("Position:")
logger.Info(position)

Properties example:

// Return whether or not the motor supports certain optional features.
properties, err := myMotorComponent.Properties(context.Background(), nil)

// Log the properties.
logger.Info("Properties:")
logger.Info(properties)

IsPowered example:

// Check whether the motor is currently running.
powered, pct, err := myMotorComponent.IsPowered(context.Background(), nil)

logger.Info("Is powered?")
logger.Info(powered)
logger.Info("Power percent:")
logger.Info(pct)

func FromDependencies

func FromDependencies(deps resource.Dependencies, name string) (Motor, error)

FromDependencies is a helper for getting the named motor from a collection of dependencies.

func FromRobot

func FromRobot(r robot.Robot, name string) (Motor, error)

FromRobot is a helper for getting the named motor from the given Robot.

func NewClientFromConn

func NewClientFromConn(
	ctx context.Context,
	conn rpc.ClientConn,
	remoteName string,
	name resource.Name,
	logger logging.Logger,
) (Motor, error)

NewClientFromConn constructs a new Client from connection passed in.

type Properties added in v0.6.0

type Properties struct {
	PositionReporting bool
}

Properties is struct contaning the motor properties.

func ProtoFeaturesToProperties added in v0.6.0

func ProtoFeaturesToProperties(resp *pb.GetPropertiesResponse) Properties

ProtoFeaturesToProperties takes a GetPropertiesResponse and returns an equivalent Properties struct.

Directories

Path Synopsis
Package fake implements a fake motor.
Package fake implements a fake motor.
Package gpio implements a GPIO based motor.
Package gpio implements a GPIO based motor.
Package gpiostepper implements a GPIO based stepper motor
Package gpiostepper implements a GPIO based stepper motor
Package register registers all relevant motors
Package register registers all relevant motors

Jump to

Keyboard shortcuts

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