2015-02-18 04:11:02 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*LIDARLiteDriver)(nil)
|
|
|
|
|
2015-07-04 09:57:29 +08:00
|
|
|
const lidarliteAddress = 0x62
|
|
|
|
|
2015-02-18 04:11:02 +08:00
|
|
|
type LIDARLiteDriver struct {
|
|
|
|
name string
|
|
|
|
connection I2c
|
|
|
|
}
|
|
|
|
|
2016-09-25 20:08:18 +08:00
|
|
|
// NewLIDARLiteDriver creates a new driver with specified i2c interface
|
|
|
|
func NewLIDARLiteDriver(a I2c) *LIDARLiteDriver {
|
2015-02-18 04:11:02 +08:00
|
|
|
return &LIDARLiteDriver{
|
2016-10-04 01:06:37 +08:00
|
|
|
name: "LIDARLite",
|
2015-02-18 04:11:02 +08:00
|
|
|
connection: a,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *LIDARLiteDriver) Name() string { return h.name }
|
2016-09-25 20:08:18 +08:00
|
|
|
func (h *LIDARLiteDriver) SetName(n string) { h.name = n }
|
2015-02-18 04:11:02 +08:00
|
|
|
func (h *LIDARLiteDriver) Connection() gobot.Connection { return h.connection.(gobot.Connection) }
|
|
|
|
|
|
|
|
// Start initialized the LIDAR
|
2016-11-07 21:55:21 +08:00
|
|
|
func (h *LIDARLiteDriver) Start() (err error) {
|
2015-07-04 09:57:29 +08:00
|
|
|
if err := h.connection.I2cStart(lidarliteAddress); err != nil {
|
2016-11-07 21:55:21 +08:00
|
|
|
return err
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Halt returns true if devices is halted successfully
|
2016-11-07 21:55:21 +08:00
|
|
|
func (h *LIDARLiteDriver) Halt() (err error) { return }
|
2015-02-18 04:11:02 +08:00
|
|
|
|
2015-07-13 06:22:23 +08:00
|
|
|
// Distance returns the current distance in cm
|
2015-02-18 04:11:02 +08:00
|
|
|
func (h *LIDARLiteDriver) Distance() (distance int, err error) {
|
2015-07-04 09:57:29 +08:00
|
|
|
if err = h.connection.I2cWrite(lidarliteAddress, []byte{0x00, 0x04}); err != nil {
|
2015-02-18 04:11:02 +08:00
|
|
|
return
|
|
|
|
}
|
2016-11-05 20:05:49 +08:00
|
|
|
time.Sleep(20 * time.Millisecond)
|
2015-02-18 04:11:02 +08:00
|
|
|
|
2015-07-13 06:22:23 +08:00
|
|
|
if err = h.connection.I2cWrite(lidarliteAddress, []byte{0x0F}); err != nil {
|
2015-02-18 04:11:02 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-13 06:22:23 +08:00
|
|
|
upper, err := h.connection.I2cRead(lidarliteAddress, 1)
|
2015-02-18 04:11:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-07-13 06:22:23 +08:00
|
|
|
|
|
|
|
if len(upper) != 1 {
|
|
|
|
err = ErrNotEnoughBytes
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.connection.I2cWrite(lidarliteAddress, []byte{0x10}); err != nil {
|
2015-02-18 04:11:02 +08:00
|
|
|
return
|
2015-07-13 06:22:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
lower, err := h.connection.I2cRead(lidarliteAddress, 1)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lower) != 1 {
|
2015-02-18 04:11:02 +08:00
|
|
|
err = ErrNotEnoughBytes
|
2015-07-13 06:22:23 +08:00
|
|
|
return
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
2015-07-13 06:22:23 +08:00
|
|
|
|
|
|
|
distance = ((int(upper[0]) & 0xff) << 8) | (int(lower[0]) & 0xff)
|
|
|
|
|
2015-02-18 04:11:02 +08:00
|
|
|
return
|
|
|
|
}
|