Skip to content

Commit

Permalink
gopigo3: integration of recent GoPiGo3 contributions
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Sep 14, 2017
1 parent 40fc1a2 commit c7a68b1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 37 deletions.
4 changes: 2 additions & 2 deletions examples/gopigo3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"time"

"gobot.io/x/gobot"
g "gobot.io/x/gobot/platforms/gopigo3"
g "gobot.io/x/gobot/platforms/dexter/gopigo3"
"gobot.io/x/gobot/platforms/raspi"
)

func main() {
raspiAdaptor := raspi.NewAdaptor()
gopigo3 := g.NewGoPiGo3Driver(raspiAdaptor)
gopigo3 := g.NewDriver(raspiAdaptor)

work := func() {
on := uint8(0xFF)
Expand Down
4 changes: 2 additions & 2 deletions platforms/dexter/gopigo3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import (
"time"

"gobot.io/x/gobot"
g "gobot.io/x/gobot/platforms/gopigo3"
g "gobot.io/x/gobot/platforms/dexter/gopigo3"
"gobot.io/x/gobot/platforms/raspi"
)

func main() {
raspiAdaptor := raspi.NewAdaptor()
gopigo3 := g.NewGoPiGo3Driver(raspiAdaptor)
gopigo3 := g.NewDriver(raspiAdaptor)

work := func() {
on := uint8(0xFF)
Expand Down
64 changes: 31 additions & 33 deletions platforms/dexter/gopigo3/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,42 +143,40 @@ const (
I2C_ERROR
)

var _ gobot.Driver = (*GoPiGo3Driver)(nil)

// GoPiGo3Driver is a Gobot Driver for the GoPiGo3 board.
type GoPiGo3Driver struct {
// Driver is a Gobot Driver for the GoPiGo3 board.
type Driver struct {
name string
connector spi.Connector
connection spi.Connection
}

// NewGoPiGo3Driver creates a new Gobot Driver for the GoPiGo3 board.
// NewDriver creates a new Gobot Driver for the GoPiGo3 board.
//
// Params:
// a *Adaptor - the Adaptor to use with this Driver
//
func NewGoPiGo3Driver(a spi.Connector) *GoPiGo3Driver {
g := &GoPiGo3Driver{
func NewDriver(a spi.Connector) *Driver {
g := &Driver{
name: gobot.DefaultName("GoPiGo3"),
connector: a,
}
return g
}

// Name returns the name of the device.
func (g *GoPiGo3Driver) Name() string { return g.name }
func (g *Driver) Name() string { return g.name }

// SetName sets the name of the device.
func (g *GoPiGo3Driver) SetName(n string) { g.name = n }
func (g *Driver) SetName(n string) { g.name = n }

// Connection returns the Connection of the device.
func (g *GoPiGo3Driver) Connection() gobot.Connection { return g.connection.(gobot.Connection) }
func (g *Driver) Connection() gobot.Connection { return g.connection.(gobot.Connection) }

// Halt stops the driver.
func (g *GoPiGo3Driver) Halt() (err error) { return }
func (g *Driver) Halt() (err error) { return }

// Start initializes the GoPiGo3
func (g *GoPiGo3Driver) Start() (err error) {
func (g *Driver) Start() (err error) {
bus := g.connector.GetSpiDefaultBus()
mode := g.connector.GetSpiDefaultMode()
maxSpeed := g.connector.GetSpiDefaultMaxSpeed()
Expand All @@ -187,7 +185,7 @@ func (g *GoPiGo3Driver) Start() (err error) {
}

// GetManufacturerName returns the manufacturer from the firmware.
func (g *GoPiGo3Driver) GetManufacturerName() (mName string, err error) {
func (g *Driver) GetManufacturerName() (mName string, err error) {
// read 24 bytes to get manufacturer name
response, err := g.readBytes(goPiGo3Address, GET_MANUFACTURER, 24)
if err != nil {
Expand All @@ -201,7 +199,7 @@ func (g *GoPiGo3Driver) GetManufacturerName() (mName string, err error) {
}

// GetBoardName returns the board name from the firmware.
func (g *GoPiGo3Driver) GetBoardName() (bName string, err error) {
func (g *Driver) GetBoardName() (bName string, err error) {
// read 24 bytes to get board name
response, err := g.readBytes(goPiGo3Address, GET_NAME, 24)
if err != nil {
Expand All @@ -215,7 +213,7 @@ func (g *GoPiGo3Driver) GetBoardName() (bName string, err error) {
}

// GetHardwareVersion returns the hardware version from the firmware.
func (g *GoPiGo3Driver) GetHardwareVersion() (hVer string, err error) {
func (g *Driver) GetHardwareVersion() (hVer string, err error) {
response, err := g.readUint32(goPiGo3Address, GET_HARDWARE_VERSION)
if err != nil {
return hVer, err
Expand All @@ -227,7 +225,7 @@ func (g *GoPiGo3Driver) GetHardwareVersion() (hVer string, err error) {
}

// GetFirmwareVersion returns the current firmware version.
func (g *GoPiGo3Driver) GetFirmwareVersion() (fVer string, err error) {
func (g *Driver) GetFirmwareVersion() (fVer string, err error) {
response, err := g.readUint32(goPiGo3Address, GET_FIRMWARE_VERSION)
if err != nil {
return fVer, err
Expand All @@ -239,7 +237,7 @@ func (g *GoPiGo3Driver) GetFirmwareVersion() (fVer string, err error) {
}

// GetSerialNumber returns the 128-bit hardware serial number of the board.
func (g *GoPiGo3Driver) GetSerialNumber() (sNum string, err error) {
func (g *Driver) GetSerialNumber() (sNum string, err error) {
// read 20 bytes to get the serial number
response, err := g.readBytes(goPiGo3Address, GET_ID, 20)
if err != nil {
Expand All @@ -253,19 +251,19 @@ func (g *GoPiGo3Driver) GetSerialNumber() (sNum string, err error) {
}

// Get5vVoltage returns the current voltage on the 5v line.
func (g *GoPiGo3Driver) Get5vVoltage() (voltage float32, err error) {
func (g *Driver) Get5vVoltage() (voltage float32, err error) {
val, err := g.readUint16(goPiGo3Address, GET_VOLTAGE_5V)
return (float32(val) / 1000.0), err
}

// GetBatteryVoltage gets the battery voltage from the main battery pack (7v-12v).
func (g *GoPiGo3Driver) GetBatteryVoltage() (voltage float32, err error) {
func (g *Driver) GetBatteryVoltage() (voltage float32, err error) {
val, err := g.readUint16(goPiGo3Address, GET_VOLTAGE_VCC)
return (float32(val) / 1000.0), err
}

// SetLED sets rgb values from 0 to 255.
func (g *GoPiGo3Driver) SetLED(led Led, red, green, blue uint8) error {
func (g *Driver) SetLED(led Led, red, green, blue uint8) error {
return g.writeBytes([]byte{
goPiGo3Address,
SET_LED,
Expand All @@ -277,7 +275,7 @@ func (g *GoPiGo3Driver) SetLED(led Led, red, green, blue uint8) error {
}

// SetServo sets a servo's position in microseconds (0-16666).
func (g *GoPiGo3Driver) SetServo(servo Servo, us uint16) error {
func (g *Driver) SetServo(servo Servo, us uint16) error {
return g.writeBytes([]byte{
goPiGo3Address,
SET_SERVO,
Expand All @@ -287,7 +285,7 @@ func (g *GoPiGo3Driver) SetServo(servo Servo, us uint16) error {
}

// SetMotorPower from -128 to 127.
func (g *GoPiGo3Driver) SetMotorPower(motor Motor, power int8) error {
func (g *Driver) SetMotorPower(motor Motor, power int8) error {
return g.writeBytes([]byte{
goPiGo3Address,
SET_MOTOR_PWM,
Expand All @@ -297,7 +295,7 @@ func (g *GoPiGo3Driver) SetMotorPower(motor Motor, power int8) error {
}

// SetMotorPosition sets the motor's position in degrees.
func (g *GoPiGo3Driver) SetMotorPosition(motor Motor, position float64) error {
func (g *Driver) SetMotorPosition(motor Motor, position float64) error {
positionRaw := math.Float64bits(position * MOTOR_TICKS_PER_DEGREE)
return g.writeBytes([]byte{
goPiGo3Address,
Expand All @@ -311,7 +309,7 @@ func (g *GoPiGo3Driver) SetMotorPosition(motor Motor, position float64) error {
}

// SetMotorDps sets the motor target speed in degrees per second.
func (g *GoPiGo3Driver) SetMotorDps(motor Motor, dps float64) error {
func (g *Driver) SetMotorDps(motor Motor, dps float64) error {
dpsUint := math.Float64bits(dps * MOTOR_TICKS_PER_DEGREE)
return g.writeBytes([]byte{
goPiGo3Address,
Expand All @@ -323,7 +321,7 @@ func (g *GoPiGo3Driver) SetMotorDps(motor Motor, dps float64) error {
}

// SetMotorLimits sets the speed limits for a motor.
func (g *GoPiGo3Driver) SetMotorLimits(motor Motor, power int8, dps float64) error {
func (g *Driver) SetMotorLimits(motor Motor, power int8, dps float64) error {
dpsUint := math.Float64bits(dps * MOTOR_TICKS_PER_DEGREE)
return g.writeBytes([]byte{
goPiGo3Address,
Expand All @@ -336,7 +334,7 @@ func (g *GoPiGo3Driver) SetMotorLimits(motor Motor, power int8, dps float64) err
}

// GetMotorStatus returns the status for the given motor.
func (g *GoPiGo3Driver) GetMotorStatus(motor Motor) (flags uint8, power uint16, encoder, dps float64, err error) {
func (g *Driver) GetMotorStatus(motor Motor) (flags uint8, power uint16, encoder, dps float64, err error) {
message := GET_MOTOR_STATUS_RIGHT
if motor == MOTOR_LEFT {
message = GET_MOTOR_STATUS_LEFT
Expand Down Expand Up @@ -365,7 +363,7 @@ func (g *GoPiGo3Driver) GetMotorStatus(motor Motor) (flags uint8, power uint16,
}

// GetMotorEncoder reads a motor's encoder in degrees.
func (g *GoPiGo3Driver) GetMotorEncoder(motor Motor) (encoder float64, err error) {
func (g *Driver) GetMotorEncoder(motor Motor) (encoder float64, err error) {
message := GET_MOTOR_ENCODER_RIGHT
if motor == MOTOR_LEFT {
message = GET_MOTOR_ENCODER_LEFT
Expand All @@ -381,7 +379,7 @@ func (g *GoPiGo3Driver) GetMotorEncoder(motor Motor) (encoder float64, err error
}

// OffsetMotorEncoder offsets a motor's encoder for calibration purposes.
func (g *GoPiGo3Driver) OffsetMotorEncoder(motor Motor, offset float64) error {
func (g *Driver) OffsetMotorEncoder(motor Motor, offset float64) error {
offsetUint := math.Float64bits(offset * MOTOR_TICKS_PER_DEGREE)
return g.writeBytes([]byte{
goPiGo3Address,
Expand All @@ -396,7 +394,7 @@ func (g *GoPiGo3Driver) OffsetMotorEncoder(motor Motor, offset float64) error {

//TODO: add grove functions

func (g *GoPiGo3Driver) readBytes(address byte, msg byte, numBytes int) (val []byte, err error) {
func (g *Driver) readBytes(address byte, msg byte, numBytes int) (val []byte, err error) {
w := make([]byte, numBytes)
w[0] = address
w[1] = msg
Expand All @@ -408,30 +406,30 @@ func (g *GoPiGo3Driver) readBytes(address byte, msg byte, numBytes int) (val []b
return r, nil
}

func (g *GoPiGo3Driver) readUint8(address, msg byte) (val uint8, err error) {
func (g *Driver) readUint8(address, msg byte) (val uint8, err error) {
r, err := g.readBytes(address, msg, 8)
if err != nil {
return val, err
}
return uint8(r[4]) << 8, nil
}

func (g *GoPiGo3Driver) readUint16(address, msg byte) (val uint16, err error) {
func (g *Driver) readUint16(address, msg byte) (val uint16, err error) {
r, err := g.readBytes(address, msg, 8)
if err != nil {
return val, err
}
return uint16(r[4])<<8 | uint16(r[5]), nil
}

func (g *GoPiGo3Driver) readUint32(address, msg byte) (val uint32, err error) {
func (g *Driver) readUint32(address, msg byte) (val uint32, err error) {
r, err := g.readBytes(address, msg, 8)
if err != nil {
return val, err
}
return uint32(r[4])<<24 | uint32(r[5])<<16 | uint32(r[6])<<8 | uint32(r[7]), nil
}

func (g *GoPiGo3Driver) writeBytes(w []byte) (err error) {
func (g *Driver) writeBytes(w []byte) (err error) {
return g.connection.Tx(w, nil)
}
13 changes: 13 additions & 0 deletions platforms/dexter/gopigo3/driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gopigo3

import (
"testing"

"gobot.io/x/gobot"
)

var _ gobot.Driver = (*Driver)(nil)

func TestGoPiGo3Driver(t *testing.T) {
t.Skip("Tests needed here...")
}

0 comments on commit c7a68b1

Please sign in to comment.