Line data Source code
1 : /* 2 : This program is free software: you can redistribute it and/or modify 3 : it under the terms of the GNU General Public License as published by 4 : the Free Software Foundation, either version 3 of the License, or 5 : (at your option) any later version. 6 : 7 : This program is distributed in the hope that it will be useful, 8 : but WITHOUT ANY WARRANTY; without even the implied warranty of 9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 : GNU General Public License for more details. 11 : 12 : You should have received a copy of the GNU General Public License 13 : along with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : 16 : #pragma once 17 : 18 : #include "AP_RangeFinder_config.h" 19 : 20 : #if AP_RANGEFINDER_DTS6012M_ENABLED 21 : 22 : #include "AP_RangeFinder_Backend_Serial.h" 23 : #include <AP_HAL/utility/sparse-endian.h> 24 : 25 : 26 : class AP_RangeFinder_DTS6012M : public AP_RangeFinder_Backend_Serial 27 : { 28 : public: 29 1 : static AP_RangeFinder_Backend_Serial *create( 30 : RangeFinder::RangeFinder_State &_state, 31 : AP_RangeFinder_Params &_params) 32 : { 33 1 : return NEW_NOTHROW AP_RangeFinder_DTS6012M(_state, _params); 34 : } 35 : 36 : protected: 37 : using AP_RangeFinder_Backend_Serial::AP_RangeFinder_Backend_Serial; 38 : 39 342 : MAV_DISTANCE_SENSOR _get_mav_distance_sensor_type() const override { return MAV_DISTANCE_SENSOR_LASER; } 40 : 41 : private: 42 : bool get_reading(float &reading_m) override; 43 1 : uint32_t initial_baudrate(uint8_t serial_instance) const override { return 921600; } 44 686 : int8_t get_signal_quality_pct() const override { return _signal_quality_pct; } 45 : 46 : // protocol parsing state 47 : // response frame layout matching the DTS6012M wire format 48 : struct PACKED { 49 : uint8_t header; // 0xA5 50 : uint8_t device_id; // 0x03 51 : uint8_t device_type; // 0x20 52 : uint8_t cmd_echo; // 0x01 53 : uint8_t reserved; 54 : be16_t data_len; // big-endian length 55 : // measurement data (14 bytes) 56 : le16_t secondary_distance_mm; 57 : le16_t secondary_correction; 58 : le16_t secondary_intensity; 59 : le16_t primary_distance_mm; 60 : le16_t primary_correction; 61 : le16_t primary_intensity; 62 : le16_t sunlight_base; 63 : // CRC16 (big-endian) 64 : be16_t crc; 65 : } frame; 66 : uint8_t linebuf_len; 67 : bool got_reading; 68 : int8_t _signal_quality_pct = RangeFinder::SIGNAL_QUALITY_UNKNOWN; 69 : 70 : // send start stream command to sensor 71 : void send_start_command(); 72 : 73 : // search for frame header byte starting at given offset, 74 : // shifting buffer contents to re-sync after line noise 75 : void find_signature_in_buffer(uint8_t start); 76 : }; 77 : 78 : #endif // AP_RANGEFINDER_DTS6012M_ENABLED