PX4 Firmware
PX4 Autopilot Software http://px4.io
CM8JL65.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018-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 cm8jl65.cpp
36  * @author Claudio Micheli <claudio@auterion.com>
37  *
38  * Driver for the Lanbao PSK-CM8JL65-CC5 distance sensor.
39  * Make sure to disable MAVLINK messages (MAV_0_CONFIG PARAMETER)
40  * on the serial port you connect the sensor,i.e TELEM2.
41  *
42  */
43 
44 #pragma once
45 
46 #include <termios.h>
47 
48 #include <drivers/drv_hrt.h>
50 #include <perf/perf_counter.h>
51 #include <px4_platform_common/px4_config.h>
52 #include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
53 
54 
55 using namespace time_literals;
56 
57 /* Configuration Constants */
58 static constexpr uint32_t CM8JL65_MEASURE_INTERVAL{50_ms}; // 50ms default sensor conversion time.
59 
60 /* Frame start delimiter */
61 static constexpr unsigned char START_FRAME_DIGIT1{0xA5};
62 static constexpr unsigned char START_FRAME_DIGIT2{0x5A};
63 
64 /**
65  * Frame format definition
66  * 1B 1B 1B 1B 2B
67  * | 0xA5 | 0x5A | distance-MSB | distance-LSB | crc-16 |
68  *
69  * Frame data saved for CRC calculation
70  */
71 static constexpr uint8_t DISTANCE_MSB_POS{2};
72 static constexpr uint8_t DISTANCE_LSB_POS{3};
73 static constexpr uint8_t PARSER_BUF_LENGTH{4};
74 
75 class CM8JL65 : public px4::ScheduledWorkItem
76 {
77 public:
78  /**
79  * Default Constructor
80  * @param port The serial port to open for communicating with the sensor.
81  * @param rotation The sensor rotation relative to the vehicle body.
82  */
83  CM8JL65(const char *port, uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING);
84 
85  /** Virtual destructor */
86  virtual ~CM8JL65() override;
87 
88  /**
89  * Method : init()
90  * This method initializes the general driver for a range finder sensor.
91  */
92  int init();
93 
94  /**
95  * Diagnostics - print some basic information about the driver.
96  */
97  void print_info();
98 
99 private:
100 
101  enum class PARSE_STATE {
102  WAITING_FRAME = 0,
103  DIGIT_1,
104  DIGIT_2,
105  MSB_DATA,
106  LSB_DATA,
107  CHECKSUM
108  };
109 
110  /**
111  * Calculates the 16 byte crc value for the data frame.
112  * @param data_frame The data frame to compute a checksum for.
113  * @param crc16_length The length of the data frame.
114  */
115  uint16_t crc16_calc(const unsigned char *data_frame, uint8_t crc16_length);
116 
117  /**
118  * Reads data from serial UART and places it into a buffer.
119  */
120  int collect();
121 
122  int data_parser(const uint8_t check_byte, uint8_t parserbuf[PARSER_BUF_LENGTH], PARSE_STATE &state,
123  uint16_t &crc16, int &distance);
124 
125  /**
126  * Opens and configures the UART serial communications port.
127  * @param speed The baudrate (speed) to configure the serial UART port.
128  */
129  int open_serial_port(const speed_t speed = B115200);
130 
131  /**
132  * Perform a reading cycle; collect from the previous measurement
133  * and start a new one.
134  */
135  void Run() override;
136 
137  /**
138  * Initialise the automatic measurement state machine and start it.
139  * @note This function is called at open and error time. It might make sense
140  * to make it more aggressive about resetting the bus in case of errors.
141  */
142  void start();
143 
144  /**
145  * Stops the automatic measurement state machine.
146  */
147  void stop();
148 
149 
151 
152  char _port[20] {};
153 
154  unsigned char _frame_data[PARSER_BUF_LENGTH] {START_FRAME_DIGIT1, START_FRAME_DIGIT2, 0, 0};
155 
156  int _file_descriptor{-1};
157 
158  uint8_t _linebuf[25] {};
159 
160  uint16_t _crc16{0};
161 
162  PARSE_STATE _parse_state{PARSE_STATE::WAITING_FRAME};
163 
164  perf_counter_t _comms_errors{perf_alloc(PC_COUNT, MODULE_NAME": com_err")};
165  perf_counter_t _sample_perf{perf_alloc(PC_ELAPSED, MODULE_NAME": read")};
166 };
static constexpr unsigned char START_FRAME_DIGIT1
Definition: CM8JL65.hpp:61
static constexpr unsigned char START_FRAME_DIGIT2
Definition: CM8JL65.hpp:62
measure the time elapsed performing an event
Definition: perf_counter.h:56
static enum @74 state
static constexpr uint8_t DISTANCE_MSB_POS
Frame format definition 1B 1B 1B 1B 2B | 0xA5 | 0x5A | distance-MSB | distance-LSB | crc-16 |...
Definition: CM8JL65.hpp:71
static void stop()
Definition: dataman.cpp:1491
count the number of times an event occurs
Definition: perf_counter.h:55
High-resolution timer with callouts and timekeeping.
PX4Rangefinder _px4_rangefinder
Definition: CM8JL65.hpp:150
Header common to all counters.
void init()
Activates/configures the hardware registers.
#define perf_alloc(a, b)
Definition: px4io.h:59
uint16_t _crc16
Definition: sumd.cpp:71
static constexpr uint8_t PARSER_BUF_LENGTH
Definition: CM8JL65.hpp:73
uint16_t crc16(const uint8_t *data_p, uint32_t length)
Calculate buffer CRC16.
Definition: sbf.cpp:368
static int start()
Definition: dataman.cpp:1452
static constexpr uint8_t DISTANCE_LSB_POS
Definition: CM8JL65.hpp:72
static constexpr uint32_t CM8JL65_MEASURE_INTERVAL
Definition: CM8JL65.hpp:58
Performance measuring tools.