sphero

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2017 License: Apache-2.0, Apache-2.0 Imports: 7 Imported by: 0

README

Sphero

Sphero is a sophisticated and programmable robot housed in a polycarbonate sphere shell.

The Gobot Sphero Adaptor & Driver makes it easy to interact with Sphero using Go. Once you have your Sphero setup and connected to your computer you can start writing code to make Sphero move, change direction, speed and colors, or detect Sphero events and execute some code when they occur.

Learn more about the Sphero robot go here: http://www.gosphero.com/

How to Install

go get -d -u gobot.io/x/gobot/... && go install gobot.io/x/gobot/platforms/sphero

How To Connect

OSX

In order to allow Gobot running on your Mac to access the Sphero, go to "Bluetooth > Open Bluetooth Preferences > Sharing Setup" and make sure that "Bluetooth Sharing" is checked.

Now you must pair with the Sphero. Open System Preferences > Bluetooth. Now with the Bluetooth devices windows open, smack the Sphero until it starts flashing three colors. You should see "Sphero-XXX" pop up as available devices where "XXX" is the first letter of the three colors the sphero is flashing. Pair with that device. Once paired your Sphero will be accessable through the serial device similarly named as /dev/tty.Sphero-XXX-RN-SPP

Ubuntu

Connecting to the Sphero from Ubuntu or any other Linux-based OS can be done entirely from the command line using Gort CLI commands. Here are the steps.

Find the address of the Sphero, by using:

gort scan bluetooth

Pair to Sphero using this command (substituting the actual address of your Sphero):

gort bluetooth pair <address>

Connect to the Sphero using this command (substituting the actual address of your Sphero):

gort bluetooth connect <address>
Windows

You should be able to pair your Sphero using your normal system tray applet for Bluetooth, and then connect to the COM port that is bound to the device, such as COM3.

How to Use

Example of a simple program that makes the Sphero roll.

package main

import (
	"fmt"
	"time"

	"gobot.io/x/gobot"
	"gobot.io/x/gobot/platforms/sphero"
)

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

	work := func() {
		gobot.Every(3*time.Second, func() {
			driver.Roll(30, uint16(gobot.Rand(360)))
		})
	}

	robot := gobot.NewRobot("sphero",
		[]gobot.Connection{adaptor},
		[]gobot.Device{driver},
		work,
	)

	robot.Start()
}

Documentation

Overview

Package sphero provides the Gobot adaptor and driver for the Sphero.

Installing:

go get gobot.io/x/gobot/platforms/sphero

Example:

package main

import (
	"fmt"
	"time"

	"gobot.io/x/gobot"
	"gobot.io/x/gobot/platforms/sphero"
)

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

	work := func() {
		gobot.Every(3*time.Second, func() {
			driver.Roll(30, uint16(gobot.Rand(360)))
		})
	}

	robot := gobot.NewRobot("sphero",
		[]gobot.Connection{adaptor},
		[]gobot.Device{driver},
		work,
	)

	robot.Start()
}

For further information refer to sphero readme: https://github.com/hybridgroup/gobot/blob/master/platforms/sphero/README.md

Index

Constants

