vl53l0x

package module
v0.0.0-...-cfe6567 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2018 License: MIT Imports: 5 Imported by: 1

README

VL53L0X time-of-flight ranging sensor

Build Status Go Report Card GoDoc MIT License

VL53L0X (general specification, native C code API specification) is a world’s smallest time-of-flight ranging and gesture detection sensor from STMicroelectronics. Easily integrated with Arduino and Raspberry PI via i2c communication interface: image

Sensor functionality is based on laser diode emission with following photodetector signal registration. Average time duration between emission and registration is a "time-of-flight", which translates to range distance.

Here is a library written in Go programming language for Raspberry PI and counterparts, which gives you in the output measured range value (making all necessary i2c-bus interacting and values computing).

This library is an adaptation and translation of well-formed C++ code written by www.pololu.com to Golang, taken from https://github.com/pololu/vl53l0x-arduino.

Golang usage

func main() {
    // Create new connection to i2c-bus on 0 line with address 0x29.
    // Use i2cdetect utility to find device address over the i2c-bus
    i2c, err := i2c.NewI2C(0x29, 0)
    if err != nil {
        log.Fatal(err)
    }
    defer i2c.Close()

    sensor := vl53l0x.NewVl53l0x()
    // It's highly recommended to reset sensor each time before repeated initialization.
    err = sensor.Reset(i2c)
    if err != nil {
        log.Fatal(err)
    }
    // By default, sensor initialized with "RegularRange" and "RegularAccuracy" parameters.
    err = sensor.Init(i2c)
    if err != nil {
        log.Fatal(err)
    }
    rng, err := sensor.ReadRangeSingleMillimeters(i2c)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("Measured range = %v mm", rng)

Getting help

GoDoc documentation

Installation

$ go get -u github.com/d2r2/go-vl53l0x

Troubleshooting

  • How to obtain fresh Golang installation to RPi device (either any RPi clone): If your RaspberryPI golang installation taken by default from repository is outdated, you may consider to install actual golang manually from official Golang site. Download tar.gz file containing armv6l in the name. Follow installation instructions.

  • How to enable I2C bus on RPi device: If you employ RaspberryPI, use raspi-config utility to activate i2c-bus on the OS level. Go to "Interfacing Options" menu, to active I2C bus. Probably you will need to reboot to load i2c kernel module. Finally you should have device like /dev/i2c-1 present in the system.

  • How to find I2C bus allocation and device address: Use i2cdetect utility in format "i2cdetect -y X", where X may vary from 0 to 5 or more, to discover address occupied by peripheral device. To install utility you should run apt install i2c-tools on debian-kind system. i2cdetect -y 1 sample output:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- 76 --    
    

Contact

Please use Github issue tracker for filing bugs or feature requests.

License

Go-vl53l0x is licensed under MIT License.

Documentation

Index

Constants

View Source
const (
	SYSRANGE_START = 0x00

	SYSTEM_THRESH_HIGH = 0x0C
	SYSTEM_THRESH_LOW  = 0x0E

	SYSTEM_SEQUENCE_CONFIG         = 0x01
	SYSTEM_RANGE_CONFIG            = 0x09
	SYSTEM_INTERMEASUREMENT_PERIOD = 0x04

	SYSTEM_INTERRUPT_CONFIG_GPIO = 0x0A

	GPIO_HV_MUX_ACTIVE_HIGH = 0x84

	SYSTEM_INTERRUPT_CLEAR = 0x0B

	RESULT_INTERRUPT_STATUS = 0x13
	RESULT_RANGE_STATUS     = 0x14

	RESULT_CORE_AMBIENT_WINDOW_EVENTS_RTN = 0xBC
	RESULT_CORE_RANGING_TOTAL_EVENTS_RTN  = 0xC0
	RESULT_CORE_AMBIENT_WINDOW_EVENTS_REF = 0xD0
	RESULT_CORE_RANGING_TOTAL_EVENTS_REF  = 0xD4
	RESULT_PEAK_SIGNAL_RATE_REF           = 0xB6

	ALGO_PART_TO_PART_RANGE_OFFSET_MM = 0x28

	I2C_SLAVE_DEVICE_ADDRESS = 0x8A

	MSRC_CONFIG_CONTROL = 0x60

	PRE_RANGE_CONFIG_MIN_SNR           = 0x27
	PRE_RANGE_CONFIG_VALID_PHASE_LOW   = 0x56
	PRE_RANGE_CONFIG_VALID_PHASE_HIGH  = 0x57
	PRE_RANGE_MIN_COUNT_RATE_RTN_LIMIT = 0x64

	FINAL_RANGE_CONFIG_MIN_SNR                  = 0x67
	FINAL_RANGE_CONFIG_VALID_PHASE_LOW          = 0x47
	FINAL_RANGE_CONFIG_VALID_PHASE_HIGH         = 0x48
	FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT = 0x44

	PRE_RANGE_CONFIG_SIGMA_THRESH_HI = 0x61
	PRE_RANGE_CONFIG_SIGMA_THRESH_LO = 0x62

	PRE_RANGE_CONFIG_VCSEL_PERIOD      = 0x50
	PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI = 0x51
	PRE_RANGE_CONFIG_TIMEOUT_MACROP_LO = 0x52

	SYSTEM_HISTOGRAM_BIN                  = 0x81
	HISTOGRAM_CONFIG_INITIAL_PHASE_SELECT = 0x33
	HISTOGRAM_CONFIG_READOUT_CTRL         = 0x55

	FINAL_RANGE_CONFIG_VCSEL_PERIOD       = 0x70
	FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI  = 0x71
	FINAL_RANGE_CONFIG_TIMEOUT_MACROP_LO  = 0x72
	CROSSTALK_COMPENSATION_PEAK_RATE_MCPS = 0x20

	MSRC_CONFIG_TIMEOUT_MACROP = 0x46

	SOFT_RESET_GO2_SOFT_RESET_N = 0xBF
	IDENTIFICATION_MODEL_ID     = 0xC0
	IDENTIFICATION_REVISION_ID  = 0xC2

	OSC_CALIBRATE_VAL = 0xF8

	GLOBAL_CONFIG_VCSEL_WIDTH        = 0x32
	GLOBAL_CONFIG_SPAD_ENABLES_REF_0 = 0xB0
	GLOBAL_CONFIG_SPAD_ENABLES_REF_1 = 0xB1
	GLOBAL_CONFIG_SPAD_ENABLES_REF_2 = 0xB2
	GLOBAL_CONFIG_SPAD_ENABLES_REF_3 = 0xB3
	GLOBAL_CONFIG_SPAD_ENABLES_REF_4 = 0xB4
	GLOBAL_CONFIG_SPAD_ENABLES_REF_5 = 0xB5

	GLOBAL_CONFIG_REF_EN_START_SELECT   = 0xB6
	DYNAMIC_SPAD_NUM_REQUESTED_REF_SPAD = 0x4E
	DYNAMIC_SPAD_REF_EN_START_OFFSET    = 0x4F
	POWER_MANAGEMENT_GO1_POWER_FORCE    = 0x80

	VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV = 0x89

	ALGO_PHASECAL_LIM            = 0x30
	ALGO_PHASECAL_CONFIG_TIMEOUT = 0x30
)

Registers from sensor hardware.

Variables

This section is empty.

Functions

This section is empty.

Types

type RangeSpec

type RangeSpec int

RangeSpec used to configure sensor for expected distance to measure.

const (
	// Signal rate limit = 0.25 MCPS, laser pulse periods = (14, 10).
	RegularRange RangeSpec = iota + 1
	// Signal rate limit = 0.10 MCPS, laser pulse periods = (18, 14).
	// Use "long range" mode only when "regular" can't detect distance
	// (returned distance value is 8190 mm or more). It's ordinary
	// happens, when distance exceed something about a meter.
	LongRange
)

func (RangeSpec) String

func (v RangeSpec) String() string

String implement Stringer interface.

type RegBytePair

type RegBytePair struct {
	Reg   byte
	Value uint8
}

Keeps pair of register and value to write to. Used as a bunch of registers which should be initialized with corresponding values.

type SequenceStepEnables

type SequenceStepEnables struct {
	TCC        bool
	MSRC       bool
	DSS        bool
	PreRange   bool
	FinalRange bool
}

TCC: Target CentreCheck MSRC: Minimum Signal Rate Check DSS: Dynamic Spad Selection

type SequenceStepTimeouts

type SequenceStepTimeouts struct {
	PreRangeVcselPeriodPclks   uint16
	FinalRangeVcselPeriodPclks uint16

	MsrcDssTccMclks uint16
	PreRangeMclks   uint16
	FinalRangeMclks uint16

	MsrcDssTccUsec uint32
	PreRangeUsec   uint32
	FinalRangeUsec uint32
}

type SpadInfo

type SpadInfo struct {
	Count          byte
	TypeIsAperture bool
}

SpadInfo keeps information about sensor SPAD (single photon avalanche diode) photodetector structure.

type SpeedAccuracySpec

type SpeedAccuracySpec int

SpeedAccuracySpec used to configure sensor for accuracy/measure time. It's clear that to improve accuracy, you should increase measure time.

const (
	// HighSpeed distance measurement takes 20 ms.
	HighSpeed SpeedAccuracySpec = iota + 1
	// RegularAccuracy distance measurement takes 33 ms.
	RegularAccuracy
	// GoodAccuracy distance measurement takes 66 ms.
	GoodAccuracy
	// HighAccuracy distance measurement takes 100 ms.
	HighAccuracy
	// HighestAccuracy distance measurement takes 200 ms.
	HighestAccuracy
)

func (SpeedAccuracySpec) String

func (v SpeedAccuracySpec) String() string

String implement Stringer interface.

type VcselPeriodType

type VcselPeriodType int

VcselPeriodType is a type of VCSEL (vertical cavity surface emitting laser) pulse period.

const (
	// pre-range pulse period
	VcselPeriodPreRange VcselPeriodType = iota + 1
	// final range pulse period
	VcselPeriodFinalRange
)

type Vl53l0x

type Vl53l0x struct {
	// contains filtered or unexported fields
}

Vl53l0x contains sensor data and corresponding methods.

func NewVl53l0x

func NewVl53l0x() *Vl53l0x

NewVl53l0x creates sensor instance.

func (*Vl53l0x) Config

func (v *Vl53l0x) Config(i2c *i2c.I2C, rng RangeSpec, speed SpeedAccuracySpec) error

Config configure sensor expected distance range and time to make a measurement.

func (*Vl53l0x) GetProductMinorRevision

func (v *Vl53l0x) GetProductMinorRevision(i2c *i2c.I2C) (byte, error)

GetProductMinorRevision takes revision from sensor hardware. Based on VL53L0X_GetProductRevision.

func (*Vl53l0x) GetSignalRateLimit

func (v *Vl53l0x) GetSignalRateLimit(i2c *i2c.I2C) (float32, error)

GetSignalRateLimit gets the return signal rate limit check value in MCPS.

func (*Vl53l0x) Init

func (v *Vl53l0x) Init(i2c *i2c.I2C) error

Init initialize sensor using sequence based on VL53L0X_DataInit(), VL53L0X_StaticInit(), and VL53L0X_PerformRefCalibration(). This function does not perform reference SPAD calibration (VL53L0X_PerformRefSpadManagement()), since the API user manual says that it is performed by ST on the bare modules; it seems like that should work well enough unless a cover glass is added.

func (*Vl53l0x) ReadRangeContinuousMillimeters

func (v *Vl53l0x) ReadRangeContinuousMillimeters(i2c *i2c.I2C) (uint16, error)

ReadRangeContinuousMillimeters returns a range reading in millimeters when continuous mode is active (readRangeSingleMillimeters() also calls this function after starting a single-shot range measurement).

func (*Vl53l0x) ReadRangeSingleMillimeters

func (v *Vl53l0x) ReadRangeSingleMillimeters(i2c *i2c.I2C) (uint16, error)

ReadRangeSingleMillimeters performs a single-shot range measurement and returns the reading in millimeters based on VL53L0X_PerformSingleRangingMeasurement().

func (*Vl53l0x) Reset

func (v *Vl53l0x) Reset(i2c *i2c.I2C) error

Reset soft-reset the sensor. Based on VL53L0X_ResetDevice().

func (*Vl53l0x) SetAddress

func (v *Vl53l0x) SetAddress(i2cRef **i2c.I2C, newAddr byte) error

SetAddress change default address of sensor and reopen I2C-connection.

func (*Vl53l0x) SetMeasurementTimingBudget

func (v *Vl53l0x) SetMeasurementTimingBudget(i2c *i2c.I2C, budgetUsec uint32) error

SetMeasurementTimingBudget set the measurement timing budget in microseconds, which is the time allowed for one measurement; the ST API and this library take care of splitting the timing budget among the sub-steps in the ranging sequence. A longer timing budget allows for more accurate measurements. Increasing the budget by a factor of N decreases the range measurement standard deviation by a factor of sqrt(N). Defaults to about 33 milliseconds; the minimum is 20 ms. Based on VL53L0X_set_measurement_timing_budget_micro_seconds().

func (*Vl53l0x) SetSignalRateLimit

func (v *Vl53l0x) SetSignalRateLimit(i2c *i2c.I2C, limitMcps float32) error

SetSignalRateLimit set the return signal rate limit check value in units of MCPS (mega counts per second). "This represents the amplitude of the signal reflected from the target and detected by the device"; setting this limit presumably determines the minimum measurement necessary for the sensor to report a valid reading. Setting a lower limit increases the potential range of the sensor but also seems to increase the likelihood of getting an inaccurate reading because of unwanted reflections from objects other than the intended target. Defaults to 0.25 MCPS as initialized by the ST API and this library.

func (*Vl53l0x) SetVcselPulsePeriod

func (v *Vl53l0x) SetVcselPulsePeriod(i2c *i2c.I2C, tpe VcselPeriodType, periodPclks uint8) error

SetVcselPulsePeriod set the VCSEL (vertical cavity surface emitting laser) pulse period for the given period type (pre-range or final range) to the given value in PCLKs. Longer periods seem to increase the potential range of the sensor. Valid values are (even numbers only):

pre:  12 to 18 (initialized default: 14),
final: 8 to 14 (initialized default: 10).

Based on VL53L0X_set_vcsel_pulse_period().

func (*Vl53l0x) StartContinuous

func (v *Vl53l0x) StartContinuous(i2c *i2c.I2C, periodMs uint32) error

StartContinuous start continuous ranging measurements. If period_ms (optional) is 0 or not given, continuous back-to-back mode is used (the sensor takes measurements as often as possible); otherwise, continuous timed mode is used, with the given inter-measurement period in milliseconds determining how often the sensor takes a measurement. Based on VL53L0X_StartMeasurement().

func (*Vl53l0x) StopContinuous

func (v *Vl53l0x) StopContinuous(i2c *i2c.I2C) error

StopContinuous stop continuous measurements. Based on VL53L0X_StopMeasurement().

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL