sphero

package module
v0.0.0-...-4628442 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2013 License: MIT Imports: 9 Imported by: 2

README

Sphero

Unofficial Go library for the Sphero.

go get github.com/FreeFlow/sphero

Docs: http://godoc.org/github.com/FreeFlow/sphero

Support

  • Functional on OS X and may work on linux but is untested.
  • Windows support is iffy: I'm using a modified version of tarm's goserial to provide timeouts but I didn't make the equivalent changes for Windows.

Documentation

Overview

Package sphero provides an api for connecting and communicating with Sphero robots.

Index

Examples

Constants

View Source
const (
	SOP1                     = 0xff
	SOP2_ANSWER              = 0xff
	SOP2_ASYNC               = 0xfe
	SOP2_RESET_TIMEOUT       = 0xfd
	SOP2_ASYNC_RESET_TIMEOUT = 0xfc
)

Start of Packet values

View Source
const (
	DID_CORE       = 0x00
	DID_BOOTLOADER = 0x01
	DID_SPHERO     = 0x02
)

Device IDs

View Source
const (
	CMD_PING               = 0x01
	CMD_VERSION            = 0x02
	CMD_CONTROL_UART_TX    = 0x03
	CMD_SET_BT_NAME        = 0x10
	CMD_GET_BT_NAME        = 0x11
	CMD_SET_AUTO_RECONNECT = 0x12
	CMD_GET_AUTO_RECONNECT = 0x13
	CMD_GET_PWR_STATE      = 0x20
	CMD_SET_PWR_NOTIFY     = 0x21
	CMD_SLEEP              = 0x22
	GET_POWER_TRIPS        = 0x23
	SET_POWER_TRIPS        = 0x24
	SET_INACTIVE_TIMER     = 0x25
	CMD_GOTO_BL            = 0x30
	CMD_RUN_L1_DIAGS       = 0x40
	CMD_RUN_L2_DIAGS       = 0x41
	CMD_CLEAR_COUNTERS     = 0x42
	CMD_ASSIGN_TIME        = 0x50
	CMD_POLL_TIMES         = 0x51
)

Core Commands - DID: 0x00

View Source
const (
	BEGIN_REFLASH         = 0x02
	HERE_IS_PAGE          = 0x03
	LEAVE_BOOTLOADER      = 0x04
	IS_PAGE_BLANK         = 0x05
	CMD_ERASE_USER_CONFIG = 0x06
)

Bootloader Commands - DID: 0x01

View Source
const (
	CMD_SET_CAL                 = 0x01
	CMD_SET_STABILIZ            = 0x02
	CMD_SET_ROTATION_RATE       = 0x03
	CMD_SET_BALL_REG_WEBSITE    = 0x04
	CMD_GET_BALL_REG_WEBSITE    = 0x05
	CMD_REENABLE_DEMO           = 0x06
	CMD_GET_CHASSIS_ID          = 0x07
	CMD_SET_CHASSIS_ID          = 0x08
	CMD_SELF_LEVEL              = 0x09
	CMD_SET_VDL                 = 0x0a
	CMD_SET_DATA_STREAMING      = 0x11
	CMD_SET_COLLISION_DET       = 0x12
	CMD_LOCATOR                 = 0x13
	CMD_SET_ACCELERO            = 0x14
	CMD_READ_LOCATOR            = 0x15
	CMD_SET_RGB_LED             = 0x20
	CMD_SET_BACK_LED            = 0x21
	CMD_GET_RGB_LED             = 0x22
	CMD_ROLL                    = 0x30
	CMD_BOOST                   = 0x31
	CMD_MOVE                    = 0x32
	CMD_SET_RAW_MOTORS          = 0x33
	CMD_SET_MOTION_TO           = 0x34
	CMD_SET_OPTIONS_FLAG        = 0x35
	CMD_GET_OPTIONS_FLAG        = 0x36
	CMD_SET_TEMP_OPTIONS_FLAG   = 0x37
	CMD_GET_TEMP_OPTIONS_FLAG   = 0x38
	CMD_GET_CONFIG_BLK          = 0x40
	CMD_SET_DEVICE_MODE         = 0x42
	CMD_SET_CFG_BLOCK           = 0x43
	CMD_GET_DEVICE_MODE         = 0x44
	CMD_RUN_MACRO               = 0x50
	CMD_SAVE_TEMP_MACRO         = 0x51
	CMD_SAVE_MACRO              = 0x52
	CMD_INIT_MACRO_EXECUTIVE    = 0x54
	CMD_ABORT_MACRO             = 0x55
	CMD_MACRO_STATUS            = 0x56
	CMD_SET_MACRO_PARAM         = 0x57
	CMD_APPEND_TEMP_MACRO_CHUNK = 0x58
	CMD_ERASE_ORBBAS            = 0x60
	CMD_APPEND_FRAG             = 0x61
	CMD_EXEC_ORBBAS             = 0x62
	CMD_ABORT_ORBBAS            = 0x63
	CMD_ANSWER_INPUT            = 0x64
)