View Source
const (
	// Error event when error encountered
	Error = "error"

	// SensorData event when sensor data is received
	SensorData = "sensordata"

	// Collision event when collision is detected
	Collision = "collision"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Adaptor added in v1.0.0

type Adaptor struct {
	// contains filtered or unexported fields
}

Adaptor represents a Connection to a Sphero

func NewAdaptor added in v1.0.0

func NewAdaptor(port string) *Adaptor

NewAdaptor returns a new Sphero Adaptor given a port

func (*Adaptor) Connect added in v1.0.0

func (a *Adaptor) Connect() (err error)

Connect initiates a connection to the Sphero. Returns true on successful connection.

func (*Adaptor) Disconnect added in v1.0.0

func (a *Adaptor) Disconnect() error

Disconnect terminates the connection to the Sphero. Returns true on successful disconnect.

func (*Adaptor) Finalize added in v1.0.0

func (a *Adaptor) Finalize() error

Finalize finalizes the Sphero Adaptor

func (*Adaptor) Name added in v1.0.0

func (a *Adaptor) Name() string

Name returns the Adaptor's name

func (*Adaptor) Port added in v1.0.0

func (a *Adaptor) Port() string

Port returns the Adaptor's port

func (*Adaptor) Reconnect added in v1.0.0

func (a *Adaptor) Reconnect() (err error)

Reconnect attempts to reconnect to the Sphero. If the Sphero has an active connection it will first close that connection and then establish a new connection. Returns true on Successful reconnection

func (*Adaptor) SetName added in v1.0.0

func (a *Adaptor) SetName(n string)

SetName sets the Adaptor's name

func (*Adaptor) SetPort added in v1.0.0

func (a *Adaptor) SetPort(p string)

SetPort sets the Adaptor's port

type CollisionConfig

type CollisionConfig struct {
	// Detection method type to use. Methods 01h and 02h are supported as
	// of FW ver 1.42. Use 00h to completely disable this service.
	Method uint8
	// An 8-bit settable threshold for the X (left/right) axes of Sphero.
	// A value of 00h disables the contribution of that axis.
	Xt uint8
	// An 8-bit settable threshold for the Y (front/back) axes of Sphero.
	// A value of 00h disables the contribution of that axis.
	Yt uint8
	// An 8-bit settable speed value for the X axes. This setting is ranged
	// by the speed, then added to Xt to generate the final threshold value.
	Xs uint8
	// An 8-bit settable speed value for the Y axes. This setting is ranged
	// by the speed, then added to Yt to generate the final threshold value.
	Ys uint8
	// An 8-bit post-collision dead time to prevent retriggering; specified
	// in 10ms increments.
	Dead uint8
}

CollisionConfig provides configuration for the collision detection alogorithm. For more information refer to the official api specification https://github.com/orbotix/DeveloperResources/blob/master/docs/Collision%20detection%201.2.pdf.

func DefaultCollisionConfig

func DefaultCollisionConfig() CollisionConfig

DefaultCollisionConfig returns a CollisionConfig with sensible collision defaults

type CollisionPacket

type CollisionPacket 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
}

CollisionPacket represents the response from a Collision event

type DataStreamingConfig

type DataStreamingConfig struct {
	// Divisor of the maximum sensor sampling rate
	N uint16
	// Number of sample frames emitted per packet
	M uint16
	// Bitwise selector of data sources to stream
	Mask uint32
	// Packet count 1-255 (or 0 for unlimited streaming)
	Pcnt uint8
	// Bitwise selector of more data sources to stream (optional)
	Mask2 uint32
}

DataStreamingConfig provides configuration for Sensor Data Streaming. For more information refer to the official api specification https://github.com/orbotix/DeveloperResources/blob/master/docs/Sphero_API_1.50.pdf page 28

func DefaultDataStreamingConfig

func DefaultDataStreamingConfig() DataStreamingConfig

DefaultDataStreamingConfig returns a DataStreamingConfig with a sampling rate of 40hz, 1 sample frame per package, unlimited streaming, and will stream all available sensor information

type DataStreamingPacket

