Skip to content

Commit

Permalink
Added methods to read Sphero Power States
Browse files Browse the repository at this point in the history
  • Loading branch information
Rearth authored and deadprogram committed May 22, 2019
1 parent e964234 commit 33b932f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
39 changes: 30 additions & 9 deletions platforms/sphero/ollie/ollie_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ import (

// Driver is the Gobot driver for the Sphero Ollie robot
type Driver struct {
name string
connection gobot.Connection
seq uint8
mtx sync.Mutex
collisionResponse []uint8
packetChannel chan *Packet
asyncBuffer []byte
asyncMessage []byte
locatorCallback func(p Point2D)
name string
connection gobot.Connection
seq uint8
mtx sync.Mutex
collisionResponse []uint8
packetChannel chan *Packet
asyncBuffer []byte
asyncMessage []byte
locatorCallback func(p Point2D)
powerstateCallback func(p PowerStatePacket)
gobot.Eventer
}

Expand Down Expand Up @@ -236,6 +237,10 @@ func (b *Driver) HandleResponses(data []byte, e error) {
if data[4] == 0x0B && len(data) == 16 {
b.handleLocatorDetected(data)
}

if data[4] == 0x09 {
b.handlePowerStateDetected(data)
}
}

b.handleCollisionDetected(data)
Expand All @@ -248,6 +253,13 @@ func (b *Driver) GetLocatorData(f func(p Point2D)) {
b.locatorCallback = f
}

// GetPowerState calls the passed function with the Power State information from the sphero
func (b *Driver) GetPowerState(f func(p PowerStatePacket)) {
//CID 0x20 is the code for the power state
b.PacketChannel() <- b.craftPacket([]uint8{}, 0x00, 0x20)
b.powerstateCallback = f
}

func (b *Driver) handleDataStreaming(data []byte) {
// ensure data is the right length:
if len(data) != 88 {
Expand Down Expand Up @@ -365,6 +377,15 @@ func (b *Driver) craftPacket(body []uint8, did byte, cid byte) *Packet {
return packet
}

func (b *Driver) handlePowerStateDetected(data []uint8) {

var dataPacket PowerStatePacket
buffer := bytes.NewBuffer(data[5:]) // skip header
binary.Read(buffer, binary.BigEndian, &dataPacket)

b.powerstateCallback(dataPacket)
}

func (b *Driver) handleLocatorDetected(data []uint8) {
//read the unsigned raw values
ux := binary.BigEndian.Uint16(data[5:7])
Expand Down
14 changes: 14 additions & 0 deletions platforms/sphero/ollie/ollie_packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ func DefaultCollisionConfig() sphero.CollisionConfig {
}
}

// PowerStatePacket contains all data relevant to the power state of the sphero
type PowerStatePacket struct {
// record Version Code
RecVer uint8
// High-Level State of the Battery; 1=charging, 2=battery ok, 3=battery low, 4=battery critical
PowerState uint8
// Battery Voltage, scaled in 100th of a Volt, 0x02EF would be 7.51 volts
BattVoltage uint16
// Number of charges in the total lifetime of the sphero
NumCharges uint16
//Seconds awake since last charge
TimeSinceChg uint16
}

// DataStreamingPacket represents the response from a Data Streaming event
type DataStreamingPacket struct {
// 8000 0000h accelerometer axis X, raw -2048 to 2047 4mG
Expand Down

0 comments on commit 33b932f

Please sign in to comment.