Sphero Commands - DID: 0x02

View Source
const (
	ORBOTIX_RSP_CODE_OK           = 0x00 // Command succeeded
	ORBOTIX_RSP_CODE_EGEN         = 0x01 // General, non-specific error
	ORBOTIX_RSP_CODE_ECHKSUM      = 0x02 // Received checksum failure
	ORBOTIX_RSP_CODE_EFRAG        = 0x03 // Received command fragment
	ORBOTIX_RSP_CODE_EBAD_CMD     = 0x04 // Unknown command ID
	ORBOTIX_RSP_CODE_EUNSUPP      = 0x05 // Command currently unsupported
	ORBOTIX_RSP_CODE_EBAD_MSG     = 0x06 // Bad message format
	ORBOTIX_RSP_CODE_EPARAM       = 0x07 // Parameter value(s) invalid
	ORBOTIX_RSP_CODE_EEXEC        = 0x08 // Failed to execute command
	ORBOTIX_RSP_CODE_EBAD_DID     = 0x09 // Unknown Device ID
	ORBOTIX_RSP_CODE_POWER_NOGOOD = 0x31 // Voltage too low for reflash operation
	ORBOTIX_RSP_CODE_PAGE_ILLEGAL = 0x32 // Illegal page number provided
	ORBOTIX_RSP_CODE_FLASH_FAIL   = 0x33 // Page did not reprogram correctly
	ORBOTIX_RSP_CODE_MA_CORRUPT   = 0x34 // Main Application corrupt
	ORBOTIX_RSP_CODE_MSG_TIMEOUT  = 0x35 // Msg state machine timed out
)

Message Response Codes

View Source
const (
	ID_POWER_NOTIFICATIONS         = 0x01 // Power notifications
	ID_LEVEL_1_DIAGNOSTIC_RESPONSE = 0x02 // Level 1 Diagnostic response
	ID_SENSOR_DATA_STREAMING       = 0x03 // Sensor data streaming
	ID_CONFIG_BLOCK_CONTENTS       = 0x04 // Config block contents
	ID_PRE_SLEEP_WARNING           = 0x05 // Pre-sleep warning (10 sec)
	ID_MACRO_MARKERS               = 0x06 // Macro markers
	ID_COLLISION_DETECTED          = 0x07 // Collision detected
	ID_ORBBAS_PRINT                = 0x08 // orbBasic PRINT message
	ID_ORBBAS_ERROR_ASCII          = 0x09 // orbBasic error message, ASCII
	ID_ORBBAS_ERROR_BINARY         = 0x0a // orbBasic error message, binary
	ID_SELF_LEVEL_RESULT           = 0x0b // Self Level Result
	ID_GYRO_AXIS_LIMIT_EXCEEDED    = 0x0c // Gyro axis limit exceeded (FW ver 3.10 and later)
)

