Skip to content

Commit

Permalink
Merge pull request hybridgroup#308 from jfinken/dev
Browse files Browse the repository at this point in the history
Adafruit Motor HAT driver support
  • Loading branch information
deadprogram authored Sep 16, 2016
2 parents 574dce7 + 409cdfb commit 5442e0b
Show file tree
Hide file tree
Showing 6 changed files with 826 additions and 0 deletions.
62 changes: 62 additions & 0 deletions examples/raspi_adafruit_dcmotor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"log"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/i2c"
"github.com/hybridgroup/gobot/platforms/raspi"
)

func adafruitDCMotorRunner(a *i2c.AdafruitMotorHatDriver, dcMotor int) (err error) {

log.Printf("DC Motor Run Loop...\n")
// set the speed:
var speed int32 = 255 // 255 = full speed!
if err = a.SetDCMotorSpeed(dcMotor, speed); err != nil {
return
}
// run FORWARD
if err = a.RunDCMotor(dcMotor, i2c.AdafruitForward); err != nil {
return
}
// Sleep and RELEASE
<-time.After(2000 * time.Millisecond)
if err = a.RunDCMotor(dcMotor, i2c.AdafruitRelease); err != nil {
return
}
// run BACKWARD
if err = a.RunDCMotor(dcMotor, i2c.AdafruitBackward); err != nil {
return
}
// Sleep and RELEASE
<-time.After(2000 * time.Millisecond)
if err = a.RunDCMotor(dcMotor, i2c.AdafruitRelease); err != nil {
return
}
return
}
func main() {
gbot := gobot.NewGobot()
r := raspi.NewRaspiAdaptor("raspi")
adaFruit := i2c.NewAdafruitMotorHatDriver(r, "adafruit")

work := func() {
gobot.Every(5*time.Second, func() {

dcMotor := 2 // 0-based
adafruitDCMotorRunner(adaFruit, dcMotor)
})
}

robot := gobot.NewRobot("adaFruitBot",
[]gobot.Connection{r},
[]gobot.Device{adaFruit},
work,
)

gbot.AddRobot(robot)

gbot.Start()
}
88 changes: 88 additions & 0 deletions examples/raspi_adafruit_servo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"log"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/i2c"
"github.com/hybridgroup/gobot/platforms/raspi"
)

var (
// Min pulse length out of 4096
servoMin = 150
// Max pulse length out of 4096
servoMax = 700
// Limiting the max this servo can rotate (in deg)
maxDegree = 180
// Number of degrees to increase per call
degIncrease = 10
yawDeg = 90
)

func degree2pulse(deg int) int32 {
pulse := servoMin
pulse += ((servoMax - servoMin) / maxDegree) * deg
return int32(pulse)
}
func adafruitServoMotorRunner(a *i2c.AdafruitMotorHatDriver) (err error) {
log.Printf("Servo Motor Run Loop...\n")
// Changing from the default 0x40 address because this configuration involves
// a Servo HAT stacked on top of a DC/Stepper Motor HAT on top of the Pi.
stackedHatAddr := 0x41
var channel byte = 1
deg := 90

// update the I2C address state
a.SetServoHatAddress(stackedHatAddr)

// Do not need to set this every run loop
freq := 60.0
if err = a.SetServoMotorFreq(freq); err != nil {
log.Printf("%s", err.Error())
return
}
// start in the middle of the 180-deg range
pulse := degree2pulse(deg)
if err = a.SetServoMotorPulse(channel, 0, pulse); err != nil {
log.Printf(err.Error())
return
}
// INCR
pulse = degree2pulse(deg + degIncrease)
if err = a.SetServoMotorPulse(channel, 0, pulse); err != nil {
log.Printf(err.Error())
return
}
<-time.After(2000 * time.Millisecond)
// DECR
pulse = degree2pulse(deg - degIncrease)
if err = a.SetServoMotorPulse(channel, 0, pulse); err != nil {
log.Printf(err.Error())
return
}
return
}
func main() {
gbot := gobot.NewGobot()
r := raspi.NewRaspiAdaptor("raspi")
adaFruit := i2c.NewAdafruitMotorHatDriver(r, "adafruit")

work := func() {
gobot.Every(5*time.Second, func() {

adafruitServoMotorRunner(adaFruit)
})
}

robot := gobot.NewRobot("adaFruitBot",
[]gobot.Connection{r},
[]gobot.Device{adaFruit},
work,
)

gbot.AddRobot(robot)

gbot.Start()
}
53 changes: 53 additions & 0 deletions examples/raspi_adafruit_stepper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"log"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/i2c"
"github.com/hybridgroup/gobot/platforms/raspi"
)

func adafruitStepperMotorRunner(a *i2c.AdafruitMotorHatDriver, motor int) (err error) {
log.Printf("Stepper Motor Run Loop...\n")
// set the speed state:
speed := 30 // rpm
style := i2c.AdafruitDouble
steps := 20

a.SetStepperMotorSpeed(motor, speed)

if err = a.Step(motor, steps, i2c.AdafruitForward, style); err != nil {
log.Printf(err.Error())
return
}
if err = a.Step(motor, steps, i2c.AdafruitBackward, style); err != nil {
log.Printf(err.Error())
return
}
return
}
func main() {
gbot := gobot.NewGobot()
r := raspi.NewRaspiAdaptor("raspi")
adaFruit := i2c.NewAdafruitMotorHatDriver(r, "adafruit")

work := func() {
gobot.Every(5*time.Second, func() {

motor := 0 // 0-based
adafruitStepperMotorRunner(adaFruit, motor)
})
}

robot := gobot.NewRobot("adaFruitBot",
[]gobot.Connection{r},
[]gobot.Device{adaFruit},
work,
)

gbot.AddRobot(robot)

gbot.Start()
}
Loading

0 comments on commit 5442e0b

Please sign in to comment.