1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/examples/firmata_cat_toy.go

43 lines
1.2 KiB
Go
Raw Normal View History

2014-04-27 19:56:18 -07:00
package main
import (
"fmt"
"github.com/hybridgroup/gobot"
2014-05-22 19:22:14 -07:00
"github.com/hybridgroup/gobot/platforms/firmata"
2014-06-06 13:18:07 -07:00
"github.com/hybridgroup/gobot/platforms/gpio"
2014-05-22 19:22:14 -07:00
"github.com/hybridgroup/gobot/platforms/leap"
"time"
2014-04-27 19:56:18 -07:00
)
func main() {
2014-05-22 19:22:14 -07:00
gbot := gobot.NewGobot()
2014-04-27 19:56:18 -07:00
2014-05-22 19:22:14 -07:00
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")
servo1 := gpio.NewServoDriver(firmataAdaptor, "servo", "5")
servo2 := gpio.NewServoDriver(firmataAdaptor, "servo", "3")
2014-04-27 19:56:18 -07:00
2014-05-22 19:22:14 -07:00
leapAdaptor := leap.NewLeapMotionAdaptor("leap", "127.0.0.1:6437")
leapDriver := leap.NewLeapMotionDriver(leapAdaptor, "leap")
2014-04-27 19:56:18 -07:00
work := func() {
x := 90.0
z := 90.0
gobot.On(leapDriver.Events["Message"], func(data interface{}) {
if len(data.(leap.Frame).Hands) > 0 {
hand := data.(leap.Frame).Hands[0]
x = gobot.ToScale(gobot.FromScale(hand.X(), -300, 300), 30, 150)
z = gobot.ToScale(gobot.FromScale(hand.Z(), -300, 300), 30, 150)
}
})
2014-05-22 19:22:14 -07:00
gobot.Every(0.01*time.Second, func() {
2014-04-27 19:56:18 -07:00
servo1.Move(uint8(x))
servo2.Move(uint8(z))
fmt.Println("Current Angle: ", servo1.CurrentAngle, ",", servo2.CurrentAngle)
})
}
2014-05-22 19:22:14 -07:00
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("pwmBot", []gobot.Connection{firmataAdaptor, leapAdaptor}, []gobot.Device{servo1, servo2, leapDriver}, work))
gbot.Start()
2014-04-27 19:56:18 -07:00
}