PX4 Firmware
PX4 Autopilot Software http://px4.io
LidarLiteI2C.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2014-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file LidarLiteI2C.h
36  * @author Allyson Kreft
37  *
38  * Driver for the PulsedLight Lidar-Lite range finders connected via I2C.
39  */
40 
41 #pragma once
42 
43 #include "LidarLite.h"
44 
45 #include <drivers/device/i2c.h>
46 #include <drivers/drv_hrt.h>
47 #include <mathlib/mathlib.h>
48 #include <px4_platform_common/defines.h>
49 #include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
50 
51 
52 /* Configuration Constants */
53 static constexpr uint8_t LL40LS_BASEADDR = 0x62; /* 7-bit address */
54 static constexpr uint8_t LL40LS_BASEADDR_OLD = 0x42; /* previous 7-bit address */
55 static constexpr uint8_t LL40LS_SIG_COUNT_VAL_DEFAULT = 0x80; /* Default maximum acquisition count */
56 
57 /* LL40LS Registers addresses */
58 static constexpr uint8_t LL40LS_MEASURE_REG = 0x00; /* Measure range register */
59 static constexpr uint8_t LL40LS_MSRREG_RESET = 0x00; /* reset to power on defaults */
60 static constexpr uint8_t LL40LS_MSRREG_ACQUIRE = 0x04; /* Value to acquire a measurement, version specific */
61 static constexpr uint8_t LL40LS_DISTHIGH_REG = 0x0F; /* High byte of distance register, auto increment */
62 static constexpr uint8_t LL40LS_AUTO_INCREMENT = 0x80;
63 static constexpr uint8_t LL40LS_HW_VERSION = 0x41;
64 static constexpr uint8_t LL40LS_SW_VERSION = 0x4f;
65 static constexpr uint8_t LL40LS_SIGNAL_STRENGTH_REG = 0x0e;
66 static constexpr uint8_t LL40LS_PEAK_STRENGTH_REG = 0x0c;
67 static constexpr uint8_t LL40LS_UNIT_ID_HIGH = 0x16;
68 static constexpr uint8_t LL40LS_UNIT_ID_LOW = 0x17;
69 
70 static constexpr uint8_t LL40LS_SIG_COUNT_VAL_REG = 0x02; /* Maximum acquisition count register */
71 static constexpr uint8_t LL40LS_SIG_COUNT_VAL_MAX = 0xFF; /* Maximum acquisition count max value */
72 
73 static constexpr int LL40LS_SIGNAL_STRENGTH_MIN_V3HP = 70; /* Min signal strength for V3HP */
74 static constexpr int LL40LS_SIGNAL_STRENGTH_MAX_V3HP = 255; /* Max signal strength for V3HP */
75 
76 static constexpr int LL40LS_SIGNAL_STRENGTH_LOW = 24; /* Minimum signal strength for a valid measurement */
77 static constexpr int LL40LS_PEAK_STRENGTH_LOW = 135; /* Minimum peak strength for accepting a measurement */
78 static constexpr int LL40LS_PEAK_STRENGTH_HIGH = 234; /* Max peak strength raw value */
79 
80 
81 class LidarLiteI2C : public LidarLite, public device::I2C, public px4::ScheduledWorkItem
82 {
83 public:
84  LidarLiteI2C(const int bus, const uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING,
85  const int address = LL40LS_BASEADDR);
86  virtual ~LidarLiteI2C();
87 
88  int init() override;
89 
90  /**
91  * Print sensor registers to console for debugging.
92  */
93  void print_registers() override;
94 
95  /**
96  * Initialise the automatic measurement state machine and start it.
97  *
98  * @note This function is called at open and error time. It might make sense
99  * to make it more aggressive about resetting the bus in case of errors.
100  */
101  void start() override;
102 
103  /**
104  * Stop the automatic measurement state machine.
105  */
106  void stop() override;
107 
108 protected:
109 
110  int measure() override;
111 
112  /**
113  * Reset the sensor to power on defaults plus additional configurations.
114  */
115  int reset_sensor() override;
116 
117  int probe() override;
118 
119  int read_reg(const uint8_t reg, uint8_t &val);
120 
121  int write_reg(const uint8_t reg, const uint8_t &val);
122 
123 private:
124 
125  int collect() override;
126 
127  /**
128  * LidarLite specific transfer function. This is needed
129  * to avoid a stop transition with SCL high
130  *
131  * @param send Pointer to bytes to send.
132  * @param send_len Number of bytes to send.
133  * @param recv Pointer to buffer for bytes received.
134  * @param recv_len Number of bytes to receive.
135  * @return OK if the transfer was successful, -errno
136  * otherwise.
137  */
138  int lidar_transfer(const uint8_t *send, const unsigned send_len, uint8_t *recv, const unsigned recv_len);
139 
140  /**
141  * Test whether the device supported by the driver is present at a
142  * specific address.
143  *
144  * @param address The I2C bus address to probe.
145  * @return True if the device is present.
146  */
147  int probe_address(const uint8_t address);
148 
149  /**
150  * Perform a poll cycle; collect from the previous measurement
151  * and start a new one.
152  */
153  void Run() override;
154 
155  bool _collect_phase{false};
156  bool _is_v3hp{false};
157  bool _pause_measurements{false};
158 
159  uint8_t _hw_version{0};
160  uint8_t _sw_version{0};
161 
162  uint16_t _unit_id{0};
163  uint16_t _zero_counter{0};
164 
165  uint64_t _acquire_time_usec{0};
166 };
int collect() override
static constexpr uint8_t LL40LS_MSRREG_RESET
Definition: LidarLiteI2C.h:59
static constexpr uint8_t LL40LS_SW_VERSION
Definition: LidarLiteI2C.h:64
uint8_t _hw_version
Definition: LidarLiteI2C.h:159
static constexpr uint8_t LL40LS_BASEADDR_OLD
Definition: LidarLiteI2C.h:54
int init() override
void * send(void *data)
LidarLiteI2C(const int bus, const uint8_t rotation=distance_sensor_s::ROTATION_DOWNWARD_FACING, const int address=LL40LS_BASEADDR)
uint8_t _sw_version
Definition: LidarLiteI2C.h:160
int write_reg(const uint8_t reg, const uint8_t &val)
int lidar_transfer(const uint8_t *send, const unsigned send_len, uint8_t *recv, const unsigned recv_len)
LidarLite specific transfer function.
High-resolution timer with callouts and timekeeping.
static constexpr uint8_t LL40LS_MEASURE_REG
Definition: LidarLiteI2C.h:58
static constexpr int LL40LS_SIGNAL_STRENGTH_MAX_V3HP
Definition: LidarLiteI2C.h:74
void Run() override
Perform a poll cycle; collect from the previous measurement and start a new one.
static constexpr uint8_t LL40LS_UNIT_ID_HIGH
Definition: LidarLiteI2C.h:67
bool _collect_phase
Definition: LidarLiteI2C.h:155
static constexpr uint8_t LL40LS_SIG_COUNT_VAL_MAX
Definition: LidarLiteI2C.h:71
int reset_sensor() override
Reset the sensor to power on defaults plus additional configurations.
uint64_t _acquire_time_usec
Definition: LidarLiteI2C.h:165
virtual ~LidarLiteI2C()
static constexpr uint8_t LL40LS_AUTO_INCREMENT
Definition: LidarLiteI2C.h:62
static constexpr uint8_t LL40LS_SIG_COUNT_VAL_DEFAULT
Definition: LidarLiteI2C.h:55
int probe_address(const uint8_t address)
Test whether the device supported by the driver is present at a specific address. ...
static constexpr int LL40LS_PEAK_STRENGTH_LOW
Definition: LidarLiteI2C.h:77
int measure() override
static constexpr uint8_t LL40LS_UNIT_ID_LOW
Definition: LidarLiteI2C.h:68
uint16_t _unit_id
Definition: LidarLiteI2C.h:162
uint16_t _zero_counter
Definition: LidarLiteI2C.h:163
static constexpr int LL40LS_SIGNAL_STRENGTH_LOW
Definition: LidarLiteI2C.h:76
void print_registers() override
Print sensor registers to console for debugging.
static constexpr uint8_t LL40LS_SIGNAL_STRENGTH_REG
Definition: LidarLiteI2C.h:65
int read_reg(const uint8_t reg, uint8_t &val)
static constexpr uint8_t LL40LS_BASEADDR
Definition: LidarLiteI2C.h:53
static constexpr int LL40LS_PEAK_STRENGTH_HIGH
Definition: LidarLiteI2C.h:78
void stop() override
Stop the automatic measurement state machine.
static constexpr int LL40LS_SIGNAL_STRENGTH_MIN_V3HP
Definition: LidarLiteI2C.h:73
static constexpr uint8_t LL40LS_SIG_COUNT_VAL_REG
Definition: LidarLiteI2C.h:70
static constexpr uint8_t LL40LS_HW_VERSION
Definition: LidarLiteI2C.h:63
static constexpr uint8_t LL40LS_PEAK_STRENGTH_REG
Definition: LidarLiteI2C.h:66
static constexpr uint8_t LL40LS_DISTHIGH_REG
Definition: LidarLiteI2C.h:61
static constexpr uint8_t LL40LS_MSRREG_ACQUIRE
Definition: LidarLiteI2C.h:60
void start() override
Initialise the automatic measurement state machine and start it.
bool _pause_measurements
Definition: LidarLiteI2C.h:157
int probe() override
Base class for devices connected via I2C.