gobot.io/x/gobot@v1.16.0/examples/firmata_cat_toy.go (about)

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