2015-02-17 12:11:02 -08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot"
|
2015-02-17 12:11:02 -08:00
|
|
|
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-07-03 18:57:29 -07:00
|
|
|
const lidarliteAddress = 0x62
|
|
|
|
|
2015-02-17 12:11:02 -08:00
|
|
|
type LIDARLiteDriver struct {
|
|
|
|
name string
|
2017-02-06 00:19:42 +01:00
|
|
|
connector I2cConnector
|
|
|
|
connection I2cConnection
|
2017-02-09 14:35:48 +01:00
|
|
|
I2cConfig
|
2015-02-17 12:11:02 -08:00
|
|
|
}
|
|
|
|
|
2016-09-25 14:08:18 +02:00
|
|
|
// NewLIDARLiteDriver creates a new driver with specified i2c interface
|
2017-02-09 14:35:48 +01:00
|
|
|
func NewLIDARLiteDriver(a I2cConnector, options ...func(I2cConfig)) *LIDARLiteDriver {
|
2017-02-09 11:23:36 +01:00
|
|
|
l := &LIDARLiteDriver{
|
2017-02-06 00:19:42 +01:00
|
|
|
name: gobot.DefaultName("LIDARLite"),
|
|
|
|
connector: a,
|
2017-02-09 14:35:48 +01:00
|
|
|
I2cConfig: NewI2cConfig(),
|
2015-02-17 12:11:02 -08:00
|
|
|
}
|
2017-02-09 11:23:36 +01:00
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
option(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add commands to API
|
|
|
|
return l
|
2015-02-17 12:11:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *LIDARLiteDriver) Name() string { return h.name }
|
2016-09-25 14:08:18 +02:00
|
|
|
func (h *LIDARLiteDriver) SetName(n string) { h.name = n }
|
2017-02-06 00:19:42 +01:00
|
|
|
func (h *LIDARLiteDriver) Connection() gobot.Connection { return h.connector.(gobot.Connection) }
|
2015-02-17 12:11:02 -08:00
|
|
|
|
|
|
|
// Start initialized the LIDAR
|
2016-11-07 14:55:21 +01:00
|
|
|
func (h *LIDARLiteDriver) Start() (err error) {
|
2017-02-09 14:35:48 +01:00
|
|
|
bus := h.GetBus(h.connector.I2cGetDefaultBus())
|
|
|
|
address := h.GetAddress(lidarliteAddress)
|
2017-02-09 11:23:36 +01:00
|
|
|
|
2017-02-09 14:35:48 +01:00
|
|
|
h.connection, err = h.connector.I2cGetConnection(address, bus)
|
2017-02-06 00:19:42 +01:00
|
|
|
if err != nil {
|
2016-11-07 14:55:21 +01:00
|
|
|
return err
|
2015-02-17 12:11:02 -08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Halt returns true if devices is halted successfully
|
2016-11-07 14:55:21 +01:00
|
|
|
func (h *LIDARLiteDriver) Halt() (err error) { return }
|
2015-02-17 12:11:02 -08:00
|
|
|
|
2015-07-12 15:22:23 -07:00
|
|
|
// Distance returns the current distance in cm
|
2015-02-17 12:11:02 -08:00
|
|
|
func (h *LIDARLiteDriver) Distance() (distance int, err error) {
|
2017-02-06 00:19:42 +01:00
|
|
|
if _, err = h.connection.Write([]byte{0x00, 0x04}); err != nil {
|
2015-02-17 12:11:02 -08:00
|
|
|
return
|
|
|
|
}
|
2016-11-05 13:05:49 +01:00
|
|
|
time.Sleep(20 * time.Millisecond)
|
2015-02-17 12:11:02 -08:00
|
|
|
|
2017-02-06 00:19:42 +01:00
|
|
|
if _, err = h.connection.Write([]byte{0x0F}); err != nil {
|
2015-02-17 12:11:02 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-06 00:19:42 +01:00
|
|
|
upper := []byte{0}
|
|
|
|
bytesRead, err := h.connection.Read(upper)
|
2015-02-17 12:11:02 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-07-12 15:22:23 -07:00
|
|
|
|
2017-02-06 00:19:42 +01:00
|
|
|
if bytesRead != 1 {
|
2015-07-12 15:22:23 -07:00
|
|
|
err = ErrNotEnoughBytes
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-06 00:19:42 +01:00
|
|
|
if _, err = h.connection.Write([]byte{0x10}); err != nil {
|
2015-02-17 12:11:02 -08:00
|
|
|
return
|
2015-07-12 15:22:23 -07:00
|
|
|
}
|
|
|
|
|
2017-02-06 00:19:42 +01:00
|
|
|
lower := []byte{0}
|
|
|
|
bytesRead, err = h.connection.Read(lower)
|
2015-07-12 15:22:23 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-06 00:19:42 +01:00
|
|
|
if bytesRead != 1 {
|
2015-02-17 12:11:02 -08:00
|
|
|
err = ErrNotEnoughBytes
|
2015-07-12 15:22:23 -07:00
|
|
|
return
|
2015-02-17 12:11:02 -08:00
|
|
|
}
|
2015-07-12 15:22:23 -07:00
|
|
|
|
|
|
|
distance = ((int(upper[0]) & 0xff) << 8) | (int(lower[0]) & 0xff)
|
|
|
|
|
2015-02-17 12:11:02 -08:00
|
|
|
return
|
|
|
|
}
|