Пример #1
0
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()
}
Пример #2
0
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()
}
Пример #3
0
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()
}
Пример #4
0
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()
}
Пример #5
0
func main() {
	gbot := gobot.NewGobot()

	joystickAdaptor := joystick.NewJoystickAdaptor("ps3")
	joystick := joystick.NewJoystickDriver(joystickAdaptor, "ps3", "./platforms/joystick/configs/dualshock3.json")

	ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone")
	drone := ardrone.NewArdroneDriver(ardroneAdaptor, "Drone")

	work := func() {
		offset := 32767.0
		rightStick := pair{x: 0, y: 0}
		leftStick := pair{x: 0, y: 0}

		gobot.On(joystick.Events["square_press"], func(data interface{}) {
			drone.TakeOff()
		})
		gobot.On(joystick.Events["triangle_press"], func(data interface{}) {
			drone.Hover()
		})
		gobot.On(joystick.Events["x_press"], func(data interface{}) {
			drone.Land()
		})
		gobot.On(joystick.Events["left_x"], func(data interface{}) {
			val := float64(data.(int16))
			if leftStick.x != val {
				leftStick.x = val
			}
		})
		gobot.On(joystick.Events["left_y"], func(data interface{}) {
			val := float64(data.(int16))
			if leftStick.y != val {
				leftStick.y = val
			}
		})
		gobot.On(joystick.Events["right_x"], func(data interface{}) {
			val := float64(data.(int16))
			if rightStick.x != val {
				rightStick.x = val
			}
		})
		gobot.On(joystick.Events["right_y"], func(data interface{}) {
			val := float64(data.(int16))
			if rightStick.y != val {
				rightStick.y = val
			}
		})

		gobot.Every(10*time.Millisecond, func() {
			pair := leftStick
			if pair.y < -10 {
				drone.Forward(validatePitch(pair.y, offset))
			} else if pair.y > 10 {
				drone.Backward(validatePitch(pair.y, offset))
			} else {
				drone.Forward(0)
			}

			if pair.x > 10 {
				drone.Right(validatePitch(pair.x, offset))
			} else if pair.x < -10 {
				drone.Left(validatePitch(pair.x, offset))
			} else {
				drone.Right(0)
			}
		})

		gobot.Every(10*time.Millisecond, func() {
			pair := rightStick
			if pair.y < -10 {
				drone.Up(validatePitch(pair.y, offset))
			} else if pair.y > 10 {
				drone.Down(validatePitch(pair.y, offset))
			} else {
				drone.Up(0)
			}

			if pair.x > 20 {
				drone.Clockwise(validatePitch(pair.x, offset))
			} else if pair.x < -20 {
				drone.CounterClockwise(validatePitch(pair.x, offset))
			} else {
				drone.Clockwise(0)
			}
		})
	}

	gbot.Robots = append(gbot.Robots,
		gobot.NewRobot("ardrone", []gobot.Connection{joystickAdaptor, ardroneAdaptor}, []gobot.Device{joystick, drone}, work))

	gbot.Start()
}
Пример #6
0
func main() {
	gbot := gobot.NewGobot()

	m := mqtt.NewMqttAdaptor("mqtt", "tcp://192.168.0.90:1883", "gcs")
	ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone", "192.168.0.40")
	joystickAdaptor := joystick.NewJoystickAdaptor("ps3")

	joystick := joystick.NewJoystickDriver(joystickAdaptor,
		"ps3",
		"./dualshock3.json",
	)

	drone := ardrone.NewArdroneDriver(ardroneAdaptor, "Drone")

	work := func() {
		dgram := url.Values{
			"name":         {"gcs"},
			"dispenser_id": {"3"},
			"drink_id":     {"0"},
			"event":        {"available"},
			"details":      {"drone"},
		}
		m.Publish("gcs", []byte(dgram.Encode()))
		offset := 32767.0
		rightStick := pair{x: 0, y: 0}
		leftStick := pair{x: 0, y: 0}

		m.On("drone", func(data []byte) {
			dgram.Set("event", "delivery complete")
			m.Publish("gcs", []byte(dgram.Encode()))
		})

		gobot.On(joystick.Event("circle_press"), func(data interface{}) {
			fmt.Println("Drop!")
			m.Publish("drop", []byte{})
		})
		gobot.On(joystick.Event("square_press"), func(data interface{}) {
			drone.TakeOff()
			dgram.Set("event", "en route")
			m.Publish("gcs", []byte(dgram.Encode()))
		})
		gobot.On(joystick.Event("triangle_press"), func(data interface{}) {
			drone.Hover()
		})
		gobot.On(joystick.Event("x_press"), func(data interface{}) {
			drone.Land()
			dgram.Set("event", "available")
			m.Publish("gcs", []byte(dgram.Encode()))
		})
		gobot.On(joystick.Event("left_x"), func(data interface{}) {
			val := float64(data.(int16))
			if leftStick.x != val {
				leftStick.x = val
			}
		})
		gobot.On(joystick.Event("left_y"), func(data interface{}) {
			val := float64(data.(int16))
			if leftStick.y != val {
				leftStick.y = val
			}
		})
		gobot.On(joystick.Event("right_x"), func(data interface{}) {
			val := float64(data.(int16))
			if rightStick.x != val {
				rightStick.x = val
			}
		})
		gobot.On(joystick.Event("right_y"), func(data interface{}) {
			val := float64(data.(int16))
			if rightStick.y != val {
				rightStick.y = val
			}
		})

		gobot.Every(10*time.Millisecond, func() {
			pair := leftStick
			if pair.y < -10 {
				drone.Forward(validatePitch(pair.y, offset))
			} else if pair.y > 10 {
				drone.Backward(validatePitch(pair.y, offset))
			} else {
				drone.Forward(0)
			}

			if pair.x > 10 {
				drone.Right(validatePitch(pair.x, offset))
			} else if pair.x < -10 {
				drone.Left(validatePitch(pair.x, offset))
			} else {
				drone.Right(0)
			}
		})

		gobot.Every(10*time.Millisecond, func() {
			pair := rightStick
			if pair.y < -10 {
				drone.Up(validatePitch(pair.y, offset))
			} else if pair.y > 10 {
				drone.Down(validatePitch(pair.y, offset))
			} else {
				drone.Up(0)
			}

			if pair.x > 20 {
				drone.Clockwise(validatePitch(pair.x, offset))
			} else if pair.x < -20 {
				drone.CounterClockwise(validatePitch(pair.x, offset))
			} else {
				drone.Clockwise(0)
			}
		})
	}

	robot := gobot.NewRobot("ardrone",
		[]gobot.Connection{joystickAdaptor, ardroneAdaptor, m},
		[]gobot.Device{joystick, drone},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}