func main() { gbot := gobot.NewGobot() bleAdaptor := ble.NewBLEAdaptor("ble", os.Args[1]) drone := ble.NewBLEMinidroneDriver(bleAdaptor, "drone") work := func() { gobot.On(drone.Event("battery"), func(data interface{}) { fmt.Printf("battery: %d\n", data) }) gobot.On(drone.Event("status"), func(data interface{}) { fmt.Printf("status: %d\n", data) }) gobot.On(drone.Event("flying"), func(data interface{}) { fmt.Println("flying!") gobot.After(5*time.Second, func() { fmt.Println("forwards...") drone.Forward(10) }) gobot.After(10*time.Second, func() { fmt.Println("backwards...") drone.Backward(10) }) gobot.After(15*time.Second, func() { fmt.Println("right...") drone.Right(10) }) gobot.After(20*time.Second, func() { fmt.Println("left...") drone.Left(10) }) gobot.After(25*time.Second, func() { fmt.Println("landing...") drone.Land() }) }) gobot.On(drone.Event("landed"), func(data interface{}) { fmt.Println("landed.") }) drone.TakeOff() } robot := gobot.NewRobot("minidrone", []gobot.Connection{bleAdaptor}, []gobot.Device{drone}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() board := edison.NewEdisonAdaptor("edison") screen := i2c.NewGroveLcdDriver(board, "screen") work := func() { screen.Write("hello") screen.SetRGB(255, 0, 0) gobot.After(5*time.Second, func() { screen.Clear() screen.Home() screen.SetRGB(0, 255, 0) screen.Write("goodbye") }) screen.Home() <-time.After(1 * time.Second) screen.SetRGB(0, 0, 255) } robot := gobot.NewRobot("screenBot", []gobot.Connection{board}, []gobot.Device{screen}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() bebopAdaptor := bebop.NewBebopAdaptor("Drone") drone := bebop.NewBebopDriver(bebopAdaptor, "Drone") work := func() { gobot.On(drone.Event("flying"), func(data interface{}) { gobot.After(3*time.Second, func() { drone.Land() }) }) drone.HullProtection(true) drone.TakeOff() } robot := gobot.NewRobot("drone", []gobot.Connection{bebopAdaptor}, []gobot.Device{drone}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() bleAdaptor := ble.NewBLEAdaptor("ble", os.Args[1]) drone := ble.NewBLEMinidroneDriver(bleAdaptor, "drone") work := func() { fmt.Println("takeoff...") drone.TakeOff() gobot.After(10*time.Second, func() { fmt.Println("landing...") drone.Land() }) } robot := gobot.NewRobot("minidrone", []gobot.Connection{bleAdaptor}, []gobot.Device{drone}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() // create a ardrone adapter aAdapter := ardrone.NewArdroneAdaptor("Drone") // create a leap adaptor to connect to the leap motion via web socket. lAdapter := leap.NewLeapMotionAdaptor("leap", "127.0.0.1:6437") c := leapdrone.NewController(lAdapter, aAdapter) // implement leap worker leapWorker := func() { // NOTE: leap motion driver only add message event to it. gobot.On(c.LDriver.Event("message"), func(data interface{}) { lp := data.(leap.Frame) for _, v := range lp.Hands { // XXX: catch the first hand id and ignore other annoying hands hand := lb.NewHand(v, 200) if hand.IsForward() { fmt.Printf("moving forward\n") } if hand.IsBackward() { fmt.Printf("moving backward\n") } if hand.IsRight() { fmt.Printf("moving right\n") } if hand.IsLeft() { fmt.Printf("moving left\n") } if hand.IsUpward() { fmt.Printf("moving up\n") } if hand.IsDownward() { fmt.Printf("moving down\n") } continue } }) } // implement ARDrone worker droneWorker := func() { c.ADriver.TakeOff() gobot.On(c.ADriver.Event("flying"), func(data interface{}) { gobot.After(3*time.Second, func() { c.ADriver.Land() }) }) } // add a leap robot to gobot gbot.AddRobot(c.LeapRobot(leapWorker)) // add a ardrone robot to gobot // TODO: temporarily disable drone robot. // gbot.AddRobot(c.DroneRobot(droneWorker)) _ = droneWorker // start gbot.Start() }
func openDoor(sp gpio.DirectPinDriver, publisher *zmq.Socket) { sp.DigitalWrite(1) publisher.SendMessage("door.state.unlock", "Door Unlocked") gobot.After(5*time.Second, func() { sp.DigitalWrite(0) publisher.SendMessage("door.state.lock", "Door Locked") }) }
func main() { gbot := gobot.NewGobot() e := edison.NewEdisonAdaptor("edison") m := mqtt.NewMqttAdaptor("mqtt", "tcp://192.168.0.90:1883", "pump") lever := gpio.NewButtonDriver(e, "lever", "2") fault := gpio.NewButtonDriver(e, "fault", "4") pump := gpio.NewDirectPinDriver(e, "pump", "13") work := func() { dgram := url.Values{ "name": {"Four"}, "dispenser_id": {"4"}, "drink_id": {"0"}, "event": {"online"}, "details": {"dispenser"}, } pumping := false served := byte(0) m.On("startPump", func(data []byte) { if !pumping { pumping = true pump.DigitalWrite(1) served++ dgram.Set("event", "online") dgram.Set("drink_id", fmt.Sprintf("%v", served)) m.Publish("pumped", []byte(dgram.Encode())) gobot.After(2*time.Second, func() { pump.DigitalWrite(0) pumping = false }) } }) gobot.On(lever.Event("push"), func(data interface{}) { m.Publish("pump", []byte{}) }) m.On("startFault", func(data []byte) { dgram.Set("event", "error") m.Publish("fault", []byte(dgram.Encode())) }) gobot.On(fault.Event("push"), func(data interface{}) { m.Publish("startFault", []byte{}) }) } gbot.AddRobot(gobot.NewRobot("brewmachine", []gobot.Connection{e, m}, []gobot.Device{lever, fault, pump}, work, )) gbot.Start() }
func main() { gbot := gobot.NewGobot() ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone") drone := ardrone.NewArdroneDriver(ardroneAdaptor, "Drone") work := func() { drone.TakeOff() gobot.On(drone.Events["Flying"], func(data interface{}) { gobot.After(3*time.Second, func() { drone.Land() }) }) } gbot.Robots = append(gbot.Robots, gobot.NewRobot("drone", []gobot.Connection{ardroneAdaptor}, []gobot.Device{drone}, work)) gbot.Start() }
func main() { gbot := gobot.NewGobot() bleAdaptor := ble.NewBLEClientAdaptor("ble", os.Args[1]) drone := ble.NewBLEMinidroneDriver(bleAdaptor, "drone") work := func() { gobot.On(drone.Event("battery"), func(data interface{}) { fmt.Printf("battery: %d\n", data) }) gobot.On(drone.Event("status"), func(data interface{}) { fmt.Printf("status: %d\n", data) }) gobot.On(drone.Event("flying"), func(data interface{}) { fmt.Println("flying!") gobot.After(5*time.Second, func() { fmt.Println("landing...") drone.Land() drone.Land() }) }) gobot.On(drone.Event("landed"), func(data interface{}) { fmt.Println("landed.") }) <-time.After(1000 * time.Millisecond) drone.TakeOff() } robot := gobot.NewRobot("bleBot", []gobot.Connection{bleAdaptor}, []gobot.Device{drone}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() robot := gobot.NewRobot( "hello", func() { done := gobot.Every(500*time.Millisecond, func() { fmt.Println("Greetings human") }) gobot.After(5*time.Second, func() { done <- true fmt.Println("We're done here") }) }, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() board := joule.NewJouleAdaptor("joule") screen := i2c.NewGroveLcdDriver(board, "screen") work := func() { screen.Write("hello") screen.SetRGB(255, 0, 0) gobot.After(5*time.Second, func() { screen.Clear() screen.Home() screen.SetRGB(0, 255, 0) // set a custom character in the first position screen.SetCustomChar(0, i2c.CustomLCDChars["smiley"]) // add the custom character at the end of the string screen.Write("goodbye\nhave a nice day " + string(byte(0))) gobot.Every(500*time.Millisecond, func() { screen.Scroll(false) }) }) screen.Home() <-time.After(1 * time.Second) screen.SetRGB(0, 0, 255) } robot := gobot.NewRobot("screenBot", []gobot.Connection{board}, []gobot.Device{screen}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone") drone := ardrone.NewArdroneDriver(ardroneAdaptor, "Drone") work := func() { drone.On(ardrone.Flying, func(data interface{}) { gobot.After(3*time.Second, func() { drone.Land() }) }) drone.TakeOff() } robot := gobot.NewRobot("drone", []gobot.Connection{ardroneAdaptor}, []gobot.Device{drone}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) gbot := gobot.NewGobot() _, currentfile, _, _ := runtime.Caller(0) cascade := path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml") window := opencv.NewWindowDriver("window") camera := opencv.NewCameraDriver("camera", "tcp://192.168.1.1:5555") ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone") drone := ardrone.NewArdroneDriver(ardroneAdaptor, "drone") work := func() { detect := false drone.TakeOff() var image *cv.IplImage gobot.On(camera.Events["Frame"], func(data interface{}) { image = data.(*cv.IplImage) if detect == false { window.ShowImage(image) } }) gobot.On(drone.Events["Flying"], func(data interface{}) { gobot.After(1*time.Second, func() { drone.Up(0.2) }) gobot.After(2*time.Second, func() { drone.Hover() }) gobot.After(5*time.Second, func() { detect = true gobot.Every(300*time.Millisecond, func() { drone.Hover() i := image faces := opencv.DetectFaces(cascade, i) biggest := 0 var face *cv.Rect for _, f := range faces { if f.Width() > biggest { biggest = f.Width() face = f } } if face != nil { opencv.DrawRectangles(i, []*cv.Rect{face}, 0, 255, 0, 5) centerX := float64(image.Width()) * 0.5 turn := -(float64(face.X()) - centerX) / centerX fmt.Println("turning:", turn) if turn < 0 { drone.Clockwise(math.Abs(turn * 0.4)) } else { drone.CounterClockwise(math.Abs(turn * 0.4)) } } window.ShowImage(i) }) gobot.After(20*time.Second, func() { drone.Land() }) }) }) } gbot.Robots = append(gbot.Robots, gobot.NewRobot("face", []gobot.Connection{ardroneAdaptor}, []gobot.Device{window, camera, drone}, work)) gbot.Start() }
func ExampleAfter() { gobot.After(1*time.Second, func() { fmt.Println("Hello") }) }
"time" "github.com/hybridgroup/gobot" ) var connect = func(a *FirmataAdaptor) []error { defaultInitTimeInterval = 0 * time.Second gobot.After(1*time.Millisecond, func() { // arduino uno r3 firmware response "StandardFirmata.ino" a.board.process([]byte{240, 121, 2, 3, 83, 0, 116, 0, 97, 0, 110, 0, 100, 0, 97, 0, 114, 0, 100, 0, 70, 0, 105, 0, 114, 0, 109, 0, 97, 0, 116, 0, 97, 0, 46, 0, 105, 0, 110, 0, 111, 0, 247}) // arduino uno r3 capabilities response a.board.process([]byte{240, 108, 127, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 247}) // arduino uno r3 analog mapping response a.board.process([]byte{240, 106, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 0, 1, 2, 3, 4, 5, 247}) }) return a.Connect() } func initTestFirmataAdaptor() *FirmataAdaptor { a := NewFirmataAdaptor("board", "/dev/null") a.connect = func(port string) (io.ReadWriteCloser, error) { return &NullReadWriteCloser{}, nil