Skip to content

Commit

Permalink
Docs and examples to better show off Grove GPIO
Browse files Browse the repository at this point in the history
  • Loading branch information
deadprogram committed Oct 22, 2015
1 parent 632058c commit 929ccd2
Show file tree
Hide file tree
Showing 8 changed files with 588 additions and 50 deletions.
34 changes: 34 additions & 0 deletions examples/bebop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/bebop"
)

func main() {
gbot := gobot.NewGobot()

bebopAdaptor := bebop.NewBebopAdaptor("Drone")
drone := bebop.NewBebopDriver(bebopAdaptor, "Drone")

work := func() {
drone.HullProtection(true)
drone.TakeOff()
gobot.On(drone.Event("flying"), func(data interface{}) {
gobot.After(3*time.Second, func() {
drone.Land()
})
})
}

robot := gobot.NewRobot("drone",
[]gobot.Connection{bebopAdaptor},
[]gobot.Device{drone},
work,
)
gbot.AddRobot(robot)

gbot.Start()
}
188 changes: 188 additions & 0 deletions examples/bebop_ps3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package main

import (
"fmt"
"io"
"io/ioutil"
"math"
"os/exec"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/bebop"
"github.com/hybridgroup/gobot/platforms/joystick"
)

type pair struct {
x float64
y float64
}

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 {
buf, err := ioutil.ReadAll(stderr)
if err != nil {
fmt.Println(err)
}
if len(buf) > 0 {
fmt.Println(string(buf))
}
}
}()

return stdin, stderr, nil
}

func main() {
gbot := gobot.NewGobot()

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

bebopAdaptor := bebop.NewBebopAdaptor("Drone")
drone := bebop.NewBebopDriver(bebopAdaptor, "Drone")

work := func() {
video, _, _ := ffmpeg()

go func() {
for {
if _, err := video.Write(<-drone.Video()); err != nil {
fmt.Println(err)
return
}
}
}()

offset := 32767.0
rightStick := pair{x: 0, y: 0}
leftStick := pair{x: 0, y: 0}

recording := false

gobot.On(joystick.Event("circle_press"), func(data interface{}) {
if recording {
drone.StopRecording()
} else {
drone.StartRecording()
}
recording = !recording
})

gobot.On(joystick.Event("square_press"), func(data interface{}) {
drone.HullProtection(true)
drone.TakeOff()
})
gobot.On(joystick.Event("triangle_press"), func(data interface{}) {
drone.Stop()
})
gobot.On(joystick.Event("x_press"), func(data interface{}) {
drone.Land()
})
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("bebop",
[]gobot.Connection{joystickAdaptor, bebopAdaptor},
[]gobot.Device{joystick, drone},
work,
)

gbot.AddRobot(robot)

gbot.Start()
}

func validatePitch(data float64, offset float64) int {
value := math.Abs(data) / offset
if value >= 0.1 {
if value <= 1.0 {
return int((float64(int(value*100)) / 100) * 100)
}
return 100
}
return 0
}
35 changes: 35 additions & 0 deletions examples/edison_button_led.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/platforms/intel-iot/edison"
)

func main() {
gbot := gobot.NewGobot()

e := edison.NewEdisonAdaptor("edison")

button := gpio.NewButtonDriver(e, "myButton", "2")
led := gpio.NewLedDriver(e, "myLed", "4")

work := func() {
gobot.On(button.Event("push"), func(data interface{}) {
led.On()
})
gobot.On(button.Event("release"), func(data interface{}) {
led.Off()
})
}

robot := gobot.NewRobot("buttonBot",
[]gobot.Connection{e},
[]gobot.Device{led, button},
work,
)

gbot.AddRobot(robot)

gbot.Start()
}
38 changes: 38 additions & 0 deletions examples/edison_button_led_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/platforms/intel-iot/edison"

"github.com/hybridgroup/gobot/api"
)

func main() {
gbot := gobot.NewGobot()
api.NewAPI(gbot).Start()

e := edison.NewEdisonAdaptor("edison")

button := gpio.NewButtonDriver(e, "myButton", "2")
led := gpio.NewLedDriver(e, "myLed", "4")

work := func() {
gobot.On(button.Event("push"), func(data interface{}) {
led.On()
})
gobot.On(button.Event("release"), func(data interface{}) {
led.Off()
})
}

robot := gobot.NewRobot("buttonBot",
[]gobot.Connection{e},
[]gobot.Device{led, button},
work,
)

gbot.AddRobot(robot)

gbot.Start()
}
48 changes: 48 additions & 0 deletions examples/firmata_integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"time"
"fmt"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
)

func main() {
gbot := gobot.NewGobot()

firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0")
led1 := gpio.NewLedDriver(firmataAdaptor, "led1", "3")
led2 := gpio.NewLedDriver(firmataAdaptor, "led2", "4")
button := gpio.NewButtonDriver(firmataAdaptor, "button", "2")
sensor := gpio.NewAnalogSensorDriver(firmataAdaptor, "sensor", "0")

work := func() {
gobot.Every(1*time.Second, func() {
led1.Toggle()
})
gobot.Every(2*time.Second, func() {
led2.Toggle()
})
gobot.On(button.Event("push"), func(data interface{}) {
led2.On()
})
gobot.On(button.Event("release"), func(data interface{}) {
led2.Off()
})
gobot.On(sensor.Event("data"), func(data interface{}) {
fmt.Println("sensor", data)
})
}

robot := gobot.NewRobot("bot",
[]gobot.Connection{firmataAdaptor},
[]gobot.Device{led1, led2, button, sensor},
work,
)

gbot.AddRobot(robot)

gbot.Start()
}
46 changes: 46 additions & 0 deletions examples/sphero_dpad.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/api"
"github.com/hybridgroup/gobot/platforms/sphero"
)

func main() {
gbot := gobot.NewGobot()
a := api.NewAPI(gbot)
a.Start()

conn := sphero.NewSpheroAdaptor("Sphero", "/dev/rfcomm0")
ball := sphero.NewSpheroDriver(conn, "sphero")

robot := gobot.NewRobot("sphero-dpad",
[]gobot.Connection{conn},
[]gobot.Device{ball},
)

robot.AddCommand("move", func(params map[string]interface{}) interface{} {
direction := params["direction"].(string)

switch direction {
case "up":
ball.Roll(100, 0)
case "down":
ball.Roll(100, 180)
case "left":
ball.Roll(100, 270)
case "right":
ball.Roll(100, 90)
}

time.Sleep(2 * time.Second)
ball.Stop()
return "ok"
})

gbot.AddRobot(robot)

gbot.Start()
}
Loading

0 comments on commit 929ccd2

Please sign in to comment.