forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hybridgroup#175 from hybridgroup/lidarlite
Support for LIDAR-Lite laser rangefinder
- Loading branch information
Showing
2 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |