forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firmata_cat_toy.go
49 lines (40 loc) · 1.26 KB
/
firmata_cat_toy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/platforms/leap"
)
func main() {
gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")
servo1 := gpio.NewServoDriver(firmataAdaptor, "servo", "5")
servo2 := gpio.NewServoDriver(firmataAdaptor, "servo", "3")
leapAdaptor := leap.NewLeapMotionAdaptor("leap", "127.0.0.1:6437")
leapDriver := leap.NewLeapMotionDriver(leapAdaptor, "leap")
work := func() {
x := 90.0
z := 90.0
gobot.On(leapDriver.Event("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)
}
})
gobot.Every(10*time.Millisecond, func() {
servo1.Move(uint8(x))
servo2.Move(uint8(z))
fmt.Println("Current Angle: ", servo1.CurrentAngle, ",", servo2.CurrentAngle)
})
}
robot := gobot.NewRobot("pwmBot",
[]gobot.Connection{firmataAdaptor, leapAdaptor},
[]gobot.Device{servo1, servo2, leapDriver},
work,
)
gbot.AddRobot(robot)
gbot.Start()
}