Skip to content

Commit

Permalink
fixed initial mode
Browse files Browse the repository at this point in the history
  • Loading branch information
googolgl committed May 26, 2020
1 parent e9763b9 commit 77f4e62
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ func main() {
log.Fatal(err)
}

pca0 := pca9685.PCANew(i2c, nil)
err = pca0.Init()
if err != nil {
log.Fatal(err)
pca0 := pca9685.New(i2c, nil)
if err := pca0.Init(); err != nil {
log.Fatal(err)
}

// Sets frequency for channel 0
Expand All @@ -45,16 +44,14 @@ func main() {

// Angle in degrees. Must be in the range `0` to `Range`
// Rotates from 0 to 130 degrees
servo1 := ServoNew(pca0, 0, nil)
servo1 := pca0.ServoNew(0, nil)
for i := 0; i < 130; i++ {
servo1.Angle(i)
time.Sleep(10 * time.Millisecond)
}

// Fraction as pulse width expressed between 0.0 `MinPulse` and 1.0 `MaxPulse`
servo1.Fraction(0.5)

pca0.DeInit()
}
```

Expand Down
Binary file removed docs/PCA9685_ru.pdf
Binary file not shown.
Binary file not shown.
11 changes: 6 additions & 5 deletions pca9685.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ type Options struct {
ClockSpeed float32
}

// PCANew creates a new driver with specified i2c interface
func PCANew(i2c *i2c.I2C, optn *Options) *PCA9685 {
// New creates a new driver with specified i2c interface
func New(i2c *i2c.I2C, optn *Options) *PCA9685 {
adr := i2c.GetAddr()
pca := &PCA9685{
Conn: i2c,
Expand All @@ -82,6 +82,7 @@ func (pca *PCA9685) Init() (err error) {
if pca.Conn.GetAddr() == 0 {
return fmt.Errorf(`device %v is not initiated`, pca.Optn.Name)
}
pca.Conn.WriteRegU8(Mode1, 0x00|0xA1) // Mode 1, autoincrement on)
return pca.SetFreq(pca.Optn.Frequency)
}

Expand All @@ -106,16 +107,16 @@ func (pca *PCA9685) SetFreq(freq float32) (err error) {
return err
}
time.Sleep(5 * time.Millisecond)
return pca.Conn.WriteRegU8(Mode1, oldMode|0xA1) // Mode 1, autoincrement on)
return
}

// GetFreq returns frequency value
func (pca *PCA9685) GetFreq() float32 {
return pca.Optn.Frequency
}

// DeInit reset the chip
func (pca *PCA9685) DeInit() (err error) {
// Reset reset the chip
func (pca *PCA9685) Reset() (err error) {
return pca.Conn.WriteRegU8(Mode1, 0x00)
}

Expand Down
19 changes: 12 additions & 7 deletions servo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ const (

// Servo structure
type Servo struct {
PWM *PCA9685
Channel uint8
PCA *PCA9685
Channel int
Options *ServOptions
}

Expand All @@ -53,10 +53,10 @@ type ServOptions struct {
MaxPulse float32
}

// ServNew creates a new servo driver
func ServoNew(p *PCA9685, chn uint8, o *ServOptions) *Servo {
// ServoNew creates a new servo driver
func (pca *PCA9685) ServoNew(chn int, o *ServOptions) *Servo {
s := &Servo{
PWM: p,
PCA: pca,
Channel: chn,
Options: &ServOptions{
Range: ServoRangeDef,
Expand Down Expand Up @@ -86,12 +86,17 @@ func (s *Servo) Fraction(f float32) (err error) {
return fmt.Errorf("Must be 0.0 to 1.0")
}

freq := s.PWM.GetFreq()
freq := s.PCA.GetFreq()

minDuty := s.Options.MinPulse * freq / 1000000 * 0xFFFF
maxDuty := s.Options.MaxPulse * freq / 1000000 * 0xFFFF
dutyRange := maxDuty - minDuty
dutyCycle := (int(minDuty+f*dutyRange) + 1) >> 4

return s.PWM.SetChannel(int(s.Channel), 0, dutyCycle)
return s.PCA.SetChannel(s.Channel, 0, dutyCycle)
}

// Reset servo
func (s *Servo) Reset() (err error) {
return s.PCA.SetChannel(s.Channel, 0, 0)
}

0 comments on commit 77f4e62

Please sign in to comment.