Async Message Id Code

View Source
const (
	BATTERY_CHARGING = 0x01
	BATTERY_OK       = 0x02
	BATTERY_LOW      = 0x03
	BATTERY_CRITICAL = 0x04
)

Battery

View Source
const (
	POWER_MASK_RECVER       = 0x00 // Record version code – the following definition is for 01h
	POWER_MASK_STATE        = 0x01 // High-level state of the power system as concluded by the power manager: 01h = Battery Charging, 02h = Battery OK, 03h = Battery Low, 04h = Battery Critical
	POWER_MASK_BATT_VOLTAGE = 0x02 // Current battery voltage scaled in 100ths of a volt; 02EFh would be 7.51 volts (unsigned 16-bit value)
	POWER_MASK_NUM_CHARGES  = 0x04 // Number of battery recharges in the life of this Sphero (unsigned 16-bit value)
	POWER_MASK_TIMESINCECHG = 0x06 // Seconds awake since last recharge (unsigned 16-bit value)
)

Power State Masks

View Source
const (
	// Accelerometer measures per-axis force of gravity (e.g. shake gestures and collisions)
	ACCEL_AXIS_X_RAW = 0x80000000
	ACCEL_AXIS_Y_RAW = 0x40000000
	ACCEL_AXIS_Z_RAW = 0x20000000
	ACCEL_RAW        = 0xe0000000

	// Gyroscope measures orientation, based on angular momentum
	GYRO_AXIS_X_RAW = 0x10000000
	GYRO_AXIS_Y_RAW = 0x08000000
	GYRO_AXIS_Z_RAW = 0x04000000
	GYRO_RAW        = 0x1c000000

	// Electromotive force is the electromotive force that pushes against the current which induces it (e.g. for ground speed)
	MOTOR_RIGHT_EMF_RAW = 0x00400000
	MOTOR_LEFT_EMF_RAW  = 0x00200000
	MOTOR_EMF_RAW       = 0x00600000

	// Pulse-width modulation
	MOTOR_LEFT_PWM_RAW  = 0x00100000
	MOTOR_RIGHT_PWM_RAW = 0x00080000
	MOTOR_PWM_RAW       = 0x00180000

	// IMU uses accelerometer and gyroscope to determine the orientation
	IMU_PITCH_ANGLE_FILTERED = 0x00040000
	IMU_ROLL_ANGLE_FILTERED  = 0x00020000
	IMU_YAW_ANGLE_FILTERED   = 0x00010000
	IMU_FILTERED             = 0x00070000

	ACCEL_AXIS_X_FILTERED = 0x00008000
	ACCEL_AXIS_Y_FILTERED = 0x00004000
	ACCEL_AXIS_Z_FILTERED = 0x00002000
	ACCEL_FILTERED        = 0x0000e000

	GYRO_AXIS_X_FILTERED = 0x00001000
	GYRO_AXIS_Y_FILTERED = 0x00000800
	GYRO_AXIS_Z_FILTERED = 0x00000400
	GYRO_FILTERED        = 0x00001c00

	MOTOR_RIGHT_EMF_FILTERED = 0x00000040
	MOTOR_LEFT_EMF_FILTERED  = 0x00000020
	MOTOR_EMF_FILTERED       = 0x00000060
)

Data Streaming Masks - MASK1

View Source
const (
	// Orientation as quaternion from accelerometer and gyroscope
	QUATERNION_Q0 = 0x80000000
	QUATERNION_Q1 = 0x40000000
	QUATERNION_Q2 = 0x20000000
	QUATERNION_Q3 = 0x10000000
	QUATERNION    = 0xf0000000

	// Distance traveled
	ODOMETER_X = 0x08000000
	ODOMETER_Y = 0x04000000
	ODOMETER   = 0x0c000000

	// Combined accelerometer field
	ACCEL_ONE = 0x02000000

	// Directional speed
	VELOCITY_X = 0x01000000
	VELOCITY_Y = 0x00800000
	VELOCITY   = 0x01800000
)

