Skip to content

Commit

Permalink
sphero: return collision data as a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
nathany committed Sep 10, 2014
1 parent 96918d3 commit 46d46a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/sphero.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {

work := func() {
gobot.On(spheroDriver.Event("collision"), func(data interface{}) {
fmt.Println("Collision Detected!")
fmt.Printf("Collision Detected! %+v\n", data)
})

gobot.Every(3*time.Second, func() {
Expand Down
24 changes: 23 additions & 1 deletion platforms/sphero/sphero_driver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sphero

import (
"bytes"
"encoding/binary"
"fmt"
"time"

Expand All @@ -22,6 +24,19 @@ type SpheroDriver struct {
responseChannel chan []uint8
}

type Collision struct {
// Normalized impact components (direction of the collision event):
X, Y, Z int16
// Thresholds exceeded by X (1h) and/or Y (2h) axis (bitmask):
Axis byte
// Power that cross threshold Xt + Xs:
XMagnitude, YMagnitude int16
// Sphero's speed when impact detected:
Speed uint8
// Millisecond timer
Timestamp uint32
}

func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
s := &SpheroDriver{
Driver: *gobot.NewDriver(
Expand Down Expand Up @@ -189,7 +204,14 @@ func (s *SpheroDriver) enableStopOnDisconnect() {
}

func (s *SpheroDriver) handleCollisionDetected(data []uint8) {
gobot.Publish(s.Event("collision"), data)
// ensure data is the right length:
if len(data) != 22 || data[4] != 17 {
return
}
var collision Collision
buffer := bytes.NewBuffer(data[5:]) // skip header
binary.Read(buffer, binary.BigEndian, &collision)
gobot.Publish(s.Event("collision"), collision)
}

func (s *SpheroDriver) getSyncResponse(packet *packet) []byte {
Expand Down

0 comments on commit 46d46a8

Please sign in to comment.