pca9685

package
v3.6.6+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package pca9685 includes utilities to controls pca9685 module and servo motors.

More details

Datasheet

https://www.nxp.com/docs/en/data-sheet/PCA9685.pdf

Product page:

https://www.nxp.com/products/analog/interfaces/ic-bus/ic-led-controllers/16-channel-12-bit-pwm-fm-plus-ic-bus-led-controller:PCA9685

Example
package main

import (
	"log"

	"periph.io/x/periph/conn/i2c/i2creg"
	"periph.io/x/periph/conn/physic"
	"periph.io/x/periph/experimental/devices/pca9685"
	"periph.io/x/periph/host"
)

func main() {
	_, err := host.Init()
	if err != nil {
		log.Fatal(err)
	}

	bus, err := i2creg.Open("")
	if err != nil {
		log.Fatal(err)
	}

	pca, err := pca9685.NewI2C(bus, pca9685.I2CAddr)
	if err != nil {
		log.Fatal(err)
	}

	if err := pca.SetPwmFreq(50 * physic.Hertz); err != nil {
		log.Fatal(err)
	}
	if err := pca.SetAllPwm(0, 0); err != nil {
		log.Fatal(err)
	}
	servos := pca9685.NewServoGroup(pca, 50, 650, 0, 180)

	// This is an example of using with an Me Arm robot arm
	gripServo := servos.GetServo(0)
	baseServo := servos.GetServo(1)
	elbowServo := servos.GetServo(2)
	shoulderServo := servos.GetServo(3)

	gripServo.SetMinMaxAngle(15, 120)
	elbowServo.SetMinMaxAngle(50, 110)    // Set limit of the robot arm
	shoulderServo.SetMinMaxAngle(60, 140) // Set limit of the robot arm

	// Set all in the middle in a MeArm robot arm
	if err := gripServo.SetAngle(90); err != nil {
		log.Fatal(err)
	}
	if err := baseServo.SetAngle(90); err != nil {
		log.Fatal(err)
	}
	if err := elbowServo.SetAngle(90); err != nil {
		log.Fatal(err)
	}
	if err := shoulderServo.SetAngle(90); err != nil {
		log.Fatal(err)
	}
}
Output:

Index

Examples

Constants

View Source
const I2CAddr uint16 = 0x40

I2CAddr i2c default address.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dev

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

Dev is a handler to pca9685 controller.

func NewI2C

func NewI2C(bus i2c.Bus, address uint16) (*Dev, error)

NewI2C returns a Dev object that communicates over I2C.

To use on the default address, pca9685.I2CAddr must be passed as argument.

func (*Dev) CreatePin

func (d *Dev) CreatePin(channel int) (gpio.PinIO, error)

CreatePin creates a gpio handle for the given channel.

func (*Dev) RegisterPins

func (d *Dev) RegisterPins() error

RegisterPins makes PWM channels available as PWM pins in the pin registry

Pin names have the following format: PCA9685_<HexAddress>_<channel> (e.g. PCA9685_40_11)

func (*Dev) SetAllPwm

func (d *Dev) SetAllPwm(on, off gpio.Duty) error

SetAllPwm set a PWM value for all outputs.

func (*Dev) SetFullOff

func (d *Dev) SetFullOff(channel int) error

SetFullOff sets PWM duty to 0%.

This function uses the dedicated bit to reduce bus traffic.

func (*Dev) SetFullOn

func (d *Dev) SetFullOn(channel int) error

SetFullOn sets PWM duty to 100%.

This function uses the dedicated FULL_ON bit.

func (*Dev) SetPwm

func (d *Dev) SetPwm(channel int, on, off gpio.Duty) error

SetPwm set a PWM value for a given PCA9685 channel.

func (*Dev) SetPwmFreq

func (d *Dev) SetPwmFreq(freqHz physic.Frequency) error

SetPwmFreq set the PWM frequency.

func (*Dev) UnregisterPins

func (d *Dev) UnregisterPins() error

UnregisterPins remove all previously created pin handles of this device from the GPIO registry

type Servo

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

Servo individual servo from a group of servos connected to a pca9685 module

func (*Servo) SetAngle

func (s *Servo) SetAngle(angle physic.Angle) error

SetAngle set an angle on the servo will consider the angle limits set

func (*Servo) SetMinMaxAngle

func (s *Servo) SetMinMaxAngle(min, max physic.Angle)

SetMinMaxAngle change angle limits for the servo

func (*Servo) SetPwm

func (s *Servo) SetPwm(pwm gpio.Duty) error

SetPwm set an pmw value to the servo

type ServoGroup

type ServoGroup struct {
	*Dev
	// contains filtered or unexported fields
}

ServoGroup a group of servos connected to a pca9685 module

func NewServoGroup

func NewServoGroup(dev *Dev, minPwm, maxPwm gpio.Duty, minAngle, maxAngle physic.Angle) *ServoGroup

NewServoGroup returns a servo group connected through the pca9685 module some pwm and angle limits can be set

func (*ServoGroup) GetServo

func (s *ServoGroup) GetServo(channel int) *Servo

GetServo returns a individual Servo to be controlled

func (*ServoGroup) SetAngle

func (s *ServoGroup) SetAngle(channel int, angle physic.Angle) error

SetAngle set an angle in a given channel of the servo group

func (*ServoGroup) SetMinMaxPwm

func (s *ServoGroup) SetMinMaxPwm(minAngle, maxAngle physic.Angle, minPwm, maxPwm gpio.Duty)

SetMinMaxPwm change pwm and angle limits

Jump to

Keyboard shortcuts

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