gpio

package
v0.51.2 Latest Latest
Warning

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

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

Documentation

Overview

Package gpio implements a GPIO based motor.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMotor

func NewMotor(b board.Board, mc Config, name resource.Name, logger logging.Logger) (motor.Motor, error)

NewMotor constructs a new GPIO based motor on the given board using the given configuration.

func WrapMotorWithEncoder

func WrapMotorWithEncoder(
	ctx context.Context,
	e encoder.Encoder,
	c resource.Config,
	mc Config,
	m motor.Motor,
	logger logging.Logger,
) (motor.Motor, error)

WrapMotorWithEncoder takes a motor and adds an encoder onto it in order to understand its odometry.

Types

type Config

type Config struct {
	Pins              PinConfig       `json:"pins"`
	BoardName         string          `json:"board"`
	MinPowerPct       float64         `json:"min_power_pct,omitempty"` // min power percentage to allow for this motor default is 0.0
	MaxPowerPct       float64         `json:"max_power_pct,omitempty"` // max power percentage to allow for this motor (0.06 - 1.0)
	PWMFreq           uint            `json:"pwm_freq,omitempty"`
	DirectionFlip     bool            `json:"dir_flip,omitempty"`  // Flip the direction of the signal sent if there is a Dir pin
	Encoder           string          `json:"encoder,omitempty"`   // name of encoder
	RampRate          float64         `json:"ramp_rate,omitempty"` // how fast to ramp power to motor when using rpm control
	MaxRPM            float64         `json:"max_rpm,omitempty"`
	TicksPerRotation  int             `json:"ticks_per_rotation,omitempty"`
	ControlParameters *motorPIDConfig `json:"control_parameters,omitempty"`
}

Config describes the configuration of a motor.

func (*Config) Validate added in v0.1.6

func (conf *Config) Validate(path string) ([]string, error)

Validate ensures all parts of the config are valid.

type EncodedMotor

type EncodedMotor struct {
	resource.Named
	resource.AlwaysRebuild
	// contains filtered or unexported fields
}

EncodedMotor is a motor that utilizes an encoder to track its position.

func (*EncodedMotor) Close

func (m *EncodedMotor) Close(ctx context.Context) error

Close cleanly shuts down the motor.

func (*EncodedMotor) GoFor

func (m *EncodedMotor) GoFor(ctx context.Context, rpm, revolutions 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.

func (*EncodedMotor) GoTo

func (m *EncodedMotor) GoTo(ctx context.Context, rpm, targetPosition 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.

func (*EncodedMotor) IsMoving

func (m *EncodedMotor) IsMoving(ctx context.Context) (bool, error)

IsMoving returns if the motor is moving or not.

func (*EncodedMotor) IsPowered

func (m *EncodedMotor) IsPowered(ctx context.Context, extra map[string]interface{}) (bool, float64, 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).

func (*EncodedMotor) Position

func (m *EncodedMotor) Position(ctx context.Context, extra map[string]interface{}) (float64, error)

Position reports the position of the 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.

func (*EncodedMotor) Properties

func (m *EncodedMotor) Properties(ctx context.Context, extra map[string]interface{}) (motor.Properties, error)

Properties returns whether or not the motor supports certain optional properties.

func (*EncodedMotor) ResetZeroPosition

func (m *EncodedMotor) ResetZeroPosition(ctx context.Context, offset float64, extra map[string]interface{}) error

ResetZeroPosition sets the current position (+/- offset) to be the new zero (home) position.

func (*EncodedMotor) SetPower

func (m *EncodedMotor) SetPower(ctx context.Context, powerPct float64, extra map[string]interface{}) error

SetPower sets the percentage of power the motor should employ between -1 and 1. Negative power implies a backward directional rotational.

func (*EncodedMotor) SetRPM added in v0.29.0

func (m *EncodedMotor) SetRPM(ctx context.Context, rpm float64, extra map[string]interface{}) error

SetRPM instructs the motor to move at the specified RPM indefinitely.

func (*EncodedMotor) Stop

func (m *EncodedMotor) Stop(ctx context.Context, extra map[string]interface{}) error

Stop stops makeAdjustments and stops the real motor.

type Motor

type Motor struct {
	resource.Named
	resource.AlwaysRebuild
	resource.TriviallyCloseable

	// config
	Board                    board.Board
	A, B, Direction, PWM, En board.GPIOPin
	EnablePinLow             board.GPIOPin
	EnablePinHigh            board.GPIOPin
	// contains filtered or unexported fields
}

A Motor is a GPIO based Motor that resides on a GPIO Board.

func (*Motor) DirectionMoving added in v0.26.0

func (m *Motor) DirectionMoving() int64

DirectionMoving returns the direction we are currently moving in, with 1 representing forward and -1 representing backwards.

func (*Motor) GoFor

func (m *Motor) GoFor(ctx context.Context, rpm, revolutions float64, extra map[string]interface{}) error

GoFor moves an inputted number of revolutions at the given rpm, no encoder is present for this so power is determined via a linear relationship with the maxRPM and the distance traveled is a time based estimation based on desired RPM.

func (*Motor) GoTo

func (m *Motor) GoTo(ctx context.Context, rpm, positionRevolutions float64, extra map[string]interface{}) error

GoTo is not supported.

func (*Motor) IsMoving

func (m *Motor) IsMoving(ctx context.Context) (bool, error)

IsMoving returns if the motor is currently on or off.

func (*Motor) IsPowered

func (m *Motor) IsPowered(ctx context.Context, extra map[string]interface{}) (bool, float64, error)

IsPowered returns if the motor is currently on or off.

func (*Motor) Position

func (m *Motor) Position(ctx context.Context, extra map[string]interface{}) (float64, error)

Position always returns 0.

func (*Motor) Properties

func (m *Motor) Properties(ctx context.Context, extra map[string]interface{}) (motor.Properties, error)

Properties returns the status of whether the motor supports certain optional properties.

func (*Motor) ResetZeroPosition

func (m *Motor) ResetZeroPosition(ctx context.Context, offset float64, extra map[string]interface{}) error

ResetZeroPosition is not supported.

func (*Motor) SetPower

func (m *Motor) SetPower(ctx context.Context, powerPct float64, extra map[string]interface{}) error

SetPower instructs the motor to operate at an rpm, where the sign of the rpm indicates direction.

func (*Motor) SetRPM added in v0.29.0

func (m *Motor) SetRPM(ctx context.Context, rpm float64, extra map[string]interface{}) error

SetRPM instructs the motor to move at the specified RPM indefinitely.

func (*Motor) Stop

func (m *Motor) Stop(ctx context.Context, extra map[string]interface{}) error

Stop turns the power to the motor off immediately, without any gradual step down, by setting the appropriate pins to low states.

type MotorType added in v0.13.0

type MotorType int

MotorType represents the three accepted pin configuration settings supported by a gpio motor.

const (
	// ABPwm uses A and B direction pins and a pin for pwm signal.
	ABPwm MotorType = iota
	// DirectionPwm uses a single direction pin and a pin for pwm signal.
	DirectionPwm
	// AB uses a pwm signal on pin A for moving forwards and pin B for moving backwards.
	AB
)

ABPwm, DirectionPwm, and AB represent the three pin setups supported by a gpio motor.

type PinConfig

type PinConfig struct {
	A             string `json:"a,omitempty"`
	B             string `json:"b,omitempty"`
	Direction     string `json:"dir,omitempty"`
	PWM           string `json:"pwm,omitempty"`
	EnablePinHigh string `json:"en_high,omitempty"`
	EnablePinLow  string `json:"en_low,omitempty"`
}

PinConfig defines the mapping of where motor are wired.

func (*PinConfig) MotorType added in v0.13.0

func (conf *PinConfig) MotorType(path string) (MotorType, error)

MotorType deduces the type of motor from the pin configuration.

Jump to

Keyboard shortcuts

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