Data Streaming Masks - MASK2

Variables

View Source
var (
	NotImplementedError       = errors.New("This feature is not yet implemented")
	GeneralError              = errors.New("General, non-specific error")
	ChecksumError             = errors.New("Checksum failure")
	CommandFragmentError      = errors.New("Received command fragment")
	UnknownCommandError       = errors.New("Unknown command ID")
	UnsupportedCommandError   = errors.New("Command currently unsupported")
	BadMessageFormatError     = errors.New("Bad message format")
	InvalidParametersError    = errors.New("Parameter value(s) invalid")
	FailedExecuteCommandError = errors.New("Failed to execute command")
	UnknownDeviceError        = errors.New("Unknown device ID")
	PowerTooLowError          = errors.New("Voltage too low for reflash operation")
	IllegalPageError          = errors.New("Illegal page number provided")
	FlashFailError            = errors.New("Page did not reprogram correctly")
	ApplicationCorruptError   = errors.New("Main application corrupt")
	MessageTimeoutError       = errors.New("Message state machine timed out")
	UnknownError              = errors.New("Unkown error")
)

Functions

This section is empty.

Types

type AsyncResponse

type AsyncResponse struct {
	Sop1   byte
	Sop2   byte
	IdCode byte
	Dlen   uint16
	Data   []byte
	Chk    uint8
}

Represents an async response from one of the async data commands:

  • SetDataStreaming
  • ConfigureLocator
  • ConfigureCollisionDetection

func (*AsyncResponse) Collision

func (r *AsyncResponse) Collision() (*Collision, error)

Parses the data portion of the async response into a Collision struct. This method may be inaccurate or fail if the data doesn't represent a collision.

func (*AsyncResponse) Location

func (r *AsyncResponse) Location() (*Location, error)

Parses the data portion of the async response into a Location struct. This method may be inaccurate or fail if the data doesn't represent a location.

func (*AsyncResponse) Sensors

func (r *AsyncResponse) Sensors(d interface{}) error

Unpacks sensor data from an async response. This method may be inaccurate or fail if the data doesn't represent sensor data.

Example
r := &AsyncResponse{}

// Define a struct containing `int16` fields for each mask value in
// SetDataStreaming.
type SensorData struct {
	// Field names don't matter, however the order should match the order
	// defined in the Sphero API spec.
	AccelX, AccelY, AccelZ int16

	// Also, you can ignore fields by giving them the name `_`.
	_ int16
}

// Then when receiving an `AsyncResponse`, you would call `Sensors`.
var data SensorData
if err := r.Sensors(&data); err != nil {
	// Handle error
}
Output:

type Collision

type Collision struct {
	X, Y, Z    int16
	Axis       uint8
	XMag, YMag int16
	Speed      uint8
	TimeStamp  int32
}

Represents collision data from the Collision service. See ConfigureCollisionDetection.

type Color

type Color struct {
	R, G, B uint8
}

Simple Color struct. See GetRGBLED.

type Location

type Location struct {
	XPos, YPos, XVel, YVel, SoG uint16
}

Represents collision data from the Locator service. See ConfigureLocator.

type PowerState

type PowerState struct {
	RecVer, PowerState                    uint8
	BattVoltage, NumCharges, TimeSinceChg uint16
}

Represents the power state of the Sphero. See SetPowerNotification.

type Response

type Response struct {
	Sop1 byte
	Sop2 byte
	Mrsp byte
	Seq  uint8
	Dlen uint8
	Data []byte
	Chk  uint8
}

Represents a command response.

func (*Response) Color

func (r *Response) Color() (*Color, error)

