2015-02-18 04:11:02 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
2015-02-18 04:11:02 +08:00
|
|
|
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-07-04 09:57:29 +08:00
|
|
|
const lidarliteAddress = 0x62
|
|
|
|
|
2015-02-18 04:11:02 +08:00
|
|
|
type LIDARLiteDriver struct {
|
|
|
|
name string
|
2017-02-10 18:08:32 +08:00
|
|
|
connector Connector
|
|
|
|
connection Connection
|
|
|
|
Config
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:08:18 +08:00
|
|
|
// NewLIDARLiteDriver creates a new driver with specified i2c interface
|
2017-02-09 23:47:11 +08:00
|
|
|
// Params:
|
2017-02-10 18:08:32 +08:00
|
|
|
// conn Connector - the Adaptor to use with this Driver
|
2017-02-09 23:47:11 +08:00
|
|
|
//
|
|
|
|
// Optional params:
|
2017-02-10 18:08:32 +08:00
|
|
|
// i2c.WithBus(int): bus to use with this driver
|
|
|
|
// i2c.WithAddress(int): address to use with this driver
|
2017-02-09 23:47:11 +08:00
|
|
|
//
|
2017-02-10 18:08:32 +08:00
|
|
|
func NewLIDARLiteDriver(a Connector, options ...func(Config)) *LIDARLiteDriver {
|
2017-02-09 18:23:36 +08:00
|
|
|
l := &LIDARLiteDriver{
|
2017-02-06 07:19:42 +08:00
|
|
|
name: gobot.DefaultName("LIDARLite"),
|
|
|
|
connector: a,
|
2017-02-10 18:08:32 +08:00
|
|
|
Config: NewConfig(),
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
2017-02-09 18:23:36 +08:00
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
option(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add commands to API
|
|
|
|
return l
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2017-02-10 18:08:32 +08:00
|
|
|
// Name returns the Name for the Driver
|
|
|
|
func (h *LIDARLiteDriver) Name() string { return h.name }
|
|
|
|
|
|
|
|
// SetName sets the Name for the Driver
|
|
|
|
func (h *LIDARLiteDriver) SetName(n string) { h.name = n }
|
|
|
|
|
|
|
|
// Connection returns the connection for the Driver
|
2017-02-06 07:19:42 +08:00
|
|
|
func (h *LIDARLiteDriver) Connection() gobot.Connection { return h.connector.(gobot.Connection) }
|
2015-02-18 04:11:02 +08:00
|
|
|
|
|
|
|
// Start initialized the LIDAR
|
2016-11-07 21:55:21 +08:00
|
|
|
func (h *LIDARLiteDriver) Start() (err error) {
|
2017-02-10 18:08:32 +08:00
|
|
|
bus := h.GetBusOrDefault(h.connector.GetDefaultBus())
|
|
|
|
address := h.GetAddressOrDefault(lidarliteAddress)
|
2017-02-09 18:23:36 +08:00
|
|
|
|
2017-02-10 18:08:32 +08:00
|
|
|
h.connection, err = h.connector.GetConnection(address, bus)
|
2017-02-06 07:19:42 +08:00
|
|
|
if 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) {
|
2017-02-06 07:19:42 +08:00
|
|
|
if _, err = h.connection.Write([]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
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
if _, err = h.connection.Write([]byte{0x0F}); err != nil {
|
2015-02-18 04:11:02 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
upper := []byte{0}
|
|
|
|
bytesRead, err := h.connection.Read(upper)
|
2015-02-18 04:11:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-07-13 06:22:23 +08:00
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
if bytesRead != 1 {
|
2015-07-13 06:22:23 +08:00
|
|
|
err = ErrNotEnoughBytes
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
if _, err = h.connection.Write([]byte{0x10}); err != nil {
|
2015-02-18 04:11:02 +08:00
|
|
|
return
|
2015-07-13 06:22:23 +08:00
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
lower := []byte{0}
|
|
|
|
bytesRead, err = h.connection.Read(lower)
|
2015-07-13 06:22:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
if bytesRead != 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
|
|
|
|
}
|