2023-05-20 14:25:21 +02:00
|
|
|
//go:build example
|
2017-03-13 11:01:39 -04:00
|
|
|
// +build example
|
2023-05-20 14:25:21 +02:00
|
|
|
|
2017-03-13 11:01:39 -04:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2017-06-10 13:18:29 +02:00
|
|
|
/*
|
|
|
|
How to run
|
|
|
|
Pass the Bluetooth name or address as first param:
|
|
|
|
|
|
|
|
go run examples/minidrone_events.go "Mambo_1234"
|
|
|
|
|
|
|
|
NOTE: sudo is required to use BLE in Linux
|
|
|
|
*/
|
|
|
|
|
2016-12-27 18:30:56 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
2024-02-04 18:50:43 +01:00
|
|
|
"gobot.io/x/gobot/v2/drivers/ble/parrot"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/bleclient"
|
2016-12-27 18:30:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-04 18:50:43 +01:00
|
|
|
bleAdaptor := bleclient.NewAdaptor(os.Args[1])
|
|
|
|
drone := parrot.NewMinidroneDriver(bleAdaptor)
|
2016-12-27 18:30:56 +01:00
|
|
|
|
|
|
|
work := func() {
|
2024-02-04 18:50:43 +01:00
|
|
|
drone.On(parrot.BatteryEvent, func(data interface{}) {
|
2016-12-27 18:30:56 +01:00
|
|
|
fmt.Printf("battery: %d\n", data)
|
|
|
|
})
|
|
|
|
|
2024-02-04 18:50:43 +01:00
|
|
|
drone.On(parrot.FlightStatusEvent, func(data interface{}) {
|
2016-12-27 18:30:56 +01:00
|
|
|
fmt.Printf("flight status: %d\n", data)
|
|
|
|
})
|
|
|
|
|
2024-02-04 18:50:43 +01:00
|
|
|
drone.On(parrot.TakeoffEvent, func(data interface{}) {
|
2016-12-27 18:30:56 +01:00
|
|
|
fmt.Println("taking off...")
|
|
|
|
})
|
|
|
|
|
2024-02-04 18:50:43 +01:00
|
|
|
drone.On(parrot.HoveringEvent, func(data interface{}) {
|
2016-12-27 18:30:56 +01:00
|
|
|
fmt.Println("hovering!")
|
|
|
|
gobot.After(5*time.Second, func() {
|
|
|
|
drone.Land()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-02-04 18:50:43 +01:00
|
|
|
drone.On(parrot.LandingEvent, func(data interface{}) {
|
2016-12-27 18:30:56 +01:00
|
|
|
fmt.Println("landing...")
|
|
|
|
})
|
|
|
|
|
2024-02-04 18:50:43 +01:00
|
|
|
drone.On(parrot.LandedEvent, func(data interface{}) {
|
2016-12-27 18:30:56 +01:00
|
|
|
fmt.Println("landed.")
|
|
|
|
})
|
|
|
|
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
drone.TakeOff()
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("minidrone",
|
|
|
|
[]gobot.Connection{bleAdaptor},
|
|
|
|
[]gobot.Device{drone},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
|
|
|
}
|