sphero: return collision data as a struct

This commit is contained in:
Nathan Youngman 2014-09-09 21:45:38 -06:00
parent 96918d358d
commit 46d46a8ad0
2 changed files with 24 additions and 2 deletions

View File

@ -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() {

View File

@ -1,6 +1,8 @@
package sphero
import (
"bytes"
"encoding/binary"
"fmt"
"time"
@ -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(
@ -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 {