2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2016-12-27 02:01:52 +08:00
|
|
|
/*
|
2023-05-20 20:25:21 +08:00
|
|
|
This example will connect to the Parrot Bebop allowing you to control
|
|
|
|
it using a PS3 controller.
|
|
|
|
|
|
|
|
It also streams the drone video to a webpage via ffserver.
|
|
|
|
|
|
|
|
This requires you to have both ffmpeg and ffserver installed
|
|
|
|
on your computer.
|
2016-12-27 02:01:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
In order to run this example you will first need to start ffserver with:
|
2016-12-27 02:01:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
$ ffserver -f ff.conf
|
2016-12-27 02:01:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
then in a separate terminal run this program:
|
2016-12-27 02:01:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
$ go run bebop_ps3_video.go
|
2016-12-27 02:01:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
You can view the video feed by navigating to
|
|
|
|
http://localhost:8090/bebop.mjpeg in a web browser.
|
|
|
|
*NOTE* firefox works best for viewing the video feed.
|
2016-12-27 02:01:52 +08:00
|
|
|
*/
|
2015-10-25 07:45:53 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
2017-02-07 19:19:29 +08:00
|
|
|
"sync/atomic"
|
2015-10-25 07:45:53 +08:00
|
|
|
"time"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/joystick"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/parrot/bebop"
|
2015-10-25 07:45:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type pair struct {
|
|
|
|
x float64
|
|
|
|
y float64
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
var leftX, leftY, rightX, rightY atomic.Value
|
|
|
|
|
|
|
|
const offset = 32767.0
|
|
|
|
|
2015-10-25 07:45:53 +08:00
|
|
|
func ffmpeg() (stdin io.WriteCloser, stderr io.ReadCloser, err error) {
|
|
|
|
ffmpeg := exec.Command("ffmpeg", "-i", "pipe:0", "http://localhost:8090/bebop.ffm")
|
|
|
|
|
|
|
|
stderr, err = ffmpeg.StderrPipe()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stdin, err = ffmpeg.StdinPipe()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = ffmpeg.Start(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2023-06-11 01:25:01 +08:00
|
|
|
buf, err := io.ReadAll(stderr)
|
2015-10-25 07:45:53 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
if len(buf) > 0 {
|
|
|
|
fmt.Println(string(buf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return stdin, stderr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2023-09-23 18:32:31 +08:00
|
|
|
joystickAdaptor := joystick.NewAdaptor("0")
|
2018-04-14 19:34:28 +08:00
|
|
|
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")
|
2015-10-25 07:45:53 +08:00
|
|
|
|
2016-10-03 22:58:43 +08:00
|
|
|
bebopAdaptor := bebop.NewAdaptor()
|
|
|
|
drone := bebop.NewDriver(bebopAdaptor)
|
2015-10-25 07:45:53 +08:00
|
|
|
|
|
|
|
work := func() {
|
2016-12-27 02:01:52 +08:00
|
|
|
drone.VideoEnable(true)
|
2015-10-25 07:45:53 +08:00
|
|
|
video, _, _ := ffmpeg()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if _, err := video.Write(<-drone.Video()); err != nil {
|
|
|
|
fmt.Println(err)
|
2016-09-01 19:32:40 +08:00
|
|
|
return
|
2015-10-25 07:45:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
leftX.Store(float64(0.0))
|
|
|
|
leftY.Store(float64(0.0))
|
|
|
|
rightX.Store(float64(0.0))
|
|
|
|
rightY.Store(float64(0.0))
|
2015-10-25 07:45:53 +08:00
|
|
|
|
|
|
|
recording := false
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.CirclePress, func(data interface{}) {
|
2015-10-25 07:45:53 +08:00
|
|
|
if recording {
|
|
|
|
drone.StopRecording()
|
|
|
|
} else {
|
|
|
|
drone.StartRecording()
|
|
|
|
}
|
|
|
|
recording = !recording
|
|
|
|
})
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.SquarePress, func(data interface{}) {
|
2015-10-25 07:45:53 +08:00
|
|
|
drone.HullProtection(true)
|
|
|
|
drone.TakeOff()
|
|
|
|
})
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.TrianglePress, func(data interface{}) {
|
2015-10-25 07:45:53 +08:00
|
|
|
drone.Stop()
|
|
|
|
})
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.XPress, func(data interface{}) {
|
2015-10-25 07:45:53 +08:00
|
|
|
drone.Land()
|
|
|
|
})
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.LeftX, func(data interface{}) {
|
2015-10-25 07:45:53 +08:00
|
|
|
val := float64(data.(int16))
|
2017-02-07 19:19:29 +08:00
|
|
|
leftX.Store(val)
|
2015-10-25 07:45:53 +08:00
|
|
|
})
|
2017-02-07 19:19:29 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.LeftY, func(data interface{}) {
|
2015-10-25 07:45:53 +08:00
|
|
|
val := float64(data.(int16))
|
2017-02-07 19:19:29 +08:00
|
|
|
leftY.Store(val)
|
2015-10-25 07:45:53 +08:00
|
|
|
})
|
2017-02-07 19:19:29 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.RightX, func(data interface{}) {
|
2015-10-25 07:45:53 +08:00
|
|
|
val := float64(data.(int16))
|
2017-02-07 19:19:29 +08:00
|
|
|
rightX.Store(val)
|
2015-10-25 07:45:53 +08:00
|
|
|
})
|
2017-02-07 19:19:29 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.RightY, func(data interface{}) {
|
2015-10-25 07:45:53 +08:00
|
|
|
val := float64(data.(int16))
|
2017-02-07 19:19:29 +08:00
|
|
|
rightY.Store(val)
|
2015-10-25 07:45:53 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
gobot.Every(10*time.Millisecond, func() {
|
2017-02-07 19:19:29 +08:00
|
|
|
leftStick := getLeftStick()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case leftStick.y < -10:
|
|
|
|
drone.Forward(bebop.ValidatePitch(leftStick.y, offset))
|
|
|
|
case leftStick.y > 10:
|
|
|
|
drone.Backward(bebop.ValidatePitch(leftStick.y, offset))
|
|
|
|
default:
|
2015-10-25 07:45:53 +08:00
|
|
|
drone.Forward(0)
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
switch {
|
|
|
|
case leftStick.x > 10:
|
|
|
|
drone.Right(bebop.ValidatePitch(leftStick.x, offset))
|
|
|
|
case leftStick.x < -10:
|
|
|
|
drone.Left(bebop.ValidatePitch(leftStick.x, offset))
|
|
|
|
default:
|
2015-10-25 07:45:53 +08:00
|
|
|
drone.Right(0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
gobot.Every(10*time.Millisecond, func() {
|
2017-02-07 19:19:29 +08:00
|
|
|
rightStick := getRightStick()
|
|
|
|
switch {
|
|
|
|
case rightStick.y < -10:
|
|
|
|
drone.Up(bebop.ValidatePitch(rightStick.y, offset))
|
|
|
|
case rightStick.y > 10:
|
|
|
|
drone.Down(bebop.ValidatePitch(rightStick.y, offset))
|
|
|
|
default:
|
2015-10-25 07:45:53 +08:00
|
|
|
drone.Up(0)
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
switch {
|
|
|
|
case rightStick.x > 20:
|
|
|
|
drone.Clockwise(bebop.ValidatePitch(rightStick.x, offset))
|
|
|
|
case rightStick.x < -20:
|
|
|
|
drone.CounterClockwise(bebop.ValidatePitch(rightStick.x, offset))
|
|
|
|
default:
|
2015-10-25 07:45:53 +08:00
|
|
|
drone.Clockwise(0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("bebop",
|
|
|
|
[]gobot.Connection{joystickAdaptor, bebopAdaptor},
|
2016-09-01 18:17:43 +08:00
|
|
|
[]gobot.Device{stick, drone},
|
2015-10-25 07:45:53 +08:00
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
robot.Start()
|
2015-10-25 07:45:53 +08:00
|
|
|
}
|
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
func getLeftStick() pair {
|
|
|
|
s := pair{x: 0, y: 0}
|
|
|
|
s.x = leftX.Load().(float64)
|
|
|
|
s.y = leftY.Load().(float64)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRightStick() pair {
|
|
|
|
s := pair{x: 0, y: 0}
|
|
|
|
s.x = rightX.Load().(float64)
|
|
|
|
s.y = rightY.Load().(float64)
|
|
|
|
return s
|
2015-10-25 07:45:53 +08:00
|
|
|
}
|