type DataStreamingPacket struct {
	// 8000 0000h	accelerometer axis X, raw	-2048 to 2047	4mG
	RawAccX int16
	// 4000 0000h	accelerometer axis Y, raw	-2048 to 2047	4mG
	RawAccY int16
	// 2000 0000h	accelerometer axis Z, raw	-2048 to 2047	4mG
	RawAccZ int16
	// 1000 0000h	gyro axis X, raw	-32768 to 32767	0.068 degrees
	RawGyroX int16
	// 0800 0000h	gyro axis Y, raw	-32768 to 32767	0.068 degrees
	RawGyroY int16
	// 0400 0000h	gyro axis Z, raw	-32768 to 32767	0.068 degrees
	RawGyroZ int16
	// 0200 0000h	Reserved
	Rsrv1 int16
	// 0100 0000h	Reserved
	Rsrv2 int16
	// 0080 0000h	Reserved
	Rsrv3 int16
	// 0040 0000h	right motor back EMF, raw	-32768 to 32767	22.5 cm
	RawRMotorBack int16
	// 0020 0000h	left motor back EMF, raw	-32768 to 32767	22.5 cm
	RawLMotorBack int16
	// 0010 0000h	left motor, PWM, raw	-2048 to 2047	duty cycle
	RawLMotor int16
	// 0008 0000h	right motor, PWM raw	-2048 to 2047	duty cycle
	RawRMotor int16
	// 0004 0000h	IMU pitch angle, filtered	-179 to 180	degrees
	FiltPitch int16
	// 0002 0000h	IMU roll angle, filtered	-179 to 180	degrees
	FiltRoll int16
	// 0001 0000h	IMU yaw angle, filtered	-179 to 180	degrees
	FiltYaw int16
	// 0000 8000h	accelerometer axis X, filtered	-32768 to 32767	1/4096 G
	FiltAccX int16
	// 0000 4000h	accelerometer axis Y, filtered	-32768 to 32767	1/4096 G
	FiltAccY int16
	// 0000 2000h	accelerometer axis Z, filtered	-32768 to 32767	1/4096 G
	FiltAccZ int16
	// 0000 1000h	gyro axis X, filtered	-20000 to 20000	0.1 dps
	FiltGyroX int16
	// 0000 0800h	gyro axis Y, filtered	-20000 to 20000	0.1 dps
	FiltGyroY int16
	// 0000 0400h	gyro axis Z, filtered	-20000 to 20000	0.1 dps
	FiltGyroZ int16
	// 0000 0200h	Reserved
	Rsrv4 int16
	// 0000 0100h	Reserved
	Rsrv5 int16
	// 0000 0080h	Reserved
	Rsrv6 int16
	// 0000 0040h	right motor back EMF, filtered	-32768 to 32767	22.5 cm
	FiltRMotorBack int16
	// 0000 0020h	left motor back EMF, filtered	-32768 to 32767	22.5 cm
	FiltLMotorBack int16
	// 0000 0010h	Reserved 1
	Rsrv7 int16
	// 0000 0008h	Reserved 2
	Rsrv8 int16
	// 0000 0004h	Reserved 3
	Rsrv9 int16
	// 0000 0002h	Reserved 4
	Rsrv10 int16
	// 0000 0001h	Reserved 5
	Rsrv11 int16
	// 8000 0000h	Quaternion Q0	-10000 to 10000	1/10000 Q
	Quat0 int16
	// 4000 0000h	Quaternion Q1	-10000 to 10000	1/10000 Q
	Quat1 int16
	// 2000 0000h	Quaternion Q2	-10000 to 10000	1/10000 Q
	Quat2 int16
	// 1000 0000h	Quaternion Q3	-10000 to 10000	1/10000 Q
	Quat3 int16
	// 0800 0000h	Odometer X	-32768 to 32767	cm
	OdomX int16
	// 0400 0000h	Odometer Y	-32768 to 32767	cm
	OdomY int16
	// 0200 0000h	AccelOne	0 to 8000	1 mG
	AccelOne int16
	// 0100 0000h	Velocity X	-32768 to 32767	mm/s
	VeloX int16
	// 0080 0000h	Velocity Y	-32768 to 32767	mm/s
	VeloY int16
}

DataStreamingPacket represents the response from a Data Streaming event

type LocatorConfig

type LocatorConfig struct {
	// Determines whether calibrate commands automatically correct the yaw tare value
	Flags uint8
	// Controls how the X-plane is aligned with Sphero’s heading coordinate system.
	X int16
	// Controls how the Y-plane is aligned with Sphero’s heading coordinate system.
	Y int16
	// Controls how the X,Y-plane is aligned with Sphero’s heading coordinate system.
	YawTare int16
}

LocatorConfig provides configuration for the Location api. https://github.com/orbotix/DeveloperResources/blob/master/docs/Sphero_API_1.50.pdf The current (X,Y) coordinates of Sphero on the ground plane in centimeters.

func DefaultLocatorConfig

func DefaultLocatorConfig() LocatorConfig

DefaultLocatorConfig returns a LocatorConfig with defaults

type SpheroDriver

type SpheroDriver struct {
	gobot.Eventer
	gobot.Commander
	// contains filtered or unexported fields
}

SpheroDriver Represents a Sphero 2.0

func NewSpheroDriver

func NewSpheroDriver(a *Adaptor) *SpheroDriver

NewSpheroDriver returns a new SpheroDriver given a Sphero Adaptor.

Adds the following API Commands:

	"ConfigureLocator" - See SpheroDriver.ConfigureLocator
	"Roll" - See SpheroDriver.Roll
	"Stop" - See SpheroDriver.Stop
	"GetRGB" - See SpheroDriver.GetRGB
	"ReadLocator" - See SpheroDriver.ReadLocator
	"SetBackLED" - See SpheroDriver.SetBackLED
	"SetHeading" - See SpheroDriver.SetHeading
	"SetStabilization" - See SpheroDriver.SetStabilization
 "SetDataStreaming" - See SpheroDriver.SetDataStreaming
 "SetRotationRate" - See SpheroDriver.SetRotationRate

func (*SpheroDriver) ConfigureCollisionDetection

func (s *SpheroDriver) ConfigureCollisionDetection(cc CollisionConfig)

ConfigureCollisionDetection configures the sensitivity of the detection.

func (*SpheroDriver) ConfigureLocator

func (s *SpheroDriver) ConfigureLocator(d LocatorConfig)

ConfigureLocator configures and enables the Locator

func (*SpheroDriver) Connection

func (s *SpheroDriver) Connection() gobot.Connection

Connection returns the Driver's Connection

func (*SpheroDriver) GetRGB

func (s *SpheroDriver) GetRGB() []uint8

GetRGB returns the current r, g, b value of the Sphero

func (*SpheroDriver) Halt

func (s *SpheroDriver) Halt() (err error)

Halt halts the SpheroDriver and sends a SpheroDriver.Stop command to the Sphero. Returns true on successful halt.

func (*SpheroDriver) Name

func (s *SpheroDriver) Name() string

Name returns the Driver Name

func (*SpheroDriver) ReadLocator

func (s *SpheroDriver) ReadLocator() []int16

ReadLocator reads Sphero's current position (X,Y), component velocities and SOG (speed over ground).

func (*SpheroDriver) Roll

func (s *SpheroDriver) Roll(speed uint8, heading uint16)

Roll sends a roll command to the Sphero gives a speed and heading

func (*SpheroDriver) SetBackLED

func (s *SpheroDriver) SetBackLED(level uint8)

SetBackLED sets the Sphero Back LED to the specified brightness

func (*SpheroDriver) SetDataStreaming

func (s *SpheroDriver) SetDataStreaming(d DataStreamingConfig)

SetDataStreaming enables sensor data streaming

func (*SpheroDriver) SetHeading

func (s *SpheroDriver) SetHeading(heading uint16)

SetHeading sets the heading of the Sphero

func (*SpheroDriver) SetName added in v1.0.0

func (s *SpheroDriver) SetName(n string)

SetName sets the Driver Name

func (*SpheroDriver) SetRGB

func (s *SpheroDriver) SetRGB(r uint8, g uint8, b uint8)

SetRGB sets the Sphero to the given r, g, and b values

func (*SpheroDriver) SetRotationRate

func (s *SpheroDriver) SetRotationRate(level uint8)

SetRotationRate sets the Sphero rotation rate A value of 255 jumps to the maximum (currently 400 degrees/sec).

func (*SpheroDriver) SetStabilization

func (s *SpheroDriver) SetStabilization(on bool)

SetStabilization enables or disables the built-in auto stabilizing features of the Sphero

func (*SpheroDriver) Start

func (s *SpheroDriver) Start() (err error)

Start starts the SpheroDriver and enables Collision Detection. Returns true on successful start.

Emits the Events:

Collision  sphero.CollisionPacket - On Collision Detected
SensorData sphero.DataStreamingPacket - On Data Streaming event
Error      error- On error while processing asynchronous response

func (*SpheroDriver) Stop

func (s *SpheroDriver) Stop()

Stop sets the Sphero to a roll speed of 0

Directories

Path Synopsis
Package bb8 contains the Gobot driver for the Sphero BB-8.
Package bb8 contains the Gobot driver for the Sphero BB-8.
Package ollie contains the Gobot driver for the Sphero Ollie.
Package ollie contains the Gobot driver for the Sphero Ollie.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL