PX4 Firmware
PX4 Autopilot Software http://px4.io
LidarLitePWM.cpp
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 /**
36  * @file LidarLitePWM.h
37  * @author Johan Jansen <jnsn.johan@gmail.com>
38  * @author Ban Siesta <bansiesta@gmail.com>
39  *
40  * Driver for the PulsedLight Lidar-Lite range finders connected via PWM.
41  *
42  * This driver accesses the pwm_input published by the pwm_input driver.
43  */
44 
45 #include "LidarLitePWM.h"
46 
47 LidarLitePWM::LidarLitePWM(const uint8_t rotation) :
48  LidarLite(rotation),
49  ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::hp_default)
50 {
51 }
52 
54 {
55  stop();
56 }
57 
58 int
60 {
61  start();
62 
63  return PX4_OK;
64 }
65 
66 void
68 {
69  ScheduleOnInterval(get_measure_interval());
70 }
71 
72 void
74 {
75  ScheduleClear();
76 }
77 
78 void
80 {
81  measure();
82 }
83 
84 int
86 {
88 
89  const hrt_abstime timestamp_sample = hrt_absolute_time();
90 
91  if (PX4_OK != collect()) {
92  PX4_DEBUG("collection error");
95  return PX4_ERROR;
96  }
97 
98  const float current_distance = float(_pwm.pulse_width) * 1e-3f; /* 10 usec = 1 cm distance for LIDAR-Lite */
99 
100  /* Due to a bug in older versions of the LidarLite firmware, we have to reset sensor on (distance == 0) */
101  if (current_distance <= 0.0f) {
104  return reset_sensor();
105  }
106 
107  _px4_rangefinder.update(timestamp_sample, current_distance);
108 
110  return PX4_OK;
111 }
112 
113 int
115 {
116  int fd = ::open(PWMIN0_DEVICE_PATH, O_RDONLY);
117 
118  if (fd == -1) {
119  return PX4_ERROR;
120  }
121 
122  if (::read(fd, &_pwm, sizeof(_pwm)) == sizeof(_pwm)) {
123  ::close(fd);
124  return PX4_OK;
125  }
126 
127  ::close(fd);
128  return EAGAIN;
129 }
pwm_input_s _pwm
Definition: LidarLitePWM.h:78
void start() override
int init() override
int collect() override
PX4Rangefinder _px4_rangefinder
Definition: LidarLite.h:88
LidarLitePWM(const uint8_t rotation=distance_sensor_s::ROTATION_DOWNWARD_FACING)
static void read(bootloader_app_shared_t *pshared)
void stop() override
void update(const hrt_abstime timestamp, const float distance, const int8_t quality=-1)
void perf_count(perf_counter_t handle)
Count a performance event.
virtual int reset_sensor()
Definition: LidarLite.h:88
#define PWMIN0_DEVICE_PATH
Definition: drv_pwm_input.h:42
uint32_t pulse_width
Definition: pwm_input.h:55
uint32_t get_measure_interval() const
Definition: LidarLite.h:82
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
perf_counter_t _sensor_zero_resets
Definition: LidarLite.h:95
void perf_end(perf_counter_t handle)
End a performance event.
perf_counter_t _sample_perf
Definition: LidarLite.h:93
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
int fd
Definition: dataman.cpp:146
int measure() override
Definition: bst.cpp:62
virtual ~LidarLitePWM()
perf_counter_t _comms_errors
Definition: LidarLite.h:92
void perf_begin(perf_counter_t handle)
Begin a performance event.
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
void Run() override