mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
sphero: return collision data as a struct
This commit is contained in:
parent
96918d358d
commit
46d46a8ad0
@ -16,7 +16,7 @@ func main() {
|
|||||||
|
|
||||||
work := func() {
|
work := func() {
|
||||||
gobot.On(spheroDriver.Event("collision"), func(data interface{}) {
|
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() {
|
gobot.Every(3*time.Second, func() {
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package sphero
|
package sphero
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -22,6 +24,19 @@ type SpheroDriver struct {
|
|||||||
responseChannel chan []uint8
|
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 {
|
func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
|
||||||
s := &SpheroDriver{
|
s := &SpheroDriver{
|
||||||
Driver: *gobot.NewDriver(
|
Driver: *gobot.NewDriver(
|
||||||
@ -189,7 +204,14 @@ func (s *SpheroDriver) enableStopOnDisconnect() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SpheroDriver) handleCollisionDetected(data []uint8) {
|
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 {
|
func (s *SpheroDriver) getSyncResponse(packet *packet) []byte {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user