gobot.io/x/gobot/v2@v2.1.0/examples/firmata_cat_toy.go (about) 1 //go:build example 2 // +build example 3 4 // 5 // Do not build by default. 6 7 /* 8 How to run 9 Pass serial port to use as the first param: 10 11 go run examples/firmata_cat_toy.go /dev/ttyACM0 12 */ 13 14 package main 15 16 import ( 17 "fmt" 18 "os" 19 "time" 20 21 "gobot.io/x/gobot/v2" 22 "gobot.io/x/gobot/v2/drivers/gpio" 23 "gobot.io/x/gobot/v2/platforms/firmata" 24 "gobot.io/x/gobot/v2/platforms/leap" 25 ) 26 27 func main() { 28 firmataAdaptor := firmata.NewAdaptor(os.Args[1]) 29 servo1 := gpio.NewServoDriver(firmataAdaptor, "5") 30 servo2 := gpio.NewServoDriver(firmataAdaptor, "3") 31 32 leapAdaptor := leap.NewAdaptor("127.0.0.1:6437") 33 leapDriver := leap.NewDriver(leapAdaptor) 34 35 work := func() { 36 x := 90.0 37 z := 90.0 38 leapDriver.On(leap.MessageEvent, func(data interface{}) { 39 if len(data.(leap.Frame).Hands) > 0 { 40 hand := data.(leap.Frame).Hands[0] 41 x = gobot.ToScale(gobot.FromScale(hand.X(), -300, 300), 30, 150) 42 z = gobot.ToScale(gobot.FromScale(hand.Z(), -300, 300), 30, 150) 43 } 44 }) 45 gobot.Every(10*time.Millisecond, func() { 46 servo1.Move(uint8(x)) 47 servo2.Move(uint8(z)) 48 fmt.Println("Current Angle: ", servo1.CurrentAngle, ",", servo2.CurrentAngle) 49 }) 50 } 51 52 robot := gobot.NewRobot("pwmBot", 53 []gobot.Connection{firmataAdaptor, leapAdaptor}, 54 []gobot.Device{servo1, servo2, leapDriver}, 55 work, 56 ) 57 58 robot.Start() 59 }