Skip to content

Commit

Permalink
neurosky: use serialport adaptor and move driver to drivers/serial (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas authored Feb 12, 2024
1 parent 9430005 commit e2b710b
Show file tree
Hide file tree
Showing 54 changed files with 812 additions and 820 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ import (

func main() {
adaptor := serialport.NewAdaptor("/dev/rfcomm0")
driver := serial.NewSpheroDriver(adaptor)
driver := sphero.NewSpheroDriver(adaptor)

work := func() {
gobot.Every(3*time.Second, func() {
Expand Down Expand Up @@ -179,14 +179,14 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/api"
"gobot.io/x/gobot/v2/drivers/common/sphero"
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
"gobot.io/x/gobot/v2/drivers/serial"
"gobot.io/x/gobot/v2/platforms/serialport"
)

func NewSwarmBot(port string) *gobot.Robot {
spheroAdaptor := serialport.NewAdaptor(port)
spheroDriver := serial.NewSpheroDriver(spheroAdaptor, serial.WithName("Sphero" + port))
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, serial.WithName("Sphero" + port))

work := func() {
spheroDriver.Stop()
Expand Down Expand Up @@ -390,6 +390,7 @@ the `gobot/drivers/serial` package:

- [UART](https://en.wikipedia.org/wiki/Serial_port) <=> [Drivers](https://github.com/hybridgroup/gobot/tree/master/drivers/serial)
- Sphero: Sphero
- Neurosky: MindWave

Support for devices that use Serial Peripheral Interface (SPI) have
a shared set of drivers provided using the `gobot/drivers/spi` package:
Expand Down
4 changes: 2 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ Finally, you can use Master Gobot to add the complete Gobot API or control swarm
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/api"
"gobot.io/x/gobot/v2/drivers/common/sphero"
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
"gobot.io/x/gobot/v2/drivers/serial"
"gobot.io/x/gobot/v2/platforms/serialport"
)
func NewSwarmBot(port string) *gobot.Robot {
spheroAdaptor := serialport.NewAdaptor(port)
spheroDriver := serial.NewSpheroDriver(spheroAdaptor, serial.WithName("Sphero" + port))
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, serial.WithName("Sphero" + port))
work := func() {
spheroDriver.Stop()
Expand Down
41 changes: 38 additions & 3 deletions drivers/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ returned instead and the log output needs to be done at caller side.
### Sphero adaptor split off

The Serial Based Sphero adaptor was split off into a generic serial adaptor and the driver part. With this, the imports
needs to be adjusted. In addition all events now have a postfix "Event", see below.
needs to be adjusted. In addition all events now have a suffix "Event", see below.

```go
// old
Expand All @@ -100,19 +100,54 @@ import(
// new
import(
...
"gobot.io/x/gobot/v2/drivers/common/sphero"
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
"gobot.io/x/gobot/v2/drivers/serial"
"gobot.io/x/gobot/v2/platforms/serialport"
...
)
...
adaptor := serialport.NewAdaptor("/dev/rfcomm0")
spheroDriver := serial.NewSpheroDriver(adaptor)
spheroDriver := sphero.NewSpheroDriver(adaptor)
...
_ = spheroDriver.On(sphero.CollisionEvent, func(data interface{}) {
...
```
### Neurosky adaptor split off
The Neurosky adaptor now us the generic serial adaptor. The driver part was moved. With this, the imports needs to be
adjusted. In addition all events now have a suffix "Event", see below.
```go
// old
import(
...
"gobot.io/x/gobot/v2/platforms/neurosky"
...
)

...
adaptor := neurosky.NewAdaptor("/dev/rfcomm0")
neuro := neurosky.NewDriver(adaptor)
...
_ = neuro.On(neurosky.Extended, func(data interface{}) {
...

// new
import(
...
"gobot.io/x/gobot/v2/drivers/serial/neurosky"
"gobot.io/x/gobot/v2/platforms/serialport"
...
)
...
adaptor := serialport.NewAdaptor("/dev/rfcomm0", serialport.WithName("Neurosky"), serialport.WithBaudRate(57600))
neuro := neurosky.NewMindWaveDriver(adaptor)
...
_ = neuro.On(neurosky.ExtendedEvent, func(data interface{}) {
...
```
## Switch from version 2.2.0 (gpio drivers affected)
### gpio.ButtonDriver, gpio.PIRMotionDriver: substitute parameter "v time.duration"
Expand Down
6 changes: 3 additions & 3 deletions drivers/ble/sphero/sphero_bb8_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sphero
import (
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/ble"
"gobot.io/x/gobot/v2/drivers/common/sphero"
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
)

// BB8Driver represents a Sphero BB-8
Expand All @@ -17,8 +17,8 @@ func NewBB8Driver(a gobot.BLEConnector, opts ...ble.OptionApplier) *BB8Driver {
}

// bb8DefaultCollisionConfig returns a CollisionConfig with sensible collision defaults
func bb8DefaultCollisionConfig() sphero.CollisionConfig {
return sphero.CollisionConfig{
func bb8DefaultCollisionConfig() spherocommon.CollisionConfig {
return spherocommon.CollisionConfig{
Method: 0x01,
Xt: 0x20,
Yt: 0x20,
Expand Down
38 changes: 19 additions & 19 deletions drivers/ble/sphero/sphero_ollie_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/ble"
"gobot.io/x/gobot/v2/drivers/common/sphero"
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
)

// MotorModes is used to configure the motor
Expand Down Expand Up @@ -63,14 +63,14 @@ type Point2D struct {
type OllieDriver struct {
*ble.Driver
gobot.Eventer
defaultCollisionConfig sphero.CollisionConfig
defaultCollisionConfig spherocommon.CollisionConfig
seq uint8
collisionResponse []uint8
packetChannel chan *packet
asyncBuffer []byte
asyncMessage []byte
locatorCallback func(p Point2D)
powerstateCallback func(p sphero.PowerStatePacket)
powerstateCallback func(p spherocommon.PowerStatePacket)
}

// NewOllieDriver creates a driver for a Sphero Ollie
Expand All @@ -80,7 +80,7 @@ func NewOllieDriver(a gobot.BLEConnector, opts ...ble.OptionApplier) *OllieDrive

func newOllieBaseDriver(
a gobot.BLEConnector, name string,
dcc sphero.CollisionConfig, opts ...ble.OptionApplier,
dcc spherocommon.CollisionConfig, opts ...ble.OptionApplier,
) *OllieDriver {
d := &OllieDriver{
defaultCollisionConfig: dcc,
Expand All @@ -89,8 +89,8 @@ func newOllieBaseDriver(
}
d.Driver = ble.NewDriver(a, name, d.initialize, d.shutdown, opts...)

d.AddEvent(sphero.ErrorEvent)
d.AddEvent(sphero.CollisionEvent)
d.AddEvent(spherocommon.ErrorEvent)
d.AddEvent(spherocommon.CollisionEvent)

return d
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func (d *OllieDriver) Wake() error {
}

// ConfigureCollisionDetection configures the sensitivity of the detection.
func (d *OllieDriver) ConfigureCollisionDetection(cc sphero.CollisionConfig) {
func (d *OllieDriver) ConfigureCollisionDetection(cc spherocommon.CollisionConfig) {
d.sendCraftPacket([]uint8{cc.Method, cc.Xt, cc.Yt, cc.Xs, cc.Ys, cc.Dead}, 0x02, 0x12)
}

Expand All @@ -130,7 +130,7 @@ func (d *OllieDriver) GetLocatorData(f func(p Point2D)) {
}

// GetPowerState calls the passed function with the Power State information from the sphero
func (d *OllieDriver) GetPowerState(f func(p sphero.PowerStatePacket)) {
func (d *OllieDriver) GetPowerState(f func(p spherocommon.PowerStatePacket)) {
// CID 0x20 is the code for the power state
d.sendCraftPacket([]uint8{}, 0x00, 0x20)
d.powerstateCallback = f
Expand Down Expand Up @@ -194,7 +194,7 @@ func (d *OllieDriver) Sleep() {
}

// SetDataStreamingConfig passes the config to the sphero to stream sensor data
func (d *OllieDriver) SetDataStreamingConfig(dsc sphero.DataStreamingConfig) error {
func (d *OllieDriver) SetDataStreamingConfig(dsc spherocommon.DataStreamingConfig) error {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, dsc); err != nil {
return err
Expand Down Expand Up @@ -225,7 +225,7 @@ func (d *OllieDriver) initialize() error {
packet := <-d.packetChannel
err := d.writeCommand(packet)
if err != nil {
d.Publish(d.Event(sphero.ErrorEvent), err)
d.Publish(d.Event(spherocommon.ErrorEvent), err)
}
}
}()
Expand Down Expand Up @@ -331,13 +331,13 @@ func (d *OllieDriver) handleDataStreaming(data []byte) {

// data packet is the same as for the normal sphero, since the same communication api is used
// only difference in communication is that the "newer" spheros use BLE for communications
var dataPacket sphero.DataStreamingPacket
var dataPacket spherocommon.DataStreamingPacket
buffer := bytes.NewBuffer(data[5:]) // skip header
if err := binary.Read(buffer, binary.BigEndian, &dataPacket); err != nil {
panic(err)
}

d.Publish(sphero.SensorDataEvent, dataPacket)
d.Publish(spherocommon.SensorDataEvent, dataPacket)
}

func (d *OllieDriver) handleLocatorDetected(data []uint8) {
Expand Down Expand Up @@ -368,7 +368,7 @@ func (d *OllieDriver) handleLocatorDetected(data []uint8) {
}

func (d *OllieDriver) handlePowerStateDetected(data []uint8) {
var dataPacket sphero.PowerStatePacket
var dataPacket spherocommon.PowerStatePacket
buffer := bytes.NewBuffer(data[5:]) // skip header
if err := binary.Read(buffer, binary.BigEndian, &dataPacket); err != nil {
panic(err)
Expand Down Expand Up @@ -404,18 +404,18 @@ func (d *OllieDriver) handleCollisionDetected(data []uint8) {
// confirm checksum
size := len(d.collisionResponse)
chk := d.collisionResponse[size-1] // last byte is checksum
if chk != sphero.CalculateChecksum(d.collisionResponse[2:size-1]) {
if chk != spherocommon.CalculateChecksum(d.collisionResponse[2:size-1]) {
return
}

var collision sphero.CollisionPacket
var collision spherocommon.CollisionPacket
buffer := bytes.NewBuffer(d.collisionResponse[5:]) // skip header
if err := binary.Read(buffer, binary.BigEndian, &collision); err != nil {
panic(err)
}
d.collisionResponse = nil // clear the current response

d.Publish(sphero.CollisionEvent, collision)
d.Publish(spherocommon.CollisionEvent, collision)
}

func (d *OllieDriver) sendCraftPacket(body []uint8, did byte, cid byte) {
Expand All @@ -430,15 +430,15 @@ func (d *OllieDriver) craftPacket(body []uint8, did byte, cid byte) *packet {
packet := &packet{
body: body,
header: hdr,
checksum: sphero.CalculateChecksum(buf[2:]),
checksum: spherocommon.CalculateChecksum(buf[2:]),
}

return packet
}

// ollieDefaultCollisionConfig returns a CollisionConfig with sensible collision defaults
func ollieDefaultCollisionConfig() sphero.CollisionConfig {
return sphero.CollisionConfig{
func ollieDefaultCollisionConfig() spherocommon.CollisionConfig {
return spherocommon.CollisionConfig{
Method: 0x01,
Xt: 0x20,
Yt: 0x20,
Expand Down
6 changes: 3 additions & 3 deletions drivers/ble/sphero/sphero_ollie_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/ble"
"gobot.io/x/gobot/v2/drivers/ble/testutil"
"gobot.io/x/gobot/v2/drivers/common/sphero"
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
)

var _ gobot.Driver = (*OllieDriver)(nil)
Expand Down Expand Up @@ -82,12 +82,12 @@ func TestLocatorData(t *testing.T) {
func TestDataStreaming(t *testing.T) {
d := initTestOllieDriver()

err := d.SetDataStreamingConfig(sphero.DefaultDataStreamingConfig())
err := d.SetDataStreamingConfig(spherocommon.DefaultDataStreamingConfig())
require.NoError(t, err)

responseChan := make(chan bool)
err = d.On("sensordata", func(data interface{}) {
cont := data.(sphero.DataStreamingPacket)
cont := data.(spherocommon.DataStreamingPacket)
// fmt.Printf("got streaming packet: %+v \n", cont)
assert.Equal(t, int16(10), cont.RawAccX)
responseChan <- true
Expand Down
6 changes: 3 additions & 3 deletions drivers/ble/sphero/sphero_sprkplus_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sphero
import (
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/ble"
"gobot.io/x/gobot/v2/drivers/common/sphero"
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
)

// SPRKPlusDriver represents a Sphero SPRK+
Expand All @@ -17,8 +17,8 @@ func NewSPRKPlusDriver(a gobot.BLEConnector, opts ...ble.OptionApplier) *SPRKPlu
}

// sprkplusDefaultCollisionConfig returns a CollisionConfig with sensible collision defaults
func sprkplusDefaultCollisionConfig() sphero.CollisionConfig {
return sphero.CollisionConfig{
func sprkplusDefaultCollisionConfig() spherocommon.CollisionConfig {
return spherocommon.CollisionConfig{
Method: 0x01,
Xt: 0x20,
Yt: 0x20,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sphero
package spherocommon

const (
// ErrorEvent event when error encountered
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sphero
package spherocommon

// LocatorConfig provides configuration for the Location api.
// https://github.com/orbotix/DeveloperResources/blob/master/docs/Sphero_API_1.50.pdf
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sphero
package spherocommon

import "testing"

Expand Down
1 change: 1 addition & 0 deletions drivers/serial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Please refer to the main [README.md](https://github.com/hybridgroup/gobot/blob/r
Gobot has a extensible system for connecting to hardware devices. The following Serial devices are currently supported:

- Sphero: Sphero
- Neurosky: MindWave
28 changes: 0 additions & 28 deletions drivers/serial/helpers_test.go

This file was deleted.

13 changes: 13 additions & 0 deletions drivers/serial/neurosky/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2013-2018 The Hybrid Group

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Loading

0 comments on commit e2b710b

Please sign in to comment.