当前位置: 首页 > 工具软件 > Gobot > 使用案例 >

树莓派用gobot测试舵机的使用

柳昊焱
2023-12-01
package main

import (
        "gobot.io/x/gobot"
        "gobot.io/x/gobot/drivers/gpio"
        "gobot.io/x/gobot/platforms/firmata"
        "gobot.io/x/gobot/platforms/leap"
)

// Video: https://www.youtube.com/watch?v=ayNMyUfdAqc
func main() {
        firmataAdaptor := firmata.NewAdaptor("/dev/tty.usbmodem1451")
        //gpio端口
        servo1 := gpio.NewServoDriver(firmataAdaptor, "12")

        leapMotionAdaptor := leap.NewAdaptor("127.0.0.1:6437")
        l := leap.NewDriver(leapMotionAdaptor)

        work := func() {
                fist := false
                l.On(leap.MessageEvent, func(data interface{}) {
                        handIsOpen := len(data.(leap.Frame).Pointables) > 0
                        if handIsOpen && fist {
                                servo1.Move(0)
                                fist = false
                        } else if !handIsOpen && !fist {
                                servo1.Move(120)
                                fist = true
                        }
                })
        }

        robot := gobot.NewRobot("servoBot",
                []gobot.Connection{firmataAdaptor, leapMotionAdaptor},
                []gobot.Device{servo1, l},
                work,
        )

        robot.Start()
}

转载于:https://www.cnblogs.com/nickchou/p/8548153.html

 类似资料: