Skip to content

Commit

Permalink
Merge pull request hybridgroup#175 from hybridgroup/lidarlite
Browse files Browse the repository at this point in the history
Support for LIDAR-Lite laser rangefinder
  • Loading branch information
zankich committed Feb 18, 2015
2 parents 5ffa399 + 65fce5a commit 986a3e7
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
61 changes: 61 additions & 0 deletions platforms/i2c/lidarlite_driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package i2c

import (
"github.com/hybridgroup/gobot"

"time"
)

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

type LIDARLiteDriver struct {
name string
connection I2c
}

// NewLIDARLiteDriver creates a new driver with specified name and i2c interface
func NewLIDARLiteDriver(a I2c, name string) *LIDARLiteDriver {
return &LIDARLiteDriver{
name: name,
connection: a,
}
}

func (h *LIDARLiteDriver) Name() string { return h.name }
func (h *LIDARLiteDriver) Connection() gobot.Connection { return h.connection.(gobot.Connection) }

// Start initialized the LIDAR
func (h *LIDARLiteDriver) Start() (errs []error) {
if err := h.connection.I2cStart(0x62); err != nil {
return []error{err}
}
return
}

// Halt returns true if devices is halted successfully
func (h *LIDARLiteDriver) Halt() (errs []error) { return }

// Distance returns the current distance
func (h *LIDARLiteDriver) Distance() (distance int, err error) {
if err = h.connection.I2cWrite([]byte{0x00, 0x04}); err != nil {
return
}
<-time.After(20 * time.Millisecond)

if err = h.connection.I2cWrite([]byte{0x8f}); err != nil {
return
}
<-time.After(20 * time.Millisecond)

ret, err := h.connection.I2cRead(2)
if err != nil {
return
}
if len(ret) == 2 {
distance = (int(ret[1]) + int(ret[0])*256)
return
} else {
err = ErrNotEnoughBytes
}
return
}
99 changes: 99 additions & 0 deletions platforms/i2c/lidarlite_driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package i2c

import (
"errors"
"testing"

"github.com/hybridgroup/gobot"
)

// --------- HELPERS
func initTestLIDARLiteDriver() (driver *LIDARLiteDriver) {
driver, _ = initTestLIDARLiteDriverWithStubbedAdaptor()
return
}

func initTestLIDARLiteDriverWithStubbedAdaptor() (*LIDARLiteDriver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor("adaptor")
return NewLIDARLiteDriver(adaptor, "bot"), adaptor
}

// --------- TESTS

func TestNewLIDARLiteDriver(t *testing.T) {
// Does it return a pointer to an instance of LIDARLiteDriver?
var bm interface{} = NewLIDARLiteDriver(newI2cTestAdaptor("adaptor"), "bot")
_, ok := bm.(*LIDARLiteDriver)
if !ok {
t.Errorf("NewLIDARLiteDriver() should have returned a *LIDARLiteDriver")
}

b := NewLIDARLiteDriver(newI2cTestAdaptor("adaptor"), "bot")
gobot.Assert(t, b.Name(), "bot")
gobot.Assert(t, b.Connection().Name(), "adaptor")
}

// Methods
func TestLIDARLiteDriverStart(t *testing.T) {
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()

gobot.Assert(t, len(hmc.Start()), 0)

adaptor.i2cStartImpl = func() error {
return errors.New("start error")
}
err := hmc.Start()
gobot.Assert(t, err[0], errors.New("start error"))

}

func TestLIDARLiteDriverHalt(t *testing.T) {
hmc := initTestLIDARLiteDriver()

gobot.Assert(t, len(hmc.Halt()), 0)
}

func TestLIDARLiteDriverDistance(t *testing.T) {
// when len(data) is 2
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()

adaptor.i2cReadImpl = func() ([]byte, error) {
return []byte{99, 1}, nil
}

distance, _ := hmc.Distance()
gobot.Assert(t, distance, int(25345))

// when len(data) is not 2
hmc, adaptor = initTestLIDARLiteDriverWithStubbedAdaptor()

adaptor.i2cReadImpl = func() ([]byte, error) {
return []byte{99}, nil
}

distance, err := hmc.Distance()
gobot.Assert(t, distance, int(0))
gobot.Assert(t, err, ErrNotEnoughBytes)

// when read error
hmc, adaptor = initTestLIDARLiteDriverWithStubbedAdaptor()

adaptor.i2cReadImpl = func() ([]byte, error) {
return []byte{}, errors.New("read error")
}

distance, err = hmc.Distance()
gobot.Assert(t, distance, int(0))
gobot.Assert(t, err, errors.New("read error"))

// when write error
hmc, adaptor = initTestLIDARLiteDriverWithStubbedAdaptor()

adaptor.i2cWriteImpl = func() error {
return errors.New("write error")
}

distance, err = hmc.Distance()
gobot.Assert(t, distance, int(0))
gobot.Assert(t, err, errors.New("write error"))
}

0 comments on commit 986a3e7

Please sign in to comment.