Firmware
LidarLiteI2C.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2014, 2015 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 
41 #pragma once
42 
43 #include "LidarLite.h"
44 
45 #include <px4_workqueue.h>
46 
47 #include <perf/perf_counter.h>
48 
49 #include <drivers/device/i2c.h>
51 
52 #include <uORB/uORB.h>
53 #include <uORB/topics/distance_sensor.h>
54 
55 
56 /* Configuration Constants */
57 #define LL40LS_BASEADDR 0x62 /* 7-bit address */
58 #define LL40LS_BASEADDR_OLD 0x42 /* previous 7-bit address */
59 
60 /* LL40LS Registers addresses */
61 
62 #define LL40LS_MEASURE_REG 0x00 /* Measure range register */
63 #define LL40LS_MSRREG_RESET 0x00 /* reset to power on defaults */
64 #define LL40LS_MSRREG_ACQUIRE 0x04 /* Value to initiate a measurement, varies based on sensor revision */
65 #define LL40LS_DISTHIGH_REG 0x0F /* High byte of distance register, auto increment */
66 #define LL40LS_AUTO_INCREMENT 0x80
67 #define LL40LS_HW_VERSION 0x41
68 #define LL40LS_SW_VERSION 0x4f
69 #define LL40LS_SIGNAL_STRENGTH_REG 0x0e
70 #define LL40LS_PEAK_STRENGTH_REG 0x0c
71 #define LL40LS_UNIT_ID_HIGH 0x16
72 #define LL40LS_UNIT_ID_LOW 0x17
73 
74 #define LL40LS_SIG_COUNT_VAL_REG 0x02 /* Maximum acquisition count register */
75 #define LL40LS_SIG_COUNT_VAL_MAX 0xFF /* Maximum acquisition count max value */
76 
77 #define LL40LS_SIGNAL_STRENGTH_LOW 24 // Minimum (relative) signal strength value for accepting a measurement
78 #define LL40LS_PEAK_STRENGTH_LOW 135 // Minimum peak strength raw value for accepting a measurement
79 #define LL40LS_PEAK_STRENGTH_HIGH 234 // Max peak strength raw value
80 
81 class LidarLiteI2C : public LidarLite, public device::I2C
82 {
83 public:
84  LidarLiteI2C(int bus, const char *path,
85  uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING,
86  int address = LL40LS_BASEADDR);
87  virtual ~LidarLiteI2C();
88 
89  int init() override;
90 
91  ssize_t read(device::file_t *filp, char *buffer, size_t buflen) override;
92  int ioctl(device::file_t *filp, int cmd, unsigned long arg) override;
93 
97  void print_info() override;
98 
102  void print_registers() override;
103 
104  const char *get_dev_name() override;
105 
106 protected:
107  int probe() override;
108  int read_reg(uint8_t reg, uint8_t &val);
109  int write_reg(uint8_t reg, uint8_t val);
110 
111  int measure() override;
112  int reset_sensor() override;
113 
114 private:
115  uint8_t _rotation;
116  work_s _work;
117  ringbuffer::RingBuffer *_reports;
118  bool _sensor_ok;
119  bool _collect_phase;
120  int _class_instance;
121  int _orb_class_instance;
122 
123  orb_advert_t _distance_sensor_topic;
124 
125  perf_counter_t _sample_perf;
126  perf_counter_t _comms_errors;
127  perf_counter_t _sensor_resets;
128  perf_counter_t _sensor_zero_resets;
129  uint16_t _last_distance;
130  uint16_t _zero_counter;
131  uint64_t _acquire_time_usec;
132  volatile bool _pause_measurements;
133  uint8_t _hw_version;
134  uint8_t _sw_version;
135  uint16_t _unit_id;
136 
148  int lidar_transfer(const uint8_t *send, unsigned send_len, uint8_t *recv, unsigned recv_len);
149 
157  int probe_address(uint8_t address);
158 
165  void start() override;
166 
170  void stop() override;
171 
176  void cycle();
177  int collect() override;
178 
185  static void cycle_trampoline(void *arg);
186 
187 private:
188  LidarLiteI2C(const LidarLiteI2C &copy) = delete;
189  LidarLiteI2C operator=(const LidarLiteI2C &assignment) = delete;
190 };
A flexible ringbuffer class.
Definition: LidarLiteI2C.h:81
void print_info() override
Diagnostics - print some basic information about the driver.
Definition: LidarLiteI2C.cpp:645
Definition: LidarLite.h:57
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
void print_registers() override
print registers to console
Definition: LidarLiteI2C.cpp:372
Header common to all counters.
Definition: perf_counter.cpp:65
API for the uORB lightweight object broker.
Definition: cdev_platform.hpp:20
Definition: video_device.h:50
Performance measuring tools.