Parses the data portion of the response into a Color struct. This method may be inaccurate or fail if the data doesn't represent a color.

func (*Response) Error

func (r *Response) Error() (err error)

Returns the appropriate error from the message response (MRSP) field, if any.

func (*Response) PowerState

func (r *Response) PowerState() (*PowerState, error)

Parses the data portion of the response into a PowerState struct. This method may be inaccurate or fail if the data doesn't represent a color.

type Sphero

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

Sphero represents a connection to a single Sphero robot.

func NewSphero

func NewSphero(name string, async chan<- *AsyncResponse) (*Sphero, error)

NewSphero creates and initializes a Sphero connection. It will attempt to connect to the device at the `name` provided or throw an error if this fails.

func (*Sphero) Close

func (s *Sphero) Close() error

Implement io.Closer

func (*Sphero) ConfigureCollisionDetection

func (s *Sphero) ConfigureCollisionDetection(method, xThreshold, yThreshold, xSpeed, ySpeed, deadTime uint8, res chan<- *Response) error

ConfigureCollisionDetection method - Currently this must be either 0x01 (enabled) or 0x00 (disabled)

func (*Sphero) ConfigureLocator

func (s *Sphero) ConfigureLocator(flags uint8, x, y, yawTare uint16, res chan<- *Response) error

func (*Sphero) GetPowerState

func (s *Sphero) GetPowerState(res chan<- *Response) error

Gets the current power state of the device. See `Response.PowerState()`.

func (*Sphero) GetRGBLED

func (s *Sphero) GetRGBLED(res chan<- *Response) error

Returns the "user LED color". The color displayed after a successful bluetooth connection.

func (*Sphero) Ping

func (s *Sphero) Ping(res chan<- *Response) error

func (*Sphero) Read

func (s *Sphero) Read(data []byte) (int, error)

Implement io.Reader

func (*Sphero) ReadLocator

func (s *Sphero) ReadLocator(res chan<- *Response) error

func (*Sphero) Roll

func (s *Sphero) Roll() error

func (*Sphero) SelfLevel

func (s *Sphero) SelfLevel() error

func (*Sphero) Send

func (s *Sphero) Send(did, cid uint8, data []byte, res chan<- *Response) error

func (*Sphero) SetBackLEDOutput

func (s *Sphero) SetBackLEDOutput(brightness uint8, res chan<- *Response) error

func (*Sphero) SetDataStreaming

func (s *Sphero) SetDataStreaming(n, m int16, pcnt uint8, masks []uint32, masks2 []uint32, res chan<- *Response) error

SetDataStreaming - turns on async data streaming from sensors. n - Divisor of the maximum sensor sampling rate (e.g. 400hz / N) m - Number of sample frames emitted per packet masks - See const.go for valid masks pcnt - Packet count 1-255 (or 0 for unlimited streaming) masks2 - See const.go for valid masks

func (*Sphero) SetHeading

func (s *Sphero) SetHeading(heading int16, res chan<- *Response) error

func (*Sphero) SetPowerNotification

func (s *Sphero) SetPowerNotification(flag bool, res chan<- *Response) error

Turns on async power notifications.

func (*Sphero) SetRGBLEDOutput

func (s *Sphero) SetRGBLEDOutput(red, green, blue uint8, flag bool, res chan<- *Response) error

func (*Sphero) SetRawMotorValues

func (s *Sphero) SetRawMotorValues() error

func (*Sphero) SetRotationRate

func (s *Sphero) SetRotationRate(rate uint8, res chan<- *Response) error

func (*Sphero) SetStabilization

func (s *Sphero) SetStabilization(flag bool, res chan<- *Response) error

func (*Sphero) Sleep

func (s *Sphero) Sleep(wakeup time.Duration, macro uint8, orbBasic uint16, res chan<- *Response) error

func (*Sphero) Write

func (s *Sphero) Write(data []byte) (int, error)

Implement io.Writer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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