Skip to content

Commit

Permalink
Merge pull request hybridgroup#477 from infracloudio/dev
Browse files Browse the repository at this point in the history
Support for more commands in Ollie driver
  • Loading branch information
deadprogram authored Dec 26, 2017
2 parents 30b0eb9 + 9e56ea5 commit 8bdf882
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
39 changes: 39 additions & 0 deletions examples/ollie_boost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// +build example
//
// Do not build by default.

package main

import (
"os"
"time"

"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/ble"
"gobot.io/x/gobot/platforms/sphero/ollie"
)

func main() {
bleAdaptor := ble.NewClientAdaptor(os.Args[1])
ollieBot := ollie.NewDriver(bleAdaptor)

work := func() {
head := 90
ollieBot.SetRGB(255, 0, 0)
ollieBot.Boost(true)
gobot.Every(1*time.Second, func() {
ollieBot.Roll(0, uint16(head))
time.Sleep(1 * time.Second)
head += 90
head = head % 360
})
}

robot := gobot.NewRobot("ollieBot",
[]gobot.Connection{bleAdaptor},
[]gobot.Device{ollieBot},
work,
)

robot.Start()
}
35 changes: 35 additions & 0 deletions examples/ollie_crazy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// +build example
//
// Do not build by default.

package main

import (
"os"
"time"

"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/ble"
"gobot.io/x/gobot/platforms/sphero/ollie"
)

func main() {
bleAdaptor := ble.NewClientAdaptor(os.Args[1])
ollieBot := ollie.NewDriver(bleAdaptor)

work := func() {
ollieBot.SetRGB(255, 0, 255)
gobot.Every(1*time.Second, func() {
// Ollie performs 'crazy-ollie' trick
ollieBot.SetRawMotorValues(ollie.Forward, uint8(255), ollie.Forward, uint8(255))
})
}

robot := gobot.NewRobot("ollieBot",
[]gobot.Connection{bleAdaptor},
[]gobot.Device{ollieBot},
work,
)

robot.Start()
}
35 changes: 35 additions & 0 deletions examples/ollie_spin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// +build example
//
// Do not build by default.

package main

import (
"os"
"time"

"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/ble"
"gobot.io/x/gobot/platforms/sphero/ollie"
)

func main() {
bleAdaptor := ble.NewClientAdaptor(os.Args[1])
ollieBot := ollie.NewDriver(bleAdaptor)

work := func() {
ollieBot.SetRGB(255, 0, 255)
gobot.Every(1*time.Second, func() {
// Ollie performs 360 spin trick
ollieBot.SetRawMotorValues(ollie.Forward, uint8(255), ollie.Reverse, uint8(255))
})
}

robot := gobot.NewRobot("ollieBot",
[]gobot.Connection{bleAdaptor},
[]gobot.Device{ollieBot},
work,
)

robot.Start()
}
49 changes: 49 additions & 0 deletions platforms/sphero/ollie/ollie_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ const (
CollisionResponseSize = PacketHeaderSize + CollisionDataSize
)

type MotorModes uint8

// MotorModes required for SetRawMotorValues command
const (
Off MotorModes = iota
Forward
Reverse
Brake
Ignore
)

type packet struct {
header []uint8
body []uint8
Expand Down Expand Up @@ -195,6 +206,44 @@ func (b *Driver) Roll(speed uint8, heading uint16) {
b.packetChannel <- b.craftPacket([]uint8{speed, uint8(heading >> 8), uint8(heading & 0xFF), 0x01}, 0x02, 0x30)
}

// Boost executes the boost macro from within the SSB which takes a
// 1 byte parameter which is either 01h to begin boosting or 00h to stop.
func (b *Driver) Boost(state bool) {
s := uint8(0x01)
if !state {
s = 0x00
}
b.packetChannel <- b.craftPacket([]uint8{s}, 0x02, 0x31)
}

// SetStabilization enables or disables the built-in auto stabilizing features of the Ollie
func (b *Driver) SetStabilization(state bool) {
s := uint8(0x01)
if !state {
s = 0x00
}
b.packetChannel <- b.craftPacket([]uint8{s}, 0x02, 0x02)
}

// SetRotationRate allows you to control the rotation rate that Sphero will use to meet new
// heading commands. A value of 255 jumps to the maximum (currently 400 degrees/sec).
// A value of zero doesn't make much sense so it's interpreted as 1, the minimum.
func (b *Driver) SetRotationRate(speed uint8) {
b.packetChannel <- b.craftPacket([]uint8{speed}, 0x02, 0x03)
}

// SetRawMotorValues allows you to take over one or both of the motor output values,
// instead of having the stabilization system control them. Each motor (left and right)
// requires a mode and a power value from 0-255
func (b *Driver) SetRawMotorValues(lmode MotorModes, lpower uint8, rmode MotorModes, rpower uint8) {
b.packetChannel <- b.craftPacket([]uint8{uint8(lmode), lpower, uint8(rmode), rpower}, 0x02, 0x33)
}

// SetBackLEDOutput allows you to control the brightness of the back(tail) LED.
func (b *Driver) SetBackLEDOutput(value uint8) {
b.packetChannel <- b.craftPacket([]uint8{value}, 0x02, 0x21)
}

// Stop tells the Ollie to stop
func (b *Driver) Stop() {
b.Roll(0, 0)
Expand Down

0 comments on commit 8bdf882

Please sign